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

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

File:
1 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 */
Note: See TracChangeset for help on using the changeset viewer.