aboutsummaryrefslogtreecommitdiff
path: root/src/stdlib/integers.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/stdlib/integers.c')
-rw-r--r--src/stdlib/integers.c14
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); \
} \