source: trunk/test/half.cpp @ 879

Last change on this file since 879 was 879, checked in by sam, 8 years ago

core: tune the half precision code so that the best variants are being
used on the PS3 platform.

  • Property svn:keywords set to Id
File size: 9.0 KB
Line 
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
15#include <cppunit/extensions/HelperMacros.h>
16#include <cppunit/TestCaller.h>
17#include <cppunit/TestCase.h>
18#include <cppunit/TestSuite.h>
19
20#include <cmath>
21
22#include "core.h"
23
24/* This will not work with aggressive optimisation, but a reasonable
25 * assumption is that such environments do have a proper isnan(). */
26#if !defined isnan
27#   define isnan(x) (!((x) == (x)))
28#endif
29
30namespace lol
31{
32
33class HalfTest : public CppUnit::TestCase
34{
35    CPPUNIT_TEST_SUITE(HalfTest);
36    CPPUNIT_TEST(test_half_from_float);
37    CPPUNIT_TEST(test_half_makeaccurate);
38    CPPUNIT_TEST(test_half_makebits);
39    CPPUNIT_TEST(test_half_is_nan);
40    CPPUNIT_TEST(test_half_is_inf);
41    CPPUNIT_TEST(test_half_is_finite);
42    CPPUNIT_TEST(test_half_is_normal);
43    CPPUNIT_TEST(test_half_classify);
44    CPPUNIT_TEST(test_half_to_float);
45    CPPUNIT_TEST(test_half_to_int);
46    CPPUNIT_TEST(test_float_op_half);
47    CPPUNIT_TEST(test_half_op_float);
48    CPPUNIT_TEST_SUITE_END();
49
50public:
51    HalfTest() : CppUnit::TestCase("Matrix Test") {}
52
53    void setUp()
54    {
55    }
56
57    void tearDown() {}
58
59    void test_half_from_float()
60    {
61        for (size_t i = 0; i < sizeof(pairs) / sizeof(*pairs); i++)
62        {
63            half a = (half)pairs[i].f;
64            uint16_t b = pairs[i].x;
65            CPPUNIT_ASSERT_EQUAL(a.bits, b);
66        }
67    }
68
69    void test_half_makeaccurate()
70    {
71        for (size_t i = 0; i < sizeof(pairs) / sizeof(*pairs); i++)
72        {
73            half a = half::makeaccurate(pairs[i].f);
74            uint16_t b = pairs[i].x;
75            CPPUNIT_ASSERT_EQUAL(a.bits, b);
76        }
77    }
78
79    void test_half_makebits()
80    {
81        for (unsigned int i = 0; i < 0x10000; i++)
82        {
83            half a = half::makebits(i);
84            uint16_t b = i;
85            CPPUNIT_ASSERT_EQUAL(a.bits, b);
86        }
87    }
88
89    void test_half_is_nan()
90    {
91        CPPUNIT_ASSERT(half::makebits(0x7c01).is_nan());
92        CPPUNIT_ASSERT(half::makebits(0xfc01).is_nan());
93        CPPUNIT_ASSERT(half::makebits(0x7e00).is_nan());
94        CPPUNIT_ASSERT(half::makebits(0xfe00).is_nan());
95
96        CPPUNIT_ASSERT(!half::makebits(0x7c00).is_nan());
97        CPPUNIT_ASSERT(!half::makebits(0xfc00).is_nan());
98
99        CPPUNIT_ASSERT(!half(0.0f).is_nan());
100        CPPUNIT_ASSERT(!half(-0.0f).is_nan());
101        CPPUNIT_ASSERT(!half(2.0f).is_nan());
102        CPPUNIT_ASSERT(!half(-2.0f).is_nan());
103    }
104
105    void test_half_is_inf()
106    {
107        CPPUNIT_ASSERT(half(65536.0f).is_inf());
108        CPPUNIT_ASSERT(half(-65536.0f).is_inf());
109
110        CPPUNIT_ASSERT(!half(0.0f).is_inf());
111        CPPUNIT_ASSERT(!half(-0.0f).is_inf());
112        CPPUNIT_ASSERT(!half(65535.0f).is_inf());
113        CPPUNIT_ASSERT(!half(-65535.0f).is_inf());
114
115        CPPUNIT_ASSERT(half::makebits(0x7c00).is_inf());
116        CPPUNIT_ASSERT(half::makebits(0xfc00).is_inf());
117
118        CPPUNIT_ASSERT(!half::makebits(0x7e00).is_inf());
119        CPPUNIT_ASSERT(!half::makebits(0xfe00).is_inf());
120    }
121
122    void test_half_is_finite()
123    {
124        CPPUNIT_ASSERT(half(0.0f).is_finite());
125        CPPUNIT_ASSERT(half(-0.0f).is_finite());
126        CPPUNIT_ASSERT(half(65535.0f).is_finite());
127        CPPUNIT_ASSERT(half(-65535.0f).is_finite());
128
129        CPPUNIT_ASSERT(!half(65536.0f).is_finite());
130        CPPUNIT_ASSERT(!half(-65536.0f).is_finite());
131
132        CPPUNIT_ASSERT(!half::makebits(0x7c00).is_finite());
133        CPPUNIT_ASSERT(!half::makebits(0xfc00).is_finite());
134
135        CPPUNIT_ASSERT(!half::makebits(0x7e00).is_finite());
136        CPPUNIT_ASSERT(!half::makebits(0xfe00).is_finite());
137    }
138
139    void test_half_is_normal()
140    {
141        CPPUNIT_ASSERT(half(0.0f).is_normal());
142        CPPUNIT_ASSERT(half(-0.0f).is_normal());
143        CPPUNIT_ASSERT(half(65535.0f).is_normal());
144        CPPUNIT_ASSERT(half(-65535.0f).is_normal());
145
146        CPPUNIT_ASSERT(!half(65536.0f).is_normal());
147        CPPUNIT_ASSERT(!half(-65536.0f).is_normal());
148
149        CPPUNIT_ASSERT(!half::makebits(0x7c00).is_normal());
150        CPPUNIT_ASSERT(!half::makebits(0xfc00).is_normal());
151
152        CPPUNIT_ASSERT(!half::makebits(0x7e00).is_normal());
153        CPPUNIT_ASSERT(!half::makebits(0xfe00).is_normal());
154    }
155
156    void test_half_classify()
157    {
158        for (uint32_t i = 0; i < 0x10000; i++)
159        {
160            half h = half::makebits(i);
161            if (h.is_nan())
162            {
163                CPPUNIT_ASSERT(!h.is_inf());
164                CPPUNIT_ASSERT(!h.is_normal());
165                CPPUNIT_ASSERT(!h.is_finite());
166            }
167            else if (h.is_inf())
168            {
169                CPPUNIT_ASSERT(!h.is_normal());
170                CPPUNIT_ASSERT(!h.is_finite());
171            }
172            else
173            {
174                CPPUNIT_ASSERT(h.is_finite());
175            }
176        }
177    }
178
179    void test_half_to_float()
180    {
181        for (size_t i = 0; i < sizeof(pairs) / sizeof(*pairs); i++)
182        {
183            float a = (float)half::makebits(pairs[i].x);
184            float b = pairs[i].f;
185            CPPUNIT_ASSERT_EQUAL(a, b);
186        }
187
188        for (uint32_t i = 0; i < 0x10000; i++)
189        {
190            half h = half::makebits(i);
191            float f = (float)h;
192            half g = (half)f;
193            if (h.is_nan())
194            {
195                CPPUNIT_ASSERT(isnan(f));
196                CPPUNIT_ASSERT(g.is_nan());
197            }
198            else
199            {
200                CPPUNIT_ASSERT(!isnan(f));
201                CPPUNIT_ASSERT_EQUAL(g.bits, h.bits);
202            }
203        }
204    }
205
206    void test_half_to_int()
207    {
208        CPPUNIT_ASSERT_EQUAL((int)(half)(0.0f), 0);
209        CPPUNIT_ASSERT_EQUAL((int)(half)(-0.0f), 0);
210        CPPUNIT_ASSERT_EQUAL((int)(half)(0.9f), 0);
211        CPPUNIT_ASSERT_EQUAL((int)(half)(-0.9f), 0);
212        CPPUNIT_ASSERT_EQUAL((int)(half)(1.0f), 1);
213        CPPUNIT_ASSERT_EQUAL((int)(half)(-1.0f), -1);
214        CPPUNIT_ASSERT_EQUAL((int)(half)(1.9f), 1);
215        CPPUNIT_ASSERT_EQUAL((int)(half)(-1.9f), -1);
216        CPPUNIT_ASSERT_EQUAL((int)(half)(65504.0f), 65504);
217        CPPUNIT_ASSERT_EQUAL((int)(half)(-65504.0f), -65504);
218    }
219
220    void test_float_op_half()
221    {
222        half zero = 0;
223        half one = 1;
224        half two = 2;
225
226        float a = zero + one;
227        CPPUNIT_ASSERT_EQUAL(1.0f, a);
228        a += zero;
229        CPPUNIT_ASSERT_EQUAL(1.0f, a);
230        a -= zero;
231        CPPUNIT_ASSERT_EQUAL(1.0f, a);
232        a *= one;
233        CPPUNIT_ASSERT_EQUAL(1.0f, a);
234        a /= one;
235        CPPUNIT_ASSERT_EQUAL(1.0f, a);
236
237        float b = one + zero;
238        CPPUNIT_ASSERT_EQUAL(1.0f, b);
239        b += one;
240        CPPUNIT_ASSERT_EQUAL(2.0f, b);
241        b *= two;
242        CPPUNIT_ASSERT_EQUAL(4.0f, b);
243        b -= two;
244        CPPUNIT_ASSERT_EQUAL(2.0f, b);
245        b /= two;
246        CPPUNIT_ASSERT_EQUAL(1.0f, b);
247
248        float c = one - zero;
249        CPPUNIT_ASSERT_EQUAL(1.0f, c);
250
251        float d = two - one;
252        CPPUNIT_ASSERT_EQUAL(1.0f, d);
253
254        float e = two + (-one);
255        CPPUNIT_ASSERT_EQUAL(1.0f, e);
256
257        float f = (two * two) / (one + one);
258        CPPUNIT_ASSERT_EQUAL(2.0f, f);
259    }
260
261    void test_half_op_float()
262    {
263        half zero = 0;
264        half one = 1;
265        half two = 2;
266        half four = 4;
267
268        half a = one + 0.0f;
269        CPPUNIT_ASSERT_EQUAL(one.bits, a.bits);
270        a += 0.0f;
271        CPPUNIT_ASSERT_EQUAL(one.bits, a.bits);
272        a -= 0.0f;
273        CPPUNIT_ASSERT_EQUAL(one.bits, a.bits);
274        a *= 1.0f;
275        CPPUNIT_ASSERT_EQUAL(one.bits, a.bits);
276        a /= 1.0f;
277        CPPUNIT_ASSERT_EQUAL(one.bits, a.bits);
278
279        half b = one + 0.0f;
280        CPPUNIT_ASSERT_EQUAL(one.bits, b.bits);
281        b += 1.0f;
282        CPPUNIT_ASSERT_EQUAL(two.bits, b.bits);
283        b *= 2.0f;
284        CPPUNIT_ASSERT_EQUAL(four.bits, b.bits);
285        b -= 2.0f;
286        CPPUNIT_ASSERT_EQUAL(two.bits, b.bits);
287        b /= 2.0f;
288        CPPUNIT_ASSERT_EQUAL(one.bits, b.bits);
289
290        half c = 1.0f - zero;
291        CPPUNIT_ASSERT_EQUAL(one.bits, c.bits);
292
293        half d = 2.0f - one;
294        CPPUNIT_ASSERT_EQUAL(one.bits, d.bits);
295
296        half e = 2.0f + (-one);
297        CPPUNIT_ASSERT_EQUAL(one.bits, e.bits);
298
299        half f = (2.0f * two) / (1.0f + one);
300        CPPUNIT_ASSERT_EQUAL(two.bits, f.bits);
301    }
302
303private:
304    struct TestPair { float f; uint16_t x; };
305
306    static TestPair const pairs[11];
307};
308
309HalfTest::TestPair const HalfTest::pairs[] =
310{
311    /* All these values have exact half representations */
312    { 0.0f,      0x0000 },
313    { -0.0f,     0x8000 }, /* negative zero */
314    { 1.0f,      0x3c00 },
315    { -1.0f,     0xbc00 },
316    { 2.0f,      0x4000 },
317    { 0.5f,      0x3800 },
318    { 0.125f,    0x3000 },
319    { 15.9375f,  0x4bf8 },
320    { 0x1.fp-10, 0x17c0 },
321    { 0x1.fp-14, 0x07c0 }, /* denormal */
322    { 0x1.fp-15, 0x03e0 }, /* denormal */
323};
324
325CPPUNIT_TEST_SUITE_REGISTRATION(HalfTest);
326
327} /* namespace lol */
328
Note: See TracBrowser for help on using the repository browser.