Changeset 1176


Ignore:
Timestamp:
Mar 27, 2012, 9:53:54 PM (8 years ago)
Author:
sam
Message:

math: add vectors of "half" to vector.h.

Location:
trunk/src/lol/math
Files:
2 edited

Legend:

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

    r1161 r1176  
    3232    inline half(float f) { *this = makefast(f); }
    3333    inline half(double f) { *this = makefast((float)f); }
     34    inline half(long double f) { *this = makefast((float)f); }
    3435
    3536    inline int is_nan() const
     
    5758    inline half &operator =(float f) { return *this = makefast(f); }
    5859    inline half &operator =(double f) { return *this = makefast((float)f); }
    59     inline operator int() const { return (int)tofloat(*this); }
     60    inline half &operator =(long double f) { return *this = makefast((float)f); }
     61    inline operator int8_t() const { return (int8_t)(float)*this; }
     62    inline operator uint8_t() const { return (uint8_t)(float)*this; }
     63    inline operator int16_t() const { return (int16_t)(float)*this; }
     64    inline operator uint16_t() const { return (uint16_t)(float)*this; }
     65    inline operator int32_t() const { return (int32_t)(float)*this; }
     66    inline operator uint32_t() const { return (uint32_t)(float)*this; }
     67    inline operator int64_t() const { return (int64_t)(float)*this; }
     68    inline operator uint64_t() const { return (uint64_t)(float)*this; }
     69
    6070    inline operator float() const { return tofloat(*this); }
     71    inline operator double() const { return tofloat(*this); }
     72    inline operator long double() const { return tofloat(*this); }
    6173
    6274    static float tofloat(half h);
     
    7991    inline half operator -() const { return makebits(bits ^ 0x8000u); }
    8092    inline half operator +() const { return *this; }
    81     inline half &operator +=(float f) { return (*this = (half)(*this + f)); }
    82     inline half &operator -=(float f) { return (*this = (half)(*this - f)); }
    83     inline half &operator *=(float f) { return (*this = (half)(*this * f)); }
    84     inline half &operator /=(float f) { return (*this = (half)(*this / f)); }
    8593    inline half &operator +=(half h) { return (*this = (half)(*this + h)); }
    8694    inline half &operator -=(half h) { return (*this = (half)(*this - h)); }
     
    8896    inline half &operator /=(half h) { return (*this = (half)(*this / h)); }
    8997
    90     inline float operator +(float f) const { return (float)*this + f; }
    91     inline float operator -(float f) const { return (float)*this - f; }
    92     inline float operator *(float f) const { return (float)*this * f; }
    93     inline float operator /(float f) const { return (float)*this / f; }
    9498    inline float operator +(half h) const { return (float)*this + (float)h; }
    9599    inline float operator -(half h) const { return (float)*this - (float)h; }
     
    111115};
    112116
    113 inline float &operator +=(float &f, half h) { return f += (float)h; }
    114 inline float &operator -=(float &f, half h) { return f -= (float)h; }
    115 inline float &operator *=(float &f, half h) { return f *= (float)h; }
    116 inline float &operator /=(float &f, half h) { return f /= (float)h; }
     117#define DECLARE_COERCE_HALF_NUMERIC_OPS(op, type, ret, x2, h2) \
     118    inline ret operator op(type x, half h) { return x2 op h2; } \
     119    inline ret operator op(half h, type x) { return h2 op x2; } \
     120    inline type &operator op##=(type &x, half h) { return x = x op h2; } \
     121    inline half &operator op##=(half &h, type x) { return h = h op x2; }
    117122
    118 inline float operator +(float f, half h) { return f + (float)h; }
    119 inline float operator -(float f, half h) { return f - (float)h; }
    120 inline float operator *(float f, half h) { return f * (float)h; }
    121 inline float operator /(float f, half h) { return f / (float)h; }
     123#define DECLARE_COERCE_HALF_BOOL_OPS(op, type, x2, h2) \
     124    inline bool operator op(type x, half h) { return x2 op h2; } \
     125    inline bool operator op(half h, type x) { return h2 op x2; }
     126
     127#define DECLARE_COERCE_HALF_OPS(type, ret, x2, h2) \
     128    DECLARE_COERCE_HALF_NUMERIC_OPS(+, type, ret, x2, h2) \
     129    DECLARE_COERCE_HALF_NUMERIC_OPS(-, type, ret, x2, h2) \
     130    DECLARE_COERCE_HALF_NUMERIC_OPS(*, type, ret, x2, h2) \
     131    DECLARE_COERCE_HALF_NUMERIC_OPS(/, type, ret, x2, h2) \
     132    \
     133    DECLARE_COERCE_HALF_BOOL_OPS(==, type, x2, h2) \
     134    DECLARE_COERCE_HALF_BOOL_OPS(!=, type, x2, h2) \
     135    DECLARE_COERCE_HALF_BOOL_OPS(>=, type, x2, h2) \
     136    DECLARE_COERCE_HALF_BOOL_OPS(<=, type, x2, h2) \
     137    DECLARE_COERCE_HALF_BOOL_OPS(>, type, x2, h2) \
     138    DECLARE_COERCE_HALF_BOOL_OPS(<, type, x2, h2)
     139
     140#define DECLARE_COERCE_TO_HALF_OPS(type) \
     141    DECLARE_COERCE_HALF_OPS(type, half, (half)(int)x, h)
     142
     143#define DECLARE_COERCE_FROM_HALF_OPS(type) \
     144    DECLARE_COERCE_HALF_OPS(type, type, x, (type)h)
     145
     146DECLARE_COERCE_TO_HALF_OPS(int8_t)
     147DECLARE_COERCE_TO_HALF_OPS(uint8_t)
     148DECLARE_COERCE_TO_HALF_OPS(int16_t)
     149DECLARE_COERCE_TO_HALF_OPS(uint16_t)
     150DECLARE_COERCE_TO_HALF_OPS(int32_t)
     151DECLARE_COERCE_TO_HALF_OPS(uint32_t)
     152DECLARE_COERCE_TO_HALF_OPS(int64_t)
     153DECLARE_COERCE_TO_HALF_OPS(uint64_t)
     154
     155DECLARE_COERCE_FROM_HALF_OPS(float)
     156DECLARE_COERCE_FROM_HALF_OPS(double)
     157DECLARE_COERCE_FROM_HALF_OPS(long double)
     158
     159#undef DECLARE_COERCE_HALF_NUMERIC_OPS
     160#undef DECLARE_COERCE_HALF_OPS
     161#undef DECLARE_COERCE_TO_HALF_OPS
     162#undef DECLARE_COERCE_FROM_HALF_OPS
    122163
    123164} /* namespace lol */
  • trunk/src/lol/math/vector.h

    r1175 r1176  
    11241124    DECLARE_VEC_3_COERCE_OPS(Vec3, static, type, type, type)
    11251125
    1126 #define DECLARE_ALL_VECTOR_COERCE_OPS_INNER(tname, tlow, thigh) \
     1126#define DECLARE_VEC_ANY_COERCE_OPS(tname, tlow, thigh) \
    11271127    DECLARE_BINARY_COERCE_OPS(tname, static, tlow, thigh, thigh) \
    11281128    DECLARE_BINARY_COERCE_OPS(tname, static, thigh, tlow, thigh) \
     
    11321132
    11331133#define DECLARE_ALL_VECTOR_COERCE_OPS(tlow, thigh) \
    1134     DECLARE_ALL_VECTOR_COERCE_OPS_INNER(Vec2, tlow, thigh) \
    1135     DECLARE_ALL_VECTOR_COERCE_OPS_INNER(Vec3, tlow, thigh) \
    1136     DECLARE_ALL_VECTOR_COERCE_OPS_INNER(Vec4, tlow, thigh) \
     1134    DECLARE_VEC_ANY_COERCE_OPS(Vec2, tlow, thigh) \
     1135    DECLARE_VEC_ANY_COERCE_OPS(Vec3, tlow, thigh) \
     1136    DECLARE_VEC_ANY_COERCE_OPS(Vec4, tlow, thigh) \
    11371137    \
    11381138    DECLARE_VEC_3_COERCE_OPS(Vec3, static, tlow, thigh, thigh) \
    11391139    DECLARE_VEC_3_COERCE_OPS(Vec3, static, thigh, tlow, thigh)
     1140
     1141DECLARE_ALL_NONVECTOR_OPS(Cmplx)
     1142DECLARE_ALL_NONVECTOR_OPS(Quat)
    11401143
    11411144/* Disable warning about unary operator applied to unsigned type */
     
    11441147#   pragma warning(disable: 4146)
    11451148#endif
    1146 
    1147 DECLARE_ALL_NONVECTOR_OPS(Cmplx)
    1148 DECLARE_ALL_NONVECTOR_OPS(Quat)
    11491149
    11501150DECLARE_ALL_VECTOR_OPS(half)
     
    11791179#endif
    11801180
    1181 /* Apply the same coercion rules as in the C++ standard. However,   */
    1182 /* instead of promoting int8_t etc. to int, we apply our own rules. */
    1183 /* FIXME: "half" and "real" are deactivated for now, because we do  */
    1184 /* not implement all combinations of operators for these types yet. */
     1181/* Apply the same coercion rules as in the C++ standard. However, instead
     1182 * of always promoting smaller types to int, we allow int8_t op int16_t to
     1183 * return an int16_t. */
    11851184DECLARE_ALL_VECTOR_COERCE_OPS(int8_t, uint8_t)
    11861185DECLARE_ALL_VECTOR_COERCE_OPS(int8_t, int16_t)
     
    11901189DECLARE_ALL_VECTOR_COERCE_OPS(int8_t, int64_t)
    11911190DECLARE_ALL_VECTOR_COERCE_OPS(int8_t, uint64_t)
    1192 /* DECLARE_ALL_VECTOR_COERCE_OPS(int8_t, half) */
    11931191DECLARE_ALL_VECTOR_COERCE_OPS(int8_t, float)
    11941192DECLARE_ALL_VECTOR_COERCE_OPS(int8_t, double)
    11951193DECLARE_ALL_VECTOR_COERCE_OPS(int8_t, long double)
    1196 /* DECLARE_ALL_VECTOR_COERCE_OPS(int8_t, real) */
    11971194
    11981195DECLARE_ALL_VECTOR_COERCE_OPS(uint8_t, int16_t)
     
    12021199DECLARE_ALL_VECTOR_COERCE_OPS(uint8_t, int64_t)
    12031200DECLARE_ALL_VECTOR_COERCE_OPS(uint8_t, uint64_t)
    1204 /* DECLARE_ALL_VECTOR_COERCE_OPS(uint8_t, half) */
    12051201DECLARE_ALL_VECTOR_COERCE_OPS(uint8_t, float)
    12061202DECLARE_ALL_VECTOR_COERCE_OPS(uint8_t, double)
    12071203DECLARE_ALL_VECTOR_COERCE_OPS(uint8_t, long double)
    1208 /* DECLARE_ALL_VECTOR_COERCE_OPS(uint8_t, real) */
    12091204
    12101205DECLARE_ALL_VECTOR_COERCE_OPS(int16_t, uint16_t)
     
    12131208DECLARE_ALL_VECTOR_COERCE_OPS(int16_t, int64_t)
    12141209DECLARE_ALL_VECTOR_COERCE_OPS(int16_t, uint64_t)
    1215 /* DECLARE_ALL_VECTOR_COERCE_OPS(int16_t, half) */
    12161210DECLARE_ALL_VECTOR_COERCE_OPS(int16_t, float)
    12171211DECLARE_ALL_VECTOR_COERCE_OPS(int16_t, double)
    12181212DECLARE_ALL_VECTOR_COERCE_OPS(int16_t, long double)
    1219 /* DECLARE_ALL_VECTOR_COERCE_OPS(int16_t, real) */
    12201213
    12211214DECLARE_ALL_VECTOR_COERCE_OPS(uint16_t, int32_t)
     
    12231216DECLARE_ALL_VECTOR_COERCE_OPS(uint16_t, int64_t)
    12241217DECLARE_ALL_VECTOR_COERCE_OPS(uint16_t, uint64_t)
    1225 /* DECLARE_ALL_VECTOR_COERCE_OPS(uint16_t, half) */
    12261218DECLARE_ALL_VECTOR_COERCE_OPS(uint16_t, float)
    12271219DECLARE_ALL_VECTOR_COERCE_OPS(uint16_t, double)
    12281220DECLARE_ALL_VECTOR_COERCE_OPS(uint16_t, long double)
    1229 /* DECLARE_ALL_VECTOR_COERCE_OPS(uint16_t, real) */
    12301221
    12311222DECLARE_ALL_VECTOR_COERCE_OPS(int32_t, uint32_t)
    12321223DECLARE_ALL_VECTOR_COERCE_OPS(int32_t, int64_t)
    12331224DECLARE_ALL_VECTOR_COERCE_OPS(int32_t, uint64_t)
    1234 /* DECLARE_ALL_VECTOR_COERCE_OPS(int32_t, half) */
    12351225DECLARE_ALL_VECTOR_COERCE_OPS(int32_t, float)
    12361226DECLARE_ALL_VECTOR_COERCE_OPS(int32_t, double)
    12371227DECLARE_ALL_VECTOR_COERCE_OPS(int32_t, long double)
    1238 /* DECLARE_ALL_VECTOR_COERCE_OPS(int32_t, real) */
    12391228
    12401229DECLARE_ALL_VECTOR_COERCE_OPS(uint32_t, int64_t)
    12411230DECLARE_ALL_VECTOR_COERCE_OPS(uint32_t, uint64_t)
    1242 /* DECLARE_ALL_VECTOR_COERCE_OPS(uint32_t, half) */
    12431231DECLARE_ALL_VECTOR_COERCE_OPS(uint32_t, float)
    12441232DECLARE_ALL_VECTOR_COERCE_OPS(uint32_t, double)
    12451233DECLARE_ALL_VECTOR_COERCE_OPS(uint32_t, long double)
    1246 /* DECLARE_ALL_VECTOR_COERCE_OPS(uint32_t, real) */
    12471234
    12481235DECLARE_ALL_VECTOR_COERCE_OPS(int64_t, uint64_t)
    1249 /* DECLARE_ALL_VECTOR_COERCE_OPS(int64_t, half) */
    12501236DECLARE_ALL_VECTOR_COERCE_OPS(int64_t, float)
    12511237DECLARE_ALL_VECTOR_COERCE_OPS(int64_t, double)
    12521238DECLARE_ALL_VECTOR_COERCE_OPS(int64_t, long double)
    1253 /* DECLARE_ALL_VECTOR_COERCE_OPS(int64_t, real) */
    1254 
    1255 /* DECLARE_ALL_VECTOR_COERCE_OPS(uint64_t, half) */
     1239
    12561240DECLARE_ALL_VECTOR_COERCE_OPS(uint64_t, float)
    12571241DECLARE_ALL_VECTOR_COERCE_OPS(uint64_t, double)
    12581242DECLARE_ALL_VECTOR_COERCE_OPS(uint64_t, long double)
    1259 /* DECLARE_ALL_VECTOR_COERCE_OPS(uint64_t, real) */
    1260 
    1261 /* DECLARE_ALL_VECTOR_COERCE_OPS(half, float) */
    1262 /* DECLARE_ALL_VECTOR_COERCE_OPS(half, double) */
    1263 /* DECLARE_ALL_VECTOR_COERCE_OPS(half, long double) */
    1264 /* DECLARE_ALL_VECTOR_COERCE_OPS(half, real) */
    12651243
    12661244DECLARE_ALL_VECTOR_COERCE_OPS(float, double)
    12671245DECLARE_ALL_VECTOR_COERCE_OPS(float, long double)
    1268 /* DECLARE_ALL_VECTOR_COERCE_OPS(float, real) */
    12691246
    12701247DECLARE_ALL_VECTOR_COERCE_OPS(double, long double)
    1271 /* DECLARE_ALL_VECTOR_COERCE_OPS(double, real) */
    1272 
    1273 /* DECLARE_ALL_VECTOR_COERCE_OPS(long double, real) */
     1248
     1249/* All integer types are promoted to half; all floating point types
     1250 * cause half to be promoted. */
     1251DECLARE_ALL_VECTOR_COERCE_OPS(int8_t, half)
     1252DECLARE_ALL_VECTOR_COERCE_OPS(uint8_t, half)
     1253DECLARE_ALL_VECTOR_COERCE_OPS(int16_t, half)
     1254DECLARE_ALL_VECTOR_COERCE_OPS(uint16_t, half)
     1255DECLARE_ALL_VECTOR_COERCE_OPS(int32_t, half)
     1256DECLARE_ALL_VECTOR_COERCE_OPS(uint32_t, half)
     1257DECLARE_ALL_VECTOR_COERCE_OPS(int64_t, half)
     1258DECLARE_ALL_VECTOR_COERCE_OPS(uint64_t, half)
     1259
     1260DECLARE_ALL_VECTOR_COERCE_OPS(half, float)
     1261DECLARE_ALL_VECTOR_COERCE_OPS(half, double)
     1262DECLARE_ALL_VECTOR_COERCE_OPS(half, long double)
     1263
     1264/* FIXME: vectors of "real" are deactivated for now, because we do
     1265 * not implement all combinations of operators for these types yet. */
     1266
     1267#if 0
     1268/* All types are promoted to real */
     1269DECLARE_ALL_VECTOR_COERCE_OPS(int8_t, real)
     1270DECLARE_ALL_VECTOR_COERCE_OPS(uint8_t, real)
     1271DECLARE_ALL_VECTOR_COERCE_OPS(int16_t, real)
     1272DECLARE_ALL_VECTOR_COERCE_OPS(uint16_t, real)
     1273DECLARE_ALL_VECTOR_COERCE_OPS(int32_t, real)
     1274DECLARE_ALL_VECTOR_COERCE_OPS(uint32_t, real)
     1275DECLARE_ALL_VECTOR_COERCE_OPS(int64_t, real)
     1276DECLARE_ALL_VECTOR_COERCE_OPS(uint64_t, real)
     1277DECLARE_ALL_VECTOR_COERCE_OPS(half, real)
     1278DECLARE_ALL_VECTOR_COERCE_OPS(float, real)
     1279DECLARE_ALL_VECTOR_COERCE_OPS(double, real)
     1280DECLARE_ALL_VECTOR_COERCE_OPS(long double, real)
     1281#endif
    12741282
    12751283#if defined __GNUC__ && (__GNUC__ >= 4)
Note: See TracChangeset for help on using the changeset viewer.