From 12414bc6b98775db5d8e5bb057ba097652e241bf Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Fri, 2 Mar 2012 17:49:00 +0000 Subject: [PATCH] math: some refactoring in the vector operations, to make it easier to implement automatic promotion in the future. --- src/lol/math/vector.h | 176 ++++++++++++++++++++++-------------------- 1 file changed, 94 insertions(+), 82 deletions(-) diff --git a/src/lol/math/vector.h b/src/lol/math/vector.h index 69e4cc10..195ea3f4 100644 --- a/src/lol/math/vector.h +++ b/src/lol/math/vector.h @@ -925,53 +925,58 @@ static inline Quat operator /(Quat x, Quat const &y) * Common operators for all vector types, including quaternions */ -#define DECLARE_VECTOR_OP(tname, op, tprefix, T) \ +#define DECLARE_VECTOR_VECTOR_OP(tname, op, tprefix, type) \ tprefix \ - static inline tname operator op(tname const &a, tname const &b) \ + static inline tname operator op(tname const &a, \ + tname const &b) \ { \ - tname ret; \ - for (size_t n = 0; n < sizeof(a) / sizeof(T); n++) \ + tname ret; \ + for (size_t n = 0; n < sizeof(a) / sizeof(type); n++) \ ret[n] = a[n] op b[n]; \ return ret; \ } \ \ tprefix \ - static inline tname operator op##=(tname &a, tname const &b) \ + static inline tname operator op##=(tname &a, \ + tname const &b) \ { \ return a = a op b; \ } -#define DECLARE_BOOL_OP(tname, op, op2, ret, tprefix, T) \ +#define DECLARE_VECTOR_VECTOR_BOOLOP(tname, op, op2, ret, tprefix, type) \ tprefix \ - static inline bool operator op(tname const &a, tname const &b) \ + static inline bool operator op(tname const &a, \ + tname const &b) \ { \ - for (size_t n = 0; n < sizeof(a) / sizeof(T); n++) \ + for (size_t n = 0; n < sizeof(a) / sizeof(type); n++) \ if (!(a[n] op2 b[n])) \ return !ret; \ return ret; \ } -#define DECLARE_SCALAR_OP(tname, op, tprefix, T) \ +#define DECLARE_VECTOR_SCALAR_OP(tname, op, tprefix, type) \ tprefix \ - static inline tname operator op(tname const &a, T const &val) \ + static inline tname operator op(tname const &a, \ + type const &val) \ { \ - tname ret; \ - for (size_t n = 0; n < sizeof(a) / sizeof(T); n++) \ + tname ret; \ + for (size_t n = 0; n < sizeof(a) / sizeof(type); n++) \ ret[n] = a[n] op val; \ return ret; \ } \ \ tprefix \ - static inline tname operator op(T const &val, tname const &a) \ + static inline tname operator op(type const &val, \ + tname const &a) \ { \ - tname ret; \ - for (size_t n = 0; n < sizeof(a) / sizeof(T); n++) \ + tname ret; \ + for (size_t n = 0; n < sizeof(a) / sizeof(type); n++) \ ret[n] = a[n] op val; \ return ret; \ } \ \ tprefix \ - static inline tname operator op##=(tname &a, T const &val) \ + static inline tname operator op##=(tname &a, type const &val) \ { \ return a = a op val; \ } @@ -979,7 +984,8 @@ static inline Quat operator /(Quat x, Quat const &y) /* FIXME: this is not used yet */ #define SCALAR_PROMOTE_OP(tname, op, U) \ template \ - static inline tname operator op(U const &val, tname const &a) \ + static inline tname operator op(U const &val, \ + tname const &a) \ { \ tname ret; \ for (size_t n = 0; n < sizeof(a) / sizeof(T); n++) \ @@ -987,100 +993,106 @@ static inline Quat operator /(Quat x, Quat const &y) return ret; \ } -#define DECLARE_OTHER_GLOBAL_OPS(tname, tprefix, T) \ - DECLARE_SCALAR_OP(tname, *, tprefix, T) \ - DECLARE_SCALAR_OP(tname, /, tprefix, T) \ - \ - DECLARE_VECTOR_OP(tname, -, tprefix, T) \ - DECLARE_VECTOR_OP(tname, +, tprefix, T) \ - \ - DECLARE_BOOL_OP(tname, ==, ==, true, tprefix, T) \ - DECLARE_BOOL_OP(tname, !=, ==, false, tprefix, T) \ - \ +#define DECLARE_UNARY_OPS(tname, tprefix, type) \ tprefix \ - static inline tname operator -(tname const &a) \ + static inline tname operator -(tname const &a) \ { \ - tname ret; \ - for (size_t n = 0; n < sizeof(a) / sizeof(T); n++) \ + tname ret; \ + for (size_t n = 0; n < sizeof(a) / sizeof(type); n++) \ ret[n] = -a[n]; \ return ret; \ } \ \ tprefix \ - static inline T sqlen(tname const &a) \ + static inline type sqlen(tname const &a) \ { \ - T acc = 0; \ - for (size_t n = 0; n < sizeof(a) / sizeof(T); n++) \ + type acc = 0; \ + for (size_t n = 0; n < sizeof(a) / sizeof(type); n++) \ acc += a[n] * a[n]; \ return acc; \ } \ \ tprefix \ - static inline double len(tname const &a) \ + static inline double len(tname const &a) \ { \ using namespace std; \ return sqrt((double)sqlen(a)); \ } \ \ tprefix \ - static inline T dot(tname const &a, tname const &b) \ + static inline tname normalize(tname const &val) \ { \ - T ret = 0; \ - for (size_t n = 0; n < sizeof(a) / sizeof(T); n++) \ - ret += a[n] * b[n]; \ - return ret; \ - } \ + type norm = (type)len(val); \ + return norm ? val / norm : val * (type)0; \ + } + +#define DECLARE_BINARY_OPS(tname, tprefix, type) \ + DECLARE_VECTOR_SCALAR_OP(tname, *, tprefix, type) \ + DECLARE_VECTOR_SCALAR_OP(tname, /, tprefix, type) \ + \ + DECLARE_VECTOR_VECTOR_OP(tname, -, tprefix, type) \ + DECLARE_VECTOR_VECTOR_OP(tname, +, tprefix, type) \ + \ + DECLARE_VECTOR_VECTOR_BOOLOP(tname, ==, ==, true, tprefix, type) \ + DECLARE_VECTOR_VECTOR_BOOLOP(tname, !=, ==, false, tprefix, type) \ \ tprefix \ - static inline tname normalize(tname const &val) \ + static inline type dot(tname const &a, tname const &b) \ { \ - T norm = (T)len(val); \ - return norm ? val / norm : val * (T)0; \ + type ret = 0; \ + for (size_t n = 0; n < sizeof(a) / sizeof(type); n++) \ + ret += a[n] * b[n]; \ + return ret; \ } -#define DECLARE_GLOBAL_TEMPLATE_OPS(tname) \ - DECLARE_OTHER_GLOBAL_OPS(tname, template, T) - -#define DECLARE_ALL_GLOBAL_OPS(tname, tprefix, T) \ - DECLARE_VECTOR_OP(tname, *, tprefix, T) \ - DECLARE_VECTOR_OP(tname, /, tprefix, T) \ - \ - DECLARE_OTHER_GLOBAL_OPS(tname, tprefix, T) \ +#define DECLARE_VECTOR_OPS(tname, tprefix, type) \ + DECLARE_VECTOR_VECTOR_OP(tname, *, tprefix, type) \ + DECLARE_VECTOR_VECTOR_OP(tname, /, tprefix, type) \ \ - DECLARE_BOOL_OP(tname, <=, <=, true, tprefix, T) \ - DECLARE_BOOL_OP(tname, >=, >=, true, tprefix, T) \ - DECLARE_BOOL_OP(tname, <, <, true, tprefix, T) \ - DECLARE_BOOL_OP(tname, >, >, true, tprefix, T) - -#define DECLARE_GLOBAL_TYPED_OPS(tname) \ - DECLARE_ALL_GLOBAL_OPS(tname, /* empty */, half) \ - DECLARE_ALL_GLOBAL_OPS(tname, /* empty */, float) \ - DECLARE_ALL_GLOBAL_OPS(tname, /* empty */, double) \ - DECLARE_ALL_GLOBAL_OPS(tname, /* empty */, int8_t) \ - DECLARE_ALL_GLOBAL_OPS(tname, /* empty */, uint8_t) \ - DECLARE_ALL_GLOBAL_OPS(tname, /* empty */, int16_t) \ - DECLARE_ALL_GLOBAL_OPS(tname, /* empty */, uint16_t) \ - DECLARE_ALL_GLOBAL_OPS(tname, /* empty */, int32_t) \ - DECLARE_ALL_GLOBAL_OPS(tname, /* empty */, uint32_t) \ - DECLARE_ALL_GLOBAL_OPS(tname, /* empty */, int64_t) \ - DECLARE_ALL_GLOBAL_OPS(tname, /* empty */, uint64_t) - -DECLARE_GLOBAL_TEMPLATE_OPS(Cmplx) -DECLARE_GLOBAL_TEMPLATE_OPS(Quat) - -DECLARE_GLOBAL_TYPED_OPS(Vec2) -DECLARE_GLOBAL_TYPED_OPS(Vec3) -DECLARE_GLOBAL_TYPED_OPS(Vec4) + DECLARE_VECTOR_VECTOR_BOOLOP(tname, <=, <=, true, tprefix, type) \ + DECLARE_VECTOR_VECTOR_BOOLOP(tname, >=, >=, true, tprefix, type) \ + DECLARE_VECTOR_VECTOR_BOOLOP(tname, <, <, true, tprefix, type) \ + DECLARE_VECTOR_VECTOR_BOOLOP(tname, >, >, true, tprefix, type) + +#define DECLARE_ALL_NONVECTOR_OPS(tname) \ + DECLARE_BINARY_OPS(tname, template, T) \ + DECLARE_UNARY_OPS(tname, template, T) + +#define DECLARE_ALL_VECTOR_OPS_INNER(tname, tprefix, type) \ + DECLARE_BINARY_OPS(tname, tprefix, type) \ + DECLARE_UNARY_OPS(tname, tprefix, type) \ + DECLARE_VECTOR_OPS(tname, tprefix, type) \ + +#define DECLARE_ALL_VECTOR_OPS(tname) \ + DECLARE_ALL_VECTOR_OPS_INNER(tname, /* empty */, half) \ + DECLARE_ALL_VECTOR_OPS_INNER(tname, /* empty */, float) \ + DECLARE_ALL_VECTOR_OPS_INNER(tname, /* empty */, double) \ + DECLARE_ALL_VECTOR_OPS_INNER(tname, /* empty */, int8_t) \ + DECLARE_ALL_VECTOR_OPS_INNER(tname, /* empty */, uint8_t) \ + DECLARE_ALL_VECTOR_OPS_INNER(tname, /* empty */, int16_t) \ + DECLARE_ALL_VECTOR_OPS_INNER(tname, /* empty */, uint16_t) \ + DECLARE_ALL_VECTOR_OPS_INNER(tname, /* empty */, int32_t) \ + DECLARE_ALL_VECTOR_OPS_INNER(tname, /* empty */, uint32_t) \ + DECLARE_ALL_VECTOR_OPS_INNER(tname, /* empty */, int64_t) \ + DECLARE_ALL_VECTOR_OPS_INNER(tname, /* empty */, uint64_t) + +DECLARE_ALL_NONVECTOR_OPS(Cmplx) +DECLARE_ALL_NONVECTOR_OPS(Quat) + +DECLARE_ALL_VECTOR_OPS(Vec2) +DECLARE_ALL_VECTOR_OPS(Vec3) +DECLARE_ALL_VECTOR_OPS(Vec4) #undef DECLARE_VECTOR_TYPEDEFS #undef DECLARE_MEMBER_OPS -#undef DECLARE_VECTOR_OP -#undef DECLARE_BOOL_OP -#undef DECLARE_SCALAR_OP -#undef DECLARE_GLOBAL_OPS -#undef DECLARE_GLOBAL_TEMPLATE_OPS -#undef DECLARE_ALL_GLOBAL_OPS -#undef DECLARE_GLOBAL_TYPED_OPS +#undef DECLARE_VECTOR_VECTOR_OP +#undef DECLARE_VECTOR_VECTOR_BOOLOP +#undef DECLARE_VECTOR_SCALAR_OP +#undef DECLARE_BINARY_OPS +#undef DECLARE_UNARY_OPS +#undef DECLARE_ALL_NONVECTOR_OPS +#undef DECLARE_ALL_VECTOR_OPS_INNER +#undef DECLARE_ALL_VECTOR_OPS /* * Magic vector swizzling (part 2/2)