rcx

miscellaneous C library
git clone git://git.rr3.xyz/rcx
Log | Files | Refs | README | LICENSE

commit 835af528d383f35a80405c62faf8923245ba8506
parent 620b4405b4056905e10f3e628549a2d797f29c8e
Author: Robert Russell <robert@rr3.xyz>
Date:   Sat, 26 Oct 2024 16:28:36 -0700

Add the sign operation to the math module

Diffstat:
Minc/math.h | 42++++++++++++++++++++++++++++--------------
1 file changed, 28 insertions(+), 14 deletions(-)

diff --git a/inc/math.h b/inc/math.h @@ -7,48 +7,36 @@ /* ----- Clamp ----- */ -#define CLAMP(T, x) \ - static inline T r_clamp##x(T x, T min, T max) { \ +#define CLAMP(T, t) \ + static inline T r_clamp##t(T x, T min, T max) { \ return x < min ? min : x > max ? max : x; \ } CLAMP(char, c) CLAMP(schar, sc) CLAMP(uchar, uc) - CLAMP(short, s) CLAMP(ushort, us) - CLAMP(int, i) CLAMP(uint, ui) - CLAMP(long, l) CLAMP(ulong, ul) - CLAMP(llong, ll) CLAMP(ullong, ull) - CLAMP(i8, i8) CLAMP(u8, u8) - CLAMP(i16, i16) CLAMP(u16, u16) - CLAMP(i32, i32) CLAMP(u32, u32) - CLAMP(i64, i64) CLAMP(u64, u64) - CLAMP(imax, imax) CLAMP(umax, umax) - CLAMP(iptr, iptr) CLAMP(uptr, uptr) - CLAMP(isize, isize) CLAMP(usize, usize) - CLAMP(f32, f32) CLAMP(f64, f64) @@ -59,3 +47,29 @@ CLAMP(f64, f64) #define r_sqrtf32 sqrtf #define r_sqrtf64 sqrt + + +/* ----- Sign ----- */ + +#define SIGN(T, t) \ + static inline T r_sign##t(T x) { \ + return (x > (T)0) - (x < (T)0); \ + } + +SIGN(char, c) +SIGN(schar, sc) +SIGN(short, s) +SIGN(int, i) +SIGN(long, l) +SIGN(llong, ll) +SIGN(i8, i8) +SIGN(i16, i16) +SIGN(i32, i32) +SIGN(i64, i64) +SIGN(imax, imax) +SIGN(iptr, iptr) +SIGN(isize, isize) +SIGN(f32, f32) +SIGN(f64, f64) + +#undef SIGN