Debian Codegraph @

src/ImathTest/testLimits.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 "testLimits.h"
11 #include "half.h"
12 #include <assert.h>
13 #include <cmath>
14 #include <iostream>
15 #include "testLimits.h"
16
17 using namespace std;
18
19 namespace
20 {
21
22 float
23 mypow (int x, int y)
24 {
25 bool negative = false;
26
27 if (y < 0)
28 {
29 negative = true;
30 y = -y;
31 }
32
33 float z = 1;
34
35 while (y > 0)
36 {
37 z *= x;
38 y -= 1;
39 }
40
41 if (negative)
42 z = 1 / z;
43
44 return z;
45 }
46
47 } // namespace
48
49 void
50 testLimits()
51 {
52 cout << "values in std::numeric_limits<half>\n";
53
54 cout << "min_exponent\n";
55
56 {
57 half h (mypow (2, numeric_limits<half>::min_exponent - 1));
58 assert (h.isNormalized());
59 }
60
61 {
62 half h (mypow (2, numeric_limits<half>::min_exponent - 2));
63 assert (h.isDenormalized());
64 }
65
66 cout << "max_exponent\n";
67
68 {
69 half h (mypow (2, numeric_limits<half>::max_exponent - 1));
70 assert (h.isNormalized());
71 }
72
73 {
74 half h (mypow (2, numeric_limits<half>::max_exponent));
75 assert (h.isInfinity());
76 }
77
78 cout << "min_exponent10\n";
79
80 {
81 half h (mypow (10, numeric_limits<half>::min_exponent10));
82 assert (h.isNormalized());
83 }
84
85 {
86 half h (mypow (10, numeric_limits<half>::min_exponent10 - 1));
87 assert (h.isDenormalized());
88 }
89
90 cout << "max_exponent10\n";
91
92 {
93 half h (mypow (10, numeric_limits<half>::max_exponent10));
94 assert (h.isNormalized());
95 }
96
97 {
98 half h (mypow (10, numeric_limits<half>::max_exponent10 + 1));
99 assert (h.isInfinity());
100 }
101
102 #if __cplusplus >= 201103L
103
104 cout << "max_digits10\n";
105 assert (numeric_limits<half>::max_digits10 ==
106 std::ceil (numeric_limits<half>::digits * std::log10 (2) + 1));
107
108 cout << "lowest\n";
109 assert (numeric_limits<half>::lowest() == -HALF_MAX);
110
111 #endif
112
113 cout << "ok\n\n" << flush;
114 }
115
116 void
117 testHalfLimits()
118 {
119 cout << "values in std::numeric_limits<half>\n";
120
121 // For reference:
122 printf("HALF_DENORM_MIN %g -> 0x%04x\n", (float)HALF_DENORM_MIN, half(HALF_DENORM_MIN).bits());
123 printf("HALF_NRM_MIN %g -> 0x%04x\n", (float)HALF_NRM_MIN, half(HALF_NRM_MIN).bits());
124 printf("HALF_MIN %g -> 0x%04x\n", (float)HALF_MIN, half(HALF_MIN).bits());
125 printf("HALF_MAX %g -> 0x%04x\n", (float)HALF_MAX, half(HALF_MAX).bits());
126 printf("HALF_LOWEST %g -> 0x%04x\n", (float)-HALF_MAX, half(-HALF_MAX).bits());
127 printf("HALF_EPSILON %g -> 0x%04x\n", (float)HALF_EPSILON, half(HALF_EPSILON).bits());
128 printf("half posInf %g -> 0x%04x\n", (float)half::posInf(), half::posInf().bits());
129 printf("half negInf %g -> 0x%04x\n", (float)half::negInf(), half::negInf().bits());
130 printf("half qNan %g -> 0x%04x\n", (float)half::qNan(), half::qNan().bits());
131 printf("half sNan %g -> 0x%04x\n", (float)half::sNan(), half::sNan().bits());
132 printf("numeric_limits<half> min %g -> 0x%04x\n", (float)std::numeric_limits<half>::min(), std::numeric_limits<half>::min().bits());
133 printf("numeric_limits<half> max %g -> 0x%04x\n", (float)std::numeric_limits<half>::max(), std::numeric_limits<half>::max().bits());
134 printf("numeric_limits<half> lowest %g -> 0x%04x\n", (float)std::numeric_limits<half>::lowest(), std::numeric_limits<half>::lowest().bits());
135 printf("numeric_limits<half> epsilon %g -> 0x%04x\n", (float)std::numeric_limits<half>::epsilon(), std::numeric_limits<half>::epsilon().bits());
136 printf("numeric_limits<half> round_error %g -> 0x%04x\n", (float)std::numeric_limits<half>::round_error(), std::numeric_limits<half>::round_error().bits());
137 printf("numeric_limits<half> infinity %g -> 0x%04x\n", (float)std::numeric_limits<half>::infinity(), std::numeric_limits<half>::infinity().bits());
138 printf("numeric_limits<half> quiet_NaN %g -> 0x%04x\n", (float)std::numeric_limits<half>::quiet_NaN(), std::numeric_limits<half>::quiet_NaN().bits());
139 printf("numeric_limits<half> signaling_NaN %g -> 0x%04x\n", (float)std::numeric_limits<half>::signaling_NaN(), std::numeric_limits<half>::signaling_NaN().bits());
140 printf("numeric_limits<half> denorm_min %g -> 0x%04x\n", (float)std::numeric_limits<half>::denorm_min(), std::numeric_limits<half>::denorm_min().bits());
141
142 assert (std::numeric_limits<half>::max() == half(HALF_MAX));
143 assert (std::numeric_limits<half>::min() == half(HALF_NRM_MIN));
144 assert (std::numeric_limits<half>::denorm_min() == half(HALF_DENORM_MIN));
145 assert (std::numeric_limits<half>::lowest() == half(-HALF_MAX));
146 assert (std::numeric_limits<half>::epsilon() == half(HALF_EPSILON));
147 assert (std::numeric_limits<half>::infinity() == half::posInf());
148 assert (std::numeric_limits<half>::quiet_NaN().bits() == half::qNan().bits());
149 assert (std::numeric_limits<half>::signaling_NaN().bits() == half::sNan().bits());
150 assert (std::numeric_limits<half>::infinity() == half::posInf());
151
152 cout << "ok\n\n" << flush;
153 }