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 /src/stdlib/integers.c | |
| parent | 6c3e2cdf12a09e0cfceb2032140ac7dfa222620b (diff) | |
Move clamped() and is_between() to proper functions (not just macros)
Diffstat (limited to 'src/stdlib/integers.c')
| -rw-r--r-- | src/stdlib/integers.c | 14 |
1 files changed, 14 insertions, 0 deletions
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); \ } \ |
