Browse Source

math: implement abs() and fmod() for vector types and the half class.

legacy
Sam Hocevar sam 12 years ago
parent
commit
e4954c0a0f
2 changed files with 27 additions and 6 deletions
  1. +2
    -0
      src/lol/math/half.h
  2. +25
    -6
      src/lol/math/vector.h

+ 2
- 0
src/lol/math/half.h View File

@@ -120,6 +120,8 @@ public:

static inline half min(half a, half b) { return a < b ? a : b; }
static inline half max(half a, half b) { return a > b ? a : b; }
static inline half fmod(half a, half b) { return (half)fmod((float)a, (float)b); }
static inline half abs(half a) { return half::makebits(a.bits & 0x7fffu); }

static inline half clamp(half x, half a, half b)
{


+ 25
- 6
src/lol/math/vector.h View File

@@ -1065,9 +1065,9 @@ extern Quat<T> slerp(Quat<T> const &qa, Quat<T> const &qb, T f);
}

/*
* vec min(vec, vec) (also max)
* vec min(vec, scalar) (also max)
* vec min(scalar, vec) (also max)
* vec min(vec, vec) (also max, fmod)
* vec min(vec, scalar) (also max, fmod)
* vec min(scalar, vec) (also max, fmod)
*/
#define DECLARE_VECTOR_MINMAX_OP(tname, op, tprefix, type) \
tprefix \
@@ -1104,6 +1104,7 @@ extern Quat<T> slerp(Quat<T> const &qa, Quat<T> const &qb, T f);
* vec clamp(vec, vec, vec)
* vec clamp(vec, vec, scalar)
* vec clamp(vec, scalar, vec)
* vec clamp(vec, scalar, scalar)
*/
#define DECLARE_VECTOR_CLAMP_OP(tname, tprefix, type) \
tprefix \
@@ -1125,6 +1126,13 @@ extern Quat<T> slerp(Quat<T> const &qa, Quat<T> const &qb, T f);
tname<type> const &a, type const &b) \
{ \
return max(min(x, b), a); \
} \
\
tprefix \
inline tname<type> clamp(tname<type> const &x, \
type const &a, type const &b) \
{ \
return max(min(x, b), a); \
}

/*
@@ -1210,10 +1218,20 @@ extern Quat<T> slerp(Quat<T> const &qa, Quat<T> const &qb, T f);
} \
\
tprefix \
inline tname<type> normalize(tname<type> const &val) \
inline tname<type> normalize(tname<type> const &a) \
{ \
type norm = (type)length(val); \
return norm ? val / norm : val * (type)0; \
type norm = (type)length(a); \
return norm ? a / norm : a * (type)0; \
} \
\
tprefix \
inline tname<type> abs(tname<type> const &a) \
{ \
using std::abs; \
tname<type> ret; \
for (size_t n = 0; n < sizeof(a) / sizeof(type); n++) \
ret[n] = abs(a[n]); \
return ret; \
}

#define DECLARE_BINARY_NONVECTOR_COERCE_OPS(tname, tprefix, t1, t2, tf) \
@@ -1270,6 +1288,7 @@ extern Quat<T> slerp(Quat<T> const &qa, Quat<T> const &qb, T f);
\
DECLARE_VECTOR_MINMAX_OP(tname, min, tprefix, type) \
DECLARE_VECTOR_MINMAX_OP(tname, max, tprefix, type) \
DECLARE_VECTOR_MINMAX_OP(tname, fmod, tprefix, type) \
DECLARE_VECTOR_CLAMP_OP(tname, tprefix, type)

#define DECLARE_VECTOR_COERCE_OPS(tname, tprefix, t1, t2, tf) \


Loading…
Cancel
Save