8 namespace util = satcat5::util;
11 #ifndef SATCAT5_GCC_INTRINSICS
13 #define SATCAT5_GCC_INTRINSICS 1
15 #define SATCAT5_GCC_INTRINSICS 0
23 if ((a > b) && (a > c))
return a;
31 return (a == c) ? 1 : 0;
36 #if SATCAT5_GCC_INTRINSICS
38 return (
unsigned)__builtin_popcount(x);
42 x = x - ((x >> 1) & 0x55555555);
43 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
44 x = (x + (x >> 4)) & 0x0F0F0F0F;
53 for (
unsigned b = 0 ; b < 8 ; ++b)
54 result ^= ((x >> b) & 1);
55 return result ? 1 : 0;
59 for (
unsigned b = 0 ; b < 16 ; ++b)
60 result ^= ((x >> b) & 1);
61 return result ? 1 : 0;
65 for (
unsigned b = 0 ; b < 32 ; ++b)
66 result ^= ((x >> b) & 1);
67 return result ? 1 : 0;
71 for (
unsigned b = 0 ; b < 64 ; ++b)
72 result ^= ((x >> b) & 1);
73 return result ? 1 : 0;
78 static const u32 HALF_MAX = (1u << 31);
87 while ((x < HALF_MAX) && (2*x < y)) {
100 u64 rem = 0, root = 0;
101 for (
unsigned i = 0 ; i < 32 ; ++i) {
113 return (u32) (root >> 1);
116 u32 rem = 0, root = 0;
117 for (
unsigned i = 0 ; i < 16 ; ++i) {
129 return (u16) (root >> 1);
132 u16 rem = 0, root = 0;
134 for (i = 0 ; i < 8 ; ++i) {
146 return (u8) (root >> 1);
152 return 256 * (u16)src[0]
156 return 16777216 * (u32)src[0]
157 + 65536 * (u32)src[1]
162 return 72057594037927936ull * (u64)src[0]
163 + 281474976710656ull * (u64)src[1]
164 + 1099511627776ull * (u64)src[2]
165 + 4294967296ull * (u64)src[3]
166 + 16777216ull * (u64)src[4]
167 + 65536ull * (u64)src[5]
168 + 256ull * (u64)src[6]
169 + 1ull * (u64)src[7];
174 dst[0] = (u8)(val >> 8);
175 dst[1] = (u8)(val >> 0);
178 dst[0] = (u8)(val >> 24);
179 dst[1] = (u8)(val >> 16);
180 dst[2] = (u8)(val >> 8);
181 dst[3] = (u8)(val >> 0);
184 dst[0] = (u8)(val >> 56);
185 dst[1] = (u8)(val >> 48);
186 dst[2] = (u8)(val >> 40);
187 dst[3] = (u8)(val >> 32);
188 dst[4] = (u8)(val >> 24);
189 dst[5] = (u8)(val >> 16);
190 dst[6] = (u8)(val >> 8);
191 dst[7] = (u8)(val >> 0);
196 m_state ^= (m_state >> 12);
197 m_state ^= (m_state << 25);
198 m_state ^= (m_state >> 27);
199 u64
next = m_state * 0x2545F4914F6CDD1Dull;
200 return (u32)(
next >> 32);
204 u64 scale = next() * (1ull + u64(mx) - u64(mn));
205 return mn + u32(scale >> 32);
208 static const char* LABEL_NONE =
"None";
210 util::RunningMax::RunningMax()
211 : m_label(LABEL_NONE)
218 m_label = LABEL_NONE;
223 if (value > m_maximum) {
Simple cross-platform psuedorandom number generator (PRNG).
u32 next()
Range [0..2^32)
void clear()
Reset recorded maximum to zero.
void update(const char *lbl, u32 value)
Update stats if new value exceeds previous record.
Miscellaneous mathematical utility functions.
unsigned popcount(u32 x)
Count the number of '1' bits in an integer.
u64 extract_be_u64(const u8 *src)
Extract fields from a big-endian byte array.
void write_be_u16(u8 *dst, u16 val)
Store fields into a big-endian byte array.
bool xor_reduce_u8(u8 x)
XOR-reduction of all bits in a word:
bool xor_reduce_u64(u64 x)
XOR-reduction of all bits in a word:
u32 sqrt_u64(u64 x)
Find integer square root y = floor(sqrt(x))
void write_be_u32(u8 *dst, u32 val)
Store fields into a big-endian byte array.
u8 sqrt_u16(u16 x)
Find integer square root y = floor(sqrt(x))
bool xor_reduce_u16(u16 x)
XOR-reduction of all bits in a word:
bool is_multiple_u32(u32 a, u32 b)
Check if A is a multiple of B:
satcat5::util::Prng prng
Global instance of the Prng class.
constexpr u32 max_u32(u32 a, u32 b)
Min and max functions.
u32 extract_be_u32(const u8 *src)
Extract fields from a big-endian byte array.
void write_be_u64(u8 *dst, u64 val)
Store fields into a big-endian byte array.
u16 extract_be_u16(const u8 *src)
Extract fields from a big-endian byte array.
bool xor_reduce_u32(u32 x)
XOR-reduction of all bits in a word:
u16 sqrt_u32(u32 x)
Find integer square root y = floor(sqrt(x))
unsigned min_2n(u32 x, u32 y)
Given X and Y, find the minimum N such that X * 2^N >= Y.