diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2025-04-07 03:21:58 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2025-04-07 03:21:58 -0400 |
| commit | 82f5f05bb9440b758ae5e4a7c168e36ecff06db5 (patch) | |
| tree | e9f997f469a1c26016ff2b58c716bfb643930272 | |
| parent | 6c3e2cdf12a09e0cfceb2032140ac7dfa222620b (diff) | |
Move clamped() and is_between() to proper functions (not just macros)
| -rw-r--r-- | src/stdlib/bytes.c | 4 | ||||
| -rw-r--r-- | src/stdlib/bytes.h | 4 | ||||
| -rw-r--r-- | src/stdlib/integers.c | 14 | ||||
| -rw-r--r-- | src/stdlib/integers.h | 17 | ||||
| -rw-r--r-- | src/stdlib/nums.c | 15 | ||||
| -rw-r--r-- | src/stdlib/nums.h | 16 |
6 files changed, 42 insertions, 28 deletions
diff --git a/src/stdlib/bytes.c b/src/stdlib/bytes.c index 1203aa55..b543c7c6 100644 --- a/src/stdlib/bytes.c +++ b/src/stdlib/bytes.c @@ -16,6 +16,10 @@ PUREFUNC public Text_t Byte$as_text(const void *b, bool colorize, const TypeInfo return Text$format(colorize ? "\x1b[35m0x%02X\x1b[m" : "0x%02X", *(Byte_t*)b); } +public CONSTFUNC bool Byte$is_between(const Byte_t x, const Byte_t low, const Byte_t high) { + return low <= x && x <= high; +} + public Text_t Byte$hex(Byte_t byte, bool uppercase, bool prefix) { struct Text_s text = {.tag=TEXT_ASCII}; text.ascii = GC_MALLOC_ATOMIC(8); diff --git a/src/stdlib/bytes.h b/src/stdlib/bytes.h index 2794d1d9..5c64687d 100644 --- a/src/stdlib/bytes.h +++ b/src/stdlib/bytes.h @@ -22,9 +22,7 @@ Byte_t Byte$from_int16(int16_t i, bool truncate); Closure_t Byte$to(Byte_t first, Byte_t last, OptionalInt8_t step); MACROLIKE Byte_t Byte$from_int8(int8_t i) { return (Byte_t)i; } MACROLIKE Byte_t Byte$from_bool(bool b) { return (Byte_t)b; } -MACROLIKE CONSTFUNC bool Byte$is_between(const Byte_t x, const Byte_t low, const Byte_t high) { - return low <= x && x <= high; -} +CONSTFUNC bool Byte$is_between(const Byte_t x, const Byte_t low, const Byte_t high); extern const Byte_t Byte$min; extern const Byte_t Byte$max; diff --git a/src/stdlib/integers.c b/src/stdlib/integers.c index 7943caee..8fff9bd6 100644 --- a/src/stdlib/integers.c +++ b/src/stdlib/integers.c @@ -73,6 +73,14 @@ public PUREFUNC bool Int$equal(const void *x, const void *y, const TypeInfo_t*) return Int$equal_value(*(Int_t*)x, *(Int_t*)y); } +public CONSTFUNC Int_t Int$clamped(Int_t x, Int_t low, Int_t high) { + return (Int$compare(&x, &low, &Int$info) <= 0) ? low : (Int$compare(&x, &high, &Int$info) >= 0 ? high : x); +} + +public CONSTFUNC bool Int$is_between(const Int_t x, const Int_t low, const Int_t high) { + return Int$compare_value(low, x) <= 0 && Int$compare_value(x, high) <= 0; +} + public PUREFUNC uint64_t Int$hash(const void *vx, const TypeInfo_t*) { Int_t *x = (Int_t*)vx; if (likely(x->small & 1L)) { @@ -569,6 +577,12 @@ public void Int32$deserialize(FILE *in, void *outval, List_t*, const TypeInfo_t* public PUREFUNC bool KindOfInt ## $equal(const void *x, const void *y, const TypeInfo_t*) { \ return *(c_type*)x == *(c_type*)y; \ } \ + public CONSTFUNC bool KindOfInt ## $is_between(const c_type x, const c_type low, const c_type high) { \ + return low <= x && x <= high; \ + } \ + public CONSTFUNC c_type KindOfInt ## $clamped(c_type x, c_type min, c_type max) { \ + return x < min ? min : (x > max ? max : x); \ + } \ public Text_t KindOfInt ## $format(c_type i, Int_t digits_int) { \ return Text$format("%0*ld", Int32$from_int(digits_int, false), (int64_t)i); \ } \ diff --git a/src/stdlib/integers.h b/src/stdlib/integers.h index 12fec62b..c7643a8d 100644 --- a/src/stdlib/integers.h +++ b/src/stdlib/integers.h @@ -33,12 +33,8 @@ Closure_t type_name ## $to(c_type first, c_type last, Optional ## type_name ## _t step); \ Closure_t type_name ## $onward(c_type first, c_type step); \ PUREFUNC Optional ## type_name ## _t type_name ## $parse(Text_t text); \ - MACROLIKE CONSTFUNC bool type_name ## $is_between(const c_type x, const c_type low, const c_type high) { \ - return low <= x && x <= high; \ - } \ - MACROLIKE PUREFUNC c_type type_name ## $clamped(c_type x, c_type min, c_type max) { \ - return x < min ? min : (x > max ? max : x); \ - } \ + CONSTFUNC bool type_name ## $is_between(const c_type x, const c_type low, const c_type high); \ + CONSTFUNC c_type type_name ## $clamped(c_type x, c_type min, c_type max); \ MACROLIKE CONSTFUNC c_type type_name ## $from_byte(Byte_t b) { return (c_type)b; } \ MACROLIKE CONSTFUNC c_type type_name ## $from_bool(Bool_t b) { return (c_type)b; } \ CONSTFUNC c_type type_name ## $gcd(c_type x, c_type y); \ @@ -96,9 +92,8 @@ Text_t Int$value_as_text(Int_t i); PUREFUNC uint64_t Int$hash(const void *x, const TypeInfo_t *type); PUREFUNC int32_t Int$compare(const void *x, const void *y, const TypeInfo_t *type); PUREFUNC int32_t Int$compare_value(const Int_t x, const Int_t y); -MACROLIKE CONSTFUNC bool Int$is_between(const Int_t x, const Int_t low, const Int_t high) { - return Int$compare_value(low, x) <= 0 && Int$compare_value(x, high) <= 0; -} +CONSTFUNC bool Int$is_between(const Int_t x, const Int_t low, const Int_t high); +CONSTFUNC Int_t Int$clamped(Int_t x, Int_t low, Int_t high); PUREFUNC bool Int$equal(const void *x, const void *y, const TypeInfo_t *type); PUREFUNC bool Int$equal_value(const Int_t x, const Int_t y); Text_t Int$format(Int_t i, Int_t digits); @@ -157,10 +152,6 @@ Int_t Int$factorial(Int_t n); extern const TypeInfo_t Int$info; -MACROLIKE PUREFUNC Int_t Int$clamped(Int_t x, Int_t low, Int_t high) { - return (Int$compare(&x, &low, &Int$info) <= 0) ? low : (Int$compare(&x, &high, &Int$info) >= 0 ? high : x); -} - // Fast-path inline versions for the common case where integer arithmetic is // between two small ints. diff --git a/src/stdlib/nums.c b/src/stdlib/nums.c index 9edb8751..ed1c64be 100644 --- a/src/stdlib/nums.c +++ b/src/stdlib/nums.c @@ -76,6 +76,13 @@ public CONSTFUNC double Num$mix(double amount, double x, double y) { return (1.0-amount)*x + amount*y; } +public CONSTFUNC bool Num$is_between(const double x, const double low, const double high) { + return low <= x && x <= high; +} +public CONSTFUNC double Num$clamped(double x, double low, double high) { + return (x <= low) ? low : (x >= high ? high : x); +} + public OptionalNum_t Num$parse(Text_t text) { const char *str = Text$as_c_string(text); char *end = NULL; @@ -161,6 +168,14 @@ public CONSTFUNC float Num32$mix(float amount, float x, float y) { return (1.0f-amount)*x + amount*y; } +public CONSTFUNC bool Num32$is_between(const float x, const float low, const float high) { + return low <= x && x <= high; +} + +public CONSTFUNC float Num32$clamped(float x, float low, float high) { + return (x <= low) ? low : (x >= high ? high : x); +} + public OptionalNum32_t Num32$parse(Text_t text) { const char *str = Text$as_c_string(text); char *end = NULL; diff --git a/src/stdlib/nums.h b/src/stdlib/nums.h index 84d87dd5..5871c904 100644 --- a/src/stdlib/nums.h +++ b/src/stdlib/nums.h @@ -32,12 +32,8 @@ CONSTFUNC bool Num$isnan(double n); double Num$nan(Text_t tag); CONSTFUNC double Num$mix(double amount, double x, double y); OptionalNum_t Num$parse(Text_t text); -MACROLIKE CONSTFUNC bool Num$is_between(const double x, const double low, const double high) { - return low <= x && x <= high; -} -MACROLIKE CONSTFUNC double Num$clamped(double x, double low, double high) { - return (x <= low) ? low : (x >= high ? high : x); -} +CONSTFUNC bool Num$is_between(const double x, const double low, const double high); +CONSTFUNC double Num$clamped(double x, double low, double high); MACROLIKE CONSTFUNC double Num$from_num32(Num32_t n) { return (double)n; } #ifdef __GNUC__ #pragma GCC diagnostic push @@ -91,12 +87,8 @@ CONSTFUNC bool Num32$isnan(float n); CONSTFUNC float Num32$mix(float amount, float x, float y); OptionalNum32_t Num32$parse(Text_t text); float Num32$nan(Text_t tag); -MACROLIKE CONSTFUNC bool Num32$is_between(const float x, const float low, const float high) { - return low <= x && x <= high; -} -MACROLIKE CONSTFUNC float Num32$clamped(float x, float low, float high) { - return (x <= low) ? low : (x >= high ? high : x); -} +CONSTFUNC bool Num32$is_between(const float x, const float low, const float high); +CONSTFUNC float Num32$clamped(float x, float low, float high); MACROLIKE CONSTFUNC float Num32$from_num(Num_t n) { return (float)n; } #ifdef __GNUC__ #pragma GCC diagnostic push |
