source: trunk/test/half.cpp @ 872

Last change on this file since 872 was 872, checked in by sam, 9 years ago

core: minor refactoring in the float / half conversions to accomodate
for future array versions.

  • Property svn:keywords set to Id
File size: 8.8 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 "core.h"
21
22namespace lol
23{
24
25class HalfTest : public CppUnit::TestCase
26{
27    CPPUNIT_TEST_SUITE(HalfTest);
28    CPPUNIT_TEST(test_half_makebits);
29    CPPUNIT_TEST(test_half_makeslow);
30    CPPUNIT_TEST(test_half_makefast);
31    CPPUNIT_TEST(test_half_isnan);
32    CPPUNIT_TEST(test_half_isinf);
33    CPPUNIT_TEST(test_half_isfinite);
34    CPPUNIT_TEST(test_half_isnormal);
35    CPPUNIT_TEST(test_half_classify);
36    CPPUNIT_TEST(test_half_to_float);
37    CPPUNIT_TEST(test_half_to_int);
38    CPPUNIT_TEST(test_float_op_half);
39    CPPUNIT_TEST(test_half_op_float);
40    CPPUNIT_TEST_SUITE_END();
41
42public:
43    HalfTest() : CppUnit::TestCase("Matrix Test") {}
44
45    void setUp()
46    {
47    }
48
49    void tearDown() {}
50
51    void test_half_makebits()
52    {
53        for (unsigned int i = 0; i < 0x10000; i++)
54        {
55            half a = half::makebits(i);
56            uint16_t b = i;
57            CPPUNIT_ASSERT_EQUAL(a.bits, b);
58        }
59    }
60
61    void test_half_makeslow()
62    {
63        for (size_t i = 0; i < sizeof(pairs) / sizeof(*pairs); i++)
64        {
65            half a = half::makeslow(pairs[i].f);
66            uint16_t b = pairs[i].x;
67            CPPUNIT_ASSERT_EQUAL(a.bits, b);
68        }
69    }
70
71    void test_half_makefast()
72    {
73        for (size_t i = 0; i < sizeof(pairs) / sizeof(*pairs); i++)
74        {
75            half a = half::makefast(pairs[i].f);
76            uint16_t b = pairs[i].x;
77            CPPUNIT_ASSERT_EQUAL(a.bits, b);
78        }
79    }
80
81    void test_half_isnan()
82    {
83        CPPUNIT_ASSERT(half::makebits(0x7c01).isnan());
84        CPPUNIT_ASSERT(half::makebits(0xfc01).isnan());
85        CPPUNIT_ASSERT(half::makebits(0x7e00).isnan());
86        CPPUNIT_ASSERT(half::makebits(0xfe00).isnan());
87
88        CPPUNIT_ASSERT(!half::makebits(0x7c00).isnan());
89        CPPUNIT_ASSERT(!half::makebits(0xfc00).isnan());
90
91        CPPUNIT_ASSERT(!half(0.0f).isnan());
92        CPPUNIT_ASSERT(!half(-0.0f).isnan());
93        CPPUNIT_ASSERT(!half(2.0f).isnan());
94        CPPUNIT_ASSERT(!half(-2.0f).isnan());
95    }
96
97    void test_half_isinf()
98    {
99        CPPUNIT_ASSERT(half(65536.0f).isinf());
100        CPPUNIT_ASSERT(half(-65536.0f).isinf());
101
102        CPPUNIT_ASSERT(!half(0.0f).isinf());
103        CPPUNIT_ASSERT(!half(-0.0f).isinf());
104        CPPUNIT_ASSERT(!half(65535.0f).isinf());
105        CPPUNIT_ASSERT(!half(-65535.0f).isinf());
106
107        CPPUNIT_ASSERT(half::makebits(0x7c00).isinf());
108        CPPUNIT_ASSERT(half::makebits(0xfc00).isinf());
109
110        CPPUNIT_ASSERT(!half::makebits(0x7e00).isinf());
111        CPPUNIT_ASSERT(!half::makebits(0xfe00).isinf());
112    }
113
114    void test_half_isfinite()
115    {
116        CPPUNIT_ASSERT(half(0.0f).isfinite());
117        CPPUNIT_ASSERT(half(-0.0f).isfinite());
118        CPPUNIT_ASSERT(half(65535.0f).isfinite());
119        CPPUNIT_ASSERT(half(-65535.0f).isfinite());
120
121        CPPUNIT_ASSERT(!half(65536.0f).isfinite());
122        CPPUNIT_ASSERT(!half(-65536.0f).isfinite());
123
124        CPPUNIT_ASSERT(!half::makebits(0x7c00).isfinite());
125        CPPUNIT_ASSERT(!half::makebits(0xfc00).isfinite());
126
127        CPPUNIT_ASSERT(!half::makebits(0x7e00).isfinite());
128        CPPUNIT_ASSERT(!half::makebits(0xfe00).isfinite());
129    }
130
131    void test_half_isnormal()
132    {
133        CPPUNIT_ASSERT(half(0.0f).isnormal());
134        CPPUNIT_ASSERT(half(-0.0f).isnormal());
135        CPPUNIT_ASSERT(half(65535.0f).isnormal());
136        CPPUNIT_ASSERT(half(-65535.0f).isnormal());
137
138        CPPUNIT_ASSERT(!half(65536.0f).isnormal());
139        CPPUNIT_ASSERT(!half(-65536.0f).isnormal());
140
141        CPPUNIT_ASSERT(!half::makebits(0x7c00).isnormal());
142        CPPUNIT_ASSERT(!half::makebits(0xfc00).isnormal());
143
144        CPPUNIT_ASSERT(!half::makebits(0x7e00).isnormal());
145        CPPUNIT_ASSERT(!half::makebits(0xfe00).isnormal());
146    }
147
148    void test_half_classify()
149    {
150        for (uint32_t i = 0; i < 0x10000; i++)
151        {
152            half h = half::makebits(i);
153            if (h.isnan())
154            {
155                CPPUNIT_ASSERT(!h.isinf());
156                CPPUNIT_ASSERT(!h.isnormal());
157                CPPUNIT_ASSERT(!h.isfinite());
158            }
159            else if (h.isinf())
160            {
161                CPPUNIT_ASSERT(!h.isnormal());
162                CPPUNIT_ASSERT(!h.isfinite());
163            }
164            else
165            {
166                CPPUNIT_ASSERT(h.isfinite());
167            }
168        }
169    }
170
171    void test_half_to_float()
172    {
173        for (size_t i = 0; i < sizeof(pairs) / sizeof(*pairs); i++)
174        {
175            float a = (float)half::makebits(pairs[i].x);
176            float b = pairs[i].f;
177            CPPUNIT_ASSERT_EQUAL(a, b);
178        }
179
180        for (uint32_t i = 0; i < 0x10000; i++)
181        {
182            half h = half::makebits(i);
183            float f = (float)h;
184            half g = (half)f;
185            if (h.isnan())
186            {
187                CPPUNIT_ASSERT(isnan(f));
188                CPPUNIT_ASSERT(g.isnan());
189            }
190            else
191            {
192                CPPUNIT_ASSERT(!isnan(f));
193                CPPUNIT_ASSERT_EQUAL(g.bits, h.bits);
194            }
195        }
196    }
197
198    void test_half_to_int()
199    {
200        CPPUNIT_ASSERT_EQUAL((int)(half)(0.0f), 0);
201        CPPUNIT_ASSERT_EQUAL((int)(half)(-0.0f), 0);
202        CPPUNIT_ASSERT_EQUAL((int)(half)(0.9f), 0);
203        CPPUNIT_ASSERT_EQUAL((int)(half)(-0.9f), 0);
204        CPPUNIT_ASSERT_EQUAL((int)(half)(1.0f), 1);
205        CPPUNIT_ASSERT_EQUAL((int)(half)(-1.0f), -1);
206        CPPUNIT_ASSERT_EQUAL((int)(half)(1.9f), 1);
207        CPPUNIT_ASSERT_EQUAL((int)(half)(-1.9f), -1);
208        CPPUNIT_ASSERT_EQUAL((int)(half)(65504.0f), 65504);
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);
293    }
294
295private:
296    struct TestPair { float f; uint16_t x; };
297
298    static TestPair const pairs[11];
299};
300
301HalfTest::TestPair const HalfTest::pairs[] =
302{
303    /* All these values have exact half representations */
304    { 0.0f,      0x0000 },
305    { -0.0f,     0x8000 }, /* negative zero */
306    { 1.0f,      0x3c00 },
307    { -1.0f,     0xbc00 },
308    { 2.0f,      0x4000 },
309    { 0.5f,      0x3800 },
310    { 0.125f,    0x3000 },
311    { 15.9375f,  0x4bf8 },
312    { 0x1.fp-10, 0x17c0 },
313    { 0x1.fp-14, 0x07c0 }, /* denormal */
314    { 0x1.fp-15, 0x03e0 }, /* denormal */
315};
316
317CPPUNIT_TEST_SUITE_REGISTRATION(HalfTest);
318
319} /* namespace lol */
320
Note: See TracBrowser for help on using the repository browser.