Use least vs fast int types
This commit is contained in:
parent
2355feaa70
commit
c54fc75221
@ -34,10 +34,10 @@
|
||||
#endif
|
||||
|
||||
#if defined(GUF_DICT_32_BIT_HASH)
|
||||
#define GUF_DICT_HASH_T uint_fast32_t
|
||||
#define GUF_DICT_HASH_T uint_least32_t
|
||||
#define GUF_DICT_HASH_T_MAX GUF_UINT32_MAX
|
||||
#elif defined(GUF_DICT_64_BIT_HASH)
|
||||
#define GUF_DICT_HASH_T uint_fast64_t
|
||||
#define GUF_DICT_HASH_T uint_least64_t
|
||||
#define GUF_DICT_HASH_T_MAX GUF_UINT64_MAX
|
||||
#else
|
||||
#define GUF_DICT_HASH_T guf_hash_size_t
|
||||
|
||||
@ -21,18 +21,18 @@
|
||||
#define GUF_HASH32_INIT UINT32_C(2166136261)
|
||||
#define GUF_HASH64_INIT UINT64_C(14695981039346656037)
|
||||
|
||||
GUF_HASH_KWRDS uint_fast32_t guf_hash32(const void *data, ptrdiff_t num_bytes, uint_fast32_t hash); // FNV-1a (32 bit)
|
||||
GUF_HASH_KWRDS uint_fast64_t guf_hash64(const void *data, ptrdiff_t num_bytes, uint_fast64_t hash); // FNV-1a (64 bit)
|
||||
GUF_HASH_KWRDS uint_least32_t guf_hash32(const void *data, ptrdiff_t num_bytes, uint_least32_t hash); // FNV-1a (32 bit)
|
||||
GUF_HASH_KWRDS uint_least64_t guf_hash64(const void *data, ptrdiff_t num_bytes, uint_least64_t hash); // FNV-1a (64 bit)
|
||||
|
||||
#ifdef GUF_HASH_32_BIT
|
||||
typedef uint_fast32_t guf_hash_size_t;
|
||||
typedef uint_least32_t guf_hash_size_t;
|
||||
#define GUF_HASH_INIT GUF_HASH32_INIT
|
||||
#define GUF_HASH_MAX GUF_UINT32_MAX
|
||||
static inline guf_hash_size_t guf_hash(const void *data, ptrdiff_t num_bytes, guf_hash_size_t hash) {
|
||||
return guf_hash32(data, num_bytes, hash);
|
||||
}
|
||||
#else
|
||||
typedef uint_fast64_t guf_hash_size_t;
|
||||
typedef uint_least64_t guf_hash_size_t;
|
||||
#define GUF_HASH_INIT GUF_HASH64_INIT
|
||||
#define GUF_HASH_MAX GUF_UINT64_MAX
|
||||
static inline guf_hash_size_t guf_hash(const void *data, ptrdiff_t num_bytes, guf_hash_size_t hash) {
|
||||
@ -46,13 +46,13 @@ GUF_HASH_KWRDS uint_fast64_t guf_hash64(const void *data, ptrdiff_t num_bytes, u
|
||||
|
||||
#include "guf_assert.h"
|
||||
|
||||
GUF_HASH_KWRDS uint_fast32_t guf_hash32(const void *data, ptrdiff_t num_bytes, uint_fast32_t hash)
|
||||
GUF_HASH_KWRDS uint_least32_t guf_hash32(const void *data, ptrdiff_t num_bytes, uint_least32_t hash)
|
||||
{
|
||||
hash = GUF_UWRAP_32(hash);
|
||||
GUF_ASSERT_RELEASE(data);
|
||||
GUF_ASSERT_RELEASE(num_bytes >= 0);
|
||||
const unsigned char *data_bytes = (const unsigned char*)data; // This does not break strict-aliasing rules I think...
|
||||
const uint_fast32_t FNV_32_PRIME = UINT32_C(16777619);
|
||||
const uint_least32_t FNV_32_PRIME = UINT32_C(16777619);
|
||||
for (ptrdiff_t i = 0; i < num_bytes; ++i) {
|
||||
hash = GUF_UWRAP_32(1u * hash ^ data_bytes[i]);
|
||||
hash = GUF_UWRAP_32(1u * hash * FNV_32_PRIME);
|
||||
@ -60,13 +60,13 @@ GUF_HASH_KWRDS uint_fast32_t guf_hash32(const void *data, ptrdiff_t num_bytes, u
|
||||
return hash;
|
||||
}
|
||||
|
||||
GUF_HASH_KWRDS uint_fast64_t guf_hash64(const void *data, ptrdiff_t num_bytes, uint_fast64_t hash)
|
||||
GUF_HASH_KWRDS uint_least64_t guf_hash64(const void *data, ptrdiff_t num_bytes, uint_least64_t hash)
|
||||
{
|
||||
hash = GUF_UWRAP_64(hash);
|
||||
GUF_ASSERT_RELEASE(data);
|
||||
GUF_ASSERT_RELEASE(num_bytes >= 0);
|
||||
const unsigned char *data_bytes = (const unsigned char*)data; // This does not break strict-aliasing rules I think...
|
||||
const uint_fast64_t FNV_64_PRIME = UINT64_C(1099511628211);
|
||||
const uint_least64_t FNV_64_PRIME = UINT64_C(1099511628211);
|
||||
for (ptrdiff_t i = 0; i < num_bytes; ++i) {
|
||||
hash = GUF_UWRAP_64(1u * hash ^ data_bytes[i]);
|
||||
hash = GUF_UWRAP_64(1u * hash * FNV_64_PRIME);
|
||||
|
||||
@ -16,17 +16,10 @@
|
||||
#define GUF_MAX_F64_LT_ONE (1.0 - DBL_EPSILON/FLT_RADIX)
|
||||
|
||||
// Typesafe unsigned integer wrapping functions (generated with libguf/tools/intwrap-gen.py)
|
||||
static inline uint_least8_t guf_wrap8_uint_least8_t(uint_least8_t a) { return a & GUF_UINT8_MAX; }
|
||||
static inline uint_fast8_t guf_wrap8_uint_fast8_t(uint_fast8_t a) { return a & GUF_UINT8_MAX; }
|
||||
|
||||
static inline uint_least16_t guf_wrap16_uint_least16_t(uint_least16_t a) { return a & GUF_UINT16_MAX; }
|
||||
static inline uint_fast16_t guf_wrap16_uint_fast16_t(uint_fast16_t a) { return a & GUF_UINT16_MAX; }
|
||||
|
||||
static inline uint_least32_t guf_wrap32_uint_least32_t(uint_least32_t a) { return a & GUF_UINT32_MAX; }
|
||||
static inline uint_fast32_t guf_wrap32_uint_fast32_t(uint_fast32_t a) { return a & GUF_UINT32_MAX; }
|
||||
|
||||
static inline uint_least64_t guf_wrap64_uint_least64_t(uint_least64_t a) { return a & GUF_UINT64_MAX; }
|
||||
static inline uint_fast64_t guf_wrap64_uint_fast64_t(uint_fast64_t a) { return a & GUF_UINT64_MAX; }
|
||||
static inline uint_least8_t guf_wrap8_least_u8(uint_least8_t a) { return a & GUF_UINT8_MAX; }
|
||||
static inline uint_least16_t guf_wrap16_least_u16(uint_least16_t a) { return a & GUF_UINT16_MAX; }
|
||||
static inline uint_least32_t guf_wrap32_least_u32(uint_least32_t a) { return a & GUF_UINT32_MAX; }
|
||||
static inline uint_least64_t guf_wrap64_least_u64(uint_least64_t a) { return a & GUF_UINT64_MAX; }
|
||||
|
||||
static inline unsigned char guf_wrap8_uchar(unsigned char a) { return a & GUF_UINT8_MAX; } // unsigned char: >= 8 bits
|
||||
static inline unsigned short guf_wrap16_ushort(unsigned short a) { return a & GUF_UINT16_MAX; } // unsigned short: >= 16 bits
|
||||
@ -49,17 +42,17 @@ static inline unsigned long long guf_wrap64_ulong_long(unsigned long long a) { r
|
||||
}
|
||||
#endif
|
||||
|
||||
static inline uint_fast32_t guf_rotl32_uint_fast32_t(uint_fast32_t x, int k)
|
||||
static inline uint_least32_t guf_rotl32_least_u32(uint_least32_t x, int k)
|
||||
{
|
||||
GUF_ASSERT(k > 0);
|
||||
x = guf_wrap32_uint_fast32_t(x);
|
||||
return guf_wrap32_uint_fast32_t( (1u*x << k) | (1u*x >> (32 - k)) );
|
||||
x = guf_wrap32_least_u32(x);
|
||||
return guf_wrap32_least_u32( (1u*x << k) | (1u*x >> (32 - k)) );
|
||||
}
|
||||
static inline uint_fast64_t guf_rotl64_uint_fast64_t(uint_fast64_t x, int k)
|
||||
static inline uint_least64_t guf_rotl64_least_u64(uint_least64_t x, int k)
|
||||
{
|
||||
GUF_ASSERT(k > 0);
|
||||
x = guf_wrap64_uint_fast64_t(x);
|
||||
return guf_wrap64_uint_fast64_t( (1u*x << k) | (1u*x >> (64 - k)) );
|
||||
x = guf_wrap64_least_u64(x);
|
||||
return guf_wrap64_least_u64( (1u*x << k) | (1u*x >> (64 - k)) );
|
||||
}
|
||||
|
||||
static inline unsigned long guf_rotl32_ulong(unsigned long x, int k)
|
||||
@ -97,21 +90,21 @@ static inline ptrdiff_t guf_min_ptrdiff_t(ptrdiff_t a, ptrdiff_t b) { return a <
|
||||
static inline ptrdiff_t guf_max_ptrdiff_t(ptrdiff_t a, ptrdiff_t b) { return a > b ? a : b; }
|
||||
static inline ptrdiff_t guf_clamp_ptrdiff_t(ptrdiff_t x, ptrdiff_t min, ptrdiff_t max) { if (x < min) {return min;} if (x > max) {return max;} return x; }
|
||||
|
||||
static inline int_fast8_t guf_min_i8_fast(int_fast8_t a, int_fast8_t b) { return a < b ? a : b; }
|
||||
static inline int_fast8_t guf_max_i8_fast(int_fast8_t a, int_fast8_t b) { return a > b ? a : b; }
|
||||
static inline int_fast8_t guf_clamp_i8_fast(int_fast8_t x, int_fast8_t min, int_fast8_t max) { if (x < min) {return min;} if (x > max) {return max;} return x; }
|
||||
static inline int_least8_t guf_min_least_i8(int_least8_t a, int_least8_t b) { return a < b ? a : b; }
|
||||
static inline int_least8_t guf_max_least_i8(int_least8_t a, int_least8_t b) { return a > b ? a : b; }
|
||||
static inline int_least8_t guf_clamp_least_i8(int_least8_t x, int_least8_t min, int_least8_t max) { if (x < min) {return min;} if (x > max) {return max;} return x; }
|
||||
|
||||
static inline int_fast16_t guf_min_i16_fast(int_fast16_t a, int_fast16_t b) { return a < b ? a : b; }
|
||||
static inline int_fast16_t guf_max_i16_fast(int_fast16_t a, int_fast16_t b) { return a > b ? a : b; }
|
||||
static inline int_fast16_t guf_clamp_i16_fast(int_fast16_t x, int_fast16_t min, int_fast16_t max) { if (x < min) {return min;} if (x > max) {return max;} return x; }
|
||||
static inline int_least16_t guf_min_least_i16(int_least16_t a, int_least16_t b) { return a < b ? a : b; }
|
||||
static inline int_least16_t guf_max_least_i16(int_least16_t a, int_least16_t b) { return a > b ? a : b; }
|
||||
static inline int_least16_t guf_clamp_least_i16(int_least16_t x, int_least16_t min, int_least16_t max) { if (x < min) {return min;} if (x > max) {return max;} return x; }
|
||||
|
||||
static inline int_fast32_t guf_min_i32_fast(int_fast32_t a, int_fast32_t b) { return a < b ? a : b; }
|
||||
static inline int_fast32_t guf_max_i32_fast(int_fast32_t a, int_fast32_t b) { return a > b ? a : b; }
|
||||
static inline int_fast32_t guf_clamp_i32_fast(int_fast32_t x, int_fast32_t min, int_fast32_t max) { if (x < min) {return min;} if (x > max) {return max;} return x; }
|
||||
static inline int_least32_t guf_min_least_i32(int_least32_t a, int_least32_t b) { return a < b ? a : b; }
|
||||
static inline int_least32_t guf_max_least_i32(int_least32_t a, int_least32_t b) { return a > b ? a : b; }
|
||||
static inline int_least32_t guf_clamp_least_i32(int_least32_t x, int_least32_t min, int_least32_t max) { if (x < min) {return min;} if (x > max) {return max;} return x; }
|
||||
|
||||
static inline int_fast64_t guf_min_i64_fast(int_fast64_t a, int_fast64_t b) { return a < b ? a : b; }
|
||||
static inline int_fast64_t guf_max_i64_fast(int_fast64_t a, int_fast64_t b) { return a > b ? a : b; }
|
||||
static inline int_fast64_t guf_clamp_i64_fast(int_fast64_t x, int_fast64_t min, int_fast64_t max) { if (x < min) {return min;} if (x > max) {return max;} return x; }
|
||||
static inline int_least64_t guf_min_least_i64(int_least64_t a, int_least64_t b) { return a < b ? a : b; }
|
||||
static inline int_least64_t guf_max_least_i64(int_least64_t a, int_least64_t b) { return a > b ? a : b; }
|
||||
static inline int_least64_t guf_clamp_least_i64(int_least64_t x, int_least64_t min, int_least64_t max) { if (x < min) {return min;} if (x > max) {return max;} return x; }
|
||||
|
||||
static inline float guf_min_f32(float a, float b) { return a < b ? a : b; }
|
||||
static inline float guf_max_f32(float a, float b) { return a > b ? a : b; }
|
||||
@ -166,21 +159,21 @@ static inline size_t guf_min_size_t(size_t a, size_t b) { return a < b ? a : b;
|
||||
static inline size_t guf_max_size_t(size_t a, size_t b) { return a > b ? a : b; }
|
||||
static inline size_t guf_clamp_size_t(size_t x, size_t min, size_t max) { if (x < min) {return min;} if (x > max) {return max;} return x; }
|
||||
|
||||
static inline uint_fast8_t guf_min_u8_fast(uint_fast8_t a, uint_fast8_t b) { return a < b ? a : b; }
|
||||
static inline uint_fast8_t guf_max_u8_fast(uint_fast8_t a, uint_fast8_t b) { return a > b ? a : b; }
|
||||
static inline uint_fast8_t guf_clamp_u8_fast(uint_fast8_t x, uint_fast8_t min, uint_fast8_t max) { if (x < min) {return min;} if (x > max) {return max;} return x; }
|
||||
static inline uint_least8_t guf_min_least_u8(uint_least8_t a, uint_least8_t b) { return a < b ? a : b; }
|
||||
static inline uint_least8_t guf_max_least_u8(uint_least8_t a, uint_least8_t b) { return a > b ? a : b; }
|
||||
static inline uint_least8_t guf_clamp_least_u8(uint_least8_t x, uint_least8_t min, uint_least8_t max) { if (x < min) {return min;} if (x > max) {return max;} return x; }
|
||||
|
||||
static inline uint_fast16_t guf_min_u16_fast(uint_fast16_t a, uint_fast16_t b) { return a < b ? a : b; }
|
||||
static inline uint_fast16_t guf_max_u16_fast(uint_fast16_t a, uint_fast16_t b) { return a > b ? a : b; }
|
||||
static inline uint_fast16_t guf_clamp_u16_fast(uint_fast16_t x, uint_fast16_t min, uint_fast16_t max) { if (x < min) {return min;} if (x > max) {return max;} return x; }
|
||||
static inline uint_least16_t guf_min_least_u16(uint_least16_t a, uint_least16_t b) { return a < b ? a : b; }
|
||||
static inline uint_least16_t guf_max_least_u16(uint_least16_t a, uint_least16_t b) { return a > b ? a : b; }
|
||||
static inline uint_least16_t guf_clamp_least_u16(uint_least16_t x, uint_least16_t min, uint_least16_t max) { if (x < min) {return min;} if (x > max) {return max;} return x; }
|
||||
|
||||
static inline uint_fast32_t guf_min_u32_fast(uint_fast32_t a, uint_fast32_t b) { return a < b ? a : b; }
|
||||
static inline uint_fast32_t guf_max_u32_fast(uint_fast32_t a, uint_fast32_t b) { return a > b ? a : b; }
|
||||
static inline uint_fast32_t guf_clamp_u32_fast(uint_fast32_t x, uint_fast32_t min, uint_fast32_t max) { if (x < min) {return min;} if (x > max) {return max;} return x; }
|
||||
static inline uint_least32_t guf_min_least_u32(uint_least32_t a, uint_least32_t b) { return a < b ? a : b; }
|
||||
static inline uint_least32_t guf_max_least_u32(uint_least32_t a, uint_least32_t b) { return a > b ? a : b; }
|
||||
static inline uint_least32_t guf_clamp_least_u32(uint_least32_t x, uint_least32_t min, uint_least32_t max) { if (x < min) {return min;} if (x > max) {return max;} return x; }
|
||||
|
||||
static inline uint_fast64_t guf_min_u64_fast(uint_fast64_t a, uint_fast64_t b) { return a < b ? a : b; }
|
||||
static inline uint_fast64_t guf_max_u64_fast(uint_fast64_t a, uint_fast64_t b) { return a > b ? a : b; }
|
||||
static inline uint_fast64_t guf_clamp_u64_fast(uint_fast64_t x, uint_fast64_t min, uint_fast64_t max) { if (x < min) {return min;} if (x > max) {return max;} return x; }
|
||||
static inline uint_least64_t guf_min_least_u64(uint_least64_t a, uint_least64_t b) { return a < b ? a : b; }
|
||||
static inline uint_least64_t guf_max_least_u64(uint_least64_t a, uint_least64_t b) { return a > b ? a : b; }
|
||||
static inline uint_least64_t guf_clamp_least_u64(uint_least64_t x, uint_least64_t min, uint_least64_t max) { if (x < min) {return min;} if (x > max) {return max;} return x; }
|
||||
|
||||
#ifdef UINT8_MAX
|
||||
static inline uint8_t guf_min_u8(uint8_t a, uint8_t b) { return a < b ? a : b; }
|
||||
@ -242,10 +235,10 @@ static inline unsigned long guf_absdiff_long(long a, long b)
|
||||
static inline unsigned long long guf_absdiff_long_long(long long a, long long b) {return a > b ? (unsigned long long)a - (unsigned long long)b : (unsigned long long)b - (unsigned long long)a;}
|
||||
static inline size_t guf_absdiff_ptrdiff_t(ptrdiff_t a, ptrdiff_t b) {return a > b ? (size_t)a - (size_t)b : (size_t)b - (size_t)a;}
|
||||
|
||||
static inline uint_fast8_t guf_absdiff_int_fast8_t(int_fast8_t a, int_fast8_t b) {return a > b ? GUF_UWRAP_8( (uint_fast8_t)a - (uint_fast8_t)b) : GUF_UWRAP_8( (uint_fast8_t)b - (uint_fast8_t)a);}
|
||||
static inline uint_fast16_t guf_absdiff_int_fast16_t(int_fast16_t a, int_fast16_t b) {return a > b ? GUF_UWRAP_16((uint_fast16_t)a - (uint_fast16_t)b) : GUF_UWRAP_16((uint_fast16_t)b - (uint_fast16_t)a);}
|
||||
static inline uint_fast32_t guf_absdiff_int_fast32_t(int_fast32_t a, int_fast32_t b) {return a > b ? GUF_UWRAP_32((uint_fast32_t)a - (uint_fast32_t)b) : GUF_UWRAP_32((uint_fast32_t)b - (uint_fast32_t)a);}
|
||||
static inline uint_fast64_t guf_absdiff_int_fast64_t(int_fast64_t a, int_fast64_t b) {return a > b ? GUF_UWRAP_64((uint_fast64_t)a - (uint_fast64_t)b) : GUF_UWRAP_64((uint_fast64_t)b - (uint_fast64_t)a);}
|
||||
static inline uint_least8_t guf_absdiff_least_i8(int_least8_t a, int_least8_t b) {return a > b ? GUF_UWRAP_8( (uint_least8_t)a - (uint_least8_t)b) : GUF_UWRAP_8( (uint_least8_t)b - (uint_least8_t)a);}
|
||||
static inline uint_least16_t guf_absdiff_least_i16(int_least16_t a, int_least16_t b) {return a > b ? GUF_UWRAP_16((uint_least16_t)a - (uint_least16_t)b) : GUF_UWRAP_16((uint_least16_t)b - (uint_least16_t)a);}
|
||||
static inline uint_least32_t guf_absdiff_least_i32(int_least32_t a, int_least32_t b) {return a > b ? GUF_UWRAP_32((uint_least32_t)a - (uint_least32_t)b) : GUF_UWRAP_32((uint_least32_t)b - (uint_least32_t)a);}
|
||||
static inline uint_least64_t guf_absdiff_least_i64(int_least64_t a, int_least64_t b) {return a > b ? GUF_UWRAP_64((uint_least64_t)a - (uint_least64_t)b) : GUF_UWRAP_64((uint_least64_t)b - (uint_least64_t)a);}
|
||||
|
||||
#if defined(UINT8_MAX) && defined(INT8_MAX)
|
||||
static inline uint8_t guf_absdiff_i8(int8_t a, int8_t b) {return a > b ? (uint8_t)a - (uint8_t)b : (uint8_t)b - (uint8_t)a;}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
196
src/guf_rand.h
196
src/guf_rand.h
@ -26,24 +26,24 @@
|
||||
|
||||
// State for xoshiro128** 1.1
|
||||
typedef struct guf_rand32_state {
|
||||
uint_fast32_t s[4]; // Must not be all zero.
|
||||
uint_least32_t s[4]; // Must not be all zero.
|
||||
} guf_rand32_state;
|
||||
|
||||
// State for xoshiro256** 1.0
|
||||
typedef struct guf_rand64_state {
|
||||
uint_fast64_t s[4]; // Must not be all zero.
|
||||
uint_least64_t s[4]; // Must not be all zero.
|
||||
} guf_rand64_state;
|
||||
|
||||
#ifdef GUF_RAND_32_BIT
|
||||
// Use guf_rand32_state (i.e. xoshiro128** 1.1) as default.
|
||||
#define GUF_RAND_MAX GUF_UINT32_MAX
|
||||
typedef guf_rand32_state guf_randstate;
|
||||
typedef uint_fast32_t guf_rand_seed_t;
|
||||
typedef uint_least32_t guf_rand_seed_t;
|
||||
#else
|
||||
// Use guf_rand64_state (i.e. xoshiro256** 1.0) as default.
|
||||
#define GUF_RAND_MAX GUF_UINT64_MAX
|
||||
typedef guf_rand64_state guf_randstate;
|
||||
typedef uint_fast64_t guf_rand_seed_t;
|
||||
typedef uint_least64_t guf_rand_seed_t;
|
||||
#endif
|
||||
|
||||
/*
|
||||
@ -55,8 +55,8 @@ typedef struct guf_rand64_state {
|
||||
(If you want to initialise the guf_randstate struct manually, you have to ensure yourself the four state-integers aren't all zero.)
|
||||
*/
|
||||
GUF_RAND_KWRDS void guf_randstate_init(guf_randstate *state, guf_rand_seed_t seed);
|
||||
GUF_RAND_KWRDS void guf_rand64_state_init(guf_rand64_state *state, uint_fast64_t seed);
|
||||
GUF_RAND_KWRDS void guf_rand32_state_init(guf_rand32_state *state, uint_fast32_t seed);
|
||||
GUF_RAND_KWRDS void guf_rand64_state_init(guf_rand64_state *state, uint_least64_t seed);
|
||||
GUF_RAND_KWRDS void guf_rand32_state_init(guf_rand32_state *state, uint_least32_t seed);
|
||||
|
||||
/*
|
||||
- guf_randstate_jump(state)
|
||||
@ -76,24 +76,24 @@ GUF_RAND_KWRDS void guf_rand32_state_jump(guf_rand32_state *state); // Equivalen
|
||||
- guf_rand_splitmix32(state) -> u32 in range [0, UINT32_MAX]
|
||||
(Very simple rng with only 32-bits of state; used for "scrambling" 32-bit seeds in guf_randstate_init.)
|
||||
*/
|
||||
GUF_RAND_KWRDS uint_fast64_t guf_rand_splitmix64(uint_fast64_t *state);
|
||||
GUF_RAND_KWRDS uint_fast32_t guf_rand_splitmix32(uint_fast32_t *state);
|
||||
GUF_RAND_KWRDS uint_least64_t guf_rand_splitmix64(uint_least64_t *state);
|
||||
GUF_RAND_KWRDS uint_least32_t guf_rand_splitmix32(uint_least32_t *state);
|
||||
|
||||
/*
|
||||
- guf_rand_u32(state) -> u32 in range [0, UINT32_MAX]
|
||||
*/
|
||||
GUF_RAND_KWRDS uint_fast32_t guf_rand_u32(guf_randstate *state);
|
||||
GUF_RAND_KWRDS uint_fast32_t guf_rand64_u32(guf_rand64_state *state);
|
||||
GUF_RAND_KWRDS uint_fast32_t guf_rand32_u32(guf_rand32_state *state);
|
||||
GUF_RAND_KWRDS uint_least32_t guf_rand_u32(guf_randstate *state);
|
||||
GUF_RAND_KWRDS uint_least32_t guf_rand64_u32(guf_rand64_state *state);
|
||||
GUF_RAND_KWRDS uint_least32_t guf_rand32_u32(guf_rand32_state *state);
|
||||
|
||||
/*
|
||||
- guf_rand_u64(state) -> uint64_t (or uint_least64_t) in range [0, UINT64_MAX]
|
||||
NOTE: May be slow on 32-bit platforms.
|
||||
NOTE: If uint64_t is not available (optional according to the standards), use uint_least64_t (always available in C99 and above).
|
||||
*/
|
||||
GUF_RAND_KWRDS uint_fast64_t guf_rand_u64(guf_randstate *state);
|
||||
GUF_RAND_KWRDS uint_fast64_t guf_rand32_u64(guf_rand32_state *state);
|
||||
GUF_RAND_KWRDS uint_fast64_t guf_rand64_u64(guf_rand64_state *state);
|
||||
GUF_RAND_KWRDS uint_least64_t guf_rand_u64(guf_randstate *state);
|
||||
GUF_RAND_KWRDS uint_least64_t guf_rand32_u64(guf_rand32_state *state);
|
||||
GUF_RAND_KWRDS uint_least64_t guf_rand64_u64(guf_rand64_state *state);
|
||||
|
||||
/*
|
||||
- guf_rand_f64(state) -> double in range [0.0, 1.0)
|
||||
@ -133,21 +133,21 @@ GUF_RAND_KWRDS double guf_rand32_range_f64(guf_rand32_state *state, double min,
|
||||
NOTE: guf_randrange_u32 may be slow on 32-bit platforms (as it calls guf_rand_f64).
|
||||
This does not apply to guf_randrange_i32 (as it doesn't call guf_rand_f64).
|
||||
*/
|
||||
GUF_RAND_KWRDS int_fast32_t guf_randrange_i32(guf_randstate *state, int_fast32_t min, int_fast32_t max);
|
||||
GUF_RAND_KWRDS uint_fast32_t guf_randrange_u32(guf_randstate *state, uint_fast32_t min, uint_fast32_t max); // NOTE: may be slow on 32-bit platforms (as it calls guf_rand_f64).
|
||||
GUF_RAND_KWRDS int_least32_t guf_randrange_i32(guf_randstate *state, int_least32_t min, int_least32_t max);
|
||||
GUF_RAND_KWRDS uint_least32_t guf_randrange_u32(guf_randstate *state, uint_least32_t min, uint_least32_t max); // NOTE: may be slow on 32-bit platforms (as it calls guf_rand_f64).
|
||||
|
||||
GUF_RAND_KWRDS int_fast32_t guf_rand64_range_i32(guf_rand64_state *state, int_fast32_t min, int_fast32_t max);
|
||||
GUF_RAND_KWRDS uint_fast32_t guf_rand64_range_u32(guf_rand64_state *state, uint_fast32_t min, uint_fast32_t max);
|
||||
GUF_RAND_KWRDS int_least32_t guf_rand64_range_i32(guf_rand64_state *state, int_least32_t min, int_least32_t max);
|
||||
GUF_RAND_KWRDS uint_least32_t guf_rand64_range_u32(guf_rand64_state *state, uint_least32_t min, uint_least32_t max);
|
||||
|
||||
GUF_RAND_KWRDS int_fast32_t guf_rand32_range_i32(guf_rand32_state *state, int_fast32_t min, int_fast32_t max);
|
||||
GUF_RAND_KWRDS uint_fast32_t guf_rand32_range_u32(guf_rand32_state *state, uint_fast32_t min, uint_fast32_t max); // NOTE: may be slow on 32-bit platforms (as it calls guf_rand_f64).
|
||||
GUF_RAND_KWRDS int_least32_t guf_rand32_range_i32(guf_rand32_state *state, int_least32_t min, int_least32_t max);
|
||||
GUF_RAND_KWRDS uint_least32_t guf_rand32_range_u32(guf_rand32_state *state, uint_least32_t min, uint_least32_t max); // NOTE: may be slow on 32-bit platforms (as it calls guf_rand_f64).
|
||||
|
||||
/*
|
||||
- guf_randrange_i64(state, min, max) -> int64_t in range [min, max] (contrary to the float equivalents, max *is* inclusive)
|
||||
*/
|
||||
GUF_RAND_KWRDS int_fast64_t guf_randrange_i64(guf_randstate *state, int_fast64_t min, int_fast64_t max);
|
||||
GUF_RAND_KWRDS int_fast64_t guf_rand64_range_i64(guf_rand64_state *state, int_fast64_t min, int_fast64_t max);
|
||||
GUF_RAND_KWRDS int_fast64_t guf_rand32_range_i64(guf_rand32_state *state, int_fast64_t min, int_fast64_t max);
|
||||
GUF_RAND_KWRDS int_least64_t guf_randrange_i64(guf_randstate *state, int_least64_t min, int_least64_t max);
|
||||
GUF_RAND_KWRDS int_least64_t guf_rand64_range_i64(guf_rand64_state *state, int_least64_t min, int_least64_t max);
|
||||
GUF_RAND_KWRDS int_least64_t guf_rand32_range_i64(guf_rand32_state *state, int_least64_t min, int_least64_t max);
|
||||
|
||||
// Bernoulli-trials:
|
||||
|
||||
@ -210,11 +210,11 @@ GUF_RAND_KWRDS double guf_rand32_normal_sample_one_f64(guf_rand32_state *state,
|
||||
splitmix64 written in 2015 by Sebastiano Vigna (vigna@acm.org) (released as public domain)
|
||||
cf. https://prng.di.unimi.it/splitmix64.c (last-retrieved 2025-02-11)
|
||||
*/
|
||||
GUF_RAND_KWRDS uint_fast64_t guf_rand_splitmix64(uint_fast64_t *state)
|
||||
GUF_RAND_KWRDS uint_least64_t guf_rand_splitmix64(uint_least64_t *state)
|
||||
{
|
||||
GUF_ASSERT(state);
|
||||
*state = GUF_UWRAP_64(*state);
|
||||
uint_fast64_t z = ( *state = GUF_UWRAP_64(*state + 0x9e3779b97f4a7c15ull) );
|
||||
uint_least64_t z = ( *state = GUF_UWRAP_64(*state + 0x9e3779b97f4a7c15ull) );
|
||||
z = GUF_UWRAP_64( GUF_UWRAP_64(z ^ (z >> 30u)) * 0xbf58476d1ce4e5b9ull );
|
||||
z = GUF_UWRAP_64( GUF_UWRAP_64(z ^ (z >> 27u)) * 0x94d049bb133111ebull );
|
||||
return GUF_UWRAP_64(z ^ (z >> 31u));
|
||||
@ -224,16 +224,16 @@ GUF_RAND_KWRDS uint_fast64_t guf_rand_splitmix64(uint_fast64_t *state)
|
||||
splitmix32 written in 2016 by Kaito Udagawa (released under CC0 <http://creativecommons.org/publicdomain/zero/1.0/>)
|
||||
cf. https://github.com/umireon/my-random-stuff/blob/master/xorshift/splitmix32.c (last-retrieved 2025-03-28)
|
||||
*/
|
||||
GUF_RAND_KWRDS uint_fast32_t guf_rand_splitmix32(uint_fast32_t *state)
|
||||
GUF_RAND_KWRDS uint_least32_t guf_rand_splitmix32(uint_least32_t *state)
|
||||
{
|
||||
GUF_ASSERT(state);
|
||||
uint_fast32_t z = ( *state = GUF_UWRAP_32(*state + 0x9e3779b9ul) );
|
||||
uint_least32_t z = ( *state = GUF_UWRAP_32(*state + 0x9e3779b9ul) );
|
||||
z = GUF_UWRAP_32( GUF_UWRAP_32(z ^ (z >> 16u)) * 0x85ebca6bul );
|
||||
z = GUF_UWRAP_32( GUF_UWRAP_32(z ^ (z >> 13u)) * 0xc2b2ae35ul );
|
||||
return GUF_UWRAP_32(z ^ (z >> 16u));
|
||||
}
|
||||
|
||||
GUF_RAND_KWRDS void guf_rand32_state_init(guf_rand32_state *state, uint_fast32_t seed)
|
||||
GUF_RAND_KWRDS void guf_rand32_state_init(guf_rand32_state *state, uint_least32_t seed)
|
||||
{
|
||||
for (size_t i = 0; i < GUF_ARR_SIZE(state->s); ++i) {
|
||||
state->s[i] = guf_rand_splitmix32(&seed);
|
||||
@ -247,7 +247,7 @@ GUF_RAND_KWRDS void guf_rand32_state_init(guf_rand32_state *state, uint_fast32_t
|
||||
}
|
||||
}
|
||||
|
||||
GUF_RAND_KWRDS void guf_rand64_state_init(guf_rand64_state *state, uint_fast64_t seed)
|
||||
GUF_RAND_KWRDS void guf_rand64_state_init(guf_rand64_state *state, uint_least64_t seed)
|
||||
{
|
||||
for (size_t i = 0; i < GUF_ARR_SIZE(state->s); ++i) {
|
||||
state->s[i] = guf_rand_splitmix64(&seed);
|
||||
@ -270,7 +270,7 @@ GUF_RAND_KWRDS void guf_randstate_init(guf_randstate *state, guf_rand_seed_t see
|
||||
#endif
|
||||
}
|
||||
|
||||
GUF_RAND_KWRDS uint_fast32_t guf_rand32_u32(guf_rand32_state *state)
|
||||
GUF_RAND_KWRDS uint_least32_t guf_rand32_u32(guf_rand32_state *state)
|
||||
{
|
||||
GUF_ASSERT(state);
|
||||
GUF_ASSERT(state->s[0] || state->s[1] || state->s[2] || state->s[3]);
|
||||
@ -278,15 +278,15 @@ GUF_RAND_KWRDS uint_fast32_t guf_rand32_u32(guf_rand32_state *state)
|
||||
xoshiro128** 1.1 (public domain) written in 2018 by David Blackman and Sebastiano Vigna (vigna@acm.org)
|
||||
cf. https://prng.di.unimi.it/xoshiro128starstar.c (last-retrieved 2025-02-11)
|
||||
*/
|
||||
const uint_fast32_t result = GUF_UWRAP_32( guf_rotl32_uint_fast32_t(state->s[1] * 5u, 7) * 9u );
|
||||
const uint_fast32_t t = GUF_UWRAP_32( state->s[1] << 9u );
|
||||
const uint_least32_t result = GUF_UWRAP_32( guf_rotl32_least_u32(state->s[1] * 5u, 7) * 9u );
|
||||
const uint_least32_t t = GUF_UWRAP_32( state->s[1] << 9u );
|
||||
|
||||
state->s[2] ^= state->s[0];
|
||||
state->s[3] ^= state->s[1];
|
||||
state->s[1] ^= state->s[2];
|
||||
state->s[0] ^= state->s[3];
|
||||
state->s[2] ^= t;
|
||||
state->s[3] = guf_rotl32_uint_fast32_t(state->s[3], 11);
|
||||
state->s[3] = guf_rotl32_least_u32(state->s[3], 11);
|
||||
|
||||
state->s[0] = GUF_UWRAP_32(state->s[0]);
|
||||
state->s[1] = GUF_UWRAP_32(state->s[1]);
|
||||
@ -296,12 +296,12 @@ GUF_RAND_KWRDS uint_fast32_t guf_rand32_u32(guf_rand32_state *state)
|
||||
return result;
|
||||
}
|
||||
|
||||
GUF_RAND_KWRDS uint_fast32_t guf_rand64_u32(guf_rand64_state *state)
|
||||
GUF_RAND_KWRDS uint_least32_t guf_rand64_u32(guf_rand64_state *state)
|
||||
{
|
||||
return (uint_fast32_t)GUF_UWRAP_32( (guf_rand64_u64(state) >> 32u) );
|
||||
return (uint_least32_t)GUF_UWRAP_32( (guf_rand64_u64(state) >> 32u) );
|
||||
}
|
||||
|
||||
GUF_RAND_KWRDS uint_fast32_t guf_rand_u32(guf_randstate *state)
|
||||
GUF_RAND_KWRDS uint_least32_t guf_rand_u32(guf_randstate *state)
|
||||
{
|
||||
GUF_ASSERT(state);
|
||||
GUF_ASSERT(state->s[0] || state->s[1] || state->s[2] || state->s[3]);
|
||||
@ -313,7 +313,7 @@ GUF_RAND_KWRDS uint_fast32_t guf_rand_u32(guf_randstate *state)
|
||||
}
|
||||
|
||||
|
||||
GUF_RAND_KWRDS uint_fast64_t guf_rand64_u64(guf_rand64_state *state)
|
||||
GUF_RAND_KWRDS uint_least64_t guf_rand64_u64(guf_rand64_state *state)
|
||||
{
|
||||
GUF_ASSERT(state);
|
||||
GUF_ASSERT(state->s[0] || state->s[1] || state->s[2] || state->s[3]);
|
||||
@ -321,15 +321,15 @@ GUF_RAND_KWRDS uint_fast64_t guf_rand64_u64(guf_rand64_state *state)
|
||||
xoshiro256** 1.0 (public domain) written in 2018 by David Blackman and Sebastiano Vigna (vigna@acm.org)
|
||||
cf. https://prng.di.unimi.it/xoshiro256starstar.c (last-retrieved 2025-02-11)
|
||||
*/
|
||||
const uint_fast64_t result = GUF_UWRAP_64( guf_rotl64_uint_fast64_t(state->s[1] * 5u, 7) * 9u );
|
||||
const uint_fast64_t t = GUF_UWRAP_64( state->s[1] << 17u );
|
||||
const uint_least64_t result = GUF_UWRAP_64( guf_rotl64_least_u64(state->s[1] * 5u, 7) * 9u );
|
||||
const uint_least64_t t = GUF_UWRAP_64( state->s[1] << 17u );
|
||||
|
||||
state->s[2] ^= state->s[0];
|
||||
state->s[3] ^= state->s[1];
|
||||
state->s[1] ^= state->s[2];
|
||||
state->s[0] ^= state->s[3];
|
||||
state->s[2] ^= t;
|
||||
state->s[3] = guf_rotl64_uint_fast64_t(state->s[3], 45);
|
||||
state->s[3] = guf_rotl64_least_u64(state->s[3], 45);
|
||||
|
||||
state->s[0] = GUF_UWRAP_64(state->s[0]);
|
||||
state->s[1] = GUF_UWRAP_64(state->s[1]);
|
||||
@ -339,19 +339,19 @@ GUF_RAND_KWRDS uint_fast64_t guf_rand64_u64(guf_rand64_state *state)
|
||||
return result;
|
||||
}
|
||||
|
||||
GUF_RAND_KWRDS uint_fast64_t guf_rand32_u64(guf_rand32_state *state)
|
||||
GUF_RAND_KWRDS uint_least64_t guf_rand32_u64(guf_rand32_state *state)
|
||||
{
|
||||
GUF_ASSERT(state);
|
||||
GUF_ASSERT(state->s[0] || state->s[1] || state->s[2] || state->s[3]);
|
||||
const uint_fast32_t lower_bits = guf_rand32_u32(state);
|
||||
const uint_fast32_t upper_bits = guf_rand32_u32(state);
|
||||
const uint_least32_t lower_bits = guf_rand32_u32(state);
|
||||
const uint_least32_t upper_bits = guf_rand32_u32(state);
|
||||
GUF_ASSERT( lower_bits <= GUF_UINT32_MAX && upper_bits <= GUF_UINT32_MAX );
|
||||
GUF_ASSERT( ( ((uint_fast64_t)upper_bits << 32u) | (uint_fast64_t)lower_bits ) <= GUF_UINT32_MAX);
|
||||
return ((uint_fast64_t)upper_bits << 32u) | (uint_fast64_t)lower_bits; // TODO: not sure if that's a good idea...
|
||||
GUF_ASSERT( ( ((uint_least64_t)upper_bits << 32u) | (uint_least64_t)lower_bits ) <= GUF_UINT32_MAX);
|
||||
return ((uint_least64_t)upper_bits << 32u) | (uint_least64_t)lower_bits; // TODO: not sure if that's a good idea...
|
||||
|
||||
}
|
||||
|
||||
GUF_RAND_KWRDS uint_fast64_t guf_rand_u64(guf_randstate *state)
|
||||
GUF_RAND_KWRDS uint_least64_t guf_rand_u64(guf_randstate *state)
|
||||
{
|
||||
#ifdef GUF_RAND_32_BIT
|
||||
return guf_rand32_u64(state);
|
||||
@ -367,11 +367,11 @@ GUF_RAND_KWRDS uint_fast64_t guf_rand_u64(guf_randstate *state)
|
||||
GUF_RAND_KWRDS void guf_rand32_state_jump(guf_rand32_state *state)
|
||||
{
|
||||
GUF_ASSERT(state);
|
||||
static const uint_fast32_t JUMP[] = { 0x8764000b, 0xf542d2d3, 0x6fa035c3, 0x77f2db5b };
|
||||
uint_fast32_t s0 = 0;
|
||||
uint_fast32_t s1 = 0;
|
||||
uint_fast32_t s2 = 0;
|
||||
uint_fast32_t s3 = 0;
|
||||
static const uint_least32_t JUMP[] = { 0x8764000b, 0xf542d2d3, 0x6fa035c3, 0x77f2db5b };
|
||||
uint_least32_t s0 = 0;
|
||||
uint_least32_t s1 = 0;
|
||||
uint_least32_t s2 = 0;
|
||||
uint_least32_t s3 = 0;
|
||||
for (size_t i = 0; i < sizeof JUMP / sizeof *JUMP; ++i) {
|
||||
for (int b = 0; b < 32; ++b) {
|
||||
if (1u * JUMP[i] & UINT32_C(1) << b) {
|
||||
@ -399,11 +399,11 @@ GUF_RAND_KWRDS void guf_rand32_state_jump(guf_rand32_state *state)
|
||||
*/
|
||||
GUF_RAND_KWRDS void guf_rand64_state_jump(guf_rand64_state *state)
|
||||
{
|
||||
static const uint_fast64_t JUMP[] = { 0x180ec6d33cfd0aba, 0xd5a61266f0c9392c, 0xa9582618e03fc9aa, 0x39abdc4529b1661c };
|
||||
uint_fast64_t s0 = 0;
|
||||
uint_fast64_t s1 = 0;
|
||||
uint_fast64_t s2 = 0;
|
||||
uint_fast64_t s3 = 0;
|
||||
static const uint_least64_t JUMP[] = { 0x180ec6d33cfd0aba, 0xd5a61266f0c9392c, 0xa9582618e03fc9aa, 0x39abdc4529b1661c };
|
||||
uint_least64_t s0 = 0;
|
||||
uint_least64_t s1 = 0;
|
||||
uint_least64_t s2 = 0;
|
||||
uint_least64_t s3 = 0;
|
||||
for (size_t i = 0; i < sizeof JUMP / sizeof *JUMP; ++i) {
|
||||
for (int b = 0; b < 64; ++b) {
|
||||
if (1u * JUMP[i] & UINT64_C(1) << b) {
|
||||
@ -627,7 +627,7 @@ GUF_RAND_KWRDS float guf_randrange_f32(guf_randstate *state, float min, float en
|
||||
|
||||
|
||||
// returns uniformly-distributed random i32 in range [min, max] (max is inclusive as opposed to the f32/f64 versions)
|
||||
GUF_RAND_KWRDS int_fast32_t guf_rand64_range_i32(guf_rand64_state *state, int_fast32_t min, int_fast32_t max)
|
||||
GUF_RAND_KWRDS int_least32_t guf_rand64_range_i32(guf_rand64_state *state, int_least32_t min, int_least32_t max)
|
||||
{
|
||||
GUF_ASSERT_RELEASE(max >= min);
|
||||
if (min == max) {
|
||||
@ -637,21 +637,21 @@ GUF_RAND_KWRDS int_fast32_t guf_rand64_range_i32(guf_rand64_state *state, int_fa
|
||||
// cf. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random (last-retrieved 2025-02-12)
|
||||
const double result = floor(guf_rand64_f64(state) * (delta + 1.0) + min);
|
||||
GUF_ASSERT(result >= min && result <= max);
|
||||
GUF_ASSERT((int_fast32_t)result <= GUF_INT32_MAX && (int_fast32_t)result >= GUF_INT32_MIN);
|
||||
return (int_fast32_t)result;
|
||||
GUF_ASSERT((int_least32_t)result <= GUF_INT32_MAX && (int_least32_t)result >= GUF_INT32_MIN);
|
||||
return (int_least32_t)result;
|
||||
}
|
||||
|
||||
// returns uniformly-distributed random i32 in range [min, max] (max is inclusive as opposed to the f32/f64 versions)
|
||||
GUF_RAND_KWRDS int_fast32_t guf_rand32_range_i32(guf_rand32_state *state, int_fast32_t min, int_fast32_t max)
|
||||
GUF_RAND_KWRDS int_least32_t guf_rand32_range_i32(guf_rand32_state *state, int_least32_t min, int_least32_t max)
|
||||
{
|
||||
GUF_ASSERT_RELEASE(max >= min);
|
||||
if (min == max) {
|
||||
return min;
|
||||
}
|
||||
|
||||
const uint_fast32_t rand_max_i32 = GUF_UWRAP_32(GUF_UINT32_MAX >> 1u); // 2^31 - 1 (== INT32_MAX)
|
||||
const uint_least32_t rand_max_i32 = GUF_UWRAP_32(GUF_UINT32_MAX >> 1u); // 2^31 - 1 (== INT32_MAX)
|
||||
|
||||
const uint_fast32_t delta = guf_absdiff_int_fast32_t(max, min);
|
||||
const uint_least32_t delta = guf_absdiff_least_i32(max, min);
|
||||
if (delta > rand_max_i32) {
|
||||
guf_panic(GUF_ERR_INT_OVERFLOW, GUF_ERR_MSG("in function guf_rand32_range_i32: interval [min, max] larger than 2^31 - 1"));
|
||||
return -1;
|
||||
@ -663,26 +663,26 @@ GUF_RAND_KWRDS int_fast32_t guf_rand32_range_i32(guf_rand32_state *state, int_fa
|
||||
cf. https://c-faq.com/lib/randrange.html (last-retrieved 2025-02-11)
|
||||
https://stackoverflow.com/a/6852396 (last-retrieved 2025-02-11)
|
||||
*/
|
||||
const uint_fast32_t num_rand_vals = GUF_UWRAP_32(rand_max_i32 + 1u); // 2^31
|
||||
const uint_fast32_t num_bins = GUF_UWRAP_32(delta + 1u);
|
||||
const uint_least32_t num_rand_vals = GUF_UWRAP_32(rand_max_i32 + 1u); // 2^31
|
||||
const uint_least32_t num_bins = GUF_UWRAP_32(delta + 1u);
|
||||
|
||||
const uint_fast32_t bin_size = GUF_UWRAP_32(num_rand_vals / num_bins); // bin_size = floor(num_rand_vals / num_bins)
|
||||
const uint_fast32_t limit = GUF_UWRAP_32(num_rand_vals - (num_rand_vals % num_bins)); // limit == bin_size * num_bins
|
||||
const uint_least32_t bin_size = GUF_UWRAP_32(num_rand_vals / num_bins); // bin_size = floor(num_rand_vals / num_bins)
|
||||
const uint_least32_t limit = GUF_UWRAP_32(num_rand_vals - (num_rand_vals % num_bins)); // limit == bin_size * num_bins
|
||||
GUF_ASSERT(limit == 1u * GUF_UWRAP_32(bin_size * num_bins));
|
||||
/*
|
||||
since (num_rand_vals % num_bins) is at most 2^30 + 1 (I think...), the minimum limit is 2^31 - (2^30 + 1),
|
||||
which means in the worst case, the chance of having to iterate (i.e. step >= limit)
|
||||
is 1 - (2^31 - (2^30 + 1)) / 2^31 == 0.5
|
||||
*/
|
||||
uint_fast32_t step;
|
||||
uint_least32_t step;
|
||||
do {
|
||||
step = GUF_UWRAP_32(guf_rand32_u32(state) >> 1u); // [0, 2^31 - 1]
|
||||
} while (step >= limit);
|
||||
step = GUF_UWRAP_32(step / bin_size);
|
||||
|
||||
GUF_ASSERT(guf_ckd_add_int_fast32_t(min, step) == GUF_MATH_CKD_SUCCESS);
|
||||
GUF_ASSERT(guf_ckd_add_least_i32(min, step) == GUF_MATH_CKD_SUCCESS);
|
||||
|
||||
const int_fast32_t rnd = min + (int_fast32_t)step;
|
||||
const int_least32_t rnd = min + (int_least32_t)step;
|
||||
GUF_ASSERT(rnd >= min && rnd <= max);
|
||||
GUF_ASSERT(rnd <= GUF_INT32_MAX && rnd >= GUF_INT32_MIN);
|
||||
|
||||
@ -690,7 +690,7 @@ GUF_RAND_KWRDS int_fast32_t guf_rand32_range_i32(guf_rand32_state *state, int_fa
|
||||
}
|
||||
|
||||
// returns uniformly-distributed random i32 in range [min, max] (max is inclusive as opposed to the f32/f64 versions)
|
||||
GUF_RAND_KWRDS int_fast32_t guf_randrange_i32(guf_randstate *state, int_fast32_t min, int_fast32_t max)
|
||||
GUF_RAND_KWRDS int_least32_t guf_randrange_i32(guf_randstate *state, int_least32_t min, int_least32_t max)
|
||||
{
|
||||
#ifdef GUF_RAND_32_BIT
|
||||
return guf_rand32_range_i32(state, min, max);
|
||||
@ -699,7 +699,7 @@ GUF_RAND_KWRDS int_fast32_t guf_randrange_i32(guf_randstate *state, int_fast32_t
|
||||
#endif
|
||||
}
|
||||
|
||||
GUF_RAND_KWRDS uint_fast32_t guf_rand32_range_u32(guf_rand32_state *state, uint_fast32_t min, uint_fast32_t max)
|
||||
GUF_RAND_KWRDS uint_least32_t guf_rand32_range_u32(guf_rand32_state *state, uint_least32_t min, uint_least32_t max)
|
||||
{
|
||||
/*
|
||||
The method used in guf_rand32_range_i32 above (which uses only 32-bit integer arithmetic) could overflow here,
|
||||
@ -716,11 +716,11 @@ GUF_RAND_KWRDS uint_fast32_t guf_rand32_range_u32(guf_rand32_state *state, uint_
|
||||
const double delta = (double)max - (double)min;
|
||||
const double result = floor(guf_rand32_f64(state) * (delta + 1.0) + min); // NOTE: guf_rand32_f64 is slow for 32-bit platforms...
|
||||
GUF_ASSERT(result >= min && result <= max);
|
||||
GUF_ASSERT((uint_fast32_t)result <= GUF_UINT32_MAX);
|
||||
return (uint_fast32_t)result;
|
||||
GUF_ASSERT((uint_least32_t)result <= GUF_UINT32_MAX);
|
||||
return (uint_least32_t)result;
|
||||
}
|
||||
|
||||
GUF_RAND_KWRDS uint_fast32_t guf_rand64_range_u32(guf_rand64_state *state, uint_fast32_t min, uint_fast32_t max)
|
||||
GUF_RAND_KWRDS uint_least32_t guf_rand64_range_u32(guf_rand64_state *state, uint_least32_t min, uint_least32_t max)
|
||||
{
|
||||
min = GUF_UWRAP_32(min);
|
||||
max = GUF_UWRAP_32(max);
|
||||
@ -733,11 +733,11 @@ GUF_RAND_KWRDS uint_fast32_t guf_rand64_range_u32(guf_rand64_state *state, uint_
|
||||
const double delta = (double)max - (double)min;
|
||||
const double result = floor(guf_rand64_f64(state) * (delta + 1.0) + min);
|
||||
GUF_ASSERT(result >= min && result <= max);
|
||||
GUF_ASSERT((uint_fast32_t)result <= GUF_UINT32_MAX);
|
||||
return (uint_fast32_t)result;
|
||||
GUF_ASSERT((uint_least32_t)result <= GUF_UINT32_MAX);
|
||||
return (uint_least32_t)result;
|
||||
}
|
||||
|
||||
GUF_RAND_KWRDS uint_fast32_t guf_randrange_u32(guf_randstate *state, uint_fast32_t min, uint_fast32_t max)
|
||||
GUF_RAND_KWRDS uint_least32_t guf_randrange_u32(guf_randstate *state, uint_least32_t min, uint_least32_t max)
|
||||
{
|
||||
#ifdef GUF_RAND_32_BIT
|
||||
return guf_rand32_range_u32(state, min, max);
|
||||
@ -746,16 +746,16 @@ GUF_RAND_KWRDS uint_fast32_t guf_randrange_u32(guf_randstate *state, uint_fast32
|
||||
#endif
|
||||
}
|
||||
|
||||
GUF_RAND_KWRDS int_fast64_t guf_rand64_range_i64(guf_rand64_state *state, int_fast64_t min, int_fast64_t max)
|
||||
GUF_RAND_KWRDS int_least64_t guf_rand64_range_i64(guf_rand64_state *state, int_least64_t min, int_least64_t max)
|
||||
{
|
||||
GUF_ASSERT_RELEASE(max >= min);
|
||||
if (min == max) {
|
||||
return min;
|
||||
}
|
||||
|
||||
const uint_fast64_t rand_max_i64 = GUF_UWRAP_64(GUF_UINT64_MAX >> 1u); // 2^63 - 1 (== INT64_MAX)
|
||||
const uint_least64_t rand_max_i64 = GUF_UWRAP_64(GUF_UINT64_MAX >> 1u); // 2^63 - 1 (== INT64_MAX)
|
||||
|
||||
const uint_fast64_t delta = guf_absdiff_int_fast64_t(max, min);
|
||||
const uint_least64_t delta = guf_absdiff_least_i64(max, min);
|
||||
if (delta > rand_max_i64) {
|
||||
guf_panic(GUF_ERR_INT_OVERFLOW, GUF_ERR_MSG("in function guf_randrange_i64: interval [min, max] larger than 2^63 - 1"));
|
||||
return -1;
|
||||
@ -766,40 +766,40 @@ GUF_RAND_KWRDS int_fast64_t guf_rand64_range_i64(guf_rand64_state *state, int_fa
|
||||
cf. https://c-faq.com/lib/randrange.html (last-retrieved 2025-02-11)
|
||||
https://stackoverflow.com/a/6852396 (last-retrieved 2025-02-11)
|
||||
*/
|
||||
const uint_fast64_t num_rand_vals = GUF_UWRAP_64(rand_max_i64 + 1u); // 2^63
|
||||
const uint_fast64_t num_bins = GUF_UWRAP_64(delta + 1u);
|
||||
const uint_least64_t num_rand_vals = GUF_UWRAP_64(rand_max_i64 + 1u); // 2^63
|
||||
const uint_least64_t num_bins = GUF_UWRAP_64(delta + 1u);
|
||||
|
||||
const uint_fast64_t bin_size = GUF_UWRAP_64(num_rand_vals / num_bins); // bin_size = floor(num_rand_vals / num_bins)
|
||||
const uint_fast64_t limit = GUF_UWRAP_64(num_rand_vals - (num_rand_vals % num_bins)); // limit == bin_size * num_bins
|
||||
const uint_least64_t bin_size = GUF_UWRAP_64(num_rand_vals / num_bins); // bin_size = floor(num_rand_vals / num_bins)
|
||||
const uint_least64_t limit = GUF_UWRAP_64(num_rand_vals - (num_rand_vals % num_bins)); // limit == bin_size * num_bins
|
||||
GUF_ASSERT(limit == 1u * GUF_UWRAP_64(bin_size * num_bins));
|
||||
/*
|
||||
since (num_rand_vals % num_bins) is at most 2^62 + 1 (I think...), the minimum limit is 2^63 - (2^62 + 1),
|
||||
which means in the worst case, the chance of having to iterate (i.e. step >= limit)
|
||||
is 1 - (2^63 - (2^62 + 1)) / 2^63 == 0.5
|
||||
*/
|
||||
uint_fast64_t step;
|
||||
uint_least64_t step;
|
||||
do {
|
||||
step = GUF_UWRAP_64(guf_rand64_u64(state) >> 1); // [0, 2^63 - 1]
|
||||
} while (step >= limit);
|
||||
step = GUF_UWRAP_64(step / bin_size);
|
||||
|
||||
GUF_ASSERT(guf_ckd_add_int_fast64_t(min, step) == GUF_MATH_CKD_SUCCESS);
|
||||
GUF_ASSERT(guf_ckd_add_least_i64(min, step) == GUF_MATH_CKD_SUCCESS);
|
||||
|
||||
const int_fast64_t rnd = min + (int_fast64_t)step;
|
||||
const int_least64_t rnd = min + (int_least64_t)step;
|
||||
GUF_ASSERT(rnd >= min && rnd <= max);
|
||||
return rnd;
|
||||
}
|
||||
|
||||
GUF_RAND_KWRDS int_fast64_t guf_rand32_range_i64(guf_rand32_state *state, int_fast64_t min, int_fast64_t max)
|
||||
GUF_RAND_KWRDS int_least64_t guf_rand32_range_i64(guf_rand32_state *state, int_least64_t min, int_least64_t max)
|
||||
{
|
||||
GUF_ASSERT_RELEASE(max >= min);
|
||||
if (min == max) {
|
||||
return min;
|
||||
}
|
||||
|
||||
const uint_fast64_t rand_max_i64 = GUF_UWRAP_64(GUF_UINT64_MAX >> 1u); // 2^63 - 1 (== INT64_MAX)
|
||||
const uint_least64_t rand_max_i64 = GUF_UWRAP_64(GUF_UINT64_MAX >> 1u); // 2^63 - 1 (== INT64_MAX)
|
||||
|
||||
const uint_fast64_t delta = guf_absdiff_int_fast64_t(max, min);
|
||||
const uint_least64_t delta = guf_absdiff_least_i64(max, min);
|
||||
if (delta > rand_max_i64) {
|
||||
guf_panic(GUF_ERR_INT_OVERFLOW, GUF_ERR_MSG("in function guf_randrange_i64: interval [min, max] larger than 2^63 - 1"));
|
||||
return -1;
|
||||
@ -810,32 +810,32 @@ GUF_RAND_KWRDS int_fast64_t guf_rand32_range_i64(guf_rand32_state *state, int_fa
|
||||
cf. https://c-faq.com/lib/randrange.html (last-retrieved 2025-02-11)
|
||||
https://stackoverflow.com/a/6852396 (last-retrieved 2025-02-11)
|
||||
*/
|
||||
const uint_fast64_t num_rand_vals = GUF_UWRAP_64(rand_max_i64 + 1u); // 2^63
|
||||
const uint_fast64_t num_bins = GUF_UWRAP_64(delta + 1u);
|
||||
const uint_least64_t num_rand_vals = GUF_UWRAP_64(rand_max_i64 + 1u); // 2^63
|
||||
const uint_least64_t num_bins = GUF_UWRAP_64(delta + 1u);
|
||||
|
||||
const uint_fast64_t bin_size = GUF_UWRAP_64(num_rand_vals / num_bins); // bin_size = floor(num_rand_vals / num_bins)
|
||||
const uint_fast64_t limit = GUF_UWRAP_64(num_rand_vals - (num_rand_vals % num_bins)); // limit == bin_size * num_bins
|
||||
const uint_least64_t bin_size = GUF_UWRAP_64(num_rand_vals / num_bins); // bin_size = floor(num_rand_vals / num_bins)
|
||||
const uint_least64_t limit = GUF_UWRAP_64(num_rand_vals - (num_rand_vals % num_bins)); // limit == bin_size * num_bins
|
||||
GUF_ASSERT(limit == 1u * GUF_UWRAP_64(bin_size * num_bins));
|
||||
/*
|
||||
since (num_rand_vals % num_bins) is at most 2^62 + 1 (I think...), the minimum limit is 2^63 - (2^62 + 1),
|
||||
which means in the worst case, the chance of having to iterate (i.e. step >= limit)
|
||||
is 1 - (2^63 - (2^62 + 1)) / 2^63 == 0.5
|
||||
*/
|
||||
uint_fast64_t step;
|
||||
uint_least64_t step;
|
||||
do {
|
||||
step = GUF_UWRAP_64(guf_rand32_u64(state) >> 1); // [0, 2^63 - 1]
|
||||
} while (step >= limit);
|
||||
step = GUF_UWRAP_64(step / bin_size);
|
||||
|
||||
GUF_ASSERT(guf_ckd_add_int_fast64_t(min, step) == GUF_MATH_CKD_SUCCESS);
|
||||
GUF_ASSERT(guf_ckd_add_least_i64(min, step) == GUF_MATH_CKD_SUCCESS);
|
||||
|
||||
const int_fast64_t rnd = min + (int_fast64_t)step;
|
||||
const int_least64_t rnd = min + (int_least64_t)step;
|
||||
GUF_ASSERT(rnd >= min && rnd <= max);
|
||||
return rnd;
|
||||
}
|
||||
|
||||
// returns uniformly-distributed random int64_t in range [min, max] (max is inclusive as opposed to the f32/f64 versions)
|
||||
GUF_RAND_KWRDS int_fast64_t guf_randrange_i64(guf_randstate *state, int_fast64_t min, int_fast64_t max)
|
||||
GUF_RAND_KWRDS int_least64_t guf_randrange_i64(guf_randstate *state, int_least64_t min, int_least64_t max)
|
||||
{
|
||||
#ifdef GUF_RAND_32_BIT
|
||||
return guf_rand32_range_i64(state, min, max);
|
||||
|
||||
@ -35,9 +35,9 @@ GUF_UTF8_KWRDS int guf_utf8_char_num_bytes(const guf_utf8_char *c);
|
||||
GUF_UTF8_KWRDS bool guf_utf8_char_is_valid(const guf_utf8_char *c);
|
||||
GUF_UTF8_KWRDS bool guf_utf8_char_is_whitespace(const guf_utf8_char *c);
|
||||
|
||||
GUF_UTF8_KWRDS guf_utf8_char guf_utf8_char_new(uint_fast32_t codepoint); // Returns GUF_UTF8_REPLACEMENT_CHAR for invalid codepoints (and for GUF_UTF8_REPLACEMENT_CHAR_CODEPOINT).
|
||||
GUF_UTF8_KWRDS bool guf_utf8_encode(guf_utf8_char *result, uint_fast32_t codepoint); // Returns false for invalid codepoints.
|
||||
GUF_UTF8_KWRDS int_fast32_t guf_utf8_decode(const guf_utf8_char *utf8); // Returns -1 for invalid utf-8.
|
||||
GUF_UTF8_KWRDS guf_utf8_char guf_utf8_char_new(uint_least32_t codepoint); // Returns GUF_UTF8_REPLACEMENT_CHAR for invalid codepoints (and for GUF_UTF8_REPLACEMENT_CHAR_CODEPOINT).
|
||||
GUF_UTF8_KWRDS bool guf_utf8_encode(guf_utf8_char *result, uint_least32_t codepoint); // Returns false for invalid codepoints.
|
||||
GUF_UTF8_KWRDS int_least32_t guf_utf8_decode(const guf_utf8_char *utf8); // Returns -1 for invalid utf-8.
|
||||
|
||||
GUF_UTF8_KWRDS bool guf_utf8_equal(const guf_utf8_char *a, const guf_utf8_char *b);
|
||||
|
||||
@ -93,7 +93,7 @@ GUF_UTF8_KWRDS bool guf_utf8_equal(const guf_utf8_char *a, const guf_utf8_char *
|
||||
}
|
||||
|
||||
// cf. https://datatracker.ietf.org/doc/html/rfc3629#section-3 (last-retrieved 2025-03-02)
|
||||
GUF_UTF8_KWRDS bool guf_utf8_encode(guf_utf8_char *result, uint_fast32_t cp)
|
||||
GUF_UTF8_KWRDS bool guf_utf8_encode(guf_utf8_char *result, uint_least32_t cp)
|
||||
{
|
||||
GUF_ASSERT(result);
|
||||
|
||||
@ -138,7 +138,7 @@ GUF_UTF8_KWRDS bool guf_utf8_encode(guf_utf8_char *result, uint_fast32_t cp)
|
||||
int cp_bits = 0;
|
||||
for (int byte_n = num_bytes - 1; byte_n >= 0 && cp > 0; --byte_n) {
|
||||
const int bits = (byte_n == 0) ? first_byte_bits : tail_byte_bits;
|
||||
const uint_fast32_t cp_mask = GUF_UWRAP_32( (UINT32_C(1) << bits) - 1 );
|
||||
const uint_least32_t cp_mask = GUF_UWRAP_32( (UINT32_C(1) << bits) - 1 );
|
||||
result->bytes[byte_n] = (char)(1u * (unsigned char)result->bytes[byte_n] | (cp & cp_mask));
|
||||
cp = cp >> bits;
|
||||
cp_bits += bits;
|
||||
@ -155,7 +155,7 @@ GUF_UTF8_KWRDS bool guf_utf8_encode(guf_utf8_char *result, uint_fast32_t cp)
|
||||
}
|
||||
}
|
||||
|
||||
GUF_UTF8_KWRDS guf_utf8_char guf_utf8_char_new(uint_fast32_t codepoint)
|
||||
GUF_UTF8_KWRDS guf_utf8_char guf_utf8_char_new(uint_least32_t codepoint)
|
||||
{
|
||||
guf_utf8_char result = GUF_UTF8_REPLACEMENT_CHAR;
|
||||
guf_utf8_encode(&result, codepoint);
|
||||
@ -163,7 +163,7 @@ GUF_UTF8_KWRDS guf_utf8_char guf_utf8_char_new(uint_fast32_t codepoint)
|
||||
}
|
||||
|
||||
// cf. https://datatracker.ietf.org/doc/html/rfc3629#section-3 (last-retrieved 2025-03-02)
|
||||
GUF_UTF8_KWRDS int_fast32_t guf_utf8_decode(const guf_utf8_char *c)
|
||||
GUF_UTF8_KWRDS int_least32_t guf_utf8_decode(const guf_utf8_char *c)
|
||||
{
|
||||
if (!guf_utf8_char_is_valid(c)) {
|
||||
return -1;
|
||||
@ -189,12 +189,12 @@ GUF_UTF8_KWRDS int_fast32_t guf_utf8_decode(const guf_utf8_char *c)
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint_fast32_t cp = 0;
|
||||
uint_least32_t cp = 0;
|
||||
int cp_bits = 0;
|
||||
for (int byte_n = num_bytes - 1; byte_n >= 0; --byte_n) {
|
||||
const int bits = (byte_n == 0) ? first_byte_bits : tail_byte_bits;
|
||||
const uint_fast32_t byte_mask = GUF_UWRAP_32( (UINT32_C(1) << bits) - 1 );
|
||||
cp = GUF_UWRAP_32( cp | GUF_UWRAP_32( 1u * ((uint_fast32_t)c->bytes[byte_n] & byte_mask) << cp_bits ) );
|
||||
const uint_least32_t byte_mask = GUF_UWRAP_32( (UINT32_C(1) << bits) - 1 );
|
||||
cp = GUF_UWRAP_32( cp | GUF_UWRAP_32( 1u * ((uint_least32_t)c->bytes[byte_n] & byte_mask) << cp_bits ) );
|
||||
cp_bits += bits;
|
||||
}
|
||||
GUF_ASSERT(cp_bits == first_byte_bits + (num_bytes - 1) * tail_byte_bits);
|
||||
@ -207,8 +207,8 @@ GUF_UTF8_KWRDS int_fast32_t guf_utf8_decode(const guf_utf8_char *c)
|
||||
#ifdef INT32_MAX
|
||||
GUF_ASSERT(cp <= INT32_MAX);
|
||||
#endif
|
||||
GUF_ASSERT(cp <= INT_FAST32_MAX);
|
||||
return (int_fast32_t)cp;
|
||||
GUF_ASSERT(cp <= INT_LEAST32_MAX);
|
||||
return (int_least32_t)cp;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -27,7 +27,7 @@ void CkdIntTest::test_ckd()
|
||||
const int32_t add_res = a + b;
|
||||
const guf_math_ckd_result ckd_add = guf_ckd_add_i8((int8_t)a, (int8_t)b);
|
||||
TEST_CHECK(ckd_add == guf_ckd_add_i8((int8_t)b, (int8_t)a));
|
||||
TEST_CHECK(ckd_add == guf_ckd_add_int_fast8_t((int_fast8_t)a, (int_fast8_t)b));
|
||||
TEST_CHECK(ckd_add == guf_ckd_add_least_i8((int_least8_t)a, (int_least8_t)b));
|
||||
if (add_res > INT8_MAX) {
|
||||
TEST_CHECK(ckd_add == GUF_MATH_CKD_OVERFLOW_POS);
|
||||
int8_t saturated, saturated2;
|
||||
@ -73,7 +73,7 @@ void CkdIntTest::test_ckd()
|
||||
|
||||
const int32_t sub_res = a - b;
|
||||
const guf_math_ckd_result ckd_sub = guf_ckd_sub_i8((int8_t)a, (int8_t)b);
|
||||
TEST_CHECK(ckd_sub == guf_ckd_sub_int_fast8_t((int_fast8_t)a, (int_fast8_t)b));
|
||||
TEST_CHECK(ckd_sub == guf_ckd_sub_least_i8((int_least8_t)a, (int_least8_t)b));
|
||||
if (sub_res > INT8_MAX) {
|
||||
TEST_CHECK(ckd_sub == GUF_MATH_CKD_OVERFLOW_POS);
|
||||
int8_t saturated;
|
||||
@ -102,7 +102,7 @@ void CkdIntTest::test_ckd()
|
||||
|
||||
const int32_t mul_res = a * b;
|
||||
const guf_math_ckd_result ckd_mul = guf_ckd_mul_i8((int8_t)a, (int8_t)b);
|
||||
TEST_CHECK(ckd_mul == guf_ckd_mul_int_fast8_t((int_fast8_t)a, (int_fast8_t)b));
|
||||
TEST_CHECK(ckd_mul == guf_ckd_mul_least_i8((int_least8_t)a, (int_least8_t)b));
|
||||
TEST_CHECK(ckd_mul == guf_ckd_mul_i8((int8_t)b, (int8_t)a));
|
||||
if (mul_res > INT8_MAX) {
|
||||
TEST_CHECK(ckd_mul == GUF_MATH_CKD_OVERFLOW_POS);
|
||||
@ -224,53 +224,53 @@ void CkdIntTest::test_ckd()
|
||||
TEST_CHECK(mul_i32_res == 1693839360);
|
||||
|
||||
|
||||
int_fast32_t mul_i32fast_res = -12345;
|
||||
TEST_CHECK(guf_wrapping_mul_int_fast32_t(INT32_MAX, 2, &mul_i32fast_res) == GUF_MATH_CKD_OVERFLOW_POS);
|
||||
TEST_CHECK(mul_i32fast_res == -2);
|
||||
mul_i32fast_res = -12345;
|
||||
TEST_CHECK(guf_wrapping_mul_int_fast32_t(2, INT32_MAX, &mul_i32fast_res) == GUF_MATH_CKD_OVERFLOW_POS);
|
||||
TEST_CHECK(mul_i32fast_res == -2);
|
||||
int_least32_t mul_i32least_res = -12345;
|
||||
TEST_CHECK(guf_wrapping_mul_least_i32(INT32_MAX, 2, &mul_i32least_res) == GUF_MATH_CKD_OVERFLOW_POS);
|
||||
TEST_CHECK(mul_i32least_res == -2);
|
||||
mul_i32least_res = -12345;
|
||||
TEST_CHECK(guf_wrapping_mul_least_i32(2, INT32_MAX, &mul_i32least_res) == GUF_MATH_CKD_OVERFLOW_POS);
|
||||
TEST_CHECK(mul_i32least_res == -2);
|
||||
|
||||
mul_i32fast_res = -12345;
|
||||
TEST_CHECK(guf_wrapping_mul_int_fast32_t(INT32_MAX, -2, &mul_i32fast_res) == GUF_MATH_CKD_OVERFLOW_NEG);
|
||||
TEST_CHECK(mul_i32fast_res == 2);
|
||||
mul_i32fast_res = -12345;
|
||||
TEST_CHECK(guf_wrapping_mul_int_fast32_t(-2, INT32_MAX, &mul_i32fast_res) == GUF_MATH_CKD_OVERFLOW_NEG);
|
||||
TEST_CHECK(mul_i32fast_res == 2);
|
||||
mul_i32least_res = -12345;
|
||||
TEST_CHECK(guf_wrapping_mul_least_i32(INT32_MAX, -2, &mul_i32least_res) == GUF_MATH_CKD_OVERFLOW_NEG);
|
||||
TEST_CHECK(mul_i32least_res == 2);
|
||||
mul_i32least_res = -12345;
|
||||
TEST_CHECK(guf_wrapping_mul_least_i32(-2, INT32_MAX, &mul_i32least_res) == GUF_MATH_CKD_OVERFLOW_NEG);
|
||||
TEST_CHECK(mul_i32least_res == 2);
|
||||
|
||||
TEST_CHECK(guf_wrapping_mul_int_fast32_t(42002718, 314159265, &mul_i32fast_res) == GUF_MATH_CKD_OVERFLOW_POS);
|
||||
TEST_CHECK(mul_i32fast_res == -972735522);
|
||||
mul_i32fast_res = -12345;
|
||||
TEST_CHECK(guf_wrapping_mul_int_fast32_t(314159265, 42002718, &mul_i32fast_res) == GUF_MATH_CKD_OVERFLOW_POS);
|
||||
TEST_CHECK(mul_i32fast_res == -972735522);
|
||||
TEST_CHECK(guf_wrapping_mul_least_i32(42002718, 314159265, &mul_i32least_res) == GUF_MATH_CKD_OVERFLOW_POS);
|
||||
TEST_CHECK(mul_i32least_res == -972735522);
|
||||
mul_i32least_res = -12345;
|
||||
TEST_CHECK(guf_wrapping_mul_least_i32(314159265, 42002718, &mul_i32least_res) == GUF_MATH_CKD_OVERFLOW_POS);
|
||||
TEST_CHECK(mul_i32least_res == -972735522);
|
||||
|
||||
mul_i32fast_res = 12345;
|
||||
guf_wrapping_mul_int_fast32_t(42002718, 314159265, &mul_i32fast_res);
|
||||
TEST_CHECK(mul_i32fast_res == -972735522);
|
||||
mul_i32least_res = 12345;
|
||||
guf_wrapping_mul_least_i32(42002718, 314159265, &mul_i32least_res);
|
||||
TEST_CHECK(mul_i32least_res == -972735522);
|
||||
|
||||
mul_i32fast_res = 12345;
|
||||
guf_wrapping_mul_int_fast32_t(-42002718, 314159265, &mul_i32fast_res);
|
||||
TEST_CHECK(mul_i32fast_res == 972735522);
|
||||
mul_i32least_res = 12345;
|
||||
guf_wrapping_mul_least_i32(-42002718, 314159265, &mul_i32least_res);
|
||||
TEST_CHECK(mul_i32least_res == 972735522);
|
||||
|
||||
mul_i32fast_res = 12345;
|
||||
guf_wrapping_mul_int_fast32_t(-88888888, 99999999, &mul_i32fast_res);
|
||||
TEST_CHECK(mul_i32fast_res == 1374494264);
|
||||
mul_i32least_res = 12345;
|
||||
guf_wrapping_mul_least_i32(-88888888, 99999999, &mul_i32least_res);
|
||||
TEST_CHECK(mul_i32least_res == 1374494264);
|
||||
|
||||
mul_i32fast_res = 12345;
|
||||
guf_wrapping_mul_int_fast32_t(INT32_MIN, -1, &mul_i32fast_res);
|
||||
TEST_CHECK(mul_i32fast_res == INT32_MIN);
|
||||
mul_i32least_res = 12345;
|
||||
guf_wrapping_mul_least_i32(INT32_MIN, -1, &mul_i32least_res);
|
||||
TEST_CHECK(mul_i32least_res == INT32_MIN);
|
||||
|
||||
mul_i32fast_res = 12345;
|
||||
guf_wrapping_mul_int_fast32_t(-2147483648, 2147483640, &mul_i32fast_res);
|
||||
TEST_CHECK(mul_i32fast_res == 0);
|
||||
mul_i32least_res = 12345;
|
||||
guf_wrapping_mul_least_i32(-2147483648, 2147483640, &mul_i32least_res);
|
||||
TEST_CHECK(mul_i32least_res == 0);
|
||||
|
||||
mul_i32fast_res = 12345;
|
||||
guf_wrapping_mul_int_fast32_t(-2048, -314159265, &mul_i32fast_res);
|
||||
TEST_CHECK(mul_i32fast_res == -846919680);
|
||||
mul_i32least_res = 12345;
|
||||
guf_wrapping_mul_least_i32(-2048, -314159265, &mul_i32least_res);
|
||||
TEST_CHECK(mul_i32least_res == -846919680);
|
||||
|
||||
mul_i32fast_res = 12345;
|
||||
guf_wrapping_mul_int_fast32_t(4096, -314159265, &mul_i32fast_res);
|
||||
TEST_CHECK(mul_i32fast_res == 1693839360);
|
||||
mul_i32least_res = 12345;
|
||||
guf_wrapping_mul_least_i32(4096, -314159265, &mul_i32least_res);
|
||||
TEST_CHECK(mul_i32least_res == 1693839360);
|
||||
|
||||
|
||||
|
||||
@ -295,7 +295,7 @@ void CkdIntTest::test_ckd_uint()
|
||||
for (int32_t b = 0; b <= UINT8_MAX; ++b) {
|
||||
const int32_t add_res = a + b;
|
||||
const guf_math_ckd_result ckd_add = guf_ckd_add_u8((uint8_t)a, (uint8_t)b);
|
||||
GUF_ASSERT(ckd_add == guf_ckd_add_uint_fast8_t((uint_fast8_t)a, (uint_fast8_t)b));
|
||||
GUF_ASSERT(ckd_add == guf_ckd_add_least_u8((uint_least8_t)a, (uint_least8_t)b));
|
||||
if (add_res > UINT8_MAX) {
|
||||
TEST_CHECK(ckd_add == GUF_MATH_CKD_OVERFLOW_POS);
|
||||
uint8_t saturated;
|
||||
@ -317,7 +317,7 @@ void CkdIntTest::test_ckd_uint()
|
||||
|
||||
const int32_t sub_res = a - b;
|
||||
const guf_math_ckd_result ckd_sub = guf_ckd_sub_u8((uint8_t)a, (uint8_t)b);
|
||||
GUF_ASSERT(ckd_sub == guf_ckd_sub_uint_fast8_t((uint_fast8_t)a, (uint_fast8_t)b));
|
||||
GUF_ASSERT(ckd_sub == guf_ckd_sub_least_u8((uint_least8_t)a, (uint_least8_t)b));
|
||||
if (sub_res < 0) {
|
||||
TEST_CHECK(ckd_sub == GUF_MATH_CKD_OVERFLOW_NEG);
|
||||
uint8_t saturated;
|
||||
@ -338,7 +338,7 @@ void CkdIntTest::test_ckd_uint()
|
||||
|
||||
const int32_t mul_res = a * b;
|
||||
const guf_math_ckd_result ckd_mul = guf_ckd_mul_u8((uint8_t)a, (uint8_t)b);
|
||||
GUF_ASSERT(ckd_mul == guf_ckd_mul_uint_fast8_t((uint_fast8_t)a, (uint_fast8_t)b));
|
||||
GUF_ASSERT(ckd_mul == guf_ckd_mul_least_u8((uint_least8_t)a, (uint_least8_t)b));
|
||||
if (mul_res > UINT8_MAX) {
|
||||
TEST_CHECK(ckd_mul == GUF_MATH_CKD_OVERFLOW_POS);
|
||||
uint8_t saturated;
|
||||
|
||||
2
todo.txt
2
todo.txt
@ -1,5 +1,3 @@
|
||||
- rename int_fastN function to iN and delete the fixed-width ones?
|
||||
|
||||
- fix readonly str/uninit ?
|
||||
|
||||
- make guf_utf8_char 4 bytes (non-null terminated)
|
||||
|
||||
@ -57,7 +57,7 @@ def generate_ckdint_functions(int_types: list, uint_types: list) -> Tuple[str, s
|
||||
}}
|
||||
""")
|
||||
|
||||
ckd_add_sub_uint_FAST = textwrap.dedent("""\
|
||||
ckd_add_sub_uint_LEAST = textwrap.dedent("""\
|
||||
GUF_MATH_CKDINT_KWRDS guf_math_ckd_result guf_ckd_add_{type_abbr}({type} a, {type} b)
|
||||
{{
|
||||
a = GUF_UWRAP_{bits}(a);
|
||||
@ -378,7 +378,7 @@ def generate_ckdint_functions(int_types: list, uint_types: list) -> Tuple[str, s
|
||||
}}
|
||||
""")
|
||||
|
||||
saturating_wrapping_uint_FAST = textwrap.dedent("""\
|
||||
saturating_wrapping_uint_LEAST = textwrap.dedent("""\
|
||||
GUF_MATH_CKDINT_KWRDS guf_math_ckd_result guf_saturating_add_{type_abbr}({type} a, {type} b, {type} *result)
|
||||
{{
|
||||
const guf_math_ckd_result check = guf_ckd_add_{type_abbr}(a, b);
|
||||
@ -492,19 +492,19 @@ def generate_ckdint_functions(int_types: list, uint_types: list) -> Tuple[str, s
|
||||
text_result_header += f"#ifdef {type.INT_MAX}\n"
|
||||
end = ""
|
||||
|
||||
if "uint_fast" in type.INT_TYPE:
|
||||
if "uint_least" in type.INT_TYPE:
|
||||
bits = 0
|
||||
if "fast8" in type.INT_TYPE:
|
||||
if "least8" in type.INT_TYPE:
|
||||
bits = 8
|
||||
elif "fast16" in type.INT_TYPE:
|
||||
elif "least16" in type.INT_TYPE:
|
||||
bits = 16
|
||||
elif "fast32" in type.INT_TYPE:
|
||||
elif "least32" in type.INT_TYPE:
|
||||
bits = 32
|
||||
elif "fast64" in type.INT_TYPE:
|
||||
elif "least64" in type.INT_TYPE:
|
||||
bits = 64
|
||||
else:
|
||||
assert(False)
|
||||
text_result += ckd_add_sub_uint_FAST.format(type = type.INT_TYPE, type_abbr = type.INT_TYPE_ABBR, int_min = type.INT_MIN, int_max = type.INT_MAX, bits = str(bits))
|
||||
text_result += ckd_add_sub_uint_LEAST.format(type = type.INT_TYPE, type_abbr = type.INT_TYPE_ABBR, int_min = type.INT_MIN, int_max = type.INT_MAX, bits = str(bits))
|
||||
else:
|
||||
text_result += ckd_add_sub_uint.format(type = type.INT_TYPE, type_abbr = type.INT_TYPE_ABBR, int_min = type.INT_MIN, int_max = type.INT_MAX) + end
|
||||
text_result_header += ckd_add_sub_uint_header.format(type = type.INT_TYPE, type_abbr = type.INT_TYPE_ABBR, int_min = type.INT_MIN, int_max = type.INT_MAX) + end
|
||||
@ -540,19 +540,19 @@ def generate_ckdint_functions(int_types: list, uint_types: list) -> Tuple[str, s
|
||||
text_result_header += f"#ifdef {type.INT_MAX}\n"
|
||||
end = ""
|
||||
|
||||
if "uint_fast" in type.INT_TYPE:
|
||||
if "uint_least" in type.INT_TYPE:
|
||||
bits = 0
|
||||
if "fast8" in type.INT_TYPE:
|
||||
if "least8" in type.INT_TYPE:
|
||||
bits = 8
|
||||
elif "fast16" in type.INT_TYPE:
|
||||
elif "least16" in type.INT_TYPE:
|
||||
bits = 16
|
||||
elif "fast32" in type.INT_TYPE:
|
||||
elif "least32" in type.INT_TYPE:
|
||||
bits = 32
|
||||
elif "fast64" in type.INT_TYPE:
|
||||
elif "least64" in type.INT_TYPE:
|
||||
bits = 64
|
||||
else:
|
||||
assert(False)
|
||||
text_result += saturating_wrapping_uint_FAST.format(type = type.INT_TYPE, type_abbr = type.INT_TYPE_ABBR, int_min = type.INT_MIN, int_max = type.INT_MAX, bits = str(bits)) + end
|
||||
text_result += saturating_wrapping_uint_LEAST.format(type = type.INT_TYPE, type_abbr = type.INT_TYPE_ABBR, int_min = type.INT_MIN, int_max = type.INT_MAX, bits = str(bits)) + end
|
||||
else:
|
||||
text_result += saturating_wrapping_uint.format(type = type.INT_TYPE, type_abbr = type.INT_TYPE_ABBR, int_min = type.INT_MIN, int_max = type.INT_MAX) + end
|
||||
|
||||
@ -577,10 +577,10 @@ if __name__ == "__main__":
|
||||
# TODO: size_t is not necessarily the unsigned ptrdiff_t equivalent
|
||||
IntType(INT_TYPE = "ptrdiff_t", INT_TYPE_ABBR = "ptrdiff_t", INT_MIN = "PTRDIFF_MIN", INT_MAX = "PTRDIFF_MAX", UINT_TYPE = "size_t", UINT_MAX = "SIZE_MAX"),
|
||||
|
||||
IntType(INT_TYPE = "int_fast8_t", INT_TYPE_ABBR = "int_fast8_t", INT_MIN = "GUF_INT8_MIN", INT_MAX = "GUF_INT8_MAX", UINT_TYPE = "uint_fast8_t", UINT_MAX = "GUF_UINT8_MAX"),
|
||||
IntType(INT_TYPE = "int_fast16_t", INT_TYPE_ABBR = "int_fast16_t", INT_MIN = "GUF_INT16_MIN", INT_MAX = "GUF_INT16_MAX", UINT_TYPE = "uint_fast16_t", UINT_MAX = "GUF_UINT16_MAX"),
|
||||
IntType(INT_TYPE = "int_fast32_t", INT_TYPE_ABBR = "int_fast32_t", INT_MIN = "GUF_INT32_MIN", INT_MAX = "GUF_INT32_MAX", UINT_TYPE = "uint_fast32_t", UINT_MAX = "GUF_UINT32_MAX"),
|
||||
IntType(INT_TYPE = "int_fast64_t", INT_TYPE_ABBR = "int_fast64_t", INT_MIN = "GUF_INT64_MIN", INT_MAX = "GUF_INT64_MAX", UINT_TYPE = "uint_fast64_t", UINT_MAX = "GUF_UINT64_MAX"),
|
||||
IntType(INT_TYPE = "int_least8_t", INT_TYPE_ABBR = "least_i8", INT_MIN = "GUF_INT8_MIN", INT_MAX = "GUF_INT8_MAX", UINT_TYPE = "uint_least8_t", UINT_MAX = "GUF_UINT8_MAX"),
|
||||
IntType(INT_TYPE = "int_least16_t", INT_TYPE_ABBR = "least_i16", INT_MIN = "GUF_INT16_MIN", INT_MAX = "GUF_INT16_MAX", UINT_TYPE = "uint_least16_t", UINT_MAX = "GUF_UINT16_MAX"),
|
||||
IntType(INT_TYPE = "int_least32_t", INT_TYPE_ABBR = "least_i32", INT_MIN = "GUF_INT32_MIN", INT_MAX = "GUF_INT32_MAX", UINT_TYPE = "uint_least32_t", UINT_MAX = "GUF_UINT32_MAX"),
|
||||
IntType(INT_TYPE = "int_least64_t", INT_TYPE_ABBR = "least_i64", INT_MIN = "GUF_INT64_MIN", INT_MAX = "GUF_INT64_MAX", UINT_TYPE = "uint_least64_t", UINT_MAX = "GUF_UINT64_MAX"),
|
||||
|
||||
IntType(INT_TYPE = "int8_t", INT_TYPE_ABBR = "i8", INT_MIN = "INT8_MIN", INT_MAX = "INT8_MAX", UINT_TYPE = "uint8_t", UINT_MAX = "GUF_UINT8_MAX", is_optional = True),
|
||||
IntType(INT_TYPE = "int16_t", INT_TYPE_ABBR = "i16", INT_MIN = "INT16_MIN", INT_MAX = "INT16_MAX", UINT_TYPE = "uint16_t", UINT_MAX = "GUF_UINT16_MAX", is_optional = True),
|
||||
@ -596,10 +596,10 @@ if __name__ == "__main__":
|
||||
|
||||
UintType(INT_TYPE = "size_t", INT_TYPE_ABBR = "size_t", INT_MIN = "0", INT_MAX = "SIZE_MAX"),
|
||||
|
||||
UintType(INT_TYPE = "uint_fast8_t", INT_TYPE_ABBR = "uint_fast8_t", INT_MIN = "0", INT_MAX = "GUF_UINT8_MAX"),
|
||||
UintType(INT_TYPE = "uint_fast16_t", INT_TYPE_ABBR = "uint_fast16_t", INT_MIN = "0", INT_MAX = "GUF_UINT16_MAX"),
|
||||
UintType(INT_TYPE = "uint_fast32_t", INT_TYPE_ABBR = "uint_fast32_t", INT_MIN = "0", INT_MAX = "GUF_UINT32_MAX"),
|
||||
UintType(INT_TYPE = "uint_fast64_t", INT_TYPE_ABBR = "uint_fast64_t", INT_MIN = "0", INT_MAX = "GUF_UINT64_MAX"),
|
||||
UintType(INT_TYPE = "uint_least8_t", INT_TYPE_ABBR = "least_u8", INT_MIN = "0", INT_MAX = "GUF_UINT8_MAX"),
|
||||
UintType(INT_TYPE = "uint_least16_t", INT_TYPE_ABBR = "least_u16", INT_MIN = "0", INT_MAX = "GUF_UINT16_MAX"),
|
||||
UintType(INT_TYPE = "uint_least32_t", INT_TYPE_ABBR = "least_u32", INT_MIN = "0", INT_MAX = "GUF_UINT32_MAX"),
|
||||
UintType(INT_TYPE = "uint_least64_t", INT_TYPE_ABBR = "least_u64", INT_MIN = "0", INT_MAX = "GUF_UINT64_MAX"),
|
||||
|
||||
UintType(INT_TYPE = "uint8_t", INT_TYPE_ABBR = "u8", INT_MIN = "0", INT_MAX = "UINT8_MAX", is_optional = True),
|
||||
UintType(INT_TYPE = "uint16_t", INT_TYPE_ABBR = "u16", INT_MIN = "0", INT_MAX = "UINT16_MAX", is_optional = True),
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user