rcx

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

math.h (1108B)


      1 #pragma once
      2 
      3 #include <math.h>
      4 
      5 #include "def.h"
      6 
      7 
      8 /* ----- Clamp ----- */
      9 
     10 #define CLAMP(T, t) \
     11 	static inline T r_clamp##t(T x, T min, T max) { \
     12 		return x < min ? min : x > max ? max : x; \
     13 	}
     14 
     15 CLAMP(char, c)
     16 CLAMP(schar, sc)
     17 CLAMP(uchar, uc)
     18 CLAMP(short, s)
     19 CLAMP(ushort, us)
     20 CLAMP(int, i)
     21 CLAMP(uint, ui)
     22 CLAMP(long, l)
     23 CLAMP(ulong, ul)
     24 CLAMP(llong, ll)
     25 CLAMP(ullong, ull)
     26 CLAMP(i8, i8)
     27 CLAMP(u8, u8)
     28 CLAMP(i16, i16)
     29 CLAMP(u16, u16)
     30 CLAMP(i32, i32)
     31 CLAMP(u32, u32)
     32 CLAMP(i64, i64)
     33 CLAMP(u64, u64)
     34 CLAMP(imax, imax)
     35 CLAMP(umax, umax)
     36 CLAMP(iptr, iptr)
     37 CLAMP(uptr, uptr)
     38 CLAMP(isize, isize)
     39 CLAMP(usize, usize)
     40 CLAMP(f32, f32)
     41 CLAMP(f64, f64)
     42 
     43 #undef CLAMP
     44 
     45 
     46 /* ----- Square root ----- */
     47 
     48 #define r_sqrtf32 sqrtf
     49 #define r_sqrtf64 sqrt
     50 
     51 
     52 /* ----- Sign ----- */
     53 
     54 #define SIGN(T, t) \
     55 	static inline T r_sign##t(T x) { \
     56 		return (x > (T)0) - (x < (T)0); \
     57 	}
     58 
     59 SIGN(char, c)
     60 SIGN(schar, sc)
     61 SIGN(short, s)
     62 SIGN(int, i)
     63 SIGN(long, l)
     64 SIGN(llong, ll)
     65 SIGN(i8, i8)
     66 SIGN(i16, i16)
     67 SIGN(i32, i32)
     68 SIGN(i64, i64)
     69 SIGN(imax, imax)
     70 SIGN(iptr, iptr)
     71 SIGN(isize, isize)
     72 SIGN(f32, f32)
     73 SIGN(f64, f64)
     74 
     75 #undef SIGN