source: trunk/test/half.cpp @ 870

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

test: augment the half precision numbers testsuite.

  • Property svn:keywords set to Id
File size: 6.6 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_SUITE_END();
39
40public:
41    HalfTest() : CppUnit::TestCase("Matrix Test") {}
42
43    void setUp()
44    {
45    }
46
47    void tearDown() {}
48
49    void test_half_makebits()
50    {
51        for (unsigned int i = 0; i < 0x10000; i++)
52        {
53            half a = half::makebits(i);
54            uint16_t b = i;
55            CPPUNIT_ASSERT_EQUAL(a.bits(), b);
56        }
57    }
58
59    void test_half_makeslow()
60    {
61        for (size_t i = 0; i < sizeof(pairs) / sizeof(*pairs); i++)
62        {
63            half a = half::makeslow(pairs[i].f);
64            uint16_t b = pairs[i].x;
65            CPPUNIT_ASSERT_EQUAL(a.bits(), b);
66        }
67    }
68
69    void test_half_makefast()
70    {
71        for (size_t i = 0; i < sizeof(pairs) / sizeof(*pairs); i++)
72        {
73            half a = half::makefast(pairs[i].f);
74            uint16_t b = pairs[i].x;
75            CPPUNIT_ASSERT_EQUAL(a.bits(), b);
76        }
77    }
78
79    void test_half_isnan()
80    {
81        CPPUNIT_ASSERT(half::makebits(0x7c01).isnan());
82        CPPUNIT_ASSERT(half::makebits(0xfc01).isnan());
83        CPPUNIT_ASSERT(half::makebits(0x7e00).isnan());
84        CPPUNIT_ASSERT(half::makebits(0xfe00).isnan());
85
86        CPPUNIT_ASSERT(!half::makebits(0x7c00).isnan());
87        CPPUNIT_ASSERT(!half::makebits(0xfc00).isnan());
88
89        CPPUNIT_ASSERT(!half(0.0f).isnan());
90        CPPUNIT_ASSERT(!half(-0.0f).isnan());
91        CPPUNIT_ASSERT(!half(2.0f).isnan());
92        CPPUNIT_ASSERT(!half(-2.0f).isnan());
93    }
94
95    void test_half_isinf()
96    {
97        CPPUNIT_ASSERT(half(65536.0f).isinf());
98        CPPUNIT_ASSERT(half(-65536.0f).isinf());
99
100        CPPUNIT_ASSERT(!half(0.0f).isinf());
101        CPPUNIT_ASSERT(!half(-0.0f).isinf());
102        CPPUNIT_ASSERT(!half(65535.0f).isinf());
103        CPPUNIT_ASSERT(!half(-65535.0f).isinf());
104
105        CPPUNIT_ASSERT(half::makebits(0x7c00).isinf());
106        CPPUNIT_ASSERT(half::makebits(0xfc00).isinf());
107
108        CPPUNIT_ASSERT(!half::makebits(0x7e00).isinf());
109        CPPUNIT_ASSERT(!half::makebits(0xfe00).isinf());
110    }
111
112    void test_half_isfinite()
113    {
114        CPPUNIT_ASSERT(half(0.0f).isfinite());
115        CPPUNIT_ASSERT(half(-0.0f).isfinite());
116        CPPUNIT_ASSERT(half(65535.0f).isfinite());
117        CPPUNIT_ASSERT(half(-65535.0f).isfinite());
118
119        CPPUNIT_ASSERT(!half(65536.0f).isfinite());
120        CPPUNIT_ASSERT(!half(-65536.0f).isfinite());
121
122        CPPUNIT_ASSERT(!half::makebits(0x7c00).isfinite());
123        CPPUNIT_ASSERT(!half::makebits(0xfc00).isfinite());
124
125        CPPUNIT_ASSERT(!half::makebits(0x7e00).isfinite());
126        CPPUNIT_ASSERT(!half::makebits(0xfe00).isfinite());
127    }
128
129    void test_half_isnormal()
130    {
131        CPPUNIT_ASSERT(half(0.0f).isnormal());
132        CPPUNIT_ASSERT(half(-0.0f).isnormal());
133        CPPUNIT_ASSERT(half(65535.0f).isnormal());
134        CPPUNIT_ASSERT(half(-65535.0f).isnormal());
135
136        CPPUNIT_ASSERT(!half(65536.0f).isnormal());
137        CPPUNIT_ASSERT(!half(-65536.0f).isnormal());
138
139        CPPUNIT_ASSERT(!half::makebits(0x7c00).isnormal());
140        CPPUNIT_ASSERT(!half::makebits(0xfc00).isnormal());
141
142        CPPUNIT_ASSERT(!half::makebits(0x7e00).isnormal());
143        CPPUNIT_ASSERT(!half::makebits(0xfe00).isnormal());
144    }
145
146    void test_half_classify()
147    {
148        for (uint32_t i = 0; i < 0x10000; i++)
149        {
150            half h = half::makebits(i);
151            if (h.isnan())
152            {
153                CPPUNIT_ASSERT(!h.isinf());
154                CPPUNIT_ASSERT(!h.isnormal());
155                CPPUNIT_ASSERT(!h.isfinite());
156            }
157            else if (h.isinf())
158            {
159                CPPUNIT_ASSERT(!h.isnormal());
160                CPPUNIT_ASSERT(!h.isfinite());
161            }
162            else
163            {
164                CPPUNIT_ASSERT(h.isfinite());
165            }
166        }
167    }
168
169    void test_half_to_float()
170    {
171        for (size_t i = 0; i < sizeof(pairs) / sizeof(*pairs); i++)
172        {
173            float a = (float)half::makebits(pairs[i].x);
174            float b = pairs[i].f;
175            CPPUNIT_ASSERT_EQUAL(a, b);
176        }
177
178        for (uint32_t i = 0; i < 0x10000; i++)
179        {
180            half h = half::makebits(i);
181            float f = (float)h;
182            half g = (half)f;
183            if (h.isnan())
184            {
185                CPPUNIT_ASSERT(isnan(f));
186                CPPUNIT_ASSERT(g.isnan());
187            }
188            else
189            {
190                CPPUNIT_ASSERT(!isnan(f));
191                CPPUNIT_ASSERT_EQUAL(g.bits(), h.bits());
192            }
193        }
194    }
195
196    void test_half_to_int()
197    {
198        CPPUNIT_ASSERT_EQUAL((int)(half)(0.0f), 0);
199        CPPUNIT_ASSERT_EQUAL((int)(half)(-0.0f), 0);
200        CPPUNIT_ASSERT_EQUAL((int)(half)(0.9f), 0);
201        CPPUNIT_ASSERT_EQUAL((int)(half)(-0.9f), 0);
202        CPPUNIT_ASSERT_EQUAL((int)(half)(1.0f), 1);
203        CPPUNIT_ASSERT_EQUAL((int)(half)(-1.0f), -1);
204        CPPUNIT_ASSERT_EQUAL((int)(half)(1.9f), 1);
205        CPPUNIT_ASSERT_EQUAL((int)(half)(-1.9f), -1);
206        CPPUNIT_ASSERT_EQUAL((int)(half)(65504.0f), 65504);
207        CPPUNIT_ASSERT_EQUAL((int)(half)(-65504.0f), -65504);
208    }
209
210private:
211    struct TestPair { float f; uint16_t x; };
212
213    static TestPair const pairs[11];
214};
215
216HalfTest::TestPair const HalfTest::pairs[] =
217{
218    /* All these values have exact half representations */
219    { 0.0f,      0x0000 },
220    { -0.0f,     0x8000 }, /* negative zero */
221    { 1.0f,      0x3c00 },
222    { -1.0f,     0xbc00 },
223    { 2.0f,      0x4000 },
224    { 0.5f,      0x3800 },
225    { 0.125f,    0x3000 },
226    { 15.9375f,  0x4bf8 },
227    { 0x1.fp-10, 0x17c0 },
228    { 0x1.fp-14, 0x07c0 }, /* denormal */
229    { 0x1.fp-15, 0x03e0 }, /* denormal */
230};
231
232CPPUNIT_TEST_SUITE_REGISTRATION(HalfTest);
233
234} /* namespace lol */
235
Note: See TracBrowser for help on using the repository browser.