1 | /*
|
---|
2 | * Copyright (c) 2019 Matthieu Riolo
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #include <pcut/pcut.h>
|
---|
30 | #include <ieee_double.h>
|
---|
31 |
|
---|
32 | /*
|
---|
33 | * This is a test for double precision 64-bit
|
---|
34 | * floating numbers which are specified
|
---|
35 | * according to IE754
|
---|
36 | *
|
---|
37 | * A very useful JS calculator for IEEE 754 numbers
|
---|
38 | * for 64bits
|
---|
39 | * weitz.de/ieee/
|
---|
40 | *
|
---|
41 | * for 32 bits
|
---|
42 | * www.h-schmidt.net/FloatConverter/IEEE754.html
|
---|
43 | */
|
---|
44 |
|
---|
45 | union {
|
---|
46 | uint64_t integer;
|
---|
47 | double floating;
|
---|
48 | } ieee_double_bits;
|
---|
49 |
|
---|
50 | const int EXP_BIAS = 1023;
|
---|
51 | const int EXP_BIAS_UNDERFLOWED = 1075;
|
---|
52 | const int SIGN_SHIFT = 52;
|
---|
53 | const uint64_t HIDDEN_BIT = 1ULL << SIGN_SHIFT;
|
---|
54 |
|
---|
55 | PCUT_INIT;
|
---|
56 |
|
---|
57 | PCUT_TEST_SUITE(ieee_double);
|
---|
58 |
|
---|
59 | PCUT_TEST(extract_ieee_sizeof_double)
|
---|
60 | {
|
---|
61 | PCUT_ASSERT_INT_EQUALS(8, sizeof(double));
|
---|
62 | }
|
---|
63 |
|
---|
64 | PCUT_TEST(extract_ieee_double_pos_infinity)
|
---|
65 | {
|
---|
66 | ieee_double_bits.integer = 0x7FF0000000000000ULL;
|
---|
67 | ieee_double_t d = extract_ieee_double(ieee_double_bits.floating);
|
---|
68 |
|
---|
69 | PCUT_ASSERT_TRUE(d.is_special);
|
---|
70 | PCUT_ASSERT_FALSE(d.is_nan);
|
---|
71 | PCUT_ASSERT_TRUE(d.is_infinity);
|
---|
72 | PCUT_ASSERT_FALSE(d.is_negative);
|
---|
73 | PCUT_ASSERT_TRUE(d.is_denormal);
|
---|
74 | PCUT_ASSERT_FALSE(d.is_accuracy_step);
|
---|
75 |
|
---|
76 | PCUT_ASSERT_INT_EQUALS(0, d.pos_val.significand);
|
---|
77 | PCUT_ASSERT_INT_EQUALS(0, d.pos_val.exponent);
|
---|
78 | }
|
---|
79 |
|
---|
80 | PCUT_TEST(extract_ieee_double_neg_infinity)
|
---|
81 | {
|
---|
82 | ieee_double_bits.integer = 0xFFF0000000000000ULL;
|
---|
83 | ieee_double_t d = extract_ieee_double(ieee_double_bits.floating);
|
---|
84 |
|
---|
85 | PCUT_ASSERT_TRUE(d.is_special);
|
---|
86 | PCUT_ASSERT_FALSE(d.is_nan);
|
---|
87 | PCUT_ASSERT_TRUE(d.is_infinity);
|
---|
88 | PCUT_ASSERT_TRUE(d.is_negative);
|
---|
89 | PCUT_ASSERT_TRUE(d.is_denormal);
|
---|
90 | PCUT_ASSERT_FALSE(d.is_accuracy_step);
|
---|
91 |
|
---|
92 | PCUT_ASSERT_INT_EQUALS(0, d.pos_val.significand);
|
---|
93 | PCUT_ASSERT_INT_EQUALS(0, d.pos_val.exponent);
|
---|
94 | }
|
---|
95 |
|
---|
96 | PCUT_TEST(extract_ieee_double_nan)
|
---|
97 | {
|
---|
98 | ieee_double_bits.integer = 0xFFFFFFFFFFFFFFFFULL;
|
---|
99 | ieee_double_t d = extract_ieee_double(ieee_double_bits.floating);
|
---|
100 |
|
---|
101 | PCUT_ASSERT_TRUE(d.is_special);
|
---|
102 | PCUT_ASSERT_TRUE(d.is_nan);
|
---|
103 | PCUT_ASSERT_FALSE(d.is_infinity);
|
---|
104 | PCUT_ASSERT_TRUE(d.is_negative);
|
---|
105 | PCUT_ASSERT_TRUE(d.is_denormal);
|
---|
106 | PCUT_ASSERT_FALSE(d.is_accuracy_step);
|
---|
107 |
|
---|
108 | PCUT_ASSERT_INT_EQUALS(0, d.pos_val.significand);
|
---|
109 | PCUT_ASSERT_INT_EQUALS(0, d.pos_val.exponent);
|
---|
110 | }
|
---|
111 |
|
---|
112 | PCUT_TEST(extract_ieee_double_pos_zero)
|
---|
113 | {
|
---|
114 | ieee_double_bits.integer = 0x0000000000000000ULL;
|
---|
115 | ieee_double_t d = extract_ieee_double(ieee_double_bits.floating);
|
---|
116 |
|
---|
117 | PCUT_ASSERT_FALSE(d.is_special);
|
---|
118 | PCUT_ASSERT_FALSE(d.is_nan);
|
---|
119 | PCUT_ASSERT_FALSE(d.is_infinity);
|
---|
120 | PCUT_ASSERT_FALSE(d.is_negative);
|
---|
121 | PCUT_ASSERT_TRUE(d.is_denormal);
|
---|
122 | PCUT_ASSERT_FALSE(d.is_accuracy_step);
|
---|
123 |
|
---|
124 | PCUT_ASSERT_INT_EQUALS(0, d.pos_val.significand);
|
---|
125 | PCUT_ASSERT_INT_EQUALS(-EXP_BIAS_UNDERFLOWED, d.pos_val.exponent);
|
---|
126 | }
|
---|
127 |
|
---|
128 | PCUT_TEST(extract_ieee_double_neg_zero)
|
---|
129 | {
|
---|
130 | ieee_double_bits.integer = 0x8000000000000000ULL;
|
---|
131 | ieee_double_t d = extract_ieee_double(ieee_double_bits.floating);
|
---|
132 |
|
---|
133 | PCUT_ASSERT_FALSE(d.is_special);
|
---|
134 | PCUT_ASSERT_FALSE(d.is_nan);
|
---|
135 | PCUT_ASSERT_FALSE(d.is_infinity);
|
---|
136 |
|
---|
137 | PCUT_ASSERT_TRUE(d.is_negative);
|
---|
138 | PCUT_ASSERT_TRUE(d.is_denormal);
|
---|
139 | PCUT_ASSERT_FALSE(d.is_accuracy_step);
|
---|
140 |
|
---|
141 | PCUT_ASSERT_INT_EQUALS(0, d.pos_val.significand);
|
---|
142 | PCUT_ASSERT_INT_EQUALS(-EXP_BIAS_UNDERFLOWED, d.pos_val.exponent);
|
---|
143 | }
|
---|
144 |
|
---|
145 | PCUT_TEST(extract_ieee_double_normal_pos_one)
|
---|
146 | {
|
---|
147 | ieee_double_bits.integer = 0x3FF0000000000000ULL;
|
---|
148 | ieee_double_t d = extract_ieee_double(ieee_double_bits.floating);
|
---|
149 |
|
---|
150 | PCUT_ASSERT_FALSE(d.is_special);
|
---|
151 | PCUT_ASSERT_FALSE(d.is_nan);
|
---|
152 | PCUT_ASSERT_FALSE(d.is_infinity);
|
---|
153 | PCUT_ASSERT_FALSE(d.is_negative);
|
---|
154 | PCUT_ASSERT_FALSE(d.is_denormal);
|
---|
155 | PCUT_ASSERT_TRUE(d.is_accuracy_step);
|
---|
156 |
|
---|
157 | PCUT_ASSERT_INT_EQUALS(HIDDEN_BIT, d.pos_val.significand);
|
---|
158 | PCUT_ASSERT_INT_EQUALS(EXP_BIAS - EXP_BIAS_UNDERFLOWED, d.pos_val.exponent);
|
---|
159 | }
|
---|
160 |
|
---|
161 | PCUT_TEST(extract_ieee_double_normal_neg_one)
|
---|
162 | {
|
---|
163 | ieee_double_bits.integer = 0xBFF0000000000000ULL;
|
---|
164 | ieee_double_t d = extract_ieee_double(ieee_double_bits.floating);
|
---|
165 |
|
---|
166 | PCUT_ASSERT_FALSE(d.is_special);
|
---|
167 | PCUT_ASSERT_FALSE(d.is_nan);
|
---|
168 | PCUT_ASSERT_FALSE(d.is_infinity);
|
---|
169 | PCUT_ASSERT_TRUE(d.is_negative);
|
---|
170 | PCUT_ASSERT_FALSE(d.is_denormal);
|
---|
171 | PCUT_ASSERT_TRUE(d.is_accuracy_step);
|
---|
172 |
|
---|
173 | PCUT_ASSERT_INT_EQUALS(HIDDEN_BIT, d.pos_val.significand);
|
---|
174 | PCUT_ASSERT_INT_EQUALS(EXP_BIAS - EXP_BIAS_UNDERFLOWED, d.pos_val.exponent);
|
---|
175 | }
|
---|
176 |
|
---|
177 | PCUT_TEST(extract_ieee_double_denormal_pos_smallest)
|
---|
178 | {
|
---|
179 | ieee_double_bits.integer = 0x0000000000000001ULL;
|
---|
180 | ieee_double_t d = extract_ieee_double(ieee_double_bits.floating);
|
---|
181 |
|
---|
182 | PCUT_ASSERT_FALSE(d.is_special);
|
---|
183 | PCUT_ASSERT_FALSE(d.is_nan);
|
---|
184 | PCUT_ASSERT_FALSE(d.is_infinity);
|
---|
185 | PCUT_ASSERT_FALSE(d.is_negative);
|
---|
186 | PCUT_ASSERT_TRUE(d.is_denormal);
|
---|
187 | PCUT_ASSERT_FALSE(d.is_accuracy_step);
|
---|
188 |
|
---|
189 | PCUT_ASSERT_INT_EQUALS(1, d.pos_val.significand);
|
---|
190 | PCUT_ASSERT_INT_EQUALS(1 - EXP_BIAS_UNDERFLOWED, d.pos_val.exponent);
|
---|
191 | }
|
---|
192 |
|
---|
193 | PCUT_TEST(extract_ieee_double_denormal_neg_smallest)
|
---|
194 | {
|
---|
195 | ieee_double_bits.integer = 0x8000000000000001ULL;
|
---|
196 | ieee_double_t d = extract_ieee_double(ieee_double_bits.floating);
|
---|
197 |
|
---|
198 | PCUT_ASSERT_FALSE(d.is_special);
|
---|
199 | PCUT_ASSERT_FALSE(d.is_nan);
|
---|
200 | PCUT_ASSERT_FALSE(d.is_infinity);
|
---|
201 | PCUT_ASSERT_TRUE(d.is_negative);
|
---|
202 | PCUT_ASSERT_TRUE(d.is_denormal);
|
---|
203 | PCUT_ASSERT_FALSE(d.is_accuracy_step);
|
---|
204 |
|
---|
205 | PCUT_ASSERT_INT_EQUALS(1, d.pos_val.significand);
|
---|
206 | PCUT_ASSERT_INT_EQUALS(1 - EXP_BIAS_UNDERFLOWED, d.pos_val.exponent);
|
---|
207 | }
|
---|
208 |
|
---|
209 | PCUT_TEST(extract_ieee_double_denormal_pos_largest)
|
---|
210 | {
|
---|
211 | ieee_double_bits.integer = 0x000FFFFFFFFFFFFFULL;
|
---|
212 | ieee_double_t d = extract_ieee_double(ieee_double_bits.floating);
|
---|
213 |
|
---|
214 | PCUT_ASSERT_FALSE(d.is_special);
|
---|
215 | PCUT_ASSERT_FALSE(d.is_nan);
|
---|
216 | PCUT_ASSERT_FALSE(d.is_infinity);
|
---|
217 | PCUT_ASSERT_FALSE(d.is_negative);
|
---|
218 | PCUT_ASSERT_TRUE(d.is_denormal);
|
---|
219 | PCUT_ASSERT_FALSE(d.is_accuracy_step);
|
---|
220 |
|
---|
221 | PCUT_ASSERT_INT_EQUALS(0xFFFFFFFFFFFFFULL, d.pos_val.significand);
|
---|
222 | PCUT_ASSERT_INT_EQUALS(1 - EXP_BIAS_UNDERFLOWED, d.pos_val.exponent);
|
---|
223 | }
|
---|
224 |
|
---|
225 | PCUT_TEST(extract_ieee_double_denormal_neg_largest)
|
---|
226 | {
|
---|
227 | ieee_double_bits.integer = 0x800FFFFFFFFFFFFFULL;
|
---|
228 | ieee_double_t d = extract_ieee_double(ieee_double_bits.floating);
|
---|
229 |
|
---|
230 | PCUT_ASSERT_FALSE(d.is_special);
|
---|
231 | PCUT_ASSERT_FALSE(d.is_nan);
|
---|
232 | PCUT_ASSERT_FALSE(d.is_infinity);
|
---|
233 | PCUT_ASSERT_TRUE(d.is_negative);
|
---|
234 | PCUT_ASSERT_TRUE(d.is_denormal);
|
---|
235 | PCUT_ASSERT_FALSE(d.is_accuracy_step);
|
---|
236 | PCUT_ASSERT_INT_EQUALS(0xFFFFFFFFFFFFFULL, d.pos_val.significand);
|
---|
237 | PCUT_ASSERT_INT_EQUALS(1 - EXP_BIAS_UNDERFLOWED, d.pos_val.exponent);
|
---|
238 | }
|
---|
239 |
|
---|
240 | PCUT_TEST(extract_ieee_double_normal_pos_smallest)
|
---|
241 | {
|
---|
242 | ieee_double_bits.integer = 0x0010000000000000ULL;
|
---|
243 | ieee_double_t d = extract_ieee_double(ieee_double_bits.floating);
|
---|
244 |
|
---|
245 | PCUT_ASSERT_FALSE(d.is_special);
|
---|
246 | PCUT_ASSERT_FALSE(d.is_nan);
|
---|
247 | PCUT_ASSERT_FALSE(d.is_infinity);
|
---|
248 | PCUT_ASSERT_FALSE(d.is_negative);
|
---|
249 | PCUT_ASSERT_FALSE(d.is_denormal);
|
---|
250 | PCUT_ASSERT_FALSE(d.is_accuracy_step);
|
---|
251 |
|
---|
252 | PCUT_ASSERT_INT_EQUALS(HIDDEN_BIT, d.pos_val.significand);
|
---|
253 | PCUT_ASSERT_INT_EQUALS(1 - EXP_BIAS_UNDERFLOWED, d.pos_val.exponent);
|
---|
254 | }
|
---|
255 |
|
---|
256 | PCUT_TEST(extract_ieee_double_normal_neg_smallest)
|
---|
257 | {
|
---|
258 | ieee_double_bits.integer = 0x8010000000000000ULL;
|
---|
259 | ieee_double_t d = extract_ieee_double(ieee_double_bits.floating);
|
---|
260 |
|
---|
261 | PCUT_ASSERT_FALSE(d.is_special);
|
---|
262 | PCUT_ASSERT_FALSE(d.is_nan);
|
---|
263 | PCUT_ASSERT_FALSE(d.is_infinity);
|
---|
264 | PCUT_ASSERT_TRUE(d.is_negative);
|
---|
265 | PCUT_ASSERT_FALSE(d.is_denormal);
|
---|
266 | PCUT_ASSERT_FALSE(d.is_accuracy_step);
|
---|
267 |
|
---|
268 | PCUT_ASSERT_INT_EQUALS(HIDDEN_BIT, d.pos_val.significand);
|
---|
269 | PCUT_ASSERT_INT_EQUALS(1 - EXP_BIAS_UNDERFLOWED, d.pos_val.exponent);
|
---|
270 | }
|
---|
271 |
|
---|
272 | PCUT_TEST(extract_ieee_double_normal_pos_largest)
|
---|
273 | {
|
---|
274 | ieee_double_bits.integer = 0x7FEFFFFFFFFFFFFFULL;
|
---|
275 | ieee_double_t d = extract_ieee_double(ieee_double_bits.floating);
|
---|
276 |
|
---|
277 | PCUT_ASSERT_FALSE(d.is_special);
|
---|
278 | PCUT_ASSERT_FALSE(d.is_nan);
|
---|
279 | PCUT_ASSERT_FALSE(d.is_infinity);
|
---|
280 | PCUT_ASSERT_FALSE(d.is_negative);
|
---|
281 | PCUT_ASSERT_FALSE(d.is_denormal);
|
---|
282 | PCUT_ASSERT_FALSE(d.is_accuracy_step);
|
---|
283 |
|
---|
284 | PCUT_ASSERT_INT_EQUALS(0xFFFFFFFFFFFFFULL + HIDDEN_BIT, d.pos_val.significand);
|
---|
285 | PCUT_ASSERT_INT_EQUALS(0x7FEULL - EXP_BIAS_UNDERFLOWED, d.pos_val.exponent);
|
---|
286 | }
|
---|
287 |
|
---|
288 | PCUT_TEST(extract_ieee_double_normal_neg_largest)
|
---|
289 | {
|
---|
290 | ieee_double_bits.integer = 0xFFEFFFFFFFFFFFFFULL;
|
---|
291 | ieee_double_t d = extract_ieee_double(ieee_double_bits.floating);
|
---|
292 |
|
---|
293 | PCUT_ASSERT_FALSE(d.is_special);
|
---|
294 | PCUT_ASSERT_FALSE(d.is_nan);
|
---|
295 | PCUT_ASSERT_FALSE(d.is_infinity);
|
---|
296 | PCUT_ASSERT_TRUE(d.is_negative);
|
---|
297 | PCUT_ASSERT_FALSE(d.is_denormal);
|
---|
298 | PCUT_ASSERT_FALSE(d.is_accuracy_step);
|
---|
299 |
|
---|
300 | PCUT_ASSERT_INT_EQUALS(0xFFFFFFFFFFFFFULL + HIDDEN_BIT, d.pos_val.significand);
|
---|
301 | PCUT_ASSERT_INT_EQUALS(0x7FEULL - EXP_BIAS_UNDERFLOWED, d.pos_val.exponent);
|
---|
302 | }
|
---|
303 |
|
---|
304 | PCUT_EXPORT(ieee_double);
|
---|