Changeset 1153
- Timestamp:
- Mar 5, 2012, 8:06:40 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lol/math/vector.h
r1151 r1153 926 926 */ 927 927 928 #define DECLARE_VECTOR_VECTOR_ OP(tname, op, tprefix, type) \928 #define DECLARE_VECTOR_VECTOR_COERCE_OP(tname, op, tprefix, t1, t2, tf) \ 929 929 tprefix \ 930 static inline tname<t ype> 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) \ 932 932 { \ 933 tname<t ype> ret; \934 for (size_t n = 0; n < sizeof(a) / sizeof(t ype); n++) \933 tname<tf> ret; \ 934 for (size_t n = 0; n < sizeof(a) / sizeof(t1); n++) \ 935 935 ret[n] = a[n] op b[n]; \ 936 936 return ret; \ 937 } \ 938 \ 937 } 938 939 #define DECLARE_VECTOR_VECTOR_OP(tname, op, tprefix, type) \ 939 940 tprefix \ 940 941 static inline tname<type> operator op##=(tname<type> &a, \ … … 944 945 } 945 946 946 #define DECLARE_VECTOR_VECTOR_BOOLOP(tname, op, op2, ret, tprefix, t ype) \947 #define DECLARE_VECTOR_VECTOR_BOOLOP(tname, op, op2, ret, tprefix, t1, t2) \ 947 948 tprefix \ 948 static inline bool operator op(tname<t ype> const &a, \949 tname<t ype> const &b) \949 static inline bool operator op(tname<t1> const &a, \ 950 tname<t2> const &b) \ 950 951 { \ 951 for (size_t n = 0; n < sizeof(a) / sizeof(t ype); n++) \952 for (size_t n = 0; n < sizeof(a) / sizeof(t1); n++) \ 952 953 if (!(a[n] op2 b[n])) \ 953 954 return !ret; \ … … 955 956 } 956 957 957 #define DECLARE_VECTOR_SCALAR_ OP(tname, op, tprefix, type) \958 #define DECLARE_VECTOR_SCALAR_COERCE_OP(tname, op, tprefix, t1, t2, tf) \ 958 959 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) \ 961 961 { \ 962 tname<t ype> ret; \963 for (size_t n = 0; n < sizeof(a) / sizeof(t ype); n++) \962 tname<tf> ret; \ 963 for (size_t n = 0; n < sizeof(a) / sizeof(t1); n++) \ 964 964 ret[n] = a[n] op val; \ 965 965 return ret; \ … … 967 967 \ 968 968 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) \ 971 970 { \ 972 tname<t ype> ret; \973 for (size_t n = 0; n < sizeof(a) / sizeof(t ype); n++) \971 tname<tf> ret; \ 972 for (size_t n = 0; n < sizeof(a) / sizeof(t2); n++) \ 974 973 ret[n] = a[n] op val; \ 975 974 return ret; \ 976 } \ 977 \ 975 } 976 977 #define DECLARE_VECTOR_SCALAR_OP(tname, op, tprefix, type) \ 978 978 tprefix \ 979 979 static inline tname<type> operator op##=(tname<type> &a, type const &val) \ 980 980 { \ 981 981 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; \994 982 } 995 983 … … 1027 1015 } 1028 1016 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 1029 1036 #define DECLARE_BINARY_OPS(tname, tprefix, type) \ 1037 DECLARE_BINARY_COERCE_OPS(tname, tprefix, type, type, type) \ 1038 \ 1030 1039 DECLARE_VECTOR_SCALAR_OP(tname, *, tprefix, type) \ 1031 1040 DECLARE_VECTOR_SCALAR_OP(tname, /, tprefix, type) \ 1032 1041 \ 1033 1042 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) 1047 1053 1048 1054 #define DECLARE_VECTOR_OPS(tname, tprefix, type) \ 1055 DECLARE_VECTOR_COERCE_OPS(tname, /* empty */, type, type, type) \ 1056 \ 1049 1057 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) 1056 1059 1057 1060 #define DECLARE_ALL_NONVECTOR_OPS(tname) \ … … 1059 1062 DECLARE_UNARY_OPS(tname, template<typename T>, T) 1060 1063 1061 #define DECLARE_ALL_VECTOR_OPS_INNER(tname, t prefix, 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) \ 1065 1068 1066 1069 #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) */ 1078 1183 1079 1184 DECLARE_ALL_NONVECTOR_OPS(Cmplx) … … 1083 1188 DECLARE_ALL_VECTOR_OPS(Vec3) 1084 1189 DECLARE_ALL_VECTOR_OPS(Vec4) 1190 1191 DECLARE_ALL_VECTOR_COERCE_OPS(Vec2) 1192 DECLARE_ALL_VECTOR_COERCE_OPS(Vec3) 1193 DECLARE_ALL_VECTOR_COERCE_OPS(Vec4) 1085 1194 1086 1195 #undef DECLARE_VECTOR_TYPEDEFS
Note: See TracChangeset
for help on using the changeset viewer.