Changeset 871


Ignore:
Timestamp:
Aug 28, 2011, 11:07:12 PM (8 years ago)
Author:
sam
Message:

core: implement new half / float operations and the associated tests.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core.h

    r867 r871  
    1818
    1919// Base types
     20#include "half.h"
    2021#include "matrix.h"
    2122#include "numeric.h"
    22 #include "half.h"
    2323#include "timer.h"
    2424
  • trunk/src/half.h

    r869 r871  
    6464    inline operator int() const { return (int)(float)*this; }
    6565
     66    /* Operations */
     67    inline half operator -() { return makebits(m_bits ^ 0x8000u); }
     68    inline half &operator +=(float f) { return (*this = (half)(*this + f)); }
     69    inline half &operator -=(float f) { return (*this = (half)(*this - f)); }
     70    inline half &operator *=(float f) { return (*this = (half)(*this * f)); }
     71    inline half &operator /=(float f) { return (*this = (half)(*this / f)); }
     72    inline half &operator +=(half h) { return (*this = (half)(*this + h)); }
     73    inline half &operator -=(half h) { return (*this = (half)(*this - h)); }
     74    inline half &operator *=(half h) { return (*this = (half)(*this * h)); }
     75    inline half &operator /=(half h) { return (*this = (half)(*this / h)); }
     76
     77    inline float operator +(float f) const { return (float)*this + f; }
     78    inline float operator -(float f) const { return (float)*this - f; }
     79    inline float operator *(float f) const { return (float)*this * f; }
     80    inline float operator /(float f) const { return (float)*this / f; }
     81    inline float operator +(half h) const { return (float)*this + (float)h; }
     82    inline float operator -(half h) const { return (float)*this - (float)h; }
     83    inline float operator *(half h) const { return (float)*this * (float)h; }
     84    inline float operator /(half h) const { return (float)*this / (float)h; }
     85
    6686    /* Factories */
    6787    static half makeslow(float f);
     
    7595};
    7696
     97inline float &operator +=(float &f, half h) { return f += (float)h; }
     98inline float &operator -=(float &f, half h) { return f -= (float)h; }
     99inline float &operator *=(float &f, half h) { return f *= (float)h; }
     100inline float &operator /=(float &f, half h) { return f /= (float)h; }
     101
     102inline float operator +(float f, half h) { return f + (float)h; }
     103inline float operator -(float f, half h) { return f - (float)h; }
     104inline float operator *(float f, half h) { return f * (float)h; }
     105inline float operator /(float f, half h) { return f / (float)h; }
     106
    77107} /* namespace lol */
    78108
  • trunk/test/half.cpp

    r870 r871  
    3636    CPPUNIT_TEST(test_half_to_float);
    3737    CPPUNIT_TEST(test_half_to_int);
     38    CPPUNIT_TEST(test_float_op_half);
     39    CPPUNIT_TEST(test_half_op_float);
    3840    CPPUNIT_TEST_SUITE_END();
    3941
     
    206208        CPPUNIT_ASSERT_EQUAL((int)(half)(65504.0f), 65504);
    207209        CPPUNIT_ASSERT_EQUAL((int)(half)(-65504.0f), -65504);
     210    }
     211
     212    void test_float_op_half()
     213    {
     214        half zero = 0;
     215        half one = 1;
     216        half two = 2;
     217
     218        float a = zero + one;
     219        CPPUNIT_ASSERT_EQUAL(1.0f, a);
     220        a += zero;
     221        CPPUNIT_ASSERT_EQUAL(1.0f, a);
     222        a -= zero;
     223        CPPUNIT_ASSERT_EQUAL(1.0f, a);
     224        a *= one;
     225        CPPUNIT_ASSERT_EQUAL(1.0f, a);
     226        a /= one;
     227        CPPUNIT_ASSERT_EQUAL(1.0f, a);
     228
     229        float b = one + zero;
     230        CPPUNIT_ASSERT_EQUAL(1.0f, b);
     231        b += one;
     232        CPPUNIT_ASSERT_EQUAL(2.0f, b);
     233        b *= two;
     234        CPPUNIT_ASSERT_EQUAL(4.0f, b);
     235        b -= two;
     236        CPPUNIT_ASSERT_EQUAL(2.0f, b);
     237        b /= two;
     238        CPPUNIT_ASSERT_EQUAL(1.0f, b);
     239
     240        float c = one - zero;
     241        CPPUNIT_ASSERT_EQUAL(1.0f, c);
     242
     243        float d = two - one;
     244        CPPUNIT_ASSERT_EQUAL(1.0f, d);
     245
     246        float e = two + (-one);
     247        CPPUNIT_ASSERT_EQUAL(1.0f, e);
     248
     249        float f = (two * two) / (one + one);
     250        CPPUNIT_ASSERT_EQUAL(2.0f, f);
     251    }
     252
     253    void test_half_op_float()
     254    {
     255        half zero = 0;
     256        half one = 1;
     257        half two = 2;
     258        half four = 4;
     259
     260        half a = one + 0.0f;
     261        CPPUNIT_ASSERT_EQUAL(one.bits(), a.bits());
     262        a += 0.0f;
     263        CPPUNIT_ASSERT_EQUAL(one.bits(), a.bits());
     264        a -= 0.0f;
     265        CPPUNIT_ASSERT_EQUAL(one.bits(), a.bits());
     266        a *= 1.0f;
     267        CPPUNIT_ASSERT_EQUAL(one.bits(), a.bits());
     268        a /= 1.0f;
     269        CPPUNIT_ASSERT_EQUAL(one.bits(), a.bits());
     270
     271        half b = one + 0.0f;
     272        CPPUNIT_ASSERT_EQUAL(one.bits(), b.bits());
     273        b += 1.0f;
     274        CPPUNIT_ASSERT_EQUAL(two.bits(), b.bits());
     275        b *= 2.0f;
     276        CPPUNIT_ASSERT_EQUAL(four.bits(), b.bits());
     277        b -= 2.0f;
     278        CPPUNIT_ASSERT_EQUAL(two.bits(), b.bits());
     279        b /= 2.0f;
     280        CPPUNIT_ASSERT_EQUAL(one.bits(), b.bits());
     281
     282        half c = 1.0f - zero;
     283        CPPUNIT_ASSERT_EQUAL(one.bits(), c.bits());
     284
     285        half d = 2.0f - one;
     286        CPPUNIT_ASSERT_EQUAL(one.bits(), d.bits());
     287
     288        half e = 2.0f + (-one);
     289        CPPUNIT_ASSERT_EQUAL(one.bits(), e.bits());
     290
     291        half f = (2.0f * two) / (1.0f + one);
     292        CPPUNIT_ASSERT_EQUAL(two.bits(), f.bits());
    208293    }
    209294
Note: See TracChangeset for help on using the changeset viewer.