diff --git a/src/engine/math_util.c b/src/engine/math_util.c index 65a62c044..c85ac1567 100644 --- a/src/engine/math_util.c +++ b/src/engine/math_util.c @@ -12,47 +12,6 @@ #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wreturn-local-addr" -#if defined(__clang__) || defined(__GNUC__) - -// Use built-in functions when using Clang or GCC -inline f32 minf(f32 a, f32 b) { - return __builtin_fminf(a, b); -} - -inline f32 maxf(f32 a, f32 b) { - return __builtin_fmaxf(a, b); -} - -#else - -// Fallback to the original implementation for iDO -inline f32 minf(f32 a, f32 b) { - return (a <= b) ? a : b; -} - -inline f32 maxf(f32 a, f32 b) { - return (a > b) ? a : b; -} - -#endif - -// The sqr, min, max, and trig functions do not have/need built-ins, so it's safe to leave them as is -inline s16 (min)(s16 a, s16 b) { - return (a <= b) ? a : b; -} - -inline s16 (max)(s16 a, s16 b) { - return (a > b) ? a : b; -} - -inline f32 sqrf(f32 x) { - return x * x; -} - -inline s16 (sqr)(s16 x) { - return x * x; -} - inline f32 sins(s16 sm64Angle) { return gSineTable[(u16) (sm64Angle) >> 4]; } @@ -226,7 +185,7 @@ void vec3f_combine(Vec3f dest, Vec3f vecA, Vec3f vecB, f32 sclA, f32 sclB) { */ void *vec3f_rotate_zxy(Vec3f dest, Vec3s rotate) { Vec3f v = { dest[0], dest[1], dest[2] }; - + f32 sx = sins(rotate[0]); f32 cx = coss(rotate[0]); @@ -235,7 +194,7 @@ void *vec3f_rotate_zxy(Vec3f dest, Vec3s rotate) { f32 sz = sins(rotate[2]); f32 cz = coss(rotate[2]); - + f32 sysz = (sy * sz); f32 cycz = (cy * cz); f32 cysz = (cy * sz); @@ -244,7 +203,7 @@ void *vec3f_rotate_zxy(Vec3f dest, Vec3s rotate) { dest[0] = v[0] * ((sysz * sx) + cycz) + v[1] * ((sycz * sx) - cysz) + v[2] * (cx * sy); dest[1] = v[0] * (cx * sz) + v[1] * (cx * cz) + v[2] * -sx; dest[2] = v[0] * ((cysz * sx) - sycz) + v[1] * ((cycz * sx) + sysz) + v[2] * (cx * cy); - + return dest; } @@ -316,7 +275,7 @@ void mtxf_lookat(Mat4 mtx, Vec3f from, Vec3f to, s16 roll) { } else { dx = dz = 0.0f; } - + sinRoll = sins(roll); cosRoll = coss(roll); @@ -336,7 +295,7 @@ void mtxf_lookat(Mat4 mtx, Vec3f from, Vec3f to, s16 roll) { up[0] = forward[1] * right[2] - forward[2] * right[1]; up[1] = forward[2] * right[0] - forward[0] * right[2]; up[2] = forward[0] * right[1] - forward[1] * right[0]; - + mtx[0][0] = right[0]; mtx[1][0] = right[1]; mtx[2][0] = right[2]; diff --git a/src/engine/math_util.h b/src/engine/math_util.h index b6d1d51a3..3a503eb18 100644 --- a/src/engine/math_util.h +++ b/src/engine/math_util.h @@ -33,33 +33,59 @@ extern f32 gCosineTable[]; #endif // Inline Function prototypes -f32 minf(f32 a, f32 b); -s16 min(s16 a, s16 b); -f32 maxf(f32 a, f32 b); -s16 max(s16 a, s16 b); -f32 sqrf(f32 x); -s16 sqr(s16 x); +#if defined(__clang__) || defined(__GNUC__) + +// Use built-in functions when using Clang or GCC +static inline f32 minf(f32 a, f32 b) { return __builtin_fminf(a, b); } +static inline f32 maxf(f32 a, f32 b) { return __builtin_fmaxf(a, b); } +#else + +// Fallback to the original implementation for iDO +static inline f32 minf(f32 a, f32 b) { return (a <= b) ? a : b; } +static inline f32 maxf(f32 a, f32 b) { return (a > b) ? a : b; } +#endif + +static inline f32 sqrf(f32 x) { return x * x; } + f32 sins(s16 sm64Angle); f32 coss(s16 sm64Angle); -// Generic macros help the inline functions be noted in autodoc while retaining original functionality +#define DEFINE_FOR_TYPE(t) \ + static inline t min_##t(t a, t b) { return (a < b) ? a : b; } \ + static inline t max_##t(t a, t b) { return (a > b) ? a : b; } \ + static inline t sqr_##t(t x) { return x * x; } +DEFINE_FOR_TYPE(s16); +DEFINE_FOR_TYPE(s32); +DEFINE_FOR_TYPE(u8); +DEFINE_FOR_TYPE(size_t); + +// These macros intentionally don't have a default. +// If you get an error saying that a type is not compatible, +// it must be added within this file. + #define min(a, b) _Generic((a), \ - f32: minf(a, b), \ - s16: (min)(a, b), \ - default: ((a) < (b) ? (a) : (b)) \ -) + f32: minf, \ + s16: min_s16, \ + s32: min_s32, \ + u8: min_u8, \ + size_t: min_size_t \ +)(a, b) #define max(a, b) _Generic((a), \ - f32: maxf(a, b), \ - s16: (max)(a, b), \ - default: ((a) > (b) ? (a) : (b)) \ -) + f32: maxf, \ + s16: max_s16, \ + s32: max_s32, \ + u8: max_u8, \ + size_t: max_size_t \ +)(a, b) #define sqr(x) _Generic((x), \ - f32: sqrf(x), \ - s16: (sqr)(x), \ - default: ((x) * (x)) \ -) + f32: sqrf, \ + s16: sqr_s16, \ + s32: sqr_s32, \ + u8: sqr_u8, \ + size_t: sqr_size_t \ +)(x) #if defined(__clang__) || defined(__GNUC__)