Debian Codegraph @

src/ImathTest/testError.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 <random>
14 #include <stdlib.h>
15 #include "testError.h"
16
17 using namespace std;
18
19 namespace
20 {
21
22 float
23 drand()
24 {
25 static std::default_random_engine generator;
26 static std::uniform_real_distribution<float> distribution (0.0f, 1.0f);
27 float r = distribution (generator);
28 return r;
29 }
30
31 } // namespace
32
33 void
34 testNormalizedConversionError()
35 {
36 cout << "float-to-half conversion error for normalized half numbers\n";
37
38 float eMax = 0;
39
40 for (int i = 0; i < 20000000; i++)
41 {
42 float f (drand() * HALF_MAX);
43
44 if (f < HALF_NRM_MIN)
45 continue;
46
47 if (i & 1)
48 f = -f;
49
50 half h (f);
51 float e = 1.0f - h / f;
52
53 if (e < 0)
54 e = -e;
55
56 if (e > HALF_EPSILON * 0.5)
57 {
58 cout << "float = " << f << ", half = " << h << ", error = " << e << endl;
59
60 assert (false);
61 }
62
63 if (e > eMax)
64 eMax = e;
65 }
66
67 cout << "max error = " << eMax << endl;
68 cout << "max expected error = " << HALF_EPSILON * 0.5 << endl;
69 cout << "ok\n\n" << flush;
70 }
71
72 void
73 testDenormalizedConversionError()
74 {
75 cout << "float-to-half conversion error for denormalized half numbers\n";
76
77 float eMax = 0;
78
79 for (int i = 0; i < 20000000; i++)
80 {
81 float f (drand() * (HALF_NRM_MIN - HALF_DENORM_MIN));
82
83 if (i & 1)
84 f = -f;
85
86 half h (f);
87 float e = h - f;
88
89 if (e < 0)
90 e = -e;
91
92 if (e > HALF_DENORM_MIN * 0.5)
93 {
94 cout << "float = " << f << ", half = " << h << ", error = " << e << endl;
95
96 assert (false);
97 }
98
99 if (e > eMax)
100 eMax = e;
101 }
102
103 cout << "max error = " << eMax << endl;
104 cout << "max expected error = " << HALF_DENORM_MIN * 0.5 << endl;
105 cout << "ok\n\n" << flush;
106 }
107
108 namespace
109 {
110
111 void
112 testNormalizedRounding (int n)
113 {
114 cout << "rounding normalized numbers to " << n << "-bit precision\n";
115
116 float eExpected = (n < 10) ? HALF_EPSILON * 0.5f * (1 << (10 - n)) : 0;
117 float eMax = 0;
118
119 for (int i = 0; i < 200000; i++)
120 {
121 half h (drand() * HALF_MAX);
122
123 if (h < HALF_NRM_MIN)
124 continue;
125
126 if (i & 1)
127 h = -h;
128
129 half r (h.round (n));
130 float e = 1.0f - r / h;
131
132 if (e < 0)
133 e = -e;
134
135 if (e > eExpected)
136 {
137 cout << "half = " << h << ", rounded = " << r << ", error = " << e
138 << ", expected error = " << eExpected << endl;
139
140 printBits (cout, h);
141 cout << endl;
142 printBits (cout, r);
143 cout << endl;
144
145 assert (false);
146 }
147
148 if (e > eMax)
149 eMax = e;
150 }
151
152 cout << "max error = " << eMax << endl;
153 cout << "max expected error = " << eExpected << endl;
154 cout << "ok\n\n" << flush;
155 }
156
157 void
158 testDenormalizedRounding (int n)
159 {
160 cout << "rounding denormalized numbers to " << n << "-bit precision\n";
161
162 float eExpected = (n < 10) ? HALF_DENORM_MIN * 0.5f * (1 << (10 - n)) : 0;
163 float eMax = 0;
164
165 for (int i = 0; i < 200000; i++)
166 {
167 half h (drand() * (HALF_NRM_MIN - HALF_DENORM_MIN));
168
169 if (i & 1)
170 h = -h;
171
172 half r (h.round (n));
173 float e = r - h;
174
175 if (e < 0)
176 e = -e;
177
178 if (e > eExpected)
179 {
180 cout << "half = " << h << ", rounded = " << r << ", error = " << e
181 << ", expected error = " << eExpected << endl;
182
183 printBits (cout, h);
184 cout << endl;
185 printBits (cout, r);
186 cout << endl;
187
188 assert (false);
189 }
190
191 if (e > eMax)
192 eMax = e;
193 }
194
195 cout << "max error = " << eMax << endl;
196 cout << "max expected error = " << eExpected << endl;
197 cout << "ok\n\n" << flush;
198 }
199
200 } // namespace
201
202 void
203 testRoundingError()
204 {
205 testNormalizedRounding (10);
206 testDenormalizedRounding (10);
207 testNormalizedRounding (9);
208 testDenormalizedRounding (9);
209 testNormalizedRounding (1);
210 testDenormalizedRounding (1);
211 testNormalizedRounding (0);
212 testDenormalizedRounding (0);
213 }