Move clamped() and is_between() to proper functions (not just macros)
This commit is contained in:
parent
6c3e2cdf12
commit
82f5f05bb9
@ -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);
|
||||
|
@ -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;
|
||||
|
@ -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); \
|
||||
} \
|
||||
|
@ -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.
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user