Changeset 1153


Ignore:
Timestamp:
Mar 5, 2012, 8:06:40 PM (8 years ago)
Author:
sam
Message:

math: add new coercion rules for vector classes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lol/math/vector.h

    r1151 r1153  
    926926 */
    927927
    928 #define DECLARE_VECTOR_VECTOR_OP(tname, op, tprefix, type) \
     928#define DECLARE_VECTOR_VECTOR_COERCE_OP(tname, op, tprefix, t1, t2, tf) \
    929929    tprefix \
    930     static inline tname<type> operator op(tname<type> const &a, \
    931                                           tname<type> const &b) \
     930    static inline tname<tf> operator op(tname<t1> const &a, \
     931                                        tname<t2> const &b) \
    932932    { \
    933         tname<type> ret; \
    934         for (size_t n = 0; n < sizeof(a) / sizeof(type); n++) \
     933        tname<tf> ret; \
     934        for (size_t n = 0; n < sizeof(a) / sizeof(t1); n++) \
    935935            ret[n] = a[n] op b[n]; \
    936936        return ret; \
    937     } \
    938     \
     937    }
     938
     939#define DECLARE_VECTOR_VECTOR_OP(tname, op, tprefix, type) \
    939940    tprefix \
    940941    static inline tname<type> operator op##=(tname<type> &a, \
     
    944945    }
    945946
    946 #define DECLARE_VECTOR_VECTOR_BOOLOP(tname, op, op2, ret, tprefix, type) \
     947#define DECLARE_VECTOR_VECTOR_BOOLOP(tname, op, op2, ret, tprefix, t1, t2) \
    947948    tprefix \
    948     static inline bool operator op(tname<type> const &a, \
    949                                    tname<type> const &b) \
     949    static inline bool operator op(tname<t1> const &a, \
     950                                   tname<t2> const &b) \
    950951    { \
    951         for (size_t n = 0; n < sizeof(a) / sizeof(type); n++) \
     952        for (size_t n = 0; n < sizeof(a) / sizeof(t1); n++) \
    952953            if (!(a[n] op2 b[n])) \
    953954                return !ret; \
     
    955956    }
    956957
    957 #define DECLARE_VECTOR_SCALAR_OP(tname, op, tprefix, type) \
     958#define DECLARE_VECTOR_SCALAR_COERCE_OP(tname, op, tprefix, t1, t2, tf) \
    958959    tprefix \
    959     static inline tname<type> operator op(tname<type> const &a, \
    960                                           type const &val) \
     960    static inline tname<tf> operator op(tname<t1> const &a, t2 const &val) \
    961961    { \
    962         tname<type> ret; \
    963         for (size_t n = 0; n < sizeof(a) / sizeof(type); n++) \
     962        tname<tf> ret; \
     963        for (size_t n = 0; n < sizeof(a) / sizeof(t1); n++) \
    964964            ret[n] = a[n] op val; \
    965965        return ret; \
     
    967967    \
    968968    tprefix \
    969     static inline tname<type> operator op(type const &val, \
    970                                           tname<type> const &a) \
     969    static inline tname<tf> operator op(t1 const &val, tname<t2> const &a) \
    971970    { \
    972         tname<type> ret; \
    973         for (size_t n = 0; n < sizeof(a) / sizeof(type); n++) \
     971        tname<tf> ret; \
     972        for (size_t n = 0; n < sizeof(a) / sizeof(t2); n++) \
    974973            ret[n] = a[n] op val; \
    975974        return ret; \
    976     } \
    977     \
     975    }
     976
     977#define DECLARE_VECTOR_SCALAR_OP(tname, op, tprefix, type) \
    978978    tprefix \
    979979    static inline tname<type> operator op##=(tname<type> &a, type const &val) \
    980980    { \
    981981        return a = a op val; \
    982     }
    983 
    984 /* FIXME: this is not used yet */
    985 #define SCALAR_PROMOTE_OP(tname, op, U) \
    986     template<typename T> \
    987     static inline tname<U> operator op(U const &val, \
    988                                        tname<T> const &a) \
    989     { \
    990         tname<U> ret; \
    991         for (size_t n = 0; n < sizeof(a) / sizeof(T); n++) \
    992             ret[n] = val op a[n]; \
    993         return ret; \
    994982    }
    995983
     
    10271015    }
    10281016
     1017#define DECLARE_BINARY_COERCE_OPS(tname, tprefix, t1, t2, tf) \
     1018    DECLARE_VECTOR_SCALAR_COERCE_OP(tname, *, tprefix, t1, t2, tf) \
     1019    DECLARE_VECTOR_SCALAR_COERCE_OP(tname, /, tprefix, t1, t2, tf) \
     1020    \
     1021    DECLARE_VECTOR_VECTOR_COERCE_OP(tname, -, tprefix, t1, t2, tf) \
     1022    DECLARE_VECTOR_VECTOR_COERCE_OP(tname, +, tprefix, t1, t2, tf) \
     1023    \
     1024    DECLARE_VECTOR_VECTOR_BOOLOP(tname, ==, ==, true, tprefix, t1, t2) \
     1025    DECLARE_VECTOR_VECTOR_BOOLOP(tname, !=, ==, false, tprefix, t1, t2) \
     1026    \
     1027    tprefix \
     1028    static inline tf dot(tname<t1> const &a, tname<t2> const &b) \
     1029    { \
     1030        tf ret = 0; \
     1031        for (size_t n = 0; n < sizeof(a) / sizeof(t1); n++) \
     1032            ret += a[n] * b[n]; \
     1033        return ret; \
     1034    }
     1035
    10291036#define DECLARE_BINARY_OPS(tname, tprefix, type) \
     1037    DECLARE_BINARY_COERCE_OPS(tname, tprefix, type, type, type) \
     1038    \
    10301039    DECLARE_VECTOR_SCALAR_OP(tname, *, tprefix, type) \
    10311040    DECLARE_VECTOR_SCALAR_OP(tname, /, tprefix, type) \
    10321041    \
    10331042    DECLARE_VECTOR_VECTOR_OP(tname, -, tprefix, type) \
    1034     DECLARE_VECTOR_VECTOR_OP(tname, +, tprefix, type) \
    1035     \
    1036     DECLARE_VECTOR_VECTOR_BOOLOP(tname, ==, ==, true, tprefix, type) \
    1037     DECLARE_VECTOR_VECTOR_BOOLOP(tname, !=, ==, false, tprefix, type) \
    1038     \
    1039     tprefix \
    1040     static inline type dot(tname<type> const &a, tname<type> const &b) \
    1041     { \
    1042         type ret = 0; \
    1043         for (size_t n = 0; n < sizeof(a) / sizeof(type); n++) \
    1044             ret += a[n] * b[n]; \
    1045         return ret; \
    1046     }
     1043    DECLARE_VECTOR_VECTOR_OP(tname, +, tprefix, type)
     1044
     1045#define DECLARE_VECTOR_COERCE_OPS(tname, tprefix, t1, t2, tf) \
     1046    DECLARE_VECTOR_VECTOR_COERCE_OP(tname, *, tprefix, t1, t2, tf) \
     1047    DECLARE_VECTOR_VECTOR_COERCE_OP(tname, /, tprefix, t1, t2, tf) \
     1048    \
     1049    DECLARE_VECTOR_VECTOR_BOOLOP(tname, <=, <=, true, tprefix, t1, t2) \
     1050    DECLARE_VECTOR_VECTOR_BOOLOP(tname, >=, >=, true, tprefix, t1, t2) \
     1051    DECLARE_VECTOR_VECTOR_BOOLOP(tname, <, <, true, tprefix, t1, t2) \
     1052    DECLARE_VECTOR_VECTOR_BOOLOP(tname, >, >, true, tprefix, t1, t2)
    10471053
    10481054#define DECLARE_VECTOR_OPS(tname, tprefix, type) \
     1055    DECLARE_VECTOR_COERCE_OPS(tname, /* empty */, type, type, type) \
     1056    \
    10491057    DECLARE_VECTOR_VECTOR_OP(tname, *, tprefix, type) \
    1050     DECLARE_VECTOR_VECTOR_OP(tname, /, tprefix, type) \
    1051     \
    1052     DECLARE_VECTOR_VECTOR_BOOLOP(tname, <=, <=, true, tprefix, type) \
    1053     DECLARE_VECTOR_VECTOR_BOOLOP(tname, >=, >=, true, tprefix, type) \
    1054     DECLARE_VECTOR_VECTOR_BOOLOP(tname, <, <, true, tprefix, type) \
    1055     DECLARE_VECTOR_VECTOR_BOOLOP(tname, >, >, true, tprefix, type)
     1058    DECLARE_VECTOR_VECTOR_OP(tname, /, tprefix, type)
    10561059
    10571060#define DECLARE_ALL_NONVECTOR_OPS(tname) \
     
    10591062    DECLARE_UNARY_OPS(tname, template<typename T>, T)
    10601063
    1061 #define DECLARE_ALL_VECTOR_OPS_INNER(tname, tprefix, type) \
    1062     DECLARE_BINARY_OPS(tname, tprefix, type) \
    1063     DECLARE_UNARY_OPS(tname, tprefix, type) \
    1064     DECLARE_VECTOR_OPS(tname, tprefix, type) \
     1064#define DECLARE_ALL_VECTOR_OPS_INNER(tname, type) \
     1065    DECLARE_BINARY_OPS(tname, /* empty */, type) \
     1066    DECLARE_UNARY_OPS(tname, /* empty */, type) \
     1067    DECLARE_VECTOR_OPS(tname, /* empty */, type) \
    10651068
    10661069#define DECLARE_ALL_VECTOR_OPS(tname) \
    1067     DECLARE_ALL_VECTOR_OPS_INNER(tname, /* empty */, half) \
    1068     DECLARE_ALL_VECTOR_OPS_INNER(tname, /* empty */, float) \
    1069     DECLARE_ALL_VECTOR_OPS_INNER(tname, /* empty */, double) \
    1070     DECLARE_ALL_VECTOR_OPS_INNER(tname, /* empty */, int8_t) \
    1071     DECLARE_ALL_VECTOR_OPS_INNER(tname, /* empty */, uint8_t) \
    1072     DECLARE_ALL_VECTOR_OPS_INNER(tname, /* empty */, int16_t) \
    1073     DECLARE_ALL_VECTOR_OPS_INNER(tname, /* empty */, uint16_t) \
    1074     DECLARE_ALL_VECTOR_OPS_INNER(tname, /* empty */, int32_t) \
    1075     DECLARE_ALL_VECTOR_OPS_INNER(tname, /* empty */, uint32_t) \
    1076     DECLARE_ALL_VECTOR_OPS_INNER(tname, /* empty */, int64_t) \
    1077     DECLARE_ALL_VECTOR_OPS_INNER(tname, /* empty */, uint64_t)
     1070    DECLARE_ALL_VECTOR_OPS_INNER(tname, half) \
     1071    DECLARE_ALL_VECTOR_OPS_INNER(tname, float) \
     1072    DECLARE_ALL_VECTOR_OPS_INNER(tname, double) \
     1073    DECLARE_ALL_VECTOR_OPS_INNER(tname, int8_t) \
     1074    DECLARE_ALL_VECTOR_OPS_INNER(tname, uint8_t) \
     1075    DECLARE_ALL_VECTOR_OPS_INNER(tname, int16_t) \
     1076    DECLARE_ALL_VECTOR_OPS_INNER(tname, uint16_t) \
     1077    DECLARE_ALL_VECTOR_OPS_INNER(tname, int32_t) \
     1078    DECLARE_ALL_VECTOR_OPS_INNER(tname, uint32_t) \
     1079    DECLARE_ALL_VECTOR_OPS_INNER(tname, int64_t) \
     1080    DECLARE_ALL_VECTOR_OPS_INNER(tname, uint64_t)
     1081
     1082#define DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, tlow, thigh) \
     1083    DECLARE_BINARY_COERCE_OPS(tname, /* empty */, tlow, thigh, thigh) \
     1084    DECLARE_BINARY_COERCE_OPS(tname, /* empty */, thigh, tlow, thigh) \
     1085    \
     1086    DECLARE_VECTOR_COERCE_OPS(tname, /* empty */, tlow, thigh, thigh) \
     1087    DECLARE_VECTOR_COERCE_OPS(tname, /* empty */, thigh, tlow, thigh)
     1088
     1089#define DECLARE_ALL_VECTOR_COERCE_OPS(tname) \
     1090    /* Apply the same coercion rules as in the C++ standard. However,   */ \
     1091    /* instead of promoting int8_t etc. to int, we apply our own rules. */ \
     1092    /* FIXME: "half" and "real" are deactivated for now, because we do  */ \
     1093    /* not implement all combinations of operators for these types yet. */ \
     1094    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int8_t, uint8_t) \
     1095    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int8_t, int16_t) \
     1096    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int8_t, uint16_t) \
     1097    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int8_t, int32_t) \
     1098    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int8_t, uint32_t) \
     1099    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int8_t, int64_t) \
     1100    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int8_t, uint64_t) \
     1101    /* DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int8_t, half) */ \
     1102    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int8_t, float) \
     1103    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int8_t, double) \
     1104    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int8_t, long double) \
     1105    /* DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int8_t, real) */ \
     1106    \
     1107    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint8_t, int16_t) \
     1108    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint8_t, uint16_t) \
     1109    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint8_t, int32_t) \
     1110    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint8_t, uint32_t) \
     1111    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint8_t, int64_t) \
     1112    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint8_t, uint64_t) \
     1113    /* DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint8_t, half) */ \
     1114    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint8_t, float) \
     1115    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint8_t, double) \
     1116    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint8_t, long double) \
     1117    /* DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint8_t, real) */ \
     1118    \
     1119    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int16_t, uint16_t) \
     1120    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int16_t, int32_t) \
     1121    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int16_t, uint32_t) \
     1122    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int16_t, int64_t) \
     1123    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int16_t, uint64_t) \
     1124    /* DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int16_t, half) */ \
     1125    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int16_t, float) \
     1126    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int16_t, double) \
     1127    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int16_t, long double) \
     1128    /* DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int16_t, real) */ \
     1129    \
     1130    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint16_t, int32_t) \
     1131    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint16_t, uint32_t) \
     1132    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint16_t, int64_t) \
     1133    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint16_t, uint64_t) \
     1134    /* DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint16_t, half) */ \
     1135    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint16_t, float) \
     1136    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint16_t, double) \
     1137    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint16_t, long double) \
     1138    /* DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint16_t, real) */ \
     1139    \
     1140    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int32_t, uint32_t) \
     1141    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int32_t, int64_t) \
     1142    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int32_t, uint64_t) \
     1143    /* DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int32_t, half) */ \
     1144    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int32_t, float) \
     1145    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int32_t, double) \
     1146    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int32_t, long double) \
     1147    /* DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int32_t, real) */ \
     1148    \
     1149    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint32_t, int64_t) \
     1150    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint32_t, uint64_t) \
     1151    /* DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint32_t, half) */ \
     1152    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint32_t, float) \
     1153    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint32_t, double) \
     1154    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint32_t, long double) \
     1155    /* DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint32_t, real) */ \
     1156    \
     1157    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int64_t, uint64_t) \
     1158    /* DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int64_t, half) */ \
     1159    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int64_t, float) \
     1160    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int64_t, double) \
     1161    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int64_t, long double) \
     1162    /* DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, int64_t, real) */ \
     1163    \
     1164    /* DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint64_t, half) */ \
     1165    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint64_t, float) \
     1166    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint64_t, double) \
     1167    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint64_t, long double) \
     1168    /* DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, uint64_t, real) */ \
     1169    \
     1170    /* DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, half, float) */ \
     1171    /* DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, half, double) */ \
     1172    /* DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, half, long double) */ \
     1173    /* DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, half, real) */ \
     1174    \
     1175    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, float, double) \
     1176    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, float, long double) \
     1177    /* DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, float, real) */ \
     1178    \
     1179    DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, double, long double) \
     1180    /* DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, double, real) */ \
     1181    \
     1182    /* DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, long double, real) */
    10781183
    10791184DECLARE_ALL_NONVECTOR_OPS(Cmplx)
     
    10831188DECLARE_ALL_VECTOR_OPS(Vec3)
    10841189DECLARE_ALL_VECTOR_OPS(Vec4)
     1190
     1191DECLARE_ALL_VECTOR_COERCE_OPS(Vec2)
     1192DECLARE_ALL_VECTOR_COERCE_OPS(Vec3)
     1193DECLARE_ALL_VECTOR_COERCE_OPS(Vec4)
    10851194
    10861195#undef DECLARE_VECTOR_TYPEDEFS
Note: See TracChangeset for help on using the changeset viewer.