aboutsummaryrefslogtreecommitdiff
path: root/include/bits.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/bits.h')
-rw-r--r--include/bits.h141
1 files changed, 141 insertions, 0 deletions
diff --git a/include/bits.h b/include/bits.h
new file mode 100644
index 0000000..3c4eeb5
--- /dev/null
+++ b/include/bits.h
@@ -0,0 +1,141 @@
1// bits.h --- bit/memory manipulation utilities -*- C -*-
2//
3// Copyright (c) 2026 Martin Michalec <martin@michalec.dev>
4//
5// SPDX-License-Identifier: MIT
6//
7// This file is part of COSMOS Codebase.
8//
9// Commentary:
10//
11// Code:
12
13#ifndef INCLUDED_COSMOS_BITS_H
14#define INCLUDED_COSMOS_BITS_H
15
16#include "types.h"
17#include <string.h> // MAYBE(cmmm): remove libc dep? (memcpy)
18
19#if defined (__BYTE_ORDER__) && defined (__ORDER_LITTLE_ENDIAN__)
20// GCC and Clang built-ins
21#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
22#define HOST_LITTLE_ENDIAN 1
23#else
24#define HOST_LITTLE_ENDIAN 0
25#endif
26#elif defined (__LITTLE_ENDIAN__)
27#define HOST_LITTLE_ENDIAN 1
28#elif defined (__BIG_ENDIAN__)
29#define HOST_LITTLE_ENDIAN 0
30#elif defined (_WIN32) || defined (_MSC_VER)
31#define HOST_LITTLE_ENDIAN 1
32#else
33#warn "Target endianness could not be detected at compile time."
34#warn "Choosing little endian as default."
35#define HOST_LITTLE_ENDIAN 1
36#endif
37
38#if defined (__GNUC__) || defined (__clang__) || defined (__TINYC__)
39#define POPCOUNT(val) __builtin_popcount (val)
40#define BSWAP16(val) __builtin_bswap16 (val)
41#define BSWAP32(val) __builtin_bswap32 (val)
42#define BSWAP64(val) __builtin_bswap64 (val)
43#elif defined (_MSC_VER)
44#define BSWAP16(val) _byteswap_uint16 (val)
45#define BSWAP32(val) _byteswap_uint32 (val)
46#define BSWAP64(val) _byteswap_uint64 (val)
47#else
48#define BSWAP16(val) \
49 ((((val) & 0x00ff0000) >> 8) | \
50 (((val) & 0x0000ff00) << 8))
51#define BSWAP32(val) \
52 ((((val) & 0xff000000) >> 24) | \
53 (((val) & 0x00ff0000) >> 8) | \
54 (((val) & 0x0000ff00) << 8) | \
55 (((val) & 0x000000ff) << 24))
56#define BSWAP64(val) \
57 ((((val) & 0xff00000000000000) >> 56) | \
58 (((val) & 0x00ff000000000000) >> 40) | \
59 (((val) & 0x0000ff0000000000) >> 24) | \
60 (((val) & 0x000000ff00000000) >> 8) | \
61 (((val) & 0x00000000ff000000) << 8) | \
62 (((val) & 0x0000000000ff0000) << 24) | \
63 (((val) & 0x000000000000ff00) << 40) | \
64 (((val) & 0x00000000000000ff) << 56))
65#endif
66
67#define IS_POWER_OF_2(num) (((num) & ((num) - 1)) == 0)
68
69#define ALIGN_UPTO( ptr, mask) ((typeof (ptr)) ((uptr) ptr + mask & ~mask))
70#define ALIGN_DOWNTO(ptr, mask) ((typeof (ptr)) ((uptr) ptr - mask & ~mask))
71
72#if HOST_LITTLE_ENDIAN == 1
73#define HTON16(host) BSWAP16 (host)
74#define HTON32(host) BSWAP32 (host)
75#define HTON64(host) BSWAP64 (host)
76#define NTOH16(net ) BSWAP16 (net )
77#define NTOH32(net ) BSWAP32 (net )
78#define NTOH64(net ) BSWAP64 (net )
79#else
80#define HTON16(host) host
81#define HTON32(host) host
82#define HTON64(host) host
83#define NTOH16(net ) net
84#define NTOH32(net ) net
85#define NTOH64(net ) net
86#endif
87
88#define ALIGN(x, n) (((x) + ((1 << (n)) - 1)) & ~((1 << (n)) - 1))
89
90static inline u8 load8 (const void *p) { u8 v; memcpy (&v, p, 1); return v; }
91static inline u16 load16 (const void *p) { u16 v; memcpy (&v, p, 2); return v; }
92static inline u32 load32 (const void *p) { u32 v; memcpy (&v, p, 4); return v; }
93static inline u64 load64 (const void *p) { u64 v; memcpy (&v, p, 8); return v; }
94
95static inline void store8 (void *p, u8 v) { memcpy (p, &v, 1); }
96static inline void store16 (void *p, u16 v) { memcpy (p, &v, 2); }
97static inline void store32 (void *p, u32 v) { memcpy (p, &v, 4); }
98static inline void store64 (void *p, u64 v) { memcpy (p, &v, 8); }
99
100static inline u8 ror8 (u8 v, int n) { return (v >> n) | (v << 8 - n); }
101static inline u16 ror16 (u16 v, int n) { return (v >> n) | (v << 16 - n); }
102static inline u32 ror32 (u32 v, int n) { return (v >> n) | (v << 32 - n); }
103static inline u64 ror64 (u64 v, int n) { return (v >> n) | (v << 64 - n); }
104
105static inline u8 rol8 (u8 v, int n) { return (v << n) | (v >> 8 - n); }
106static inline u16 rol16 (u16 v, int n) { return (v << n) | (v >> 16 - n); }
107static inline u32 rol32 (u32 v, int n) { return (v << n) | (v >> 32 - n); }
108static inline u64 rol64 (u64 v, int n) { return (v << n) | (v >> 64 - n); }
109
110static inline u64
111splitmix64 (u64 state)
112{
113 u64 z = state + 0x9e3779b97f4a7c15;
114 z = (z ^ (z >> 30))*0xbf58476d1ce4e5b9;
115 z = (z ^ (z >> 27))*0x94d049bb133111eb;
116 return z ^ (z >> 31);
117}
118
119static inline u64
120bit_ceil_64 (u64 x)
121{
122 if (x <= 1)
123 return 1;
124
125 // TODO(cmmm): use 1 << 64 - CLZ64 (x - 1) when available
126
127 x += 1;
128
129 x |= x >> 0x01;
130 x |= x >> 0x02;
131 x |= x >> 0x04;
132 x |= x >> 0x08;
133 x |= x >> 0x10;
134 x |= x >> 0x20;
135
136 return x + 1;
137}
138
139#endif // INCLUDED_COSMOS_BITS_H
140
141// bits.h ends here