t_ldexp.c revision 1.13 1 /* $NetBSD: t_ldexp.c,v 1.13 2014/03/12 21:40:07 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.13 2014/03/12 21:40:07 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 #if __DBL_MIN_10_EXP__ <= -40
200 const double eps = 1.0e-40;
201 #else
202 const double eps = __DBL_MIN__*4.0;
203 #endif
204 const double x = 12.0;
205 double y;
206 size_t i;
207
208 for (i = 0; i < __arraycount(n); i++) {
209
210 y = ldexp(x, n[i]);
211
212 if (fabs(y - (x * exp2(n[i]))) > eps) {
213 atf_tc_fail_nonfatal("ldexp(%0.01f, %0.01f) "
214 "!= %0.01f * exp2(%0.01f)", x, n[i], x, n[i]);
215 }
216 }
217 }
218
219 ATF_TC(ldexp_nan);
220 ATF_TC_HEAD(ldexp_nan, tc)
221 {
222 atf_tc_set_md_var(tc, "descr", "Test ldexp(NaN) == NaN");
223 }
224
225 ATF_TC_BODY(ldexp_nan, tc)
226 {
227 const double x = 0.0L / 0.0L;
228 double y;
229 size_t i;
230
231 ATF_REQUIRE(isnan(x) != 0);
232
233 for (i = 0; i < __arraycount(exps); i++) {
234 y = ldexp(x, exps[i]);
235 ATF_CHECK(isnan(y) != 0);
236 }
237 }
238
239 ATF_TC(ldexp_inf_neg);
240 ATF_TC_HEAD(ldexp_inf_neg, tc)
241 {
242 atf_tc_set_md_var(tc, "descr", "Test ldexp(-Inf) == -Inf");
243 }
244
245 ATF_TC_BODY(ldexp_inf_neg, tc)
246 {
247 const double x = -1.0L / 0.0L;
248 size_t i;
249
250 for (i = 0; i < __arraycount(exps); i++)
251 ATF_CHECK(ldexp(x, exps[i]) == x);
252 }
253
254 ATF_TC(ldexp_inf_pos);
255 ATF_TC_HEAD(ldexp_inf_pos, tc)
256 {
257 atf_tc_set_md_var(tc, "descr", "Test ldexp(+Inf) == +Inf");
258 }
259
260 ATF_TC_BODY(ldexp_inf_pos, tc)
261 {
262 const double x = 1.0L / 0.0L;
263 size_t i;
264
265 for (i = 0; i < __arraycount(exps); i++)
266 ATF_CHECK(ldexp(x, exps[i]) == x);
267 }
268
269 ATF_TC(ldexp_zero_neg);
270 ATF_TC_HEAD(ldexp_zero_neg, tc)
271 {
272 atf_tc_set_md_var(tc, "descr", "Test ldexp(-0.0) == -0.0");
273 }
274
275 ATF_TC_BODY(ldexp_zero_neg, tc)
276 {
277 const double x = -0.0L;
278 double y;
279 size_t i;
280
281 ATF_REQUIRE(signbit(x) != 0);
282
283 for (i = 0; i < __arraycount(exps); i++) {
284 y = ldexp(x, exps[i]);
285 ATF_CHECK(x == y);
286 ATF_CHECK(signbit(y) != 0);
287 }
288 }
289
290 ATF_TC(ldexp_zero_pos);
291 ATF_TC_HEAD(ldexp_zero_pos, tc)
292 {
293 atf_tc_set_md_var(tc, "descr", "Test ldexp(+0.0) == +0.0");
294 }
295
296 ATF_TC_BODY(ldexp_zero_pos, tc)
297 {
298 const double x = 0.0L;
299 double y;
300 size_t i;
301
302 ATF_REQUIRE(signbit(x) == 0);
303
304 for (i = 0; i < __arraycount(exps); i++) {
305 y = ldexp(x, exps[i]);
306 ATF_CHECK(x == y);
307 ATF_CHECK(signbit(y) == 0);
308 }
309 }
310
311 /*
312 * ldexpf(3)
313 */
314
315 ATF_TC(ldexpf_exp2f);
316 ATF_TC_HEAD(ldexpf_exp2f, tc)
317 {
318 atf_tc_set_md_var(tc, "descr", "Test ldexpf(x, n) == x * exp2f(n)");
319 }
320
321 ATF_TC_BODY(ldexpf_exp2f, tc)
322 {
323 const float n[] = { 1, 2, 3, 10, 50, 100 };
324 const float eps = 1.0e-9;
325 const float x = 12.0;
326 float y;
327 size_t i;
328
329 for (i = 0; i < __arraycount(n); i++) {
330
331 y = ldexpf(x, n[i]);
332
333 if (fabsf(y - (x * exp2f(n[i]))) > eps) {
334 atf_tc_fail_nonfatal("ldexpf(%0.01f, %0.01f) "
335 "!= %0.01f * exp2f(%0.01f)", x, n[i], x, n[i]);
336 }
337 }
338 }
339
340 ATF_TC(ldexpf_nan);
341 ATF_TC_HEAD(ldexpf_nan, tc)
342 {
343 atf_tc_set_md_var(tc, "descr", "Test ldexpf(NaN) == NaN");
344 }
345
346 ATF_TC_BODY(ldexpf_nan, tc)
347 {
348 const float x = 0.0L / 0.0L;
349 float y;
350 size_t i;
351
352 ATF_REQUIRE(isnan(x) != 0);
353
354 for (i = 0; i < __arraycount(exps); i++) {
355 y = ldexpf(x, exps[i]);
356 ATF_CHECK(isnan(y) != 0);
357 }
358 }
359
360 ATF_TC(ldexpf_inf_neg);
361 ATF_TC_HEAD(ldexpf_inf_neg, tc)
362 {
363 atf_tc_set_md_var(tc, "descr", "Test ldexpf(-Inf) == -Inf");
364 }
365
366 ATF_TC_BODY(ldexpf_inf_neg, tc)
367 {
368 const float x = -1.0L / 0.0L;
369 size_t i;
370
371 for (i = 0; i < __arraycount(exps); i++)
372 ATF_CHECK(ldexpf(x, exps[i]) == x);
373 }
374
375 ATF_TC(ldexpf_inf_pos);
376 ATF_TC_HEAD(ldexpf_inf_pos, tc)
377 {
378 atf_tc_set_md_var(tc, "descr", "Test ldexpf(+Inf) == +Inf");
379 }
380
381 ATF_TC_BODY(ldexpf_inf_pos, tc)
382 {
383 const float x = 1.0L / 0.0L;
384 size_t i;
385
386 for (i = 0; i < __arraycount(exps); i++)
387 ATF_CHECK(ldexpf(x, exps[i]) == x);
388 }
389
390 ATF_TC(ldexpf_zero_neg);
391 ATF_TC_HEAD(ldexpf_zero_neg, tc)
392 {
393 atf_tc_set_md_var(tc, "descr", "Test ldexpf(-0.0) == -0.0");
394 }
395
396 ATF_TC_BODY(ldexpf_zero_neg, tc)
397 {
398 const float x = -0.0L;
399 float y;
400 size_t i;
401
402 ATF_REQUIRE(signbit(x) != 0);
403
404 for (i = 0; i < __arraycount(exps); i++) {
405 y = ldexpf(x, exps[i]);
406 ATF_CHECK(x == y);
407 ATF_CHECK(signbit(y) != 0);
408 }
409 }
410
411 ATF_TC(ldexpf_zero_pos);
412 ATF_TC_HEAD(ldexpf_zero_pos, tc)
413 {
414 atf_tc_set_md_var(tc, "descr", "Test ldexpf(+0.0) == +0.0");
415 }
416
417 ATF_TC_BODY(ldexpf_zero_pos, tc)
418 {
419 const float x = 0.0L;
420 float y;
421 size_t i;
422
423 ATF_REQUIRE(signbit(x) == 0);
424
425 for (i = 0; i < __arraycount(exps); i++) {
426 y = ldexpf(x, exps[i]);
427 ATF_CHECK(x == y);
428 ATF_CHECK(signbit(y) == 0);
429 }
430 }
431
432 #define TEST(name, desc) \
433 ATF_TC(name); \
434 ATF_TC_HEAD(name, tc) \
435 { \
436 \
437 atf_tc_set_md_var(tc, "descr", \
438 "Test ldexp(3) for " ___STRING(desc)); \
439 } \
440 ATF_TC_BODY(name, tc) \
441 { \
442 if (strcmp("vax", MACHINE_ARCH) == 0) \
443 atf_tc_skip("Test not valid for " MACHINE_ARCH); \
444 run_test(name); \
445 }
446
447 TEST(ldexp_basic, basics)
448 TEST(ldexp_zero, zero)
449 TEST(ldexp_infinity, infinity)
450 TEST(ldexp_overflow, overflow)
451 TEST(ldexp_denormal, denormal)
452 TEST(ldexp_denormal_large, large)
453 TEST(ldexp_underflow, underflow)
454
455 ATF_TP_ADD_TCS(tp)
456 {
457
458 ATF_TP_ADD_TC(tp, ldexp_basic);
459 ATF_TP_ADD_TC(tp, ldexp_zero);
460 ATF_TP_ADD_TC(tp, ldexp_infinity);
461 ATF_TP_ADD_TC(tp, ldexp_overflow);
462 ATF_TP_ADD_TC(tp, ldexp_denormal);
463 ATF_TP_ADD_TC(tp, ldexp_underflow);
464 ATF_TP_ADD_TC(tp, ldexp_denormal_large);
465
466 ATF_TP_ADD_TC(tp, ldexp_exp2);
467 ATF_TP_ADD_TC(tp, ldexp_nan);
468 ATF_TP_ADD_TC(tp, ldexp_inf_neg);
469 ATF_TP_ADD_TC(tp, ldexp_inf_pos);
470 ATF_TP_ADD_TC(tp, ldexp_zero_neg);
471 ATF_TP_ADD_TC(tp, ldexp_zero_pos);
472
473 ATF_TP_ADD_TC(tp, ldexpf_exp2f);
474 ATF_TP_ADD_TC(tp, ldexpf_nan);
475 ATF_TP_ADD_TC(tp, ldexpf_inf_neg);
476 ATF_TP_ADD_TC(tp, ldexpf_inf_pos);
477 ATF_TP_ADD_TC(tp, ldexpf_zero_neg);
478 ATF_TP_ADD_TC(tp, ldexpf_zero_pos);
479
480 return atf_no_error();
481 }
482