[868] | 1 | // |
---|
| 2 | // Lol Engine |
---|
| 3 | // |
---|
| 4 | // Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net> |
---|
| 5 | // This program is free software; you can redistribute it and/or |
---|
| 6 | // modify it under the terms of the Do What The Fuck You Want To |
---|
| 7 | // Public License, Version 2, as published by Sam Hocevar. See |
---|
| 8 | // http://sam.zoy.org/projects/COPYING.WTFPL for more details. |
---|
| 9 | // |
---|
| 10 | |
---|
| 11 | #if defined HAVE_CONFIG_H |
---|
| 12 | # include "config.h" |
---|
| 13 | #endif |
---|
| 14 | |
---|
[875] | 15 | #include <cmath> |
---|
| 16 | |
---|
[868] | 17 | #include "core.h" |
---|
[912] | 18 | #include "lol/unit.h" |
---|
[868] | 19 | |
---|
[887] | 20 | /* Ensure isnan() is present even on systems that don't define it, or |
---|
| 21 | * when -ffast-math is being used. */ |
---|
| 22 | #if defined __FAST_MATH__ |
---|
| 23 | # undef isnan |
---|
| 24 | #endif |
---|
[875] | 25 | #if !defined isnan |
---|
[887] | 26 | static inline int isnan(float f) |
---|
| 27 | { |
---|
| 28 | union { float f; uint32_t x; } u = { f }; |
---|
[892] | 29 | return (u.x << 1) > 0xff000000u; |
---|
[887] | 30 | } |
---|
[875] | 31 | #endif |
---|
| 32 | |
---|
[868] | 33 | namespace lol |
---|
| 34 | { |
---|
| 35 | |
---|
[912] | 36 | LOLUNIT_FIXTURE(HalfTest) |
---|
[868] | 37 | { |
---|
| 38 | public: |
---|
[912] | 39 | void setUp() {} |
---|
[868] | 40 | void tearDown() {} |
---|
| 41 | |
---|
[912] | 42 | LOLUNIT_TEST(test_half_from_float) |
---|
[868] | 43 | { |
---|
[877] | 44 | for (size_t i = 0; i < sizeof(pairs) / sizeof(*pairs); i++) |
---|
[868] | 45 | { |
---|
[877] | 46 | half a = (half)pairs[i].f; |
---|
| 47 | uint16_t b = pairs[i].x; |
---|
[912] | 48 | LOLUNIT_ASSERT_EQUAL(a.bits, b); |
---|
[868] | 49 | } |
---|
| 50 | } |
---|
| 51 | |
---|
[912] | 52 | LOLUNIT_TEST(test_half_makeaccurate) |
---|
[868] | 53 | { |
---|
[870] | 54 | for (size_t i = 0; i < sizeof(pairs) / sizeof(*pairs); i++) |
---|
[868] | 55 | { |
---|
[879] | 56 | half a = half::makeaccurate(pairs[i].f); |
---|
[870] | 57 | uint16_t b = pairs[i].x; |
---|
[912] | 58 | LOLUNIT_ASSERT_EQUAL(a.bits, b); |
---|
[868] | 59 | } |
---|
| 60 | } |
---|
| 61 | |
---|
[912] | 62 | LOLUNIT_TEST(test_half_makebits) |
---|
[868] | 63 | { |
---|
[877] | 64 | for (unsigned int i = 0; i < 0x10000; i++) |
---|
[868] | 65 | { |
---|
[877] | 66 | half a = half::makebits(i); |
---|
| 67 | uint16_t b = i; |
---|
[912] | 68 | LOLUNIT_ASSERT_EQUAL(a.bits, b); |
---|
[868] | 69 | } |
---|
| 70 | } |
---|
| 71 | |
---|
[912] | 72 | LOLUNIT_TEST(test_half_is_nan) |
---|
[870] | 73 | { |
---|
[912] | 74 | LOLUNIT_ASSERT(half::makebits(0x7c01).is_nan()); |
---|
| 75 | LOLUNIT_ASSERT(half::makebits(0xfc01).is_nan()); |
---|
| 76 | LOLUNIT_ASSERT(half::makebits(0x7e00).is_nan()); |
---|
| 77 | LOLUNIT_ASSERT(half::makebits(0xfe00).is_nan()); |
---|
[870] | 78 | |
---|
[912] | 79 | LOLUNIT_ASSERT(!half::makebits(0x7c00).is_nan()); |
---|
| 80 | LOLUNIT_ASSERT(!half::makebits(0xfc00).is_nan()); |
---|
[870] | 81 | |
---|
[912] | 82 | LOLUNIT_ASSERT(!half(0.0f).is_nan()); |
---|
| 83 | LOLUNIT_ASSERT(!half(-0.0f).is_nan()); |
---|
| 84 | LOLUNIT_ASSERT(!half(2.0f).is_nan()); |
---|
| 85 | LOLUNIT_ASSERT(!half(-2.0f).is_nan()); |
---|
[870] | 86 | } |
---|
| 87 | |
---|
[912] | 88 | LOLUNIT_TEST(test_half_is_inf) |
---|
[870] | 89 | { |
---|
[912] | 90 | LOLUNIT_ASSERT(half(65536.0f).is_inf()); |
---|
| 91 | LOLUNIT_ASSERT(half(-65536.0f).is_inf()); |
---|
[870] | 92 | |
---|
[912] | 93 | LOLUNIT_ASSERT(!half(0.0f).is_inf()); |
---|
| 94 | LOLUNIT_ASSERT(!half(-0.0f).is_inf()); |
---|
| 95 | LOLUNIT_ASSERT(!half(65535.0f).is_inf()); |
---|
| 96 | LOLUNIT_ASSERT(!half(-65535.0f).is_inf()); |
---|
[870] | 97 | |
---|
[912] | 98 | LOLUNIT_ASSERT(half::makebits(0x7c00).is_inf()); |
---|
| 99 | LOLUNIT_ASSERT(half::makebits(0xfc00).is_inf()); |
---|
[870] | 100 | |
---|
[912] | 101 | LOLUNIT_ASSERT(!half::makebits(0x7e00).is_inf()); |
---|
| 102 | LOLUNIT_ASSERT(!half::makebits(0xfe00).is_inf()); |
---|
[870] | 103 | } |
---|
| 104 | |
---|
[912] | 105 | LOLUNIT_TEST(test_half_is_finite) |
---|
[870] | 106 | { |
---|
[912] | 107 | LOLUNIT_ASSERT(half(0.0f).is_finite()); |
---|
| 108 | LOLUNIT_ASSERT(half(-0.0f).is_finite()); |
---|
| 109 | LOLUNIT_ASSERT(half(65535.0f).is_finite()); |
---|
| 110 | LOLUNIT_ASSERT(half(-65535.0f).is_finite()); |
---|
[870] | 111 | |
---|
[912] | 112 | LOLUNIT_ASSERT(!half(65536.0f).is_finite()); |
---|
| 113 | LOLUNIT_ASSERT(!half(-65536.0f).is_finite()); |
---|
[870] | 114 | |
---|
[912] | 115 | LOLUNIT_ASSERT(!half::makebits(0x7c00).is_finite()); |
---|
| 116 | LOLUNIT_ASSERT(!half::makebits(0xfc00).is_finite()); |
---|
[870] | 117 | |
---|
[912] | 118 | LOLUNIT_ASSERT(!half::makebits(0x7e00).is_finite()); |
---|
| 119 | LOLUNIT_ASSERT(!half::makebits(0xfe00).is_finite()); |
---|
[870] | 120 | } |
---|
| 121 | |
---|
[912] | 122 | LOLUNIT_TEST(test_half_is_normal) |
---|
[870] | 123 | { |
---|
[912] | 124 | LOLUNIT_ASSERT(half(0.0f).is_normal()); |
---|
| 125 | LOLUNIT_ASSERT(half(-0.0f).is_normal()); |
---|
| 126 | LOLUNIT_ASSERT(half(65535.0f).is_normal()); |
---|
| 127 | LOLUNIT_ASSERT(half(-65535.0f).is_normal()); |
---|
[870] | 128 | |
---|
[912] | 129 | LOLUNIT_ASSERT(!half(65536.0f).is_normal()); |
---|
| 130 | LOLUNIT_ASSERT(!half(-65536.0f).is_normal()); |
---|
[870] | 131 | |
---|
[912] | 132 | LOLUNIT_ASSERT(!half::makebits(0x7c00).is_normal()); |
---|
| 133 | LOLUNIT_ASSERT(!half::makebits(0xfc00).is_normal()); |
---|
[870] | 134 | |
---|
[912] | 135 | LOLUNIT_ASSERT(!half::makebits(0x7e00).is_normal()); |
---|
| 136 | LOLUNIT_ASSERT(!half::makebits(0xfe00).is_normal()); |
---|
[870] | 137 | } |
---|
| 138 | |
---|
[912] | 139 | LOLUNIT_TEST(test_half_classify) |
---|
[870] | 140 | { |
---|
| 141 | for (uint32_t i = 0; i < 0x10000; i++) |
---|
| 142 | { |
---|
| 143 | half h = half::makebits(i); |
---|
[874] | 144 | if (h.is_nan()) |
---|
[870] | 145 | { |
---|
[912] | 146 | LOLUNIT_ASSERT(!h.is_inf()); |
---|
| 147 | LOLUNIT_ASSERT(!h.is_normal()); |
---|
| 148 | LOLUNIT_ASSERT(!h.is_finite()); |
---|
[870] | 149 | } |
---|
[874] | 150 | else if (h.is_inf()) |
---|
[870] | 151 | { |
---|
[912] | 152 | LOLUNIT_ASSERT(!h.is_normal()); |
---|
| 153 | LOLUNIT_ASSERT(!h.is_finite()); |
---|
[870] | 154 | } |
---|
| 155 | else |
---|
| 156 | { |
---|
[912] | 157 | LOLUNIT_ASSERT(h.is_finite()); |
---|
[870] | 158 | } |
---|
| 159 | } |
---|
| 160 | } |
---|
| 161 | |
---|
[912] | 162 | LOLUNIT_TEST(test_half_to_float) |
---|
[868] | 163 | { |
---|
[870] | 164 | for (size_t i = 0; i < sizeof(pairs) / sizeof(*pairs); i++) |
---|
[868] | 165 | { |
---|
[870] | 166 | float a = (float)half::makebits(pairs[i].x); |
---|
| 167 | float b = pairs[i].f; |
---|
[912] | 168 | LOLUNIT_ASSERT_EQUAL(a, b); |
---|
[868] | 169 | } |
---|
[870] | 170 | |
---|
| 171 | for (uint32_t i = 0; i < 0x10000; i++) |
---|
| 172 | { |
---|
| 173 | half h = half::makebits(i); |
---|
| 174 | float f = (float)h; |
---|
| 175 | half g = (half)f; |
---|
[874] | 176 | if (h.is_nan()) |
---|
[870] | 177 | { |
---|
[912] | 178 | LOLUNIT_ASSERT(isnan(f)); |
---|
| 179 | LOLUNIT_ASSERT(g.is_nan()); |
---|
[870] | 180 | } |
---|
| 181 | else |
---|
| 182 | { |
---|
[912] | 183 | LOLUNIT_ASSERT(!isnan(f)); |
---|
| 184 | LOLUNIT_ASSERT_EQUAL(g.bits, h.bits); |
---|
[870] | 185 | } |
---|
| 186 | } |
---|
[868] | 187 | } |
---|
[870] | 188 | |
---|
[912] | 189 | LOLUNIT_TEST(test_half_to_int) |
---|
[870] | 190 | { |
---|
[912] | 191 | LOLUNIT_ASSERT_EQUAL((int)(half)(0.0f), 0); |
---|
| 192 | LOLUNIT_ASSERT_EQUAL((int)(half)(-0.0f), 0); |
---|
| 193 | LOLUNIT_ASSERT_EQUAL((int)(half)(0.9f), 0); |
---|
| 194 | LOLUNIT_ASSERT_EQUAL((int)(half)(-0.9f), 0); |
---|
| 195 | LOLUNIT_ASSERT_EQUAL((int)(half)(1.0f), 1); |
---|
| 196 | LOLUNIT_ASSERT_EQUAL((int)(half)(-1.0f), -1); |
---|
| 197 | LOLUNIT_ASSERT_EQUAL((int)(half)(1.9f), 1); |
---|
| 198 | LOLUNIT_ASSERT_EQUAL((int)(half)(-1.9f), -1); |
---|
| 199 | LOLUNIT_ASSERT_EQUAL((int)(half)(65504.0f), 65504); |
---|
| 200 | LOLUNIT_ASSERT_EQUAL((int)(half)(-65504.0f), -65504); |
---|
[868] | 201 | } |
---|
| 202 | |
---|
[912] | 203 | LOLUNIT_TEST(test_float_op_half) |
---|
[871] | 204 | { |
---|
| 205 | half zero = 0; |
---|
| 206 | half one = 1; |
---|
| 207 | half two = 2; |
---|
| 208 | |
---|
| 209 | float a = zero + one; |
---|
[912] | 210 | LOLUNIT_ASSERT_EQUAL(1.0f, a); |
---|
[871] | 211 | a += zero; |
---|
[912] | 212 | LOLUNIT_ASSERT_EQUAL(1.0f, a); |
---|
[871] | 213 | a -= zero; |
---|
[912] | 214 | LOLUNIT_ASSERT_EQUAL(1.0f, a); |
---|
[871] | 215 | a *= one; |
---|
[912] | 216 | LOLUNIT_ASSERT_EQUAL(1.0f, a); |
---|
[871] | 217 | a /= one; |
---|
[912] | 218 | LOLUNIT_ASSERT_EQUAL(1.0f, a); |
---|
[871] | 219 | |
---|
| 220 | float b = one + zero; |
---|
[912] | 221 | LOLUNIT_ASSERT_EQUAL(1.0f, b); |
---|
[871] | 222 | b += one; |
---|
[912] | 223 | LOLUNIT_ASSERT_EQUAL(2.0f, b); |
---|
[871] | 224 | b *= two; |
---|
[912] | 225 | LOLUNIT_ASSERT_EQUAL(4.0f, b); |
---|
[871] | 226 | b -= two; |
---|
[912] | 227 | LOLUNIT_ASSERT_EQUAL(2.0f, b); |
---|
[871] | 228 | b /= two; |
---|
[912] | 229 | LOLUNIT_ASSERT_EQUAL(1.0f, b); |
---|
[871] | 230 | |
---|
| 231 | float c = one - zero; |
---|
[912] | 232 | LOLUNIT_ASSERT_EQUAL(1.0f, c); |
---|
[871] | 233 | |
---|
| 234 | float d = two - one; |
---|
[912] | 235 | LOLUNIT_ASSERT_EQUAL(1.0f, d); |
---|
[871] | 236 | |
---|
| 237 | float e = two + (-one); |
---|
[912] | 238 | LOLUNIT_ASSERT_EQUAL(1.0f, e); |
---|
[871] | 239 | |
---|
| 240 | float f = (two * two) / (one + one); |
---|
[912] | 241 | LOLUNIT_ASSERT_EQUAL(2.0f, f); |
---|
[871] | 242 | } |
---|
| 243 | |
---|
[912] | 244 | LOLUNIT_TEST(test_half_op_float) |
---|
[871] | 245 | { |
---|
| 246 | half zero = 0; |
---|
| 247 | half one = 1; |
---|
| 248 | half two = 2; |
---|
| 249 | half four = 4; |
---|
| 250 | |
---|
| 251 | half a = one + 0.0f; |
---|
[912] | 252 | LOLUNIT_ASSERT_EQUAL(one.bits, a.bits); |
---|
[871] | 253 | a += 0.0f; |
---|
[912] | 254 | LOLUNIT_ASSERT_EQUAL(one.bits, a.bits); |
---|
[871] | 255 | a -= 0.0f; |
---|
[912] | 256 | LOLUNIT_ASSERT_EQUAL(one.bits, a.bits); |
---|
[871] | 257 | a *= 1.0f; |
---|
[912] | 258 | LOLUNIT_ASSERT_EQUAL(one.bits, a.bits); |
---|
[871] | 259 | a /= 1.0f; |
---|
[912] | 260 | LOLUNIT_ASSERT_EQUAL(one.bits, a.bits); |
---|
[871] | 261 | |
---|
| 262 | half b = one + 0.0f; |
---|
[912] | 263 | LOLUNIT_ASSERT_EQUAL(one.bits, b.bits); |
---|
[871] | 264 | b += 1.0f; |
---|
[912] | 265 | LOLUNIT_ASSERT_EQUAL(two.bits, b.bits); |
---|
[871] | 266 | b *= 2.0f; |
---|
[912] | 267 | LOLUNIT_ASSERT_EQUAL(four.bits, b.bits); |
---|
[871] | 268 | b -= 2.0f; |
---|
[912] | 269 | LOLUNIT_ASSERT_EQUAL(two.bits, b.bits); |
---|
[871] | 270 | b /= 2.0f; |
---|
[912] | 271 | LOLUNIT_ASSERT_EQUAL(one.bits, b.bits); |
---|
[871] | 272 | |
---|
| 273 | half c = 1.0f - zero; |
---|
[912] | 274 | LOLUNIT_ASSERT_EQUAL(one.bits, c.bits); |
---|
[871] | 275 | |
---|
| 276 | half d = 2.0f - one; |
---|
[912] | 277 | LOLUNIT_ASSERT_EQUAL(one.bits, d.bits); |
---|
[871] | 278 | |
---|
| 279 | half e = 2.0f + (-one); |
---|
[912] | 280 | LOLUNIT_ASSERT_EQUAL(one.bits, e.bits); |
---|
[871] | 281 | |
---|
| 282 | half f = (2.0f * two) / (1.0f + one); |
---|
[912] | 283 | LOLUNIT_ASSERT_EQUAL(two.bits, f.bits); |
---|
[871] | 284 | } |
---|
| 285 | |
---|
[868] | 286 | private: |
---|
| 287 | struct TestPair { float f; uint16_t x; }; |
---|
| 288 | |
---|
[870] | 289 | static TestPair const pairs[11]; |
---|
[868] | 290 | }; |
---|
| 291 | |
---|
[870] | 292 | HalfTest::TestPair const HalfTest::pairs[] = |
---|
[868] | 293 | { |
---|
[870] | 294 | /* All these values have exact half representations */ |
---|
| 295 | { 0.0f, 0x0000 }, |
---|
| 296 | { -0.0f, 0x8000 }, /* negative zero */ |
---|
| 297 | { 1.0f, 0x3c00 }, |
---|
| 298 | { -1.0f, 0xbc00 }, |
---|
| 299 | { 2.0f, 0x4000 }, |
---|
| 300 | { 0.5f, 0x3800 }, |
---|
| 301 | { 0.125f, 0x3000 }, |
---|
| 302 | { 15.9375f, 0x4bf8 }, |
---|
[911] | 303 | { 31.0f / (1 << 14), 0x17c0 }, /* 0x1.fp-10 */ |
---|
| 304 | { 31.0f / (1 << 18), 0x07c0 }, /* 0x1.fp-14, denormal */ |
---|
| 305 | { 31.0f / (1 << 19), 0x03e0 }, /* 0x1.fp-15, denormal */ |
---|
[868] | 306 | }; |
---|
| 307 | |
---|
[912] | 308 | LOLUNIT_SETUP_FIXTURE(HalfTest); |
---|
[868] | 309 | |
---|
| 310 | } /* namespace lol */ |
---|
| 311 | |
---|