t_ldexp.c revision 1.12 1 /* $NetBSD: t_ldexp.c,v 1.12 2014/03/03 10:39:08 martin Exp $ */
2
3 /*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jukka Ruohonen.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_ldexp.c,v 1.12 2014/03/03 10:39:08 martin Exp $");
33
34 #include <sys/param.h>
35
36 #include <atf-c.h>
37 #include <atf-c/config.h>
38
39 #include <math.h>
40 #include <limits.h>
41 #include <stdio.h>
42 #include <string.h>
43
44 #define SKIP 9999
45 #define FORMAT "%23.23lg"
46
47 static const int exps[] = { 0, 1, -1, 100, -100 };
48
49 struct ldexp_test {
50 double x;
51 int exp1;
52 int exp2;
53 const char *result;
54 };
55
56 struct ldexp_test ldexp_basic[] = {
57 { 1.0, 5, SKIP, " 32" },
58 { 1.0, 1022, SKIP, "4.4942328371557897693233e+307" },
59 { 1.0, 1023, -1, "4.4942328371557897693233e+307" },
60 { 1.0, 1023, SKIP, "8.9884656743115795386465e+307" },
61 { 1.0, 1022, 1, "8.9884656743115795386465e+307" },
62 { 1.0, -1022, 2045, "8.9884656743115795386465e+307" },
63 { 1.0, -5, SKIP, " 0.03125" },
64 { 1.0, -1021, SKIP, "4.4501477170144027661805e-308" },
65 { 1.0, -1022, 1, "4.4501477170144027661805e-308" },
66 { 1.0, -1022, SKIP, "2.2250738585072013830902e-308" },
67 { 1.0, -1021, -1, "2.2250738585072013830902e-308" },
68 { 1.0, 1023, -2045, "2.2250738585072013830902e-308" },
69 { 1.0, 1023, -1023, " 1" },
70 { 1.0, -1022, 1022, " 1" },
71 { 0, 0, 0, NULL }
72 };
73
74 struct ldexp_test ldexp_zero[] = {
75 { 0.0, -1, SKIP, " 0" },
76 { 0.0, 0, SKIP, " 0" },
77 { 0.0, 1, SKIP, " 0" },
78 { 0.0, 1024, SKIP, " 0" },
79 { 0.0, 1025, SKIP, " 0" },
80 { 0.0, -1023, SKIP, " 0" },
81 { 0.0, -1024, SKIP, " 0" },
82 { 0, 0, 0, NULL }
83 };
84
85 struct ldexp_test ldexp_infinity[] = {
86 { 1.0, 1024, -1, " inf" },
87 { 1.0, 1024, 0, " inf" },
88 { 1.0, 1024, 1, " inf" },
89 { -1.0, 1024, -1, " -inf" },
90 { -1.0, 1024, 0, " -inf" },
91 { -1.0, 1024, 1, " -inf" },
92 { 0, 0, 0, NULL }
93 };
94
95 struct ldexp_test ldexp_overflow[] = {
96 { 1.0, 1024, SKIP, " inf" },
97 { 1.0, 1023, 1, " inf" },
98 { 1.0, -1022, 2046, " inf" },
99 { 1.0, 1025, SKIP, " inf" },
100 { -1.0, 1024, SKIP, " -inf" },
101 { -1.0, 1023, 1, " -inf" },
102 { -1.0, -1022, 2046, " -inf" },
103 { -1.0, 1025, SKIP, " -inf" },
104 { 0, 0, 0, NULL }
105 };
106
107 struct ldexp_test ldexp_denormal[] = {
108 { 1.0, -1023, SKIP, "1.1125369292536006915451e-308" },
109 { 1.0, -1022, -1, "1.1125369292536006915451e-308" },
110 { 1.0, 1023, -2046, "1.1125369292536006915451e-308" },
111 { 1.0, -1024, SKIP, "5.5626846462680034577256e-309" },
112 { 1.0, -1074, SKIP, "4.9406564584124654417657e-324" },
113 { -1.0, -1023, SKIP, "-1.1125369292536006915451e-308" },
114 { -1.0, -1022, -1, "-1.1125369292536006915451e-308" },
115 { -1.0, 1023, -2046, "-1.1125369292536006915451e-308" },
116 { -1.0, -1024, SKIP, "-5.5626846462680034577256e-309" },
117 { -1.0, -1074, SKIP, "-4.9406564584124654417657e-324" },
118 { 0, 0, 0, NULL }
119 };
120
121 struct ldexp_test ldexp_underflow[] = {
122 { 1.0, -1075, SKIP, " 0" },
123 { 1.0, -1074, -1, " 0" },
124 { 1.0, 1023, -2098, " 0" },
125 { 1.0, -1076, SKIP, " 0" },
126 { -1.0, -1075, SKIP, " -0" },
127 { -1.0, -1074, -1, " -0" },
128 { -1.0, 1023, -2098, " -0" },
129 { -1.0, -1076, SKIP, " -0" },
130 { 0, 0, 0, NULL }
131 };
132
133 struct ldexp_test ldexp_denormal_large[] = {
134 { 1.0, -1028, 1024, " 0.0625" },
135 { 1.0, -1028, 1025, " 0.125" },
136 { 1.0, -1028, 1026, " 0.25" },
137 { 1.0, -1028, 1027, " 0.5" },
138 { 1.0, -1028, 1028, " 1" },
139 { 1.0, -1028, 1029, " 2" },
140 { 1.0, -1028, 1030, " 4" },
141 { 1.0, -1028, 1040, " 4096" },
142 { 1.0, -1028, 1050, " 4194304" },
143 { 1.0, -1028, 1060, " 4294967296" },
144 { 1.0, -1028, 1100, " 4722366482869645213696" },
145 { 1.0, -1028, 1200, "5.9863107065073783529623e+51" },
146 { 1.0, -1028, 1300, "7.5885503602567541832791e+81" },
147 { 1.0, -1028, 1400, "9.6196304190416209014353e+111" },
148 { 1.0, -1028, 1500, "1.2194330274671844653834e+142" },
149 { 1.0, -1028, 1600, "1.5458150092069033378781e+172" },
150 { 1.0, -1028, 1700, "1.9595533242629369747791e+202" },
151 { 1.0, -1028, 1800, "2.4840289476811342962384e+232" },
152 { 1.0, -1028, 1900, "3.1488807865122869393369e+262" },
153 { 1.0, -1028, 2000, "3.9916806190694396233127e+292" },
154 { 1.0, -1028, 2046, "2.808895523222368605827e+306" },
155 { 1.0, -1028, 2047, "5.6177910464447372116541e+306" },
156 { 1.0, -1028, 2048, "1.1235582092889474423308e+307" },
157 { 1.0, -1028, 2049, "2.2471164185778948846616e+307" },
158 { 1.0, -1028, 2050, "4.4942328371557897693233e+307" },
159 { 1.0, -1028, 2051, "8.9884656743115795386465e+307" },
160 { 0, 0, 0, NULL }
161 };
162
163 static void
164 run_test(struct ldexp_test *table)
165 {
166 char outbuf[64];
167 size_t i;
168 double v;
169
170 for (i = 0; table->result != NULL; table++, i++) {
171
172 v = ldexp(table->x, table->exp1);
173
174 if (table->exp2 == SKIP)
175 continue;
176
177 v = ldexp(v, table->exp2);
178
179 (void)snprintf(outbuf, sizeof(outbuf), FORMAT, v);
180
181 ATF_CHECK_STREQ_MSG(table->result, outbuf,
182 "Entry %zu:\n\tExp: \"%s\"\n\tAct: \"%s\"",
183 i, table->result, outbuf);
184 }
185 }
186
187 /*
188 * ldexp(3)
189 */
190 ATF_TC(ldexp_exp2);
191 ATF_TC_HEAD(ldexp_exp2, tc)
192 {
193 atf_tc_set_md_var(tc, "descr", "Test ldexp(x, n) == x * exp2(n)");
194 }
195
196 ATF_TC_BODY(ldexp_exp2, tc)
197 {
198 const double n[] = { 1, 2, 3, 10, 50, 100 };
199 const double eps = 1.0e-40;
200 const double x = 12.0;
201 double y;
202 size_t i;
203
204 for (i = 0; i < __arraycount(n); i++) {
205
206 y = ldexp(x, n[i]);
207
208 if (fabs(y - (x * exp2(n[i]))) > eps) {
209 atf_tc_fail_nonfatal("ldexp(%0.01f, %0.01f) "
210 "!= %0.01f * exp2(%0.01f)", x, n[i], x, n[i]);
211 }
212 }
213 }
214
215 ATF_TC(ldexp_nan);
216 ATF_TC_HEAD(ldexp_nan, tc)
217 {
218 atf_tc_set_md_var(tc, "descr", "Test ldexp(NaN) == NaN");
219 }
220
221 ATF_TC_BODY(ldexp_nan, tc)
222 {
223 const double x = 0.0L / 0.0L;
224 double y;
225 size_t i;
226
227 ATF_REQUIRE(isnan(x) != 0);
228
229 for (i = 0; i < __arraycount(exps); i++) {
230 y = ldexp(x, exps[i]);
231 ATF_CHECK(isnan(y) != 0);
232 }
233 }
234
235 ATF_TC(ldexp_inf_neg);
236 ATF_TC_HEAD(ldexp_inf_neg, tc)
237 {
238 atf_tc_set_md_var(tc, "descr", "Test ldexp(-Inf) == -Inf");
239 }
240
241 ATF_TC_BODY(ldexp_inf_neg, tc)
242 {
243 const double x = -1.0L / 0.0L;
244 size_t i;
245
246 for (i = 0; i < __arraycount(exps); i++)
247 ATF_CHECK(ldexp(x, exps[i]) == x);
248 }
249
250 ATF_TC(ldexp_inf_pos);
251 ATF_TC_HEAD(ldexp_inf_pos, tc)
252 {
253 atf_tc_set_md_var(tc, "descr", "Test ldexp(+Inf) == +Inf");
254 }
255
256 ATF_TC_BODY(ldexp_inf_pos, tc)
257 {
258 const double x = 1.0L / 0.0L;
259 size_t i;
260
261 for (i = 0; i < __arraycount(exps); i++)
262 ATF_CHECK(ldexp(x, exps[i]) == x);
263 }
264
265 ATF_TC(ldexp_zero_neg);
266 ATF_TC_HEAD(ldexp_zero_neg, tc)
267 {
268 atf_tc_set_md_var(tc, "descr", "Test ldexp(-0.0) == -0.0");
269 }
270
271 ATF_TC_BODY(ldexp_zero_neg, tc)
272 {
273 const double x = -0.0L;
274 double y;
275 size_t i;
276
277 ATF_REQUIRE(signbit(x) != 0);
278
279 for (i = 0; i < __arraycount(exps); i++) {
280 y = ldexp(x, exps[i]);
281 ATF_CHECK(x == y);
282 ATF_CHECK(signbit(y) != 0);
283 }
284 }
285
286 ATF_TC(ldexp_zero_pos);
287 ATF_TC_HEAD(ldexp_zero_pos, tc)
288 {
289 atf_tc_set_md_var(tc, "descr", "Test ldexp(+0.0) == +0.0");
290 }
291
292 ATF_TC_BODY(ldexp_zero_pos, tc)
293 {
294 const double x = 0.0L;
295 double y;
296 size_t i;
297
298 ATF_REQUIRE(signbit(x) == 0);
299
300 for (i = 0; i < __arraycount(exps); i++) {
301 y = ldexp(x, exps[i]);
302 ATF_CHECK(x == y);
303 ATF_CHECK(signbit(y) == 0);
304 }
305 }
306
307 /*
308 * ldexpf(3)
309 */
310
311 ATF_TC(ldexpf_exp2f);
312 ATF_TC_HEAD(ldexpf_exp2f, tc)
313 {
314 atf_tc_set_md_var(tc, "descr", "Test ldexpf(x, n) == x * exp2f(n)");
315 }
316
317 ATF_TC_BODY(ldexpf_exp2f, tc)
318 {
319 const float n[] = { 1, 2, 3, 10, 50, 100 };
320 const float eps = 1.0e-9;
321 const float x = 12.0;
322 float y;
323 size_t i;
324
325 for (i = 0; i < __arraycount(n); i++) {
326
327 y = ldexpf(x, n[i]);
328
329 if (fabsf(y - (x * exp2f(n[i]))) > eps) {
330 atf_tc_fail_nonfatal("ldexpf(%0.01f, %0.01f) "
331 "!= %0.01f * exp2f(%0.01f)", x, n[i], x, n[i]);
332 }
333 }
334 }
335
336 ATF_TC(ldexpf_nan);
337 ATF_TC_HEAD(ldexpf_nan, tc)
338 {
339 atf_tc_set_md_var(tc, "descr", "Test ldexpf(NaN) == NaN");
340 }
341
342 ATF_TC_BODY(ldexpf_nan, tc)
343 {
344 const float x = 0.0L / 0.0L;
345 float y;
346 size_t i;
347
348 ATF_REQUIRE(isnan(x) != 0);
349
350 for (i = 0; i < __arraycount(exps); i++) {
351 y = ldexpf(x, exps[i]);
352 ATF_CHECK(isnan(y) != 0);
353 }
354 }
355
356 ATF_TC(ldexpf_inf_neg);
357 ATF_TC_HEAD(ldexpf_inf_neg, tc)
358 {
359 atf_tc_set_md_var(tc, "descr", "Test ldexpf(-Inf) == -Inf");
360 }
361
362 ATF_TC_BODY(ldexpf_inf_neg, tc)
363 {
364 const float x = -1.0L / 0.0L;
365 size_t i;
366
367 for (i = 0; i < __arraycount(exps); i++)
368 ATF_CHECK(ldexpf(x, exps[i]) == x);
369 }
370
371 ATF_TC(ldexpf_inf_pos);
372 ATF_TC_HEAD(ldexpf_inf_pos, tc)
373 {
374 atf_tc_set_md_var(tc, "descr", "Test ldexpf(+Inf) == +Inf");
375 }
376
377 ATF_TC_BODY(ldexpf_inf_pos, tc)
378 {
379 const float x = 1.0L / 0.0L;
380 size_t i;
381
382 for (i = 0; i < __arraycount(exps); i++)
383 ATF_CHECK(ldexpf(x, exps[i]) == x);
384 }
385
386 ATF_TC(ldexpf_zero_neg);
387 ATF_TC_HEAD(ldexpf_zero_neg, tc)
388 {
389 atf_tc_set_md_var(tc, "descr", "Test ldexpf(-0.0) == -0.0");
390 }
391
392 ATF_TC_BODY(ldexpf_zero_neg, tc)
393 {
394 const float x = -0.0L;
395 float y;
396 size_t i;
397
398 ATF_REQUIRE(signbit(x) != 0);
399
400 for (i = 0; i < __arraycount(exps); i++) {
401 y = ldexpf(x, exps[i]);
402 ATF_CHECK(x == y);
403 ATF_CHECK(signbit(y) != 0);
404 }
405 }
406
407 ATF_TC(ldexpf_zero_pos);
408 ATF_TC_HEAD(ldexpf_zero_pos, tc)
409 {
410 atf_tc_set_md_var(tc, "descr", "Test ldexpf(+0.0) == +0.0");
411 }
412
413 ATF_TC_BODY(ldexpf_zero_pos, tc)
414 {
415 const float x = 0.0L;
416 float y;
417 size_t i;
418
419 ATF_REQUIRE(signbit(x) == 0);
420
421 for (i = 0; i < __arraycount(exps); i++) {
422 y = ldexpf(x, exps[i]);
423 ATF_CHECK(x == y);
424 ATF_CHECK(signbit(y) == 0);
425 }
426 }
427
428 #define TEST(name, desc) \
429 ATF_TC(name); \
430 ATF_TC_HEAD(name, tc) \
431 { \
432 \
433 atf_tc_set_md_var(tc, "descr", \
434 "Test ldexp(3) for " ___STRING(desc)); \
435 } \
436 ATF_TC_BODY(name, tc) \
437 { \
438 if (strcmp("vax", MACHINE_ARCH) == 0) \
439 atf_tc_skip("Test not valid for " MACHINE_ARCH); \
440 run_test(name); \
441 }
442
443 TEST(ldexp_basic, basics)
444 TEST(ldexp_zero, zero)
445 TEST(ldexp_infinity, infinity)
446 TEST(ldexp_overflow, overflow)
447 TEST(ldexp_denormal, denormal)
448 TEST(ldexp_denormal_large, large)
449 TEST(ldexp_underflow, underflow)
450
451 ATF_TP_ADD_TCS(tp)
452 {
453
454 ATF_TP_ADD_TC(tp, ldexp_basic);
455 ATF_TP_ADD_TC(tp, ldexp_zero);
456 ATF_TP_ADD_TC(tp, ldexp_infinity);
457 ATF_TP_ADD_TC(tp, ldexp_overflow);
458 ATF_TP_ADD_TC(tp, ldexp_denormal);
459 ATF_TP_ADD_TC(tp, ldexp_underflow);
460 ATF_TP_ADD_TC(tp, ldexp_denormal_large);
461
462 ATF_TP_ADD_TC(tp, ldexp_exp2);
463 ATF_TP_ADD_TC(tp, ldexp_nan);
464 ATF_TP_ADD_TC(tp, ldexp_inf_neg);
465 ATF_TP_ADD_TC(tp, ldexp_inf_pos);
466 ATF_TP_ADD_TC(tp, ldexp_zero_neg);
467 ATF_TP_ADD_TC(tp, ldexp_zero_pos);
468
469 ATF_TP_ADD_TC(tp, ldexpf_exp2f);
470 ATF_TP_ADD_TC(tp, ldexpf_nan);
471 ATF_TP_ADD_TC(tp, ldexpf_inf_neg);
472 ATF_TP_ADD_TC(tp, ldexpf_inf_pos);
473 ATF_TP_ADD_TC(tp, ldexpf_zero_neg);
474 ATF_TP_ADD_TC(tp, ldexpf_zero_pos);
475
476 return atf_no_error();
477 }
478