From e00d98708316182096e217a975e212c33afec837 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Wed, 2 Jan 2013 12:53:39 +0000 Subject: [PATCH] math: implement fract() for vectors and scalar types. --- src/lol/math/half.h | 1 + src/lol/math/math.h | 15 +++++++++++++++ src/lol/math/vector.h | 32 +++++++++++++++++++++++++++++++- 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/lol/math/half.h b/src/lol/math/half.h index 65a93fe8..b554e190 100644 --- a/src/lol/math/half.h +++ b/src/lol/math/half.h @@ -125,6 +125,7 @@ static inline half fmod(half a, half b) using std::fmod; return (half)fmod((float)a, (float)b); } +static inline half fract(half a) { return (half)fract((float)a); } static inline half abs(half a) { return half::makebits(a.bits & 0x7fffu); } static inline half clamp(half x, half a, half b) diff --git a/src/lol/math/math.h b/src/lol/math/math.h index 3e0836c5..24b01ed8 100644 --- a/src/lol/math/math.h +++ b/src/lol/math/math.h @@ -33,6 +33,9 @@ namespace lol static inline double sqrt(double const &x) { return std::sqrt(x); } static inline float sqrt(float const &x) { return std::sqrt(x); } +static inline double exp(double const &x) { return std::exp(x); } +static inline float exp(float const &x) { return std::exp(x); } + static inline double sin(double const &x) { return std::sin(x); } static inline double cos(double const &x) { return std::cos(x); } static inline double tan(double const &x) { return std::tan(x); } @@ -138,6 +141,18 @@ static inline float fmod(float x, float y) { return std::fmod(x, y); } static inline double fmod(double x, double y) { return std::fmod(x, y); } static inline ldouble fmod(ldouble x, ldouble y) { return std::fmod(x, y); } +static inline uint8_t fract(uint8_t x) { return 0; } +static inline int8_t fract(int8_t x) { return 0; } +static inline uint16_t fract(uint16_t x) { return 0; } +static inline int16_t fract(int16_t x) { return 0; } +static inline uint32_t fract(uint32_t x) { return 0; } +static inline int32_t fract(int32_t x) { return 0; } +static inline uint64_t fract(uint64_t x) { return 0; } +static inline int64_t fract(int64_t x) { return 0; } +static inline float fract(float x) { return x - std::floor(x); } +static inline double fract(double x) { return x - std::floor(x); } +static inline ldouble fract(ldouble x) { return x - std::floor(x); } + static inline uint8_t min(uint8_t x, uint8_t y) { return std::min(x, y); } static inline int8_t min(int8_t x, int8_t y) { return std::min(x, y); } static inline uint16_t min(uint16_t x, uint16_t y) { return std::min(x, y); } diff --git a/src/lol/math/vector.h b/src/lol/math/vector.h index c9fe9f84..cf8d4676 100644 --- a/src/lol/math/vector.h +++ b/src/lol/math/vector.h @@ -1134,6 +1134,25 @@ extern Quat slerp(Quat const &qa, Quat const &qb, T f); return max(min(x, b), a); \ } +/* + * vec mix(vec, vec, vec) + * vec mix(vec, vec, scalar) + */ +#define LOL_VECTOR_MIX_FUN(tname, tprefix, type) \ + tprefix \ + inline tname mix(tname const &x, \ + tname const &y, tname const &a) \ + { \ + return x + a * (y - x); \ + } \ + \ + tprefix \ + inline tname mix(tname const &x, \ + tname const &y, type const &a) \ + { \ + return x + a * (y - x); \ + } + /* * bool ==(vec, vec) (also complex & quaternion) * bool !=(vec, vec) (also complex & quaternion) @@ -1218,6 +1237,15 @@ extern Quat slerp(Quat const &qa, Quat const &qb, T f); } \ \ tprefix \ + inline tname fract(tname const &a) \ + { \ + tname ret; \ + for (size_t n = 0; n < sizeof(a) / sizeof(type); n++) \ + ret[n] = fract(a[n]); \ + return ret; \ + } \ + \ + tprefix \ inline tname normalize(tname const &a) \ { \ type norm = (type)length(a); \ @@ -1293,7 +1321,8 @@ extern Quat slerp(Quat const &qa, Quat const &qb, T f); LOL_VECTOR_MINMAX_FUN(tname, min, tprefix, type) \ LOL_VECTOR_MINMAX_FUN(tname, max, tprefix, type) \ LOL_VECTOR_MINMAX_FUN(tname, fmod, tprefix, type) \ - LOL_VECTOR_CLAMP_FUN(tname, tprefix, type) + LOL_VECTOR_CLAMP_FUN(tname, tprefix, type) \ + LOL_VECTOR_MIX_FUN(tname, tprefix, type) #define LOL_VECTOR_COERCE_OPS(tname, tprefix, t1, t2, tf) \ LOL_VECTOR_VECTOR_COERCE_OP(tname, *, tprefix, t1, t2, tf) \ @@ -1563,6 +1592,7 @@ ACTIVATE_COERCE_NAMESPACES(real) #undef LOL_VECTOR_VECTOR_OP #undef LOL_VECTOR_MINMAX_FUN #undef LOL_VECTOR_CLAMP_FUN +#undef LOL_VECTOR_MIX_FUN #undef LOL_VECTOR_VECTOR_BOOL_OP #undef LOL_VECTOR_SCALAR_COERCE_OP #undef LOL_SCALAR_VECTOR_COERCE_OP