Refactor to use portable minimum-width integers.

The signed and unsigned fixed-width integers (int32_t, uint32_t etc.) are optional
in C99 (and above). Use the non-optional minimum-width integers (int_fast32_t, uint_fast32_t and int_least32_t, uint_least32_t etc.) instead.

To simulate unsigned wrap-around, use the GUF_UWRAP macros in guf_common.h

cf. https://en.cppreference.com/w/c/types/integer (last-retrieved: 2025-05-18)
This commit is contained in:
jun 2025-05-18 22:03:03 +02:00
parent 7ec2af0c33
commit 57f0e47efc
13 changed files with 3613 additions and 1871 deletions

View File

@ -16,13 +16,13 @@ typedef struct guf_alloc_tracker {
const char *name; const char *name;
size_t alloc_count, realloc_count, free_count; size_t alloc_count, realloc_count, free_count;
ptrdiff_t allocated_bytes; ptrdiff_t allocated_bytes;
uint32_t id; unsigned id;
bool enabled; bool enabled;
} guf_alloc_tracker; } guf_alloc_tracker;
#if !defined(GUF_ALLOC_TRACKER_IMPL_STATIC) && !defined(GUF_ALLOC_TRACKER_IMPL) #if !defined(GUF_ALLOC_TRACKER_IMPL_STATIC) && !defined(GUF_ALLOC_TRACKER_IMPL)
GUF_ALLOC_TRACKER_KWRDS guf_alloc_tracker *guf_alloc_tracker_init(guf_alloc_tracker *t, uint32_t id, const char* name, FILE *log, FILE *err_log); GUF_ALLOC_TRACKER_KWRDS guf_alloc_tracker *guf_alloc_tracker_init(guf_alloc_tracker *t, unsigned id, const char* name, FILE *log, FILE *err_log);
GUF_ALLOC_TRACKER_KWRDS guf_alloc_tracker guf_alloc_tracker_new(uint32_t id, const char* name, FILE *log, FILE *err_log); GUF_ALLOC_TRACKER_KWRDS guf_alloc_tracker guf_alloc_tracker_new(unsigned id, const char* name, FILE *log, FILE *err_log);
GUF_ALLOC_TRACKER_KWRDS void guf_alloc_tracker_print(const guf_alloc_tracker *t, FILE *f); GUF_ALLOC_TRACKER_KWRDS void guf_alloc_tracker_print(const guf_alloc_tracker *t, FILE *f);
GUF_ALLOC_TRACKER_KWRDS bool guf_alloc_tracker_found_leak(const guf_alloc_tracker *t); GUF_ALLOC_TRACKER_KWRDS bool guf_alloc_tracker_found_leak(const guf_alloc_tracker *t);
@ -38,7 +38,7 @@ typedef struct guf_alloc_tracker {
#define GUF_MATH_CKDINT_IMPL_STATIC #define GUF_MATH_CKDINT_IMPL_STATIC
#include "guf_math_ckdint.h" #include "guf_math_ckdint.h"
GUF_ALLOC_TRACKER_KWRDS guf_alloc_tracker *guf_alloc_tracker_init(guf_alloc_tracker *t, uint32_t id, const char* name, FILE *log, FILE *err_log) GUF_ALLOC_TRACKER_KWRDS guf_alloc_tracker *guf_alloc_tracker_init(guf_alloc_tracker *t, unsigned id, const char* name, FILE *log, FILE *err_log)
{ {
GUF_ASSERT_RELEASE(t); GUF_ASSERT_RELEASE(t);
t->log = log; t->log = log;
@ -51,7 +51,7 @@ GUF_ALLOC_TRACKER_KWRDS guf_alloc_tracker *guf_alloc_tracker_init(guf_alloc_trac
return t; return t;
} }
GUF_ALLOC_TRACKER_KWRDS guf_alloc_tracker guf_alloc_tracker_new(uint32_t id, const char* name, FILE *log, FILE *err_log) GUF_ALLOC_TRACKER_KWRDS guf_alloc_tracker guf_alloc_tracker_new(unsigned id, const char* name, FILE *log, FILE *err_log)
{ {
guf_alloc_tracker t; guf_alloc_tracker t;
guf_alloc_tracker_init(&t, id, name, log, err_log); guf_alloc_tracker_init(&t, id, name, log, err_log);
@ -78,7 +78,7 @@ GUF_ALLOC_TRACKER_KWRDS void guf_alloc_tracker_print(const guf_alloc_tracker *t,
} }
fprintf(f, fprintf(f,
"guf_alloc_tracker (name '%s' id = %" PRIu32 "):\n" "guf_alloc_tracker (name '%s' id = %u):\n"
"allocated_bytes: %td\n" "allocated_bytes: %td\n"
"alloc_count: %zu\n" "alloc_count: %zu\n"
"realloc_count: %zu\n" "realloc_count: %zu\n"
@ -96,25 +96,25 @@ GUF_ALLOC_TRACKER_KWRDS bool guf_track_alloc(guf_alloc_tracker *t, ptrdiff_t siz
bool success = true; bool success = true;
if (guf_saturating_add_size_t(t->alloc_count, 1, &t->alloc_count) != GUF_MATH_CKD_SUCCESS && t->err_log) { if (guf_saturating_add_size_t(t->alloc_count, 1, &t->alloc_count) != GUF_MATH_CKD_SUCCESS && t->err_log) {
fprintf(t->err_log, "WARNING in guf_track_alloc (name '%s' id %" PRIu32 "): alloc_count reached SIZE_MAX\n", t->name ? t->name : "unnamed", t->id); fprintf(t->err_log, "WARNING in guf_track_alloc (name '%s' id %u): alloc_count reached SIZE_MAX\n", t->name ? t->name : "unnamed", t->id);
} }
if (guf_saturating_add_ptrdiff_t(t->allocated_bytes, size, &t->allocated_bytes) != GUF_MATH_CKD_SUCCESS) { if (guf_saturating_add_ptrdiff_t(t->allocated_bytes, size, &t->allocated_bytes) != GUF_MATH_CKD_SUCCESS) {
if (t->err_log) { if (t->err_log) {
fprintf(t->err_log, "ERROR in guf_track_alloc (name '%s' id %" PRIu32 "): allocated_byte overflow\n", t->name ? t->name : "unnamed", t->id); fprintf(t->err_log, "ERROR in guf_track_alloc (name '%s' id %u): allocated_byte overflow\n", t->name ? t->name : "unnamed", t->id);
} }
success = false; success = false;
} }
if (t->allocated_bytes < 0) { if (t->allocated_bytes < 0) {
if (t->err_log) { if (t->err_log) {
fprintf(t->err_log, "ERROR in guf_track_alloc (name '%s' id %" PRIu32 "): allocated_bytes < 0\n", t->name ? t->name : "unnamed", t->id); fprintf(t->err_log, "ERROR in guf_track_alloc (name '%s' id %u): allocated_bytes < 0\n", t->name ? t->name : "unnamed", t->id);
} }
success = false; success = false;
} }
if (t->log) { if (t->log) {
fprintf(t->log, "guf_alloc_tracker (name '%s' id %" PRIu32 "): alloc (%td bytes) %s\n", t->name ? t->name : "unnamed", t->id, size, success ? "SUCCESS" : "FAILURE"); fprintf(t->log, "guf_alloc_tracker (name '%s' id %u): alloc (%td bytes) %s\n", t->name ? t->name : "unnamed", t->id, size, success ? "SUCCESS" : "FAILURE");
} }
return success; return success;
} }
@ -127,40 +127,40 @@ GUF_ALLOC_TRACKER_KWRDS bool guf_track_realloc(guf_alloc_tracker *t, ptrdiff_t o
bool success = true; bool success = true;
if (guf_saturating_add_size_t(t->realloc_count, 1, &t->realloc_count) && t->err_log) { if (guf_saturating_add_size_t(t->realloc_count, 1, &t->realloc_count) && t->err_log) {
fprintf(t->err_log, "WARNING in guf_track_realloc (name '%s' id %" PRIu32 "): realloc_count reached SIZE_MAX\n", t->name ? t->name : "unnamed", t->id); fprintf(t->err_log, "WARNING in guf_track_realloc (name '%s' id %u): realloc_count reached SIZE_MAX\n", t->name ? t->name : "unnamed", t->id);
} }
if (old_size < 0 || new_size < 0) { if (old_size < 0 || new_size < 0) {
success = false; success = false;
if (t->err_log) { if (t->err_log) {
fprintf(t->err_log, "ERROR in guf_track_realloc (name '%s' id %" PRIu32 "): old_size < 0 or new_size < 0\n", t->name ? t->name : "unnamed", t->id); fprintf(t->err_log, "ERROR in guf_track_realloc (name '%s' id %u): old_size < 0 or new_size < 0\n", t->name ? t->name : "unnamed", t->id);
} }
} }
if (guf_saturating_sub_ptrdiff_t(t->allocated_bytes, old_size, &t->allocated_bytes)) { if (guf_saturating_sub_ptrdiff_t(t->allocated_bytes, old_size, &t->allocated_bytes)) {
success = false; success = false;
if (t->err_log) { if (t->err_log) {
fprintf(t->err_log, "ERROR in guf_track_realloc (name '%s' id %" PRIu32 "): allocated_bytes - old_size under/overflows\n", t->name ? t->name : "unnamed", t->id); fprintf(t->err_log, "ERROR in guf_track_realloc (name '%s' id %u): allocated_bytes - old_size under/overflows\n", t->name ? t->name : "unnamed", t->id);
} }
} }
if (t->allocated_bytes < 0) { if (t->allocated_bytes < 0) {
success = false; success = false;
fprintf(t->err_log, "ERROR in guf_track_realloc (name '%s' id %" PRIu32 "): allocated_bytes < 0 after subtracting old_size\n", t->name ? t->name : "unnamed", t->id); fprintf(t->err_log, "ERROR in guf_track_realloc (name '%s' id %u): allocated_bytes < 0 after subtracting old_size\n", t->name ? t->name : "unnamed", t->id);
} }
if (guf_saturating_add_ptrdiff_t(t->allocated_bytes, new_size, &t->allocated_bytes)) { if (guf_saturating_add_ptrdiff_t(t->allocated_bytes, new_size, &t->allocated_bytes)) {
success = false; success = false;
if (t->err_log) { if (t->err_log) {
fprintf(t->err_log, "ERROR in guf_track_realloc (name '%s' id %" PRIu32 "): allocated_bytes overflow \n", t->name ? t->name : "unnamed", t->id); fprintf(t->err_log, "ERROR in guf_track_realloc (name '%s' id %u): allocated_bytes overflow \n", t->name ? t->name : "unnamed", t->id);
} }
} }
if (t->allocated_bytes < 0) { if (t->allocated_bytes < 0) {
success = false; success = false;
fprintf(t->err_log, "ERROR in guf_track_realloc (name '%s' id %" PRIu32 "): allocated_bytes < 0 after adding new_size\n", t->name ? t->name : "unnamed", t->id); fprintf(t->err_log, "ERROR in guf_track_realloc (name '%s' id %u): allocated_bytes < 0 after adding new_size\n", t->name ? t->name : "unnamed", t->id);
} }
if (t->log) { if (t->log) {
fprintf(t->log, "guf_alloc_tracker (name '%s' id %" PRIu32 "): realloc (from %td to %td bytes) %s\n", t->name ? t->name : "unnamed", t->id, old_size, new_size, (success ? "SUCCESS" : "FAILURE")); fprintf(t->log, "guf_alloc_tracker (name '%s' id %u): realloc (from %td to %td bytes) %s\n", t->name ? t->name : "unnamed", t->id, old_size, new_size, (success ? "SUCCESS" : "FAILURE"));
} }
return success; return success;
@ -173,31 +173,31 @@ GUF_ALLOC_TRACKER_KWRDS bool guf_track_free(guf_alloc_tracker *t, ptrdiff_t size
bool success = true; bool success = true;
if (guf_saturating_add_size_t(t->free_count, 1, &t->free_count) && t->err_log) { if (guf_saturating_add_size_t(t->free_count, 1, &t->free_count) && t->err_log) {
fprintf(t->err_log, "WARNING in guf_track_free (name '%s' id %" PRIu32 "): free_count reached SIZE_MAX\n", t->name ? t->name : "unnamed", t->id); fprintf(t->err_log, "WARNING in guf_track_free (name '%s' id %u): free_count reached SIZE_MAX\n", t->name ? t->name : "unnamed", t->id);
} }
if (size < 0) { if (size < 0) {
success = false; success = false;
if (t->err_log) { if (t->err_log) {
fprintf(t->err_log, "ERROR in guf_track_free (name '%s' id %" PRIu32 "): size < 0\n", t->name ? t->name : "unnamed", t->id); fprintf(t->err_log, "ERROR in guf_track_free (name '%s' id %u): size < 0\n", t->name ? t->name : "unnamed", t->id);
} }
} }
if (t->allocated_bytes < size) { if (t->allocated_bytes < size) {
success = false; success = false;
if (t->err_log) { if (t->err_log) {
fprintf(t->err_log, "ERROR in guf_track_free (name '%s' id %" PRIu32 "): freed more bytes than allocated\n", t->name ? t->name : "unnamed", t->id); fprintf(t->err_log, "ERROR in guf_track_free (name '%s' id %u): freed more bytes than allocated\n", t->name ? t->name : "unnamed", t->id);
} }
} }
if (guf_saturating_sub_ptrdiff_t(t->allocated_bytes, size, &t->allocated_bytes)) { if (guf_saturating_sub_ptrdiff_t(t->allocated_bytes, size, &t->allocated_bytes)) {
success = false; success = false;
if (t->err_log) { if (t->err_log) {
fprintf(t->err_log, "ERROR in guf_track_free (name '%s' id %" PRIu32 "): allocated_bytes - size under/overflows\n", t->name ? t->name : "unnamed", t->id); fprintf(t->err_log, "ERROR in guf_track_free (name '%s' id %u): allocated_bytes - size under/overflows\n", t->name ? t->name : "unnamed", t->id);
} }
} }
if (t->log) { if (t->log) {
fprintf(t->log, "guf_alloc_tracker (name '%s' id %" PRIu32 "): free (%td bytes) %s\n", t->name ? t->name : "unnamed", t->id, size, (success ? "SUCCESS" : "FAILURE")); fprintf(t->log, "guf_alloc_tracker (name '%s' id %u): free (%td bytes) %s\n", t->name ? t->name : "unnamed", t->id, size, (success ? "SUCCESS" : "FAILURE"));
} }
return success; return success;

View File

@ -10,22 +10,39 @@
#include <inttypes.h> #include <inttypes.h>
#include <stddef.h> #include <stddef.h>
#ifndef GUF_PLATFORM_BIG_ENDIAN
#define GUF_PLATFORM_LITTLE_ENDIAN
#endif
#define GUF_UINT8_MAX 0xffu #define GUF_UINT8_MAX 0xffu
#define GUF_UINT16_MAX 0xffffu #define GUF_UINT16_MAX 0xffffu
#define GUF_UINT32_MAX 0xfffffffful #define GUF_UINT32_MAX 0xfffffffful
#define GUF_UINT64_MAX 0xffffffffffffffffull #define GUF_UINT64_MAX 0xffffffffffffffffull
#ifndef GUF_PLATFORM_BIG_ENDIAN #define GUF_UWRAP_8(UINT) ( (UINT) & GUF_UINT8_MAX )
#define GUF_PLATFORM_LITTLE_ENDIAN #define GUF_UWRAP_16(UINT) ( (UINT) & GUF_UINT16_MAX )
#endif #define GUF_UWRAP_32(UINT) ( (UINT) & GUF_UINT32_MAX )
#define GUF_UWRAP_64(UINT) ( (UINT) & GUF_UINT64_MAX )
#if SIZE_MAX == UINT64_MAX #define GUF_INT8_MAX 127
#define GUF_INT8_MIN -128
#define GUF_INT16_MAX 32767
#define GUF_INT16_MIN (-GUF_INT16_MAX - 1)
#define GUF_INT32_MAX 2147483647L
#define GUF_INT32_MIN (-GUF_INT32_MAX - 1)
#define GUF_INT64_MAX 9223372036854775807LL
#define GUF_INT64_MIN (-GUF_INT64_MAX - 1)
#if SIZE_MAX == GUF_UINT64_MAX
#define GUF_PLATFORM_BITS 64 #define GUF_PLATFORM_BITS 64
#elif SIZE_MAX == UINT32_MAX #elif SIZE_MAX == GUF_UINT32_MAX
#define GUF_PLATFORM_BITS 32 #define GUF_PLATFORM_BITS 32
#elif SIZE_MAX == UINT16_MAX #elif SIZE_MAX == GUF_UINT16_MAX
#define GUF_PLATFORM_BITS 16 #define GUF_PLATFORM_BITS 16
#elif SIZE_MAX == UINT8_MAX #elif SIZE_MAX == GUF_UINT8_MAX
#define GUF_PLATFORM_BITS 8 #define GUF_PLATFORM_BITS 8
#else #else
#define GUF_PLATFORM_BITS 64 #define GUF_PLATFORM_BITS 64

View File

@ -34,18 +34,18 @@
#endif #endif
#if defined(GUF_DICT_32_BIT_HASH) #if defined(GUF_DICT_32_BIT_HASH)
#define GUF_DICT_HASH_T uint32_t #define GUF_DICT_HASH_T uint_fast32_t
#define GUF_DICT_HASH_T_MAX UINT32_MAX #define GUF_DICT_HASH_T_MAX GUF_UINT32_MAX
#elif defined(GUF_DICT_64_BIT_HASH) #elif defined(GUF_DICT_64_BIT_HASH)
#define GUF_DICT_HASH_T uint64_t #define GUF_DICT_HASH_T uint_fast64_t
#define GUF_DICT_HASH_T_MAX UINT64_MAX #define GUF_DICT_HASH_T_MAX GUF_UINT64_MAX
#else #else
#define GUF_DICT_HASH_T guf_hash_size_t #define GUF_DICT_HASH_T guf_hash_size_t
#define GUF_DICT_HASH_T_MAX GUF_HASH_MAX #define GUF_DICT_HASH_T_MAX GUF_HASH_MAX
#endif #endif
#if defined (GUF_DICT_64_BIT_IDX) #if defined (GUF_DICT_64_BIT_IDX)
#define GUF_DICT_KV_META_T uint64_t #define GUF_DICT_KV_META_T uint_least64_t
/* /*
Store a 16-bit hash-fragment in the upper 16-bits of kv_meta Store a 16-bit hash-fragment in the upper 16-bits of kv_meta
-> (2^48 - 1 is IDX_NULL, 2^48 - 2 is IDX_TOMBSTONE, 2^48 - 3 is the largest actual idx, -> (2^48 - 1 is IDX_NULL, 2^48 - 2 is IDX_TOMBSTONE, 2^48 - 3 is the largest actual idx,
@ -54,15 +54,15 @@
#define GUF_DICT_KV_META_HASHFRAG_MASK UINT64_C(0xffff000000000000) #define GUF_DICT_KV_META_HASHFRAG_MASK UINT64_C(0xffff000000000000)
#define GUF_DICT_KV_META_IDX_MASK (~UINT64_C(0xffff000000000000)) #define GUF_DICT_KV_META_IDX_MASK (~UINT64_C(0xffff000000000000))
#if GUF_DICT_HASH_T_MAX == UINT64_MAX #if GUF_DICT_HASH_T_MAX == GUF_UINT64_MAX
#define GUF_DICT_HASH_T_GET_HASHFRAG(HASH) ( (HASH) & GUF_DICT_KV_META_HASHFRAG_MASK ) #define GUF_DICT_HASH_T_GET_HASHFRAG(HASH) ( (HASH) & GUF_DICT_KV_META_HASHFRAG_MASK )
#elif GUF_DICT_HASH_T_MAX == UINT32_MAX #elif GUF_DICT_HASH_T_MAX == GUF_UINT32_MAX
#define GUF_DICT_HASH_T_GET_HASHFRAG(HASH) ( (((uint64_t)(HASH)) << 32) & GUF_DICT_KV_META_HASHFRAG_MASK ) #define GUF_DICT_HASH_T_GET_HASHFRAG(HASH) ( (((uint_least64_t)(HASH)) << 32u) & GUF_DICT_KV_META_HASHFRAG_MASK )
#else #else
#error "guf_dict: invalid hash size (should not happen)" #error "guf_dict: invalid hash size (should not happen)"
#endif #endif
#else #else
#define GUF_DICT_KV_META_T uint32_t #define GUF_DICT_KV_META_T uint_least32_t
/* /*
Store a 7-bit hash-fragment in the upper 7-bits of kv_meta Store a 7-bit hash-fragment in the upper 7-bits of kv_meta
-> (2^25 - 1 is IDX_NULL, 2^25 - 2 is IDX_TOMBSTONE, 2^25 - 3 is the largest actual idx, -> (2^25 - 1 is IDX_NULL, 2^25 - 2 is IDX_TOMBSTONE, 2^25 - 3 is the largest actual idx,
@ -71,9 +71,9 @@
#define GUF_DICT_KV_META_HASHFRAG_MASK UINT32_C(0xfe000000) #define GUF_DICT_KV_META_HASHFRAG_MASK UINT32_C(0xfe000000)
#define GUF_DICT_KV_META_IDX_MASK (~UINT32_C(0xfe000000)) #define GUF_DICT_KV_META_IDX_MASK (~UINT32_C(0xfe000000))
#if GUF_DICT_HASH_T_MAX == UINT64_MAX #if GUF_DICT_HASH_T_MAX == GUF_UINT64_MAX
#define GUF_DICT_HASH_T_GET_HASHFRAG(HASH) ( ((uint32_t)((HASH) >> 32)) & GUF_DICT_KV_META_HASHFRAG_MASK ) #define GUF_DICT_HASH_T_GET_HASHFRAG(HASH) ( ((uint_least32_t)((HASH) >> 32u)) & GUF_DICT_KV_META_HASHFRAG_MASK )
#elif GUF_DICT_HASH_T_MAX == UINT32_MAX #elif GUF_DICT_HASH_T_MAX == GUF_UINT32_MAX
#define GUF_DICT_HASH_T_GET_HASHFRAG(HASH) ( (HASH) & GUF_DICT_KV_META_HASHFRAG_MASK ) #define GUF_DICT_HASH_T_GET_HASHFRAG(HASH) ( (HASH) & GUF_DICT_KV_META_HASHFRAG_MASK )
#else #else
#error "guf_dict: invalid hash size (should not happen)" #error "guf_dict: invalid hash size (should not happen)"
@ -447,10 +447,11 @@ static inline size_t GUF_CAT(GUF_DICT_NAME, _probe_offset_)(size_t probe_len)
return probe_len; // 1, 2, 3, 4, 5, ... return probe_len; // 1, 2, 3, 4, 5, ...
#else #else
/* /*
Quadratic probing:
Guaranteed to visit each index once for capacities which are powers of two. Guaranteed to visit each index once for capacities which are powers of two.
cf. https://fgiesen.wordpress.com/2015/02/22/triangular-numbers-mod-2n/ (last-retrieved 2024-07-29) cf. https://fgiesen.wordpress.com/2015/02/22/triangular-numbers-mod-2n/ (last-retrieved 2024-07-29)
*/ */
return probe_len * (probe_len + 1) / 2; // 1, 3, 6, 10, 15, 21 ... (starting from probe_len == 1) return probe_len * (probe_len + 1u) / 2u; // 1, 3, 6, 10, 15, 21 ... (starting from probe_len == 1)
#endif #endif
} }

View File

@ -19,29 +19,22 @@
*/ */
#define GUF_HASH32_INIT UINT32_C(2166136261) #define GUF_HASH32_INIT UINT32_C(2166136261)
#ifdef UINT64_MAX #define GUF_HASH64_INIT UINT64_C(14695981039346656037)
#define GUF_HASH64_INIT UINT64_C(14695981039346656037)
#endif
GUF_HASH_KWRDS uint32_t guf_hash32(const void *data, ptrdiff_t num_bytes, uint32_t hash); // FNV-1a (32 bit) GUF_HASH_KWRDS uint_fast32_t guf_hash32(const void *data, ptrdiff_t num_bytes, uint_fast32_t hash); // FNV-1a (32 bit)
#ifdef UINT64_MAX 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 uint64_t guf_hash64(const void *data, ptrdiff_t num_bytes, uint64_t hash); // FNV-1a (64 bit)
#endif
#ifdef GUF_HASH_32_BIT #ifdef GUF_HASH_32_BIT
typedef uint32_t guf_hash_size_t; typedef uint_fast32_t guf_hash_size_t;
#define GUF_HASH_INIT GUF_HASH32_INIT #define GUF_HASH_INIT GUF_HASH32_INIT
#define GUF_HASH_MAX UINT32_MAX #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) { 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); return guf_hash32(data, num_bytes, hash);
} }
#else #else
#ifndef UINT64_MAX typedef uint_fast64_t guf_hash_size_t;
#error "guf_hash.h: Platform does not support uint64_t (define GUF_HASH_32_BIT to fix)"
#endif
typedef uint64_t guf_hash_size_t;
#define GUF_HASH_INIT GUF_HASH64_INIT #define GUF_HASH_INIT GUF_HASH64_INIT
#define GUF_HASH_MAX UINT64_MAX #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) { static inline guf_hash_size_t guf_hash(const void *data, ptrdiff_t num_bytes, guf_hash_size_t hash) {
return guf_hash64(data, num_bytes, hash); return guf_hash64(data, num_bytes, hash);
} }
@ -53,36 +46,37 @@ GUF_HASH_KWRDS uint32_t guf_hash32(const void *data, ptrdiff_t num_bytes, uint32
#include "guf_assert.h" #include "guf_assert.h"
GUF_HASH_KWRDS uint32_t guf_hash32(const void *data, ptrdiff_t num_bytes, uint32_t hash) GUF_HASH_KWRDS uint_fast32_t guf_hash32(const void *data, ptrdiff_t num_bytes, uint_fast32_t hash)
{ {
hash = GUF_UWRAP_32(hash);
GUF_ASSERT_RELEASE(data); GUF_ASSERT_RELEASE(data);
GUF_ASSERT_RELEASE(num_bytes >= 0); 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 unsigned char *data_bytes = (const unsigned char*)data; // This does not break strict-aliasing rules I think...
const uint32_t FNV_32_PRIME = UINT32_C(16777619); const uint_fast32_t FNV_32_PRIME = UINT32_C(16777619);
for (ptrdiff_t i = 0; i < num_bytes; ++i) { for (ptrdiff_t i = 0; i < num_bytes; ++i) {
hash ^= 1u * data_bytes[i]; hash = GUF_UWRAP_32(1u * hash ^ data_bytes[i]);
hash *= 1u * FNV_32_PRIME; hash = GUF_UWRAP_32(1u * hash * FNV_32_PRIME);
} }
return hash; return hash;
} }
#ifdef UINT64_MAX GUF_HASH_KWRDS uint_fast64_t guf_hash64(const void *data, ptrdiff_t num_bytes, uint_fast64_t hash)
GUF_HASH_KWRDS uint64_t guf_hash64(const void *data, ptrdiff_t num_bytes, uint64_t hash) {
{ hash = GUF_UWRAP_64(hash);
GUF_ASSERT_RELEASE(data); GUF_ASSERT_RELEASE(data);
GUF_ASSERT_RELEASE(num_bytes >= 0); 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 unsigned char *data_bytes = (const unsigned char*)data; // This does not break strict-aliasing rules I think...
const uint64_t FNV_64_PRIME = UINT64_C(1099511628211); const uint_fast64_t FNV_64_PRIME = UINT64_C(1099511628211);
for (ptrdiff_t i = 0; i < num_bytes; ++i) { for (ptrdiff_t i = 0; i < num_bytes; ++i) {
hash ^= 1u * data_bytes[i]; hash = GUF_UWRAP_64(1u * hash ^ data_bytes[i]);
hash *= 1u * FNV_64_PRIME; hash = GUF_UWRAP_64(1u * hash * FNV_64_PRIME);
}
return hash;
} }
#endif return hash;
}
#undef GUF_HASH_IMPL #undef GUF_HASH_IMPL
#undef GUF_HASH_IMPL_STATIC #undef GUF_HASH_IMPL_STATIC
#endif /* endif GUF_IMPL/GUF_IMPL_STATIC */ #endif /* endif GUF_IMPL/GUF_IMPL_STATIC */
#undef GUF_HASH_KWRDS #undef GUF_HASH_KWRDS

View File

@ -15,10 +15,6 @@
#define GUF_MAX_F32_LT_ONE (1.f - FLT_EPSILON/FLT_RADIX) #define GUF_MAX_F32_LT_ONE (1.f - FLT_EPSILON/FLT_RADIX)
#define GUF_MAX_F64_LT_ONE (1.0 - DBL_EPSILON/FLT_RADIX) #define GUF_MAX_F64_LT_ONE (1.0 - DBL_EPSILON/FLT_RADIX)
// Rotate left.
static inline uint64_t guf_rotl_u64(uint64_t x, int k) {return (1u*x << k) | (1u*x >> (64 - k));}
static inline uint32_t guf_rotl_u32(uint32_t x, int k) {return (1u*x << k) | (1u*x >> (32 - k));}
// Typesafe unsigned integer wrapping functions (generated with libguf/tools/intwrap-gen.py) // 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_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_fast8_t guf_wrap8_uint_fast8_t(uint_fast8_t a) { return a & GUF_UINT8_MAX; }
@ -37,13 +33,50 @@ static inline unsigned short guf_wrap16_ushort(unsigned short a) { r
static inline unsigned long guf_wrap32_ulong(unsigned long a) { return a & GUF_UINT32_MAX; } // unsigned long: >= 32 bits static inline unsigned long guf_wrap32_ulong(unsigned long a) { return a & GUF_UINT32_MAX; } // unsigned long: >= 32 bits
static inline unsigned long long guf_wrap64_ulong_long(unsigned long long a) { return a & GUF_UINT64_MAX; } // unsigned long long: >= 64 bits static inline unsigned long long guf_wrap64_ulong_long(unsigned long long a) { return a & GUF_UINT64_MAX; } // unsigned long long: >= 64 bits
#define GUF_UWRAP_8(UINT) ( (UINT) & GUF_UINT8_MAX ) // Rotate left.
#define GUF_UWRAP_16(UINT) ( (UINT) & GUF_UINT16_MAX ) #ifdef UINT32_MAX
#define GUF_UWRAP_32(UINT) ( (UINT) & GUF_UINT32_MAX ) static inline uint32_t guf_rotl_u32(uint32_t x, int k)
#define GUF_UWRAP_64(UINT) ( (UINT) & GUF_UINT64_MAX ) {
GUF_ASSERT(k > 0);
return (1u*x << k) | (1u*x >> (32 - k));
}
#endif
#ifdef UINT64_MAX
static inline uint64_t guf_rotl_u64(uint64_t x, int k)
{
GUF_ASSERT(k > 0);
return (1u*x << k) | (1u*x >> (64 - k));
}
#endif
static inline uint_fast32_t guf_rotl32_uint_fast32_t(uint_fast32_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)) );
}
static inline uint_fast64_t guf_rotl64_uint_fast64_t(uint_fast64_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)) );
}
static inline unsigned long guf_rotl32_ulong(unsigned long x, int k)
{
GUF_ASSERT(k > 0);
x = guf_wrap32_ulong(x);
return guf_wrap32_ulong( (x << k) | (x >> (32 - k)) );
}
static inline unsigned long long guf_rotl64_ulong_long(unsigned long long x, int k)
{
GUF_ASSERT(k > 0);
x = guf_wrap64_ulong_long(x);
return guf_wrap64_ulong_long( (x << k) | (x >> (64 - k)) );
}
// Signed min, max, clamp functions (generated with libguf/tools/min_max_clamp-gen.py) // Signed min, max, clamp functions (generated with libguf/tools/min_max_clamp-gen.py)
static inline char guf_min_char(char a, char b) { return a < b ? a : b; } static inline char guf_min_char(char a, char b) { return a < b ? a : b; }
static inline char guf_max_char(char a, char b) { return a > b ? a : b; } static inline char guf_max_char(char a, char b) { return a > b ? a : b; }
static inline char guf_clamp_char(char x, char min, char max) { if (x < min) {return min;} if (x > max) {return max;} return x; } static inline char guf_clamp_char(char x, char min, char max) { if (x < min) {return min;} if (x > max) {return max;} return x; }
@ -60,26 +93,26 @@ static inline long long guf_min_long_long(long long a, long long b) { return a <
static inline long long guf_max_long_long(long long a, long long b) { return a > b ? a : b; } static inline long long guf_max_long_long(long long a, long long b) { return a > b ? a : b; }
static inline long long guf_clamp_long_long(long long x, long long min, long long max) { if (x < min) {return min;} if (x > max) {return max;} return x; } static inline long long guf_clamp_long_long(long long x, long long min, long long max) { if (x < min) {return min;} if (x > max) {return max;} return x; }
static inline int8_t guf_min_i8(int8_t a, int8_t b) { return a < b ? a : b; }
static inline int8_t guf_max_i8(int8_t a, int8_t b) { return a > b ? a : b; }
static inline int8_t guf_clamp_i8(int8_t x, int8_t min, int8_t max) { if (x < min) {return min;} if (x > max) {return max;} return x; }
static inline int16_t guf_min_i16(int16_t a, int16_t b) { return a < b ? a : b; }
static inline int16_t guf_max_i16(int16_t a, int16_t b) { return a > b ? a : b; }
static inline int16_t guf_clamp_i16(int16_t x, int16_t min, int16_t max) { if (x < min) {return min;} if (x > max) {return max;} return x; }
static inline int32_t guf_min_i32(int32_t a, int32_t b) { return a < b ? a : b; }
static inline int32_t guf_max_i32(int32_t a, int32_t b) { return a > b ? a : b; }
static inline int32_t guf_clamp_i32(int32_t x, int32_t min, int32_t max) { if (x < min) {return min;} if (x > max) {return max;} return x; }
static inline int64_t guf_min_i64(int64_t a, int64_t b) { return a < b ? a : b; }
static inline int64_t guf_max_i64(int64_t a, int64_t b) { return a > b ? a : b; }
static inline int64_t guf_clamp_i64(int64_t x, int64_t min, int64_t max) { if (x < min) {return min;} if (x > max) {return max;} return x; }
static inline ptrdiff_t guf_min_ptrdiff_t(ptrdiff_t a, ptrdiff_t b) { return a < b ? a : b; } static inline ptrdiff_t guf_min_ptrdiff_t(ptrdiff_t a, ptrdiff_t b) { return a < b ? a : b; }
static inline ptrdiff_t guf_max_ptrdiff_t(ptrdiff_t a, ptrdiff_t b) { return a > b ? a : b; } 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 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_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_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_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 float guf_min_f32(float a, float b) { return a < b ? a : b; } 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; } static inline float guf_max_f32(float a, float b) { return a > b ? a : b; }
static inline float guf_clamp_f32(float x, float min, float max) { if (x < min) {return min;} if (x > max) {return max;} return x; } static inline float guf_clamp_f32(float x, float min, float max) { if (x < min) {return min;} if (x > max) {return max;} return x; }
@ -88,8 +121,31 @@ static inline double guf_min_f64(double a, double b) { return a < b ? a : b; }
static inline double guf_max_f64(double a, double b) { return a > b ? a : b; } static inline double guf_max_f64(double a, double b) { return a > b ? a : b; }
static inline double guf_clamp_f64(double x, double min, double max) { if (x < min) {return min;} if (x > max) {return max;} return x; } static inline double guf_clamp_f64(double x, double min, double max) { if (x < min) {return min;} if (x > max) {return max;} return x; }
// Unsigned min, max, clamp functions (generated with libguf/tools/min_max_clamp-gen.py) #ifdef INT8_MAX
static inline int8_t guf_min_i8(int8_t a, int8_t b) { return a < b ? a : b; }
static inline int8_t guf_max_i8(int8_t a, int8_t b) { return a > b ? a : b; }
static inline int8_t guf_clamp_i8(int8_t x, int8_t min, int8_t max) { if (x < min) {return min;} if (x > max) {return max;} return x; }
#endif
#ifdef INT16_MAX
static inline int16_t guf_min_i16(int16_t a, int16_t b) { return a < b ? a : b; }
static inline int16_t guf_max_i16(int16_t a, int16_t b) { return a > b ? a : b; }
static inline int16_t guf_clamp_i16(int16_t x, int16_t min, int16_t max) { if (x < min) {return min;} if (x > max) {return max;} return x; }
#endif
#ifdef INT32_MAX
static inline int32_t guf_min_i32(int32_t a, int32_t b) { return a < b ? a : b; }
static inline int32_t guf_max_i32(int32_t a, int32_t b) { return a > b ? a : b; }
static inline int32_t guf_clamp_i32(int32_t x, int32_t min, int32_t max) { if (x < min) {return min;} if (x > max) {return max;} return x; }
#endif
#ifdef INT64_MAX
static inline int64_t guf_min_i64(int64_t a, int64_t b) { return a < b ? a : b; }
static inline int64_t guf_max_i64(int64_t a, int64_t b) { return a > b ? a : b; }
static inline int64_t guf_clamp_i64(int64_t x, int64_t min, int64_t max) { if (x < min) {return min;} if (x > max) {return max;} return x; }
#endif
// Unsigned min, max, clamp functions (generated with libguf/tools/min_max_clamp-gen.py)
static inline unsigned char guf_min_uchar(unsigned char a, unsigned char b) { return a < b ? a : b; } static inline unsigned char guf_min_uchar(unsigned char a, unsigned char b) { return a < b ? a : b; }
static inline unsigned char guf_max_uchar(unsigned char a, unsigned char b) { return a > b ? a : b; } static inline unsigned char guf_max_uchar(unsigned char a, unsigned char b) { return a > b ? a : b; }
static inline unsigned char guf_clamp_uchar(unsigned char x, unsigned char min, unsigned char max) { if (x < min) {return min;} if (x > max) {return max;} return x; } static inline unsigned char guf_clamp_uchar(unsigned char x, unsigned char min, unsigned char max) { if (x < min) {return min;} if (x > max) {return max;} return x; }
@ -106,51 +162,103 @@ static inline unsigned long long guf_min_ulong_long(unsigned long long a, unsign
static inline unsigned long long guf_max_ulong_long(unsigned long long a, unsigned long long b) { return a > b ? a : b; } static inline unsigned long long guf_max_ulong_long(unsigned long long a, unsigned long long b) { return a > b ? a : b; }
static inline unsigned long long guf_clamp_ulong_long(unsigned long long x, unsigned long long min, unsigned long long max) { if (x < min) {return min;} if (x > max) {return max;} return x; } static inline unsigned long long guf_clamp_ulong_long(unsigned long long x, unsigned long long min, unsigned long long max) { if (x < min) {return min;} if (x > max) {return max;} return x; }
static inline uint8_t guf_min_u8(uint8_t a, uint8_t b) { return a < b ? a : b; }
static inline uint8_t guf_max_u8(uint8_t a, uint8_t b) { return a > b ? a : b; }
static inline uint8_t guf_clamp_u8(uint8_t x, uint8_t min, uint8_t max) { if (x < min) {return min;} if (x > max) {return max;} return x; }
static inline uint16_t guf_min_u16(uint16_t a, uint16_t b) { return a < b ? a : b; }
static inline uint16_t guf_max_u16(uint16_t a, uint16_t b) { return a > b ? a : b; }
static inline uint16_t guf_clamp_u16(uint16_t x, uint16_t min, uint16_t max) { if (x < min) {return min;} if (x > max) {return max;} return x; }
static inline uint32_t guf_min_u32(uint32_t a, uint32_t b) { return a < b ? a : b; }
static inline uint32_t guf_max_u32(uint32_t a, uint32_t b) { return a > b ? a : b; }
static inline uint32_t guf_clamp_u32(uint32_t x, uint32_t min, uint32_t max) { if (x < min) {return min;} if (x > max) {return max;} return x; }
static inline uint64_t guf_min_u64(uint64_t a, uint64_t b) { return a < b ? a : b; }
static inline uint64_t guf_max_u64(uint64_t a, uint64_t b) { return a > b ? a : b; }
static inline uint64_t guf_clamp_u64(uint64_t x, uint64_t min, uint64_t max) { if (x < min) {return min;} if (x > max) {return max;} return x; }
static inline size_t guf_min_size_t(size_t a, size_t b) { return a < b ? a : b; } 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_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 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_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_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_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; }
#ifdef UINT8_MAX
static inline uint8_t guf_min_u8(uint8_t a, uint8_t b) { return a < b ? a : b; }
static inline uint8_t guf_max_u8(uint8_t a, uint8_t b) { return a > b ? a : b; }
static inline uint8_t guf_clamp_u8(uint8_t x, uint8_t min, uint8_t max) { if (x < min) {return min;} if (x > max) {return max;} return x; }
#endif
#ifdef UINT16_MAX
static inline uint16_t guf_min_u16(uint16_t a, uint16_t b) { return a < b ? a : b; }
static inline uint16_t guf_max_u16(uint16_t a, uint16_t b) { return a > b ? a : b; }
static inline uint16_t guf_clamp_u16(uint16_t x, uint16_t min, uint16_t max) { if (x < min) {return min;} if (x > max) {return max;} return x; }
#endif
#ifdef UINT32_MAX
static inline uint32_t guf_min_u32(uint32_t a, uint32_t b) { return a < b ? a : b; }
static inline uint32_t guf_max_u32(uint32_t a, uint32_t b) { return a > b ? a : b; }
static inline uint32_t guf_clamp_u32(uint32_t x, uint32_t min, uint32_t max) { if (x < min) {return min;} if (x > max) {return max;} return x; }
#endif
#ifdef UINT64_MAX
static inline uint64_t guf_min_u64(uint64_t a, uint64_t b) { return a < b ? a : b; }
static inline uint64_t guf_max_u64(uint64_t a, uint64_t b) { return a > b ? a : b; }
static inline uint64_t guf_clamp_u64(uint64_t x, uint64_t min, uint64_t max) { if (x < min) {return min;} if (x > max) {return max;} return x; }
#endif
// abs functions with signed result (can fail/panic for abs(INT_TYPE_MIN) for platforms with two's complement signed ints; C2X for example guarantees two's complement) // abs functions with signed result (can fail/panic for abs(INT_TYPE_MIN) for platforms with two's complement signed ints; C2X for example guarantees two's complement)
static inline int guf_abs_int(int x) {if (x >= 0) {return x;} GUF_ASSERT_RELEASE(-INT_MAX == INT_MIN || x > INT_MIN); return -x;} // I would not drink that... // static inline int guf_abs_int(int x) {if (x >= 0) {return x;} GUF_ASSERT_RELEASE(-INT_MAX == INT_MIN || x > INT_MIN); return -x;} // I would not drink that...
static inline int8_t guf_abs_i8 (int8_t x) {if (x >= 0) {return x;} GUF_ASSERT_RELEASE(-INT8_MAX == INT8_MIN || x > INT8_MIN); return -x;} // static inline int8_t guf_abs_i8 (int8_t x) {if (x >= 0) {return x;} GUF_ASSERT_RELEASE(-INT8_MAX == INT8_MIN || x > INT8_MIN); return -x;}
static inline int16_t guf_abs_i16(int16_t x) {if (x >= 0) {return x;} GUF_ASSERT_RELEASE(-INT16_MAX == INT16_MIN || x > INT16_MIN); return -x;} // static inline int16_t guf_abs_i16(int16_t x) {if (x >= 0) {return x;} GUF_ASSERT_RELEASE(-INT16_MAX == INT16_MIN || x > INT16_MIN); return -x;}
static inline int32_t guf_abs_i32(int32_t x) {if (x >= 0) {return x;} GUF_ASSERT_RELEASE(-INT32_MAX == INT32_MIN || x > INT32_MIN); return -x;} // static inline int32_t guf_abs_i32(int32_t x) {if (x >= 0) {return x;} GUF_ASSERT_RELEASE(-INT32_MAX == INT32_MIN || x > INT32_MIN); return -x;}
static inline int64_t guf_abs_i64(int64_t x) {if (x >= 0) {return x;} GUF_ASSERT_RELEASE(-INT64_MAX == INT64_MIN || x > INT64_MIN); return -x;} // static inline int64_t guf_abs_i64(int64_t x) {if (x >= 0) {return x;} GUF_ASSERT_RELEASE(-INT64_MAX == INT64_MIN || x > INT64_MIN); return -x;}
static inline ptrdiff_t guf_abs_ptrdiff(ptrdiff_t x) {if (x >= 0) {return x;} GUF_ASSERT_RELEASE(-PTRDIFF_MAX == PTRDIFF_MIN || x > PTRDIFF_MIN); return -x;} // static inline ptrdiff_t guf_abs_ptrdiff(ptrdiff_t x) {if (x >= 0) {return x;} GUF_ASSERT_RELEASE(-PTRDIFF_MAX == PTRDIFF_MIN || x > PTRDIFF_MIN); return -x;}
// abs functions with unsigned result functions (cannot fail) // abs functions with unsigned result functions (cannot fail)
static inline unsigned guf_uabs_int(int x) {if (x >= 0) {return x;} else if (x == INT_MIN && -INT_MAX != INT_MIN) {return (unsigned)INT_MAX + 1u;} else {return -x;}} static inline unsigned guf_uabs_int(int x) {if (x >= 0) {return x;} else if (x == INT_MIN && -INT_MAX != INT_MIN) {return (unsigned)INT_MAX + 1u;} else {return -x;}}
static inline uint8_t guf_uabs_i8(int8_t x) {if (x >= 0) {return x;} else if (x == INT8_MIN && -INT8_MAX != INT8_MIN) {return (uint8_t)INT8_MAX + 1u;} else {return -x;}} static inline unsigned long guf_uabs_long(long x) {if (x >= 0) {return x;} else if (x == LONG_MIN && -LONG_MAX != LONG_MIN) {return (unsigned long)LONG_MAX + 1u;} else {return -x;}}
static inline uint16_t guf_uabs_i16(int16_t x) {if (x >= 0) {return x;} else if (x == INT16_MIN && -INT16_MAX != INT16_MIN) {return (uint16_t)INT16_MAX + 1u;} else {return -x;}} static inline unsigned long long guf_uabs_long_long(long long x) {if (x >= 0) {return x;} else if (x == LLONG_MIN && -LLONG_MAX != LONG_MIN) {return (unsigned long long)LLONG_MAX + 1u;} else {return -x;}}
static inline uint32_t guf_uabs_i32(int32_t x) {if (x >= 0) {return x;} else if (x == INT32_MIN && -INT32_MAX != INT32_MIN) {return (uint32_t)INT32_MAX + 1u;} else {return -x;}} static inline size_t guf_uabs_ptrdiff_t(ptrdiff_t x) {if (x >= 0) {return x;} else if (x == PTRDIFF_MIN && -PTRDIFF_MAX != PTRDIFF_MIN) {return (size_t)PTRDIFF_MAX + 1u;} else {return -x;}}
static inline uint64_t guf_uabs_i64(int64_t x) {if (x >= 0) {return x;} else if (x == INT64_MIN && -INT64_MAX != INT64_MIN) {return (uint64_t)INT64_MAX + 1u;} else {return -x;}}
static inline size_t guf_uabs_ptrdiff_t(ptrdiff_t x) {if (x >= 0) {return x;} else if (x == PTRDIFF_MIN && -PTRDIFF_MAX != PTRDIFF_MIN) {return (size_t)PTRDIFF_MAX + 1u;} else {return -x;}} #if defined(UINT8_MAX) && defined(INT8_MAX)
static inline uint8_t guf_uabs_i8(int8_t x) {if (x >= 0) {return x;} else if (x == INT8_MIN && -INT8_MAX != INT8_MIN) {return (uint8_t)INT8_MAX + 1u;} else {return -x;}}
#endif
#if defined(UINT16_MAX) && defined(INT16_MAX)
static inline uint16_t guf_uabs_i16(int16_t x) {if (x >= 0) {return x;} else if (x == INT16_MIN && -INT16_MAX != INT16_MIN) {return (uint16_t)INT16_MAX + 1u;} else {return -x;}}
#endif
#if defined(UINT32_MAX) && defined(INT32_MAX)
static inline uint32_t guf_uabs_i32(int32_t x) {if (x >= 0) {return x;} else if (x == INT32_MIN && -INT32_MAX != INT32_MIN) {return (uint32_t)INT32_MAX + 1u;} else {return -x;}}
#endif
#if defined(UINT64_MAX) && defined(INT64_MAX)
static inline uint64_t guf_uabs_i64(int64_t x) {if (x >= 0) {return x;} else if (x == INT64_MIN && -INT64_MAX != INT64_MIN) {return (uint64_t)INT64_MAX + 1u;} else {return -x;}}
#endif
// absdiff functions with unsigned result (cannot fail) // absdiff functions with unsigned result (cannot fail)
static inline unsigned char guf_absdiff_char(char a, char b) {return a > b ? (unsigned char)a - (unsigned char)b : (unsigned char)b - (unsigned char)a;} static inline unsigned char guf_absdiff_char(char a, char b) {return a > b ? (unsigned char)a - (unsigned char)b : (unsigned char)b - (unsigned char)a;}
static inline unsigned guf_absdiff_int(int a, int b) {return a > b ? (unsigned)a - (unsigned)b : (unsigned)b - (unsigned)a;} static inline unsigned short guf_absdiff_short(short a, short b) {return a > b ? (unsigned short)a - (unsigned short)b : (unsigned short)b - (unsigned short)a;}
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;} static inline unsigned guf_absdiff_int(int a, int b) {return a > b ? (unsigned)a - (unsigned)b : (unsigned)b - (unsigned)a;}
static inline uint16_t guf_absdiff_i16(int16_t a, int16_t b) {return a > b ? (uint16_t)a - (uint16_t)b : (uint16_t)b - (uint16_t)a;} static inline unsigned long guf_absdiff_long(long a, long b) {return a > b ? (unsigned long)a - (unsigned long)b : (unsigned long)b - (unsigned long)a;}
static inline uint32_t guf_absdiff_i32(int32_t a, int32_t b) {return a > b ? (uint32_t)a - (uint32_t)b : (uint32_t)b - (uint32_t)a;} 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 uint64_t guf_absdiff_i64(int64_t a, int64_t b) {return a > b ? (uint64_t)a - (uint64_t)b : (uint64_t)b - (uint64_t)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 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);}
#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;}
#endif
#if defined(UINT16_MAX) && defined(INT16_MAX)
static inline uint16_t guf_absdiff_i16(int16_t a, int16_t b) {return a > b ? (uint16_t)a - (uint16_t)b : (uint16_t)b - (uint16_t)a;}
#endif
#if defined(UINT32_MAX) && defined(INT32_MAX)
static inline uint32_t guf_absdiff_i32(int32_t a, int32_t b) {return a > b ? (uint32_t)a - (uint32_t)b : (uint32_t)b - (uint32_t)a;}
#endif
#if defined(UINT64_MAX) && defined(INT64_MAX)
static inline uint64_t guf_absdiff_i64(int64_t a, int64_t b) {return a > b ? (uint64_t)a - (uint64_t)b : (uint64_t)b - (uint64_t)a;}
#endif
static inline bool guf_add_is_overflow_size_t(size_t a, size_t b) static inline bool guf_add_is_overflow_size_t(size_t a, size_t b)
@ -186,12 +294,24 @@ static inline bool guf_size_calc_safe(ptrdiff_t count, ptrdiff_t sizeof_elem, pt
// cf. https://graphics.stanford.edu/~seander/bithacks.html#DetermineIfPowerOf2 (last-retrieved 2025-03-19) // cf. https://graphics.stanford.edu/~seander/bithacks.html#DetermineIfPowerOf2 (last-retrieved 2025-03-19)
static inline bool guf_is_pow2_u8(uint8_t x) { return x && !(x & (x - 1)); } static inline bool guf_is_pow2_uchar(unsigned char x) { return x && !(x & (x - 1)); }
static inline bool guf_is_pow2_u16(uint16_t x) { return x && !(x & (x - 1)); } static inline bool guf_is_pow2_ushort(unsigned short x) { return x && !(x & (x - 1)); }
static inline bool guf_is_pow2_u32(uint32_t x) { return x && !(x & (x - 1)); } static inline bool guf_is_pow2_unsigned(unsigned x) { return x && !(x & (x - 1)); }
static inline bool guf_is_pow2_u64(uint64_t x) { return x && !(x & (x - 1)); } static inline bool guf_is_pow2_ulong(unsigned long x) { return x && !(x & (x - 1)); }
static inline bool guf_is_pow2_size_t(size_t x) { return x && !(x & (x - 1)); } static inline bool guf_is_pow2_ulong_long(unsigned long long x) { return x && !(x & (x - 1)); }
static inline bool guf_is_pow2_size_t(size_t x) { return x && !(x & (x - 1)); }
#ifdef UINT8_MAX
static inline bool guf_is_pow2_u8(uint8_t x) { return x && !(x & (x - 1)); }
#endif
#ifdef UINT16_MAX
static inline bool guf_is_pow2_u16(uint16_t x) { return x && !(x & (x - 1)); }
#endif
#ifdef UINT32_MAX
static inline bool guf_is_pow2_u32(uint32_t x) { return x && !(x & (x - 1)); }
#endif
#if UINT64_MAX
static inline bool guf_is_pow2_u64(uint64_t x) { return x && !(x & (x - 1)); }
#endif
static bool guf_nearly_zero_f32(float x, float eps) {return fabsf(x) <= eps;} static bool guf_nearly_zero_f32(float x, float eps) {return fabsf(x) <= eps;}
static bool guf_nearly_one_f32(float x, float eps) {return fabsf(x - 1) <= eps;} static bool guf_nearly_one_f32(float x, float eps) {return fabsf(x - 1) <= eps;}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -94,8 +94,8 @@ GUF_STR_KWRDS int guf_str_view_cmp(const void *str_view_a, const void *str_view_
// Hash functions. // Hash functions.
GUF_STR_KWRDS guf_hash_size_t guf_str_view_hash(const guf_str_view *sv); GUF_STR_KWRDS guf_hash_size_t guf_str_view_hash(const guf_str_view *sv);
GUF_STR_KWRDS uint64_t guf_str_view_hash64(const guf_str_view *sv); GUF_STR_KWRDS uint_fast64_t guf_str_view_hash64(const guf_str_view *sv);
GUF_STR_KWRDS uint32_t guf_str_view_hash32(const guf_str_view *sv); GUF_STR_KWRDS uint_fast32_t guf_str_view_hash32(const guf_str_view *sv);
// Return a new guf_str_view corresponding to the substring with leading/trailing ascii-whitespace chars removed from the left/right // Return a new guf_str_view corresponding to the substring with leading/trailing ascii-whitespace chars removed from the left/right
GUF_STR_KWRDS guf_str_view guf_str_view_trim_left_ascii(guf_str_view sv); GUF_STR_KWRDS guf_str_view guf_str_view_trim_left_ascii(guf_str_view sv);
@ -151,8 +151,8 @@ GUF_STR_KWRDS int guf_str_cmp(const guf_str *a, const guf_str *b);
// Hash-functions. // Hash-functions.
GUF_STR_KWRDS guf_hash_size_t guf_str_hash(const guf_str *str); GUF_STR_KWRDS guf_hash_size_t guf_str_hash(const guf_str *str);
GUF_STR_KWRDS uint64_t guf_str_hash64(const guf_str *str); GUF_STR_KWRDS uint_fast64_t guf_str_hash64(const guf_str *str);
GUF_STR_KWRDS uint32_t guf_str_hash32(const guf_str *str); GUF_STR_KWRDS uint_fast32_t guf_str_hash32(const guf_str *str);
// Reserve at least min_capacity characters (excluding the null-terminator) (try to double the current capacity first; if that's not at least min_capacity, set the new capacity to min_capacity instead). // Reserve at least min_capacity characters (excluding the null-terminator) (try to double the current capacity first; if that's not at least min_capacity, set the new capacity to min_capacity instead).
GUF_STR_KWRDS guf_str *guf_str_try_reserve(guf_str *str, ptrdiff_t min_capacity, guf_err *err); GUF_STR_KWRDS guf_str *guf_str_try_reserve(guf_str *str, ptrdiff_t min_capacity, guf_err *err);
@ -1196,13 +1196,13 @@ GUF_STR_KWRDS guf_hash_size_t guf_str_hash(const guf_str *str)
return guf_str_view_hash(&(guf_str_view){.str = guf_str_const_cstr(str), .len = guf_str_len(str)}); return guf_str_view_hash(&(guf_str_view){.str = guf_str_const_cstr(str), .len = guf_str_len(str)});
} }
GUF_STR_KWRDS uint64_t guf_str_hash64(const guf_str *str) GUF_STR_KWRDS uint_fast64_t guf_str_hash64(const guf_str *str)
{ {
GUF_ASSERT(guf_str_is_valid(str)); GUF_ASSERT(guf_str_is_valid(str));
return guf_str_view_hash64(&(guf_str_view){.str = guf_str_const_cstr(str), .len = guf_str_len(str)}); return guf_str_view_hash64(&(guf_str_view){.str = guf_str_const_cstr(str), .len = guf_str_len(str)});
} }
GUF_STR_KWRDS uint32_t guf_str_hash32(const guf_str *str) GUF_STR_KWRDS uint_fast32_t guf_str_hash32(const guf_str *str)
{ {
GUF_ASSERT(guf_str_is_valid(str)); GUF_ASSERT(guf_str_is_valid(str));
return guf_str_view_hash32(&(guf_str_view){.str = guf_str_const_cstr(str), .len = guf_str_len(str)}); return guf_str_view_hash32(&(guf_str_view){.str = guf_str_const_cstr(str), .len = guf_str_len(str)});
@ -1457,7 +1457,7 @@ GUF_STR_KWRDS guf_hash_size_t guf_str_view_hash(const guf_str_view *sv)
return guf_hash(sv->str, sv->len, GUF_HASH_INIT); return guf_hash(sv->str, sv->len, GUF_HASH_INIT);
} }
GUF_STR_KWRDS uint64_t guf_str_view_hash64(const guf_str_view *sv) GUF_STR_KWRDS uint_fast64_t guf_str_view_hash64(const guf_str_view *sv)
{ {
GUF_ASSERT(sv); GUF_ASSERT(sv);
if (!sv->str || sv->len <= 0) { if (!sv->str || sv->len <= 0) {
@ -1466,7 +1466,7 @@ GUF_STR_KWRDS uint64_t guf_str_view_hash64(const guf_str_view *sv)
return guf_hash64(sv->str, sv->len, GUF_HASH64_INIT); return guf_hash64(sv->str, sv->len, GUF_HASH64_INIT);
} }
GUF_STR_KWRDS uint32_t guf_str_view_hash32(const guf_str_view *sv) GUF_STR_KWRDS uint_fast32_t guf_str_view_hash32(const guf_str_view *sv)
{ {
GUF_ASSERT(sv); GUF_ASSERT(sv);
if (!sv->str || sv->len <= 0) { if (!sv->str || sv->len <= 0) {

View File

@ -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_valid(const guf_utf8_char *c);
GUF_UTF8_KWRDS bool guf_utf8_char_is_whitespace(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(uint32_t codepoint); // Returns GUF_UTF8_REPLACEMENT_CHAR for invalid codepoints (and for GUF_UTF8_REPLACEMENT_CHAR_CODEPOINT). 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, uint32_t codepoint); // Returns false for invalid codepoints. GUF_UTF8_KWRDS bool guf_utf8_encode(guf_utf8_char *result, uint_fast32_t codepoint); // Returns false for invalid codepoints.
GUF_UTF8_KWRDS int32_t guf_utf8_decode(const guf_utf8_char *utf8); // Returns -1 for invalid utf-8. GUF_UTF8_KWRDS int_fast32_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); 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) // 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, uint32_t cp) GUF_UTF8_KWRDS bool guf_utf8_encode(guf_utf8_char *result, uint_fast32_t cp)
{ {
GUF_ASSERT(result); GUF_ASSERT(result);
@ -138,8 +138,8 @@ GUF_UTF8_KWRDS bool guf_utf8_encode(guf_utf8_char *result, uint32_t cp)
int cp_bits = 0; int cp_bits = 0;
for (int byte_n = num_bytes - 1; byte_n >= 0 && cp > 0; --byte_n) { 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 int bits = (byte_n == 0) ? first_byte_bits : tail_byte_bits;
const uint32_t cp_mask = (UINT32_C(1) << bits) - 1; const uint_fast32_t cp_mask = GUF_UWRAP_32( (UINT32_C(1) << bits) - 1 );
result->bytes[byte_n] = (char)((unsigned char)result->bytes[byte_n] | (cp & cp_mask)); result->bytes[byte_n] = (char)(1u * (unsigned char)result->bytes[byte_n] | (cp & cp_mask));
cp = cp >> bits; cp = cp >> bits;
cp_bits += bits; cp_bits += bits;
} }
@ -155,7 +155,7 @@ GUF_UTF8_KWRDS bool guf_utf8_encode(guf_utf8_char *result, uint32_t cp)
} }
} }
GUF_UTF8_KWRDS guf_utf8_char guf_utf8_char_new(uint32_t codepoint) GUF_UTF8_KWRDS guf_utf8_char guf_utf8_char_new(uint_fast32_t codepoint)
{ {
guf_utf8_char result = GUF_UTF8_REPLACEMENT_CHAR; guf_utf8_char result = GUF_UTF8_REPLACEMENT_CHAR;
guf_utf8_encode(&result, codepoint); guf_utf8_encode(&result, codepoint);
@ -163,7 +163,7 @@ GUF_UTF8_KWRDS guf_utf8_char guf_utf8_char_new(uint32_t codepoint)
} }
// cf. https://datatracker.ietf.org/doc/html/rfc3629#section-3 (last-retrieved 2025-03-02) // cf. https://datatracker.ietf.org/doc/html/rfc3629#section-3 (last-retrieved 2025-03-02)
GUF_UTF8_KWRDS int32_t guf_utf8_decode(const guf_utf8_char *c) GUF_UTF8_KWRDS int_fast32_t guf_utf8_decode(const guf_utf8_char *c)
{ {
if (!guf_utf8_char_is_valid(c)) { if (!guf_utf8_char_is_valid(c)) {
return -1; return -1;
@ -189,12 +189,12 @@ GUF_UTF8_KWRDS int32_t guf_utf8_decode(const guf_utf8_char *c)
return -1; return -1;
} }
uint32_t cp = 0; uint_fast32_t cp = 0;
int cp_bits = 0; int cp_bits = 0;
for (int byte_n = num_bytes - 1; byte_n >= 0; --byte_n) { 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 int bits = (byte_n == 0) ? first_byte_bits : tail_byte_bits;
const uint32_t byte_mask = (UINT32_C(1) << bits) - 1; const uint_fast32_t byte_mask = GUF_UWRAP_32( (UINT32_C(1) << bits) - 1 );
cp |= ((uint32_t)c->bytes[byte_n] & byte_mask) << cp_bits; cp = GUF_UWRAP_32( cp | GUF_UWRAP_32( 1u * ((uint_fast32_t)c->bytes[byte_n] & byte_mask) << cp_bits ) );
cp_bits += bits; cp_bits += bits;
} }
GUF_ASSERT(cp_bits == first_byte_bits + (num_bytes - 1) * tail_byte_bits); GUF_ASSERT(cp_bits == first_byte_bits + (num_bytes - 1) * tail_byte_bits);
@ -204,8 +204,11 @@ GUF_UTF8_KWRDS int32_t guf_utf8_decode(const guf_utf8_char *c)
if (!valid) { if (!valid) {
return -1; return -1;
} else { } else {
GUF_ASSERT(cp <= INT32_MAX); #ifdef INT32_MAX
return (int32_t)cp; GUF_ASSERT(cp <= INT32_MAX);
#endif
GUF_ASSERT(cp <= INT_FAST32_MAX);
return (int_fast32_t)cp;
} }
} }

View File

@ -27,6 +27,7 @@ void CkdIntTest::test_ckd()
const int32_t add_res = a + b; const int32_t add_res = a + b;
const guf_math_ckd_result ckd_add = guf_ckd_add_i8((int8_t)a, (int8_t)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_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));
if (add_res > INT8_MAX) { if (add_res > INT8_MAX) {
TEST_CHECK(ckd_add == GUF_MATH_CKD_OVERFLOW_POS); TEST_CHECK(ckd_add == GUF_MATH_CKD_OVERFLOW_POS);
int8_t saturated, saturated2; int8_t saturated, saturated2;
@ -72,6 +73,7 @@ void CkdIntTest::test_ckd()
const int32_t sub_res = a - b; const int32_t sub_res = a - b;
const guf_math_ckd_result ckd_sub = guf_ckd_sub_i8((int8_t)a, (int8_t)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));
if (sub_res > INT8_MAX) { if (sub_res > INT8_MAX) {
TEST_CHECK(ckd_sub == GUF_MATH_CKD_OVERFLOW_POS); TEST_CHECK(ckd_sub == GUF_MATH_CKD_OVERFLOW_POS);
int8_t saturated; int8_t saturated;
@ -100,6 +102,7 @@ void CkdIntTest::test_ckd()
const int32_t mul_res = a * b; const int32_t mul_res = a * b;
const guf_math_ckd_result ckd_mul = guf_ckd_mul_i8((int8_t)a, (int8_t)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_i8((int8_t)b, (int8_t)a)); TEST_CHECK(ckd_mul == guf_ckd_mul_i8((int8_t)b, (int8_t)a));
if (mul_res > INT8_MAX) { if (mul_res > INT8_MAX) {
TEST_CHECK(ckd_mul == GUF_MATH_CKD_OVERFLOW_POS); TEST_CHECK(ckd_mul == GUF_MATH_CKD_OVERFLOW_POS);
@ -220,6 +223,57 @@ void CkdIntTest::test_ckd()
guf_wrapping_mul_i32(4096, -314159265, &mul_i32_res); guf_wrapping_mul_i32(4096, -314159265, &mul_i32_res);
TEST_CHECK(mul_i32_res == 1693839360); 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);
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);
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);
mul_i32fast_res = 12345;
guf_wrapping_mul_int_fast32_t(42002718, 314159265, &mul_i32fast_res);
TEST_CHECK(mul_i32fast_res == -972735522);
mul_i32fast_res = 12345;
guf_wrapping_mul_int_fast32_t(-42002718, 314159265, &mul_i32fast_res);
TEST_CHECK(mul_i32fast_res == 972735522);
mul_i32fast_res = 12345;
guf_wrapping_mul_int_fast32_t(-88888888, 99999999, &mul_i32fast_res);
TEST_CHECK(mul_i32fast_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_i32fast_res = 12345;
guf_wrapping_mul_int_fast32_t(-2147483648, 2147483640, &mul_i32fast_res);
TEST_CHECK(mul_i32fast_res == 0);
mul_i32fast_res = 12345;
guf_wrapping_mul_int_fast32_t(-2048, -314159265, &mul_i32fast_res);
TEST_CHECK(mul_i32fast_res == -846919680);
mul_i32fast_res = 12345;
guf_wrapping_mul_int_fast32_t(4096, -314159265, &mul_i32fast_res);
TEST_CHECK(mul_i32fast_res == 1693839360);
ptrdiff_t ptrdiff_res = -1234; ptrdiff_t ptrdiff_res = -1234;
TEST_CHECK(guf_saturating_add_ptrdiff_t(PTRDIFF_MAX, 1, &ptrdiff_res) == GUF_MATH_CKD_OVERFLOW_POS); TEST_CHECK(guf_saturating_add_ptrdiff_t(PTRDIFF_MAX, 1, &ptrdiff_res) == GUF_MATH_CKD_OVERFLOW_POS);
TEST_CHECK(ptrdiff_res == PTRDIFF_MAX); TEST_CHECK(ptrdiff_res == PTRDIFF_MAX);
@ -241,6 +295,7 @@ void CkdIntTest::test_ckd_uint()
for (int32_t b = 0; b <= UINT8_MAX; ++b) { for (int32_t b = 0; b <= UINT8_MAX; ++b) {
const int32_t add_res = a + 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); 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));
if (add_res > UINT8_MAX) { if (add_res > UINT8_MAX) {
TEST_CHECK(ckd_add == GUF_MATH_CKD_OVERFLOW_POS); TEST_CHECK(ckd_add == GUF_MATH_CKD_OVERFLOW_POS);
uint8_t saturated; uint8_t saturated;
@ -262,6 +317,7 @@ void CkdIntTest::test_ckd_uint()
const int32_t sub_res = a - b; const int32_t sub_res = a - b;
const guf_math_ckd_result ckd_sub = guf_ckd_sub_u8((uint8_t)a, (uint8_t)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));
if (sub_res < 0) { if (sub_res < 0) {
TEST_CHECK(ckd_sub == GUF_MATH_CKD_OVERFLOW_NEG); TEST_CHECK(ckd_sub == GUF_MATH_CKD_OVERFLOW_NEG);
uint8_t saturated; uint8_t saturated;
@ -282,6 +338,7 @@ void CkdIntTest::test_ckd_uint()
const int32_t mul_res = a * b; const int32_t mul_res = a * b;
const guf_math_ckd_result ckd_mul = guf_ckd_mul_u8((uint8_t)a, (uint8_t)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));
if (mul_res > UINT8_MAX) { if (mul_res > UINT8_MAX) {
TEST_CHECK(ckd_mul == GUF_MATH_CKD_OVERFLOW_POS); TEST_CHECK(ckd_mul == GUF_MATH_CKD_OVERFLOW_POS);
uint8_t saturated; uint8_t saturated;

View File

@ -1,3 +1,6 @@
- make guf_utf8_char 4 bytes (non-null terminated)
- guf_stack, guf_queue, guf_dqueue, guf_prio_queue (using a heap), guf_ringbuf - guf_stack, guf_queue, guf_dqueue, guf_prio_queue (using a heap), guf_ringbuf
- sort: add cpp #ifdef to remove restrict from declaration - sort: add cpp #ifdef to remove restrict from declaration

View File

@ -12,7 +12,10 @@ class IntType:
INT_TYPE_ABBR: str INT_TYPE_ABBR: str
INT_MIN: str INT_MIN: str
INT_MAX: str INT_MAX: str
UINT_TYPE: str = "size_t" UINT_TYPE: str
UINT_MAX: str
is_optional: bool = False
@dataclass @dataclass
class UintType: class UintType:
@ -20,15 +23,17 @@ class UintType:
INT_TYPE_ABBR: str INT_TYPE_ABBR: str
INT_MIN: str INT_MIN: str
INT_MAX: str INT_MAX: str
is_optional: bool = False
def generate_ckdint_functions(int_types: list, uint_types: list) -> Tuple[str, str]: def generate_ckdint_functions(int_types: list, uint_types: list) -> Tuple[str, str]:
ckd_add_sub_uint_header = textwrap.dedent(""" ckd_add_sub_uint_header = textwrap.dedent("""\
GUF_MATH_CKDINT_KWRDS guf_math_ckd_result guf_ckd_add_{type_abbr}({type} a, {type} b); GUF_MATH_CKDINT_KWRDS guf_math_ckd_result guf_ckd_add_{type_abbr}({type} a, {type} b);
GUF_MATH_CKDINT_KWRDS guf_math_ckd_result guf_ckd_sub_{type_abbr}({type} a, {type} b); GUF_MATH_CKDINT_KWRDS guf_math_ckd_result guf_ckd_sub_{type_abbr}({type} a, {type} b);
GUF_MATH_CKDINT_KWRDS guf_math_ckd_result guf_ckd_mul_{type_abbr}({type} a, {type} b); GUF_MATH_CKDINT_KWRDS guf_math_ckd_result guf_ckd_mul_{type_abbr}({type} a, {type} b);
""") """)
ckd_add_sub_uint = textwrap.dedent("""
ckd_add_sub_uint = textwrap.dedent("""\
GUF_MATH_CKDINT_KWRDS guf_math_ckd_result guf_ckd_add_{type_abbr}({type} a, {type} b) GUF_MATH_CKDINT_KWRDS guf_math_ckd_result guf_ckd_add_{type_abbr}({type} a, {type} b)
{{ {{
if (b > 0 && a > {int_max} - b) {{ if (b > 0 && a > {int_max} - b) {{
@ -52,13 +57,43 @@ def generate_ckdint_functions(int_types: list, uint_types: list) -> Tuple[str, s
}} }}
""") """)
ckd_add_sub_uint_FAST = textwrap.dedent("""\
GUF_MATH_CKDINT_KWRDS guf_math_ckd_result guf_ckd_add_{type_abbr}({type} a, {type} b)
{{
a = GUF_UWRAP_{bits}(a);
b = GUF_UWRAP_{bits}(b);
if (b > 0 && a > {int_max} - b) {{
return GUF_MATH_CKD_OVERFLOW_POS;
}} else {{
return GUF_MATH_CKD_SUCCESS;
}}
}}
GUF_MATH_CKDINT_KWRDS guf_math_ckd_result guf_ckd_sub_{type_abbr}({type} a, {type} b)
{{
a = GUF_UWRAP_{bits}(a);
b = GUF_UWRAP_{bits}(b);
if (b > a) {{
return GUF_MATH_CKD_OVERFLOW_NEG;
}} else {{
return GUF_MATH_CKD_SUCCESS;
}}
}}
GUF_MATH_CKDINT_KWRDS guf_math_ckd_result guf_ckd_mul_{type_abbr}({type} a, {type} b)
{{
a = GUF_UWRAP_{bits}(a);
b = GUF_UWRAP_{bits}(b);
const {type} c = GUF_UWRAP_{bits}( 1u * a * b );
return a != 0 && ((1u * c / a) != b) ? GUF_MATH_CKD_OVERFLOW_POS : GUF_MATH_CKD_SUCCESS;
}}
""")
ckd_add_sub_int_header = textwrap.dedent(""" ckd_add_sub_int_header = textwrap.dedent("""\
GUF_MATH_CKDINT_KWRDS guf_math_ckd_result guf_ckd_add_{type_abbr}({type} a, {type} b); GUF_MATH_CKDINT_KWRDS guf_math_ckd_result guf_ckd_add_{type_abbr}({type} a, {type} b);
GUF_MATH_CKDINT_KWRDS guf_math_ckd_result guf_ckd_sub_{type_abbr}({type} a, {type} b); GUF_MATH_CKDINT_KWRDS guf_math_ckd_result guf_ckd_sub_{type_abbr}({type} a, {type} b);
GUF_MATH_CKDINT_KWRDS guf_math_ckd_result guf_ckd_mul_{type_abbr}({type} a, {type} b); GUF_MATH_CKDINT_KWRDS guf_math_ckd_result guf_ckd_mul_{type_abbr}({type} a, {type} b);
""") """)
ckd_add_sub_int = textwrap.dedent("""
ckd_add_sub_int = textwrap.dedent("""\
GUF_MATH_CKDINT_KWRDS guf_math_ckd_result guf_ckd_add_{type_abbr}({type} a, {type} b) GUF_MATH_CKDINT_KWRDS guf_math_ckd_result guf_ckd_add_{type_abbr}({type} a, {type} b)
{{ {{
if (b > 0 && a > {int_max} - b) {{ if (b > 0 && a > {int_max} - b) {{
@ -107,7 +142,7 @@ def generate_ckdint_functions(int_types: list, uint_types: list) -> Tuple[str, s
saturating_wrapping_int_header = textwrap.dedent(""" saturating_wrapping_int_header = textwrap.dedent("""\
GUF_MATH_CKDINT_KWRDS guf_math_ckd_result guf_saturating_add_{type_abbr}({type} a, {type} b, {type} *result); GUF_MATH_CKDINT_KWRDS guf_math_ckd_result guf_saturating_add_{type_abbr}({type} a, {type} b, {type} *result);
GUF_MATH_CKDINT_KWRDS guf_math_ckd_result guf_saturating_sub_{type_abbr}({type} a, {type} b, {type} *result); GUF_MATH_CKDINT_KWRDS guf_math_ckd_result guf_saturating_sub_{type_abbr}({type} a, {type} b, {type} *result);
GUF_MATH_CKDINT_KWRDS guf_math_ckd_result guf_saturating_mul_{type_abbr}({type} a, {type} b, {type} *result); GUF_MATH_CKDINT_KWRDS guf_math_ckd_result guf_saturating_mul_{type_abbr}({type} a, {type} b, {type} *result);
@ -116,7 +151,8 @@ def generate_ckdint_functions(int_types: list, uint_types: list) -> Tuple[str, s
GUF_MATH_CKDINT_KWRDS guf_math_ckd_result guf_wrapping_sub_{type_abbr}({type} a, {type} b, {type} *result); GUF_MATH_CKDINT_KWRDS guf_math_ckd_result guf_wrapping_sub_{type_abbr}({type} a, {type} b, {type} *result);
GUF_MATH_CKDINT_KWRDS guf_math_ckd_result guf_wrapping_mul_{type_abbr}({type} a, {type} b, {type} *result); GUF_MATH_CKDINT_KWRDS guf_math_ckd_result guf_wrapping_mul_{type_abbr}({type} a, {type} b, {type} *result);
""") """)
saturating_wrapping_int = textwrap.dedent("""
saturating_wrapping_int = textwrap.dedent("""\
GUF_MATH_CKDINT_KWRDS guf_math_ckd_result guf_saturating_add_{type_abbr}({type} a, {type} b, {type} *result) 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); const guf_math_ckd_result check = guf_ckd_add_{type_abbr}(a, b);
@ -232,8 +268,9 @@ def generate_ckdint_functions(int_types: list, uint_types: list) -> Tuple[str, s
case GUF_MATH_CKD_OVERFLOW_NEG: {{ case GUF_MATH_CKD_OVERFLOW_NEG: {{
{uint_type} res = 1u * ({uint_type})a * ({uint_type})b; {uint_type} res = 1u * ({uint_type})a * ({uint_type})b;
if (res > {int_max}) {{ // This is the fix for implementation defined conversion from unsigned to signed. if (res > {int_max}) {{ // This is the fix for implementation defined conversion from unsigned to signed.
const {uint_type} mod = (1u + ({uint_type}){int_max}); const {uint_type} mod = ({uint_type}){int_max} + 1u;
res = mod > 0 ? (1u * res % mod) : res; GUF_ASSERT(mod > 0);
res = 1u * res % mod;
*result = {int_min} + ({type})res; *result = {int_min} + ({type})res;
}} else {{ }} else {{
*result = ({type})res; *result = ({type})res;
@ -248,7 +285,8 @@ def generate_ckdint_functions(int_types: list, uint_types: list) -> Tuple[str, s
}} }}
""") """)
saturating_wrapping_uint_header = textwrap.dedent("""
saturating_wrapping_uint_header = textwrap.dedent("""\
GUF_MATH_CKDINT_KWRDS guf_math_ckd_result guf_saturating_add_{type_abbr}({type} a, {type} b, {type} *result); GUF_MATH_CKDINT_KWRDS guf_math_ckd_result guf_saturating_add_{type_abbr}({type} a, {type} b, {type} *result);
GUF_MATH_CKDINT_KWRDS guf_math_ckd_result guf_saturating_sub_{type_abbr}({type} a, {type} b, {type} *result); GUF_MATH_CKDINT_KWRDS guf_math_ckd_result guf_saturating_sub_{type_abbr}({type} a, {type} b, {type} *result);
GUF_MATH_CKDINT_KWRDS guf_math_ckd_result guf_saturating_mul_{type_abbr}({type} a, {type} b, {type} *result); GUF_MATH_CKDINT_KWRDS guf_math_ckd_result guf_saturating_mul_{type_abbr}({type} a, {type} b, {type} *result);
@ -258,7 +296,7 @@ def generate_ckdint_functions(int_types: list, uint_types: list) -> Tuple[str, s
GUF_MATH_CKDINT_KWRDS guf_math_ckd_result guf_wrapping_mul_{type_abbr}({type} a, {type} b, {type} *result); GUF_MATH_CKDINT_KWRDS guf_math_ckd_result guf_wrapping_mul_{type_abbr}({type} a, {type} b, {type} *result);
""") """)
saturating_wrapping_uint = textwrap.dedent(""" saturating_wrapping_uint = textwrap.dedent("""\
GUF_MATH_CKDINT_KWRDS guf_math_ckd_result guf_saturating_add_{type_abbr}({type} a, {type} b, {type} *result) 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); const guf_math_ckd_result check = guf_ckd_add_{type_abbr}(a, b);
@ -340,29 +378,190 @@ def generate_ckdint_functions(int_types: list, uint_types: list) -> Tuple[str, s
}} }}
""") """)
saturating_wrapping_uint_FAST = 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);
GUF_ASSERT(check == GUF_MATH_CKD_SUCCESS || check == GUF_MATH_CKD_OVERFLOW_POS);
if (result) {{
switch (check) {{
case GUF_MATH_CKD_SUCCESS:
*result = a + b;
break;
case GUF_MATH_CKD_OVERFLOW_POS:
*result = {int_max};
break;
default:
GUF_ASSERT(false);
}}
}}
return check;
}}
GUF_MATH_CKDINT_KWRDS guf_math_ckd_result guf_saturating_sub_{type_abbr}({type} a, {type} b, {type} *result)
{{
const guf_math_ckd_result check = guf_ckd_sub_{type_abbr}(a, b);
GUF_ASSERT(check == GUF_MATH_CKD_SUCCESS || check == GUF_MATH_CKD_OVERFLOW_NEG);
if (result) {{
switch (check) {{
case GUF_MATH_CKD_SUCCESS:
*result = a - b;
break;
case GUF_MATH_CKD_OVERFLOW_NEG:
*result = 0;
break;
default:
GUF_ASSERT(false);
}}
}}
return check;
}}
GUF_MATH_CKDINT_KWRDS guf_math_ckd_result guf_saturating_mul_{type_abbr}({type} a, {type} b, {type} *result)
{{
const guf_math_ckd_result check = guf_ckd_mul_{type_abbr}(a, b);
GUF_ASSERT(check == GUF_MATH_CKD_SUCCESS || check == GUF_MATH_CKD_OVERFLOW_POS);
if (result) {{
switch (check) {{
case GUF_MATH_CKD_SUCCESS:
*result = a * b;
break;
case GUF_MATH_CKD_OVERFLOW_POS:
*result = {int_max};
break;
default:
GUF_ASSERT(false);
}}
}}
return check;
}}
GUF_MATH_CKDINT_KWRDS guf_math_ckd_result guf_wrapping_add_{type_abbr}({type} a, {type} b, {type} *result)
{{
a = GUF_UWRAP_{bits}(a);
b = GUF_UWRAP_{bits}(b);
const guf_math_ckd_result check = guf_ckd_add_{type_abbr}(a, b);
if (result) {{
*result = GUF_UWRAP_{bits}( 1u * a + b );
}}
return check;
}}
GUF_MATH_CKDINT_KWRDS guf_math_ckd_result guf_wrapping_sub_{type_abbr}({type} a, {type} b, {type} *result)
{{
a = GUF_UWRAP_{bits}(a);
b = GUF_UWRAP_{bits}(b);
const guf_math_ckd_result check = guf_ckd_sub_{type_abbr}(a, b);
if (result) {{
*result = GUF_UWRAP_{bits}( 1u * a - b );
}}
return check;
}}
GUF_MATH_CKDINT_KWRDS guf_math_ckd_result guf_wrapping_mul_{type_abbr}({type} a, {type} b, {type} *result)
{{
a = GUF_UWRAP_{bits}(a);
b = GUF_UWRAP_{bits}(b);
const guf_math_ckd_result check = guf_ckd_mul_{type_abbr}(a, b);
if (result) {{
*result = GUF_UWRAP_{bits}( 1u * a * b );
}}
return check;
}}
""")
text_result = "// Signed integer arithmetic checks (generated with libguf/tools/ckdint-gen.py)\n" text_result = "// Signed integer arithmetic checks (generated with libguf/tools/ckdint-gen.py)\n"
text_result_header = text_result text_result_header = text_result
for type in int_types: for type in int_types:
text_result += ckd_add_sub_int.format(type = type.INT_TYPE, type_abbr = type.INT_TYPE_ABBR, int_min = type.INT_MIN, int_max = type.INT_MAX) end = "\n"
text_result_header += ckd_add_sub_int_header.format(type = type.INT_TYPE, type_abbr = type.INT_TYPE_ABBR, int_min = type.INT_MIN, int_max = type.INT_MAX) if type.is_optional:
text_result += f"#ifdef {type.INT_MAX}\n"
text_result_header += f"#ifdef {type.INT_MAX}\n"
end = ""
text_result += ckd_add_sub_int.format(type = type.INT_TYPE, type_abbr = type.INT_TYPE_ABBR, int_min = type.INT_MIN, int_max = type.INT_MAX, uint_max = type.UINT_MAX) + end
text_result_header += ckd_add_sub_int_header.format(type = type.INT_TYPE, type_abbr = type.INT_TYPE_ABBR, int_min = type.INT_MIN, int_max = type.INT_MAX, uint_max = type.UINT_MAX) + end
if type.is_optional:
text_result += "#endif\n\n"
text_result_header += "#endif\n\n"
text_result += "\n// Unsigned integer arithmetic checks (generated with libguf/tools/ckdint-gen.py) \n" text_result += "\n// Unsigned integer arithmetic checks (generated with libguf/tools/ckdint-gen.py) \n"
text_result_header += "\n// Unsigned integer arithmetic checks (generated with libguf/tools/ckdint-gen.py) \n" text_result_header += "\n// Unsigned integer arithmetic checks (generated with libguf/tools/ckdint-gen.py) \n"
for type in uint_types: for type in uint_types:
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 = "\n"
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) if type.is_optional:
text_result += f"#ifdef {type.INT_MAX}\n"
text_result_header += f"#ifdef {type.INT_MAX}\n"
end = ""
if "uint_fast" in type.INT_TYPE:
bits = 0
if "fast8" in type.INT_TYPE:
bits = 8
elif "fast16" in type.INT_TYPE:
bits = 16
elif "fast32" in type.INT_TYPE:
bits = 32
elif "fast64" 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))
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
if type.is_optional:
text_result += "#endif\n\n"
text_result_header += "#endif\n\n"
text_result += "\n\n// Signed saturating/wrapping arithmetic (generated with libguf/tools/ckdint-gen.py)\n" text_result += "\n\n// Signed saturating/wrapping arithmetic (generated with libguf/tools/ckdint-gen.py)\n"
text_result_header += "\n\n// Signed saturating/wrapping arithmetic (generated with libguf/tools/ckdint-gen.py)\n" text_result_header += "\n\n// Signed saturating/wrapping arithmetic (generated with libguf/tools/ckdint-gen.py)\n"
for type in int_types: for type in int_types:
text_result += saturating_wrapping_int.format(type = type.INT_TYPE, type_abbr = type.INT_TYPE_ABBR, int_min = type.INT_MIN, int_max = type.INT_MAX, uint_type = type.UINT_TYPE) + "\n" end = "\n"
text_result_header += saturating_wrapping_int_header.format(type = type.INT_TYPE, type_abbr = type.INT_TYPE_ABBR, int_min = type.INT_MIN, int_max = type.INT_MAX, uint_type = type.UINT_TYPE) + "\n" if type.is_optional:
text_result += f"#ifdef {type.INT_MAX}\n"
text_result_header += f"#ifdef {type.INT_MAX}\n"
end = ""
text_result += saturating_wrapping_int.format(type = type.INT_TYPE, type_abbr = type.INT_TYPE_ABBR, int_min = type.INT_MIN, int_max = type.INT_MAX, uint_type = type.UINT_TYPE, uint_max = type.UINT_MAX) + end
text_result_header += saturating_wrapping_int_header.format(type = type.INT_TYPE, type_abbr = type.INT_TYPE_ABBR, int_min = type.INT_MIN, int_max = type.INT_MAX, uint_type = type.UINT_TYPE, uint_max = type.UINT_MAX) + end
if type.is_optional:
text_result += "#endif\n\n"
text_result_header += "#endif\n\n"
text_result += "\n// Unsigned saturating/wrapping arithmetic (generated with libguf/tools/ckdint-gen.py)\n" text_result += "\n// Unsigned saturating/wrapping arithmetic (generated with libguf/tools/ckdint-gen.py)\n"
text_result_header += "\n// Unsigned saturating/wrapping arithmetic (generated with libguf/tools/ckdint-gen.py)\n" text_result_header += "\n// Unsigned saturating/wrapping arithmetic (generated with libguf/tools/ckdint-gen.py)\n"
for type in uint_types: for type in uint_types:
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) + "\n" end = "\n"
text_result_header += saturating_wrapping_uint_header.format(type = type.INT_TYPE, type_abbr = type.INT_TYPE_ABBR, int_min = type.INT_MIN, int_max = type.INT_MAX) + "\n" if type.is_optional:
text_result += f"#ifdef {type.INT_MAX}\n"
text_result_header += f"#ifdef {type.INT_MAX}\n"
end = ""
if "uint_fast" in type.INT_TYPE:
bits = 0
if "fast8" in type.INT_TYPE:
bits = 8
elif "fast16" in type.INT_TYPE:
bits = 16
elif "fast32" in type.INT_TYPE:
bits = 32
elif "fast64" 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
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
text_result_header += saturating_wrapping_uint_header.format(type = type.INT_TYPE, type_abbr = type.INT_TYPE_ABBR, int_min = type.INT_MIN, int_max = type.INT_MAX) + end
if type.is_optional:
text_result += "#endif\n\n"
text_result_header += "#endif\n\n"
return (text_result_header, text_result) return (text_result_header, text_result)
@ -371,28 +570,41 @@ def generate_ckdint_functions(int_types: list, uint_types: list) -> Tuple[str, s
if __name__ == "__main__": if __name__ == "__main__":
int_types = [ int_types = [
IntType(INT_TYPE = "int", INT_TYPE_ABBR = "int", INT_MIN = "INT_MIN", INT_MAX = "INT_MAX", UINT_TYPE = "unsigned"), IntType(INT_TYPE = "int", INT_TYPE_ABBR = "int", INT_MIN = "INT_MIN", INT_MAX = "INT_MAX", UINT_TYPE = "unsigned", UINT_MAX = "UINT_MAX"),
IntType(INT_TYPE = "long", INT_TYPE_ABBR = "long", INT_MIN = "LONG_MIN", INT_MAX = "LONG_MAX", UINT_TYPE = "unsigned long"), IntType(INT_TYPE = "long", INT_TYPE_ABBR = "long", INT_MIN = "LONG_MIN", INT_MAX = "LONG_MAX", UINT_TYPE = "unsigned long", UINT_MAX = "ULONG_MAX"),
IntType(INT_TYPE = "long long", INT_TYPE_ABBR = "long_long", INT_MIN = "LLONG_MIN", INT_MAX = "LLONG_MAX", UINT_TYPE = "unsigned long long"), IntType(INT_TYPE = "long long", INT_TYPE_ABBR = "long_long", INT_MIN = "LLONG_MIN", INT_MAX = "LLONG_MAX", UINT_TYPE = "unsigned long long", UINT_MAX = "ULLONG_MAX"),
IntType(INT_TYPE = "int8_t", INT_TYPE_ABBR = "i8", INT_MIN = "INT8_MIN", INT_MAX = "INT8_MAX", UINT_TYPE = "uint8_t"), # TODO: size_t is not necessarily the unsigned ptrdiff_t equivalent
IntType(INT_TYPE = "int16_t", INT_TYPE_ABBR = "i16", INT_MIN = "INT16_MIN", INT_MAX = "INT16_MAX", UINT_TYPE = "uint16_t"), 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 = "int32_t", INT_TYPE_ABBR = "i32", INT_MIN = "INT32_MIN", INT_MAX = "INT32_MAX", UINT_TYPE = "uint32_t"),
IntType(INT_TYPE = "int64_t", INT_TYPE_ABBR = "i64", INT_MIN = "INT64_MIN", INT_MAX = "INT64_MAX", UINT_TYPE = "uint64_t"), 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 = "ptrdiff_t", INT_TYPE_ABBR = "ptrdiff_t", INT_MIN = "PTRDIFF_MIN", INT_MAX = "PTRDIFF_MAX", UINT_TYPE = "size_t"), # TODO: size_t is not necessarily the unsigned ptrdiff_t equivalent 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 = "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),
IntType(INT_TYPE = "int32_t", INT_TYPE_ABBR = "i32", INT_MIN = "INT32_MIN", INT_MAX = "INT32_MAX", UINT_TYPE = "uint32_t", UINT_MAX = "GUF_UINT32_MAX", is_optional = True),
IntType(INT_TYPE = "int64_t", INT_TYPE_ABBR = "i64", INT_MIN = "INT64_MIN", INT_MAX = "INT64_MAX", UINT_TYPE = "uint64_t", UINT_MAX = "GUF_UINT64_MAX", is_optional = True),
] ]
uint_types = [ uint_types = [
UintType(INT_TYPE = "unsigned char", INT_TYPE_ABBR = "uchar", INT_MIN = "0", INT_MAX = "UCHAR_MAX"), UintType(INT_TYPE = "unsigned char", INT_TYPE_ABBR = "uchar", INT_MIN = "0", INT_MAX = "UCHAR_MAX"),
UintType(INT_TYPE = "unsigned", INT_TYPE_ABBR = "unsigned", INT_MIN = "0", INT_MAX = "UINT_MAX"), UintType(INT_TYPE = "unsigned", INT_TYPE_ABBR = "unsigned", INT_MIN = "0", INT_MAX = "UINT_MAX"),
UintType(INT_TYPE = "unsigned long", INT_TYPE_ABBR = "ulong", INT_MIN = "ULONG_MIN", INT_MAX = "ULONG_MAX"), UintType(INT_TYPE = "unsigned long", INT_TYPE_ABBR = "ulong", INT_MIN = "0", INT_MAX = "ULONG_MAX"),
UintType(INT_TYPE = "unsigned long long", INT_TYPE_ABBR = "ulong_long", INT_MIN = "ULLONG_MIN", INT_MAX = "ULLONG_MAX"), UintType(INT_TYPE = "unsigned long long", INT_TYPE_ABBR = "ulong_long", INT_MIN = "0", INT_MAX = "ULLONG_MAX"),
UintType(INT_TYPE = "uint8_t", INT_TYPE_ABBR = "u8", INT_MIN = "0", INT_MAX = "UINT8_MAX"),
UintType(INT_TYPE = "uint16_t", INT_TYPE_ABBR = "u16", INT_MIN = "0", INT_MAX = "UINT16_MAX"),
UintType(INT_TYPE = "uint32_t", INT_TYPE_ABBR = "u32", INT_MIN = "0", INT_MAX = "UINT32_MAX"),
UintType(INT_TYPE = "uint64_t", INT_TYPE_ABBR = "u64", INT_MIN = "0", INT_MAX = "UINT64_MAX"),
UintType(INT_TYPE = "size_t", INT_TYPE_ABBR = "size_t", INT_MIN = "0", INT_MAX = "SIZE_MAX"), 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 = "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),
UintType(INT_TYPE = "uint32_t", INT_TYPE_ABBR = "u32", INT_MIN = "0", INT_MAX = "UINT32_MAX", is_optional = True),
UintType(INT_TYPE = "uint64_t", INT_TYPE_ABBR = "u64", INT_MIN = "0", INT_MAX = "UINT64_MAX", is_optional = True),
] ]
code_header, code_impl = generate_ckdint_functions(int_types = int_types, uint_types= uint_types) code_header, code_impl = generate_ckdint_functions(int_types = int_types, uint_types= uint_types)

View File

@ -11,6 +11,7 @@ class IntType:
INT_TYPE_ABBR: str INT_TYPE_ABBR: str
INT_MIN: str INT_MIN: str
INT_MAX: str INT_MAX: str
is_optional: bool = False
@dataclass @dataclass
class UintType: class UintType:
@ -18,10 +19,11 @@ class UintType:
INT_TYPE_ABBR: str INT_TYPE_ABBR: str
INT_MIN: str INT_MIN: str
INT_MAX: str INT_MAX: str
is_optional: bool = False
def gen_min_max_clamp(int_types: list, uint_types: list) -> str: def gen_min_max_clamp(int_types: list, uint_types: list) -> str:
template = textwrap.dedent(""" template = textwrap.dedent("""\
static inline {type} guf_min_{type_abbr}({type} a, {type} b) {{ return a < b ? a : b; }} static inline {type} guf_min_{type_abbr}({type} a, {type} b) {{ return a < b ? a : b; }}
static inline {type} guf_max_{type_abbr}({type} a, {type} b) {{ return a > b ? a : b; }} static inline {type} guf_max_{type_abbr}({type} a, {type} b) {{ return a > b ? a : b; }}
static inline {type} guf_clamp_{type_abbr}({type} x, {type} min, {type} max) {{ if (x < min) {{return min;}} if (x > max) {{return max;}} return x; }} static inline {type} guf_clamp_{type_abbr}({type} x, {type} min, {type} max) {{ if (x < min) {{return min;}} if (x > max) {{return max;}} return x; }}
@ -29,12 +31,21 @@ def gen_min_max_clamp(int_types: list, uint_types: list) -> str:
result = "\n// Signed min, max, clamp functions (generated with libguf/tools/min_max_clamp-gen.py)\n" result = "\n// Signed min, max, clamp functions (generated with libguf/tools/min_max_clamp-gen.py)\n"
for t in int_types: for t in int_types:
if t.is_optional:
result += "#ifdef {int_max}\n".format(int_max = t.INT_MAX)
result += template.format(type = t.INT_TYPE, type_abbr = t.INT_TYPE_ABBR) result += template.format(type = t.INT_TYPE, type_abbr = t.INT_TYPE_ABBR)
if t.is_optional:
result += "#endif\n"
result += "\n"
result += "\n// Unsigned min, max, clamp functions (generated with libguf/tools/min_max_clamp-gen.py)\n" result += "\n// Unsigned min, max, clamp functions (generated with libguf/tools/min_max_clamp-gen.py)\n"
for t in uint_types: for t in uint_types:
if t.is_optional:
result +="#ifdef {int_max}\n".format(int_max = t.INT_MAX)
result += template.format(type = t.INT_TYPE, type_abbr = t.INT_TYPE_ABBR) result += template.format(type = t.INT_TYPE, type_abbr = t.INT_TYPE_ABBR)
if t.is_optional:
result += "#endif\n"
result += "\n"
return result return result
@ -45,15 +56,23 @@ if __name__ == "__main__":
IntType(INT_TYPE = "int", INT_TYPE_ABBR = "int", INT_MIN = "INT_MIN", INT_MAX = "INT_MAX"), IntType(INT_TYPE = "int", INT_TYPE_ABBR = "int", INT_MIN = "INT_MIN", INT_MAX = "INT_MAX"),
IntType(INT_TYPE = "long", INT_TYPE_ABBR = "long", INT_MIN = "LONG_MIN", INT_MAX = "LONG_MAX"), IntType(INT_TYPE = "long", INT_TYPE_ABBR = "long", INT_MIN = "LONG_MIN", INT_MAX = "LONG_MAX"),
IntType(INT_TYPE = "long long", INT_TYPE_ABBR = "long_long", INT_MIN = "LLONG_MIN", INT_MAX = "LLONG_MAX"), IntType(INT_TYPE = "long long", INT_TYPE_ABBR = "long_long", INT_MIN = "LLONG_MIN", INT_MAX = "LLONG_MAX"),
IntType(INT_TYPE = "int8_t", INT_TYPE_ABBR = "i8", INT_MIN = "INT8_MIN", INT_MAX = "INT8_MAX"),
IntType(INT_TYPE = "int16_t", INT_TYPE_ABBR = "i16", INT_MIN = "INT16_MIN", INT_MAX = "INT16_MAX"),
IntType(INT_TYPE = "int32_t", INT_TYPE_ABBR = "i32", INT_MIN = "INT32_MIN", INT_MAX = "INT32_MAX"),
IntType(INT_TYPE = "int64_t", INT_TYPE_ABBR = "i64", INT_MIN = "INT64_MIN", INT_MAX = "INT64_MAX"),
IntType(INT_TYPE = "ptrdiff_t", INT_TYPE_ABBR = "ptrdiff_t", INT_MIN = "PTRDIFF_MIN", INT_MAX = "PTRDIFF_MAX"), IntType(INT_TYPE = "ptrdiff_t", INT_TYPE_ABBR = "ptrdiff_t", INT_MIN = "PTRDIFF_MIN", INT_MAX = "PTRDIFF_MAX"),
IntType(INT_TYPE = "int_fast8_t", INT_TYPE_ABBR = "i8_fast", INT_MIN = "GUF_INT8_MIN", INT_MAX = "GUF_INT8_MAX"),
IntType(INT_TYPE = "int_fast16_t", INT_TYPE_ABBR = "i16_fast", INT_MIN = "GUF_INT16_MIN", INT_MAX = "GUF_INT16_MAX"),
IntType(INT_TYPE = "int_fast32_t", INT_TYPE_ABBR = "i32_fast", INT_MIN = "GUF_INT32_MIN", INT_MAX = "GUF_INT32_MAX"),
IntType(INT_TYPE = "int_fast64_t", INT_TYPE_ABBR = "i64_fast", INT_MIN = "GUF_INT64_MIN", INT_MAX = "GUF_INT64_MAX"),
IntType(INT_TYPE = "float", INT_TYPE_ABBR = "f32", INT_MIN = "-FLT_MAX", INT_MAX = "FLT_MAX"), IntType(INT_TYPE = "float", INT_TYPE_ABBR = "f32", INT_MIN = "-FLT_MAX", INT_MAX = "FLT_MAX"),
IntType(INT_TYPE = "double", INT_TYPE_ABBR = "f64", INT_MIN = "-DBL_MAX", INT_MAX = "DBL_MAX"), IntType(INT_TYPE = "double", INT_TYPE_ABBR = "f64", INT_MIN = "-DBL_MAX", INT_MAX = "DBL_MAX"),
IntType(INT_TYPE = "int8_t", INT_TYPE_ABBR = "i8", INT_MIN = "INT8_MIN", INT_MAX = "INT8_MAX", is_optional=True),
IntType(INT_TYPE = "int16_t", INT_TYPE_ABBR = "i16", INT_MIN = "INT16_MIN", INT_MAX = "INT16_MAX", is_optional=True),
IntType(INT_TYPE = "int32_t", INT_TYPE_ABBR = "i32", INT_MIN = "INT32_MIN", INT_MAX = "INT32_MAX", is_optional=True),
IntType(INT_TYPE = "int64_t", INT_TYPE_ABBR = "i64", INT_MIN = "INT64_MIN", INT_MAX = "INT64_MAX", is_optional=True),
] ]
uint_types = [ uint_types = [
@ -62,11 +81,17 @@ if __name__ == "__main__":
UintType(INT_TYPE = "unsigned long", INT_TYPE_ABBR = "ulong", INT_MIN = "0", INT_MAX = "ULONG_MAX"), UintType(INT_TYPE = "unsigned long", INT_TYPE_ABBR = "ulong", INT_MIN = "0", INT_MAX = "ULONG_MAX"),
UintType(INT_TYPE = "unsigned long long", INT_TYPE_ABBR = "ulong_long", INT_MIN = "0", INT_MAX = "ULLONG_MAX"), UintType(INT_TYPE = "unsigned long long", INT_TYPE_ABBR = "ulong_long", INT_MIN = "0", INT_MAX = "ULLONG_MAX"),
UintType(INT_TYPE = "uint8_t", INT_TYPE_ABBR = "u8", INT_MIN = "0", INT_MAX = "UINT8_MAX"),
UintType(INT_TYPE = "uint16_t", INT_TYPE_ABBR = "u16", INT_MIN = "0", INT_MAX = "UINT16_MAX"),
UintType(INT_TYPE = "uint32_t", INT_TYPE_ABBR = "u32", INT_MIN = "0", INT_MAX = "UINT32_MAX"),
UintType(INT_TYPE = "uint64_t", INT_TYPE_ABBR = "u64", INT_MIN = "0", INT_MAX = "UINT64_MAX"),
UintType(INT_TYPE = "size_t", INT_TYPE_ABBR = "size_t", INT_MIN = "0", INT_MAX = "SIZE_MAX"), 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 = "u8_fast", INT_MIN = "0", INT_MAX = "GUF_UINT8_MAX"),
UintType(INT_TYPE = "uint_fast16_t", INT_TYPE_ABBR = "u16_fast", INT_MIN = "0", INT_MAX = "GUF_UINT16_MAX"),
UintType(INT_TYPE = "uint_fast32_t", INT_TYPE_ABBR = "u32_fast", INT_MIN = "0", INT_MAX = "GUF_UINT32_MAX"),
UintType(INT_TYPE = "uint_fast64_t", INT_TYPE_ABBR = "u64_fast", 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),
UintType(INT_TYPE = "uint32_t", INT_TYPE_ABBR = "u32", INT_MIN = "0", INT_MAX = "UINT32_MAX", is_optional=True),
UintType(INT_TYPE = "uint64_t", INT_TYPE_ABBR = "u64", INT_MIN = "0", INT_MAX = "UINT64_MAX", is_optional=True),
] ]