Debian Codegraph @

src/ImathTest/testClassification.cpp

raw
1 //
2 // SPDX-License-Identifier: BSD-3-Clause
3 // Copyright Contributors to the OpenEXR Project.
4 //
5
6 #ifdef NDEBUG
7 # undef NDEBUG
8 #endif
9
10 #include <half.h>
11 #include <assert.h>
12 #include <iostream>
13 #include <type_traits>
14 #include "testClassification.h"
15
16 using namespace std;
17
18 namespace
19 {
20
21 #if __cplusplus >= 201402L
22 static_assert (std::is_trivially_default_constructible<half>::value,
23 "half is trivial and default constructible");
24 #endif
25
26 void
27 testClass (half h,
28 bool finite,
29 bool normalized,
30 bool denormalized,
31 bool zero,
32 bool nan,
33 bool infinity,
34 bool negative)
35 {
36 cout.width (15);
37 cout.precision (8);
38
39 cout << h << " ";
40 printBits (cout, h);
41 cout << " ";
42
43 if (h.isFinite())
44 cout << "finite ";
45
46 if (h.isNormalized())
47 cout << "normalized ";
48
49 if (h.isDenormalized())
50 cout << "denormalized ";
51
52 if (h.isZero())
53 cout << "zero ";
54
55 if (h.isNan())
56 cout << "nan ";
57
58 if (h.isInfinity())
59 cout << "infinity ";
60
61 if (h.isNegative())
62 cout << "negative ";
63
64 cout << endl;
65
66 assert (h.isFinite() == finite);
67 assert (h.isNormalized() == normalized);
68 assert (h.isDenormalized() == denormalized);
69 assert (h.isZero() == zero);
70 assert (h.isNan() == nan);
71 assert (h.isInfinity() == infinity);
72 assert (h.isNegative() == negative);
73 }
74
75 float
76 floatPosInfinity()
77 {
78 half::uif x;
79 x.i = 0x7f800000;
80 return x.f;
81 }
82
83 float
84 floatNegInfinity()
85 {
86 half::uif x;
87 x.i = 0xff800000;
88 return x.f;
89 }
90
91 float
92 floatPosQNan1()
93 {
94 half::uif x;
95 x.i = 0x7fffffff;
96 return x.f;
97 }
98
99 float
100 floatNegQNan1()
101 {
102 half::uif x;
103 x.i = 0xffffffff;
104 return x.f;
105 }
106
107 float
108 floatPosQNan2()
109 {
110 half::uif x;
111 x.i = 0x7fd55555;
112 return x.f;
113 }
114
115 float
116 floatNegQNan2()
117 {
118 half::uif x;
119 x.i = 0xffd55555;
120 return x.f;
121 }
122
123 } // namespace
124
125 void
126 testClassification()
127 {
128 cout << "classification of bit patterns\n\n";
129
130 //
131 // fini norm deno zero nan inf neg
132 //
133
134 testClass (0.0, 1, 0, 0, 1, 0, 0, 0);
135
136 testClass (1.0, 1, 1, 0, 0, 0, 0, 0);
137 testClass (1.0f + HALF_EPSILON, 1, 1, 0, 0, 0, 0, 0);
138 testClass (HALF_DENORM_MIN, 1, 0, 1, 0, 0, 0, 0);
139 testClass (HALF_DENORM_MIN + HALF_DENORM_MIN, 1, 0, 1, 0, 0, 0, 0);
140 testClass (HALF_NRM_MIN, 1, 1, 0, 0, 0, 0, 0);
141 testClass (HALF_NRM_MIN + HALF_DENORM_MIN, 1, 1, 0, 0, 0, 0, 0);
142 testClass (HALF_NRM_MIN - HALF_DENORM_MIN, 1, 0, 1, 0, 0, 0, 0);
143 testClass (2.0f, 1, 1, 0, 0, 0, 0, 0);
144 testClass (3.0f, 1, 1, 0, 0, 0, 0, 0);
145 testClass (0.1f, 1, 1, 0, 0, 0, 0, 0);
146 testClass (0.2f, 1, 1, 0, 0, 0, 0, 0);
147 testClass (0.3f, 1, 1, 0, 0, 0, 0, 0);
148 testClass (HALF_MAX, 1, 1, 0, 0, 0, 0, 0);
149 testClass (floatPosInfinity(), 0, 0, 0, 0, 0, 1, 0);
150 testClass (floatPosQNan1(), 0, 0, 0, 0, 1, 0, 0);
151 testClass (floatPosQNan2(), 0, 0, 0, 0, 1, 0, 0);
152
153 testClass (-1.0f, 1, 1, 0, 0, 0, 0, 1);
154 testClass (-1.0f - HALF_EPSILON, 1, 1, 0, 0, 0, 0, 1);
155 testClass (-HALF_DENORM_MIN, 1, 0, 1, 0, 0, 0, 1);
156 testClass (-HALF_DENORM_MIN - HALF_DENORM_MIN, 1, 0, 1, 0, 0, 0, 1);
157 testClass (-HALF_NRM_MIN, 1, 1, 0, 0, 0, 0, 1);
158 testClass (-HALF_NRM_MIN - HALF_DENORM_MIN, 1, 1, 0, 0, 0, 0, 1);
159 testClass (-HALF_NRM_MIN + HALF_DENORM_MIN, 1, 0, 1, 0, 0, 0, 1);
160 testClass (-2.0f, 1, 1, 0, 0, 0, 0, 1);
161 testClass (-3.0f, 1, 1, 0, 0, 0, 0, 1);
162 testClass (-0.1f, 1, 1, 0, 0, 0, 0, 1);
163 testClass (-0.2f, 1, 1, 0, 0, 0, 0, 1);
164 testClass (-0.3f, 1, 1, 0, 0, 0, 0, 1);
165 testClass (-HALF_MAX, 1, 1, 0, 0, 0, 0, 1);
166 testClass (floatNegInfinity(), 0, 0, 0, 0, 0, 1, 1);
167 testClass (floatNegQNan1(), 0, 0, 0, 0, 1, 0, 1);
168 testClass (floatNegQNan2(), 0, 0, 0, 0, 1, 0, 1);
169
170 cout << "\n";
171
172 testClass (half::posInf(), 0, 0, 0, 0, 0, 1, 0);
173 testClass (half::negInf(), 0, 0, 0, 0, 0, 1, 1);
174 testClass (half::qNan(), 0, 0, 0, 0, 1, 0, 0);
175 testClass (half::sNan(), 0, 0, 0, 0, 1, 0, 0);
176
177 cout << "ok\n\n" << flush;
178 }