commit 02abd49d7535a673726beb862cddb714fa26c831
parent 49c7c893a72407de2f4bfd3a88f2bbd2dd357335
Author: Robert Russell <robert@rr3.xyz>
Date: Sat, 26 Oct 2024 12:06:35 -0700
Create new math module, for now with clamp functions
In the future we can add abs, min, max, aliases for std math.h FP
operations (trig, etc.), and more.
We already have MIN and MAX in def.h, but those aren't typed.
Diffstat:
2 files changed, 54 insertions(+), 0 deletions(-)
diff --git a/inc/all.h b/inc/all.h
@@ -7,6 +7,7 @@
#include "deque.h"
#include "error.h"
#include "log.h"
+#include "math.h"
#include "rand.h"
#include "rcx.h"
#include "str.h"
diff --git a/inc/math.h b/inc/math.h
@@ -0,0 +1,53 @@
+#pragma once
+
+#include "def.h"
+
+
+/* ----- Clamp ----- */
+
+#define CLAMP(T, x) \
+ static inline T r_clamp##x(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)
+
+#undef CLAMP