Debian Codegraph @

src/ImathTest/testBitPatterns.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 <float.h>
13 #include <iostream>
14 #include <string.h>
15 #include "testBitPatterns.h"
16
17 using namespace std;
18
19 namespace
20 {
21
22 bool
23 equalBitPatterns (const char* b1, const char* b2)
24 {
25 //
26 // Returns true if the characters in zero-terminated string b1
27 // are the same as the charaters in string b2, except for places
28 // where b1 or b2 contains an 'X'. For example:
29 //
30 // equalBitPatterns ("100", "100") returns true
31 // equalBitPatterns ("100", "101") returns false
32 // equalBitPatterns ("10X", "101") returns true
33 // equalBitPatterns ("10X", "100") returns true
34 //
35
36 while (*b1 && *b2)
37 {
38 if (*b1 != *b2 && *b1 != 'X' && *b2 != 'X')
39 return false;
40
41 ++b1;
42 ++b2;
43 }
44
45 return !(*b1 || *b2);
46 }
47
48 void
49 testBits (float f, const char bh[19], const char bg[35])
50 {
51 half h (f);
52 float g (h);
53
54 cout.width (15);
55 cout.precision (8);
56 cout << f << " ";
57 printBits (cout, f);
58 cout << " ";
59 printBits (cout, h);
60 cout << '\n';
61 cout.width (15);
62 cout << g << " ";
63 printBits (cout, g);
64 cout << "\n\n";
65
66 if (bh || bg)
67 {
68 char ch[19], cg[35];
69
70 printBits (ch, h);
71 printBits (cg, g);
72
73 if (!equalBitPatterns (ch, bh))
74 {
75 cout << "error: expected " << bh << ", got " << ch << endl;
76 assert (false);
77 }
78
79 if (!equalBitPatterns (cg, bg))
80 {
81 cout << "error: expected " << bg << ", got " << cg << endl;
82 assert (false);
83 }
84 }
85 }
86
87 float
88 floatPosInfinity()
89 {
90 half::uif x;
91 x.i = 0x7f800000;
92 return x.f;
93 }
94
95 float
96 floatNegInfinity()
97 {
98 half::uif x;
99 x.i = 0xff800000;
100 return x.f;
101 }
102
103 float
104 floatPosQNan1()
105 {
106 half::uif x;
107 x.i = 0x7fffffff;
108 return x.f;
109 }
110
111 float
112 floatNegQNan1()
113 {
114 half::uif x;
115 x.i = 0xffffffff;
116 return x.f;
117 }
118
119 float
120 floatPosQNan2()
121 {
122 half::uif x;
123 x.i = 0x7fd55555;
124 return x.f;
125 }
126
127 float
128 floatNegQNan2()
129 {
130 half::uif x;
131 x.i = 0xffd55555;
132 return x.f;
133 }
134
135 } // namespace
136
137 void
138 testBitPatterns()
139 {
140 cout << "specific bit patterns\n\n";
141
142 //
143 // Numbers close to 1.0
144 //
145
146 testBits (1.0f, "0 01111 0000000000", "0 01111111 00000000000000000000000");
147 testBits (1.0f + HALF_EPSILON, "0 01111 0000000001", "0 01111111 00000000010000000000000");
148 testBits (1.0f + HALF_EPSILON * 0.5f,
149 "0 01111 0000000000",
150 "0 01111111 00000000000000000000000");
151 testBits (1.0f + HALF_EPSILON * 0.4999f,
152 "0 01111 0000000000",
153 "0 01111111 00000000000000000000000");
154 testBits (1.0f + HALF_EPSILON * 0.5001f,
155 "0 01111 0000000001",
156 "0 01111111 00000000010000000000000");
157 testBits (1.0f + HALF_EPSILON + HALF_EPSILON,
158 "0 01111 0000000010",
159 "0 01111111 00000000100000000000000");
160 testBits (1.0f + HALF_EPSILON + HALF_EPSILON * 0.5f,
161 "0 01111 0000000010",
162 "0 01111111 00000000100000000000000");
163 testBits (1.0f + HALF_EPSILON + HALF_EPSILON * 0.4999f,
164 "0 01111 0000000001",
165 "0 01111111 00000000010000000000000");
166 testBits (1.0f + HALF_EPSILON + HALF_EPSILON * 0.5001f,
167 "0 01111 0000000010",
168 "0 01111111 00000000100000000000000");
169 testBits (1.0f - HALF_EPSILON * 0.5f,
170 "0 01110 1111111111",
171 "0 01111110 11111111110000000000000");
172 testBits (1.0f - HALF_EPSILON * 0.5f * 0.5f,
173 "0 01111 0000000000",
174 "0 01111111 00000000000000000000000");
175 testBits (1.0f - HALF_EPSILON * 0.5f * 0.4999f,
176 "0 01111 0000000000",
177 "0 01111111 00000000000000000000000");
178 testBits (1.0f - HALF_EPSILON * 0.5f * 0.5001f,
179 "0 01110 1111111111",
180 "0 01111110 11111111110000000000000");
181
182 //
183 // Numbers close to HALF_DENORM_MIN
184 //
185
186 testBits (HALF_DENORM_MIN, "0 00000 0000000001", "0 01100111 00000000000000000000000");
187 testBits (HALF_DENORM_MIN + HALF_DENORM_MIN, "0 00000 0000000010", "0 01101000 00000000000000000000000");
188 testBits (HALF_DENORM_MIN + HALF_DENORM_MIN * 0.5f,
189 "0 00000 0000000010",
190 "0 01101000 00000000000000000000000");
191 testBits (HALF_DENORM_MIN + HALF_DENORM_MIN * 0.4999f,
192 "0 00000 0000000001",
193 "0 01100111 00000000000000000000000");
194 testBits (HALF_DENORM_MIN + HALF_DENORM_MIN * 0.5001f,
195 "0 00000 0000000010",
196 "0 01101000 00000000000000000000000");
197 testBits (HALF_DENORM_MIN - HALF_DENORM_MIN, // NOSONAR - suppress SonarCloud bug report.
198 "0 00000 0000000000",
199 "0 00000000 00000000000000000000000");
200 testBits (HALF_DENORM_MIN - HALF_DENORM_MIN * 0.5f,
201 "0 00000 0000000000",
202 "0 00000000 00000000000000000000000");
203 testBits (HALF_DENORM_MIN - HALF_DENORM_MIN * 0.4999f,
204 "0 00000 0000000001",
205 "0 01100111 00000000000000000000000");
206 testBits (HALF_DENORM_MIN - HALF_DENORM_MIN * 0.5001f,
207 "0 00000 0000000000",
208 "0 00000000 00000000000000000000000");
209
210 //
211 // Numbers close to HALF_NRM_MIN
212 //
213
214 testBits (HALF_NRM_MIN, "0 00001 0000000000", "0 01110001 00000000000000000000000");
215 testBits (HALF_NRM_MIN + HALF_DENORM_MIN, "0 00001 0000000001", "0 01110001 00000000010000000000000");
216 testBits (HALF_NRM_MIN + HALF_DENORM_MIN * 0.5f,
217 "0 00001 0000000000",
218 "0 01110001 00000000000000000000000");
219 testBits (HALF_NRM_MIN + HALF_DENORM_MIN * 0.4999f,
220 "0 00001 0000000000",
221 "0 01110001 00000000000000000000000");
222 testBits (HALF_NRM_MIN + HALF_DENORM_MIN * 0.5001f,
223 "0 00001 0000000001",
224 "0 01110001 00000000010000000000000");
225 testBits (HALF_NRM_MIN - HALF_DENORM_MIN, "0 00000 1111111111", "0 01110000 11111111100000000000000");
226 testBits (HALF_NRM_MIN - HALF_DENORM_MIN * 0.5f,
227 "0 00001 0000000000",
228 "0 01110001 00000000000000000000000");
229 testBits (HALF_NRM_MIN - HALF_DENORM_MIN * 0.49995f,
230 "0 00001 0000000000",
231 "0 01110001 00000000000000000000000");
232 testBits (HALF_NRM_MIN - HALF_DENORM_MIN * 0.50005f,
233 "0 00000 1111111111",
234 "0 01110000 11111111100000000000000");
235
236 //
237 // Small positive integers and simple decimal fractions
238 //
239
240 testBits (2, "0 10000 0000000000", "0 10000000 00000000000000000000000");
241 testBits (3, "0 10000 1000000000", "0 10000000 10000000000000000000000");
242 testBits (10, "0 10010 0100000000", "0 10000010 01000000000000000000000");
243 testBits (0.1f, "0 01011 1001100110", "0 01111011 10011001100000000000000");
244 testBits (0.2f, "0 01100 1001100110", "0 01111100 10011001100000000000000");
245 testBits (0.3f, "0 01101 0011001101", "0 01111101 00110011010000000000000");
246
247 //
248 // Numbers close to HALF_MAX
249 //
250
251 testBits (HALF_MAX, "0 11110 1111111111", "0 10001110 11111111110000000000000");
252 testBits ((1 << HALF_MAX_EXP) * 1.0,
253 "0 11111 0000000000", // +infinity
254 "0 11111111 00000000000000000000000"); // +infinity
255 testBits ((1 << HALF_MAX_EXP) * (1.0f - HALF_EPSILON * 0.25f),
256 "0 11111 0000000000", // +infinity
257 "0 11111111 00000000000000000000000"); // +infinity
258 testBits ((1 << HALF_MAX_EXP) * (1.0f - HALF_EPSILON * 0.25005f),
259 "0 11110 1111111111",
260 "0 10001110 11111111110000000000000");
261 testBits ((1 << HALF_MAX_EXP) * (1.0f - HALF_EPSILON * 0.24995f),
262 "0 11111 0000000000", // +infinity
263 "0 11111111 00000000000000000000000"); // +infinity
264
265 //
266 // Large positive numbers, positive infinity and NANs
267 //
268
269 testBits (HALF_MAX * HALF_MAX,
270 "0 11111 0000000000", // +infinity
271 "0 11111111 00000000000000000000000"); // +infinity
272 testBits (FLT_MAX,
273 "0 11111 0000000000", // +infinity
274 "0 11111111 00000000000000000000000"); // +infinity
275 testBits (floatPosInfinity(),
276 "0 11111 0000000000", // +infinity
277 "0 11111111 00000000000000000000000"); // +infinity
278 testBits (floatPosQNan1(),
279 "0 11111 1111111111", // nan
280 "0 11111111 11111111110000000000000"); // nan
281 testBits (floatPosQNan2(),
282 "0 11111 1010101010", // nan
283 "0 11111111 10101010100000000000000"); // nan
284
285 //
286 // Numbers close to -1.0
287 //
288
289 testBits (-1.0, "1 01111 0000000000", "1 01111111 00000000000000000000000");
290 testBits (-(1.0f + HALF_EPSILON), "1 01111 0000000001", "1 01111111 00000000010000000000000");
291 testBits (-(1.0f + HALF_EPSILON * 0.5f),
292 "1 01111 0000000000",
293 "1 01111111 00000000000000000000000");
294 testBits (-(1.0f + HALF_EPSILON * 0.4999f),
295 "1 01111 0000000000",
296 "1 01111111 00000000000000000000000");
297 testBits (-(1.0f + HALF_EPSILON * 0.5001f),
298 "1 01111 0000000001",
299 "1 01111111 00000000010000000000000");
300 testBits (-(1.0f + HALF_EPSILON + HALF_EPSILON),
301 "1 01111 0000000010",
302 "1 01111111 00000000100000000000000");
303 testBits (-(1.0f + HALF_EPSILON + HALF_EPSILON * 0.5f),
304 "1 01111 0000000010",
305 "1 01111111 00000000100000000000000");
306 testBits (-(1.0f + HALF_EPSILON + HALF_EPSILON * 0.4999f),
307 "1 01111 0000000001",
308 "1 01111111 00000000010000000000000");
309 testBits (-(1.0f + HALF_EPSILON + HALF_EPSILON * 0.5001f),
310 "1 01111 0000000010",
311 "1 01111111 00000000100000000000000");
312 testBits (-(1.0f - HALF_EPSILON * 0.5f),
313 "1 01110 1111111111",
314 "1 01111110 11111111110000000000000");
315 testBits (-(1.0f - HALF_EPSILON * 0.5f * 0.5f),
316 "1 01111 0000000000",
317 "1 01111111 00000000000000000000000");
318 testBits (-(1.0f - HALF_EPSILON * 0.5f * 0.4999f),
319 "1 01111 0000000000",
320 "1 01111111 00000000000000000000000");
321 testBits (-(1.0f - HALF_EPSILON * 0.5f * 0.5001f),
322 "1 01110 1111111111",
323 "1 01111110 11111111110000000000000");
324
325 //
326 // Numbers close to -HALF_DENORM_MIN
327 //
328
329 testBits (-HALF_DENORM_MIN, "1 00000 0000000001", "1 01100111 00000000000000000000000");
330 testBits (-(HALF_DENORM_MIN + HALF_DENORM_MIN), "1 00000 0000000010", "1 01101000 00000000000000000000000");
331 testBits (-(HALF_DENORM_MIN + HALF_DENORM_MIN * 0.5f),
332 "1 00000 0000000010",
333 "1 01101000 00000000000000000000000");
334 testBits (-(HALF_DENORM_MIN + HALF_DENORM_MIN * 0.4999f),
335 "1 00000 0000000001",
336 "1 01100111 00000000000000000000000");
337 testBits (-(HALF_DENORM_MIN + HALF_DENORM_MIN * 0.5001f),
338 "1 00000 0000000010",
339 "1 01101000 00000000000000000000000");
340 testBits (-(HALF_DENORM_MIN - HALF_DENORM_MIN), // NOSONAR - suppress SonarCloud bug report.
341 "X 00000 0000000000",
342 "X 00000000 00000000000000000000000");
343 testBits (-(HALF_DENORM_MIN - HALF_DENORM_MIN * 0.5f),
344 "1 00000 0000000000",
345 "1 00000000 00000000000000000000000");
346 testBits (-(HALF_DENORM_MIN - HALF_DENORM_MIN * 0.4999f),
347 "1 00000 0000000001",
348 "1 01100111 00000000000000000000000");
349 testBits (-(HALF_DENORM_MIN - HALF_DENORM_MIN * 0.5001f),
350 "1 00000 0000000000",
351 "1 00000000 00000000000000000000000");
352
353 //
354 // Numbers close to -HALF_NRM_MIN
355 //
356
357 testBits (-HALF_NRM_MIN, "1 00001 0000000000", "1 01110001 00000000000000000000000");
358 testBits (-(HALF_NRM_MIN + HALF_DENORM_MIN),
359 "1 00001 0000000001",
360 "1 01110001 00000000010000000000000");
361 testBits (-(HALF_NRM_MIN + HALF_DENORM_MIN * 0.5f),
362 "1 00001 0000000000",
363 "1 01110001 00000000000000000000000");
364 testBits (-(HALF_NRM_MIN + HALF_DENORM_MIN * 0.4999f),
365 "1 00001 0000000000",
366 "1 01110001 00000000000000000000000");
367 testBits (-(HALF_NRM_MIN + HALF_DENORM_MIN * 0.5001f),
368 "1 00001 0000000001",
369 "1 01110001 00000000010000000000000");
370 testBits (-(HALF_NRM_MIN - HALF_DENORM_MIN),
371 "1 00000 1111111111",
372 "1 01110000 11111111100000000000000");
373 testBits (-(HALF_NRM_MIN - HALF_DENORM_MIN * 0.5f),
374 "1 00001 0000000000",
375 "1 01110001 00000000000000000000000");
376 testBits (-(HALF_NRM_MIN - HALF_DENORM_MIN * 0.49995f),
377 "1 00001 0000000000",
378 "1 01110001 00000000000000000000000");
379 testBits (-(HALF_NRM_MIN - HALF_DENORM_MIN * 0.50005f),
380 "1 00000 1111111111",
381 "1 01110000 11111111100000000000000");
382
383 //
384 // Small negative integers and simple decimal fractions
385 //
386
387 testBits (-2, "1 10000 0000000000", "1 10000000 00000000000000000000000");
388 testBits (-3, "1 10000 1000000000", "1 10000000 10000000000000000000000");
389 testBits (-10, "1 10010 0100000000", "1 10000010 01000000000000000000000");
390 testBits (-0.1f, "1 01011 1001100110", "1 01111011 10011001100000000000000");
391 testBits (-0.2f, "1 01100 1001100110", "1 01111100 10011001100000000000000");
392 testBits (-0.3f, "1 01101 0011001101", "1 01111101 00110011010000000000000");
393
394 //
395 // Numbers close to -HALF_MAX
396 //
397
398 testBits (-HALF_MAX, "1 11110 1111111111", "1 10001110 11111111110000000000000");
399 testBits (-(1 << HALF_MAX_EXP) * 1.0f,
400 "1 11111 0000000000", // +infinity
401 "1 11111111 00000000000000000000000"); // +infinity
402 testBits (-(1 << HALF_MAX_EXP) * (1.0f - HALF_EPSILON * 0.25f),
403 "1 11111 0000000000", // +infinity
404 "1 11111111 00000000000000000000000"); // +infinity
405 testBits (-(1 << HALF_MAX_EXP) * (1.0f - HALF_EPSILON * 0.25005f),
406 "1 11110 1111111111",
407 "1 10001110 11111111110000000000000");
408 testBits (-(1 << HALF_MAX_EXP) * (1.0f - HALF_EPSILON * 0.24995f),
409 "1 11111 0000000000", // +infinity
410 "1 11111111 00000000000000000000000"); // +infinity
411
412 //
413 // Large negative numbers, negative infinity and NANs
414 //
415
416 testBits (-HALF_MAX * HALF_MAX,
417 "1 11111 0000000000", // +infinity
418 "1 11111111 00000000000000000000000"); // +infinity
419 testBits (-FLT_MAX,
420 "1 11111 0000000000", // +infinity
421 "1 11111111 00000000000000000000000"); // +infinity
422 testBits (floatNegInfinity(),
423 "1 11111 0000000000", // +infinity
424 "1 11111111 00000000000000000000000"); // +infinity
425 testBits (floatNegQNan1(),
426 "1 11111 1111111111", // nan
427 "1 11111111 11111111110000000000000"); // nan
428 testBits (floatNegQNan2(),
429 "1 11111 1010101010", // nan
430 "1 11111111 10101010100000000000000"); // nan
431
432 cout << "ok\n\n" << flush;
433 }