Changeset 871
 Timestamp:
 Aug 28, 2011, 11:07:12 PM (8 years ago)
 Location:
 trunk
 Files:

 3 edited
Legend:
 Unmodified
 Added
 Removed

trunk/src/core.h
r867 r871 18 18 19 19 // Base types 20 #include "half.h" 20 21 #include "matrix.h" 21 22 #include "numeric.h" 22 #include "half.h"23 23 #include "timer.h" 24 24 
trunk/src/half.h
r869 r871 64 64 inline operator int() const { return (int)(float)*this; } 65 65 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 66 86 /* Factories */ 67 87 static half makeslow(float f); … … 75 95 }; 76 96 97 inline float &operator +=(float &f, half h) { return f += (float)h; } 98 inline float &operator =(float &f, half h) { return f = (float)h; } 99 inline float &operator *=(float &f, half h) { return f *= (float)h; } 100 inline float &operator /=(float &f, half h) { return f /= (float)h; } 101 102 inline float operator +(float f, half h) { return f + (float)h; } 103 inline float operator (float f, half h) { return f  (float)h; } 104 inline float operator *(float f, half h) { return f * (float)h; } 105 inline float operator /(float f, half h) { return f / (float)h; } 106 77 107 } /* namespace lol */ 78 108 
trunk/test/half.cpp
r870 r871 36 36 CPPUNIT_TEST(test_half_to_float); 37 37 CPPUNIT_TEST(test_half_to_int); 38 CPPUNIT_TEST(test_float_op_half); 39 CPPUNIT_TEST(test_half_op_float); 38 40 CPPUNIT_TEST_SUITE_END(); 39 41 … … 206 208 CPPUNIT_ASSERT_EQUAL((int)(half)(65504.0f), 65504); 207 209 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()); 208 293 } 209 294
Note: See TracChangeset
for help on using the changeset viewer.