1 /* $NetBSD: t_ldexp.c,v 1.10 2011/09/19 05:40:38 jruoho Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_ldexp.c,v 1.10 2011/09/19 05:40:38 jruoho Exp $");
35 #include <atf-c/config.h>
43 #define FORMAT "%23.23lg"
45 static const int exps
[] = { 0, 1, -1, 100, -100 };
54 struct ldexp_test ldexp_basic
[] = {
55 { 1.0, 5, SKIP
, " 32" },
56 { 1.0, 1022, SKIP
, "4.4942328371557897693233e+307" },
57 { 1.0, 1023, -1, "4.4942328371557897693233e+307" },
58 { 1.0, 1023, SKIP
, "8.9884656743115795386465e+307" },
59 { 1.0, 1022, 1, "8.9884656743115795386465e+307" },
60 { 1.0, -1022, 2045, "8.9884656743115795386465e+307" },
61 { 1.0, -5, SKIP
, " 0.03125" },
62 { 1.0, -1021, SKIP
, "4.4501477170144027661805e-308" },
63 { 1.0, -1022, 1, "4.4501477170144027661805e-308" },
64 { 1.0, -1022, SKIP
, "2.2250738585072013830902e-308" },
65 { 1.0, -1021, -1, "2.2250738585072013830902e-308" },
66 { 1.0, 1023, -2045, "2.2250738585072013830902e-308" },
67 { 1.0, 1023, -1023, " 1" },
68 { 1.0, -1022, 1022, " 1" },
72 struct ldexp_test ldexp_zero
[] = {
73 { 0.0, -1, SKIP
, " 0" },
74 { 0.0, 0, SKIP
, " 0" },
75 { 0.0, 1, SKIP
, " 0" },
76 { 0.0, 1024, SKIP
, " 0" },
77 { 0.0, 1025, SKIP
, " 0" },
78 { 0.0, -1023, SKIP
, " 0" },
79 { 0.0, -1024, SKIP
, " 0" },
83 struct ldexp_test ldexp_infinity
[] = {
84 { 1.0, 1024, -1, " inf" },
85 { 1.0, 1024, 0, " inf" },
86 { 1.0, 1024, 1, " inf" },
87 { -1.0, 1024, -1, " -inf" },
88 { -1.0, 1024, 0, " -inf" },
89 { -1.0, 1024, 1, " -inf" },
93 struct ldexp_test ldexp_overflow
[] = {
94 { 1.0, 1024, SKIP
, " inf" },
95 { 1.0, 1023, 1, " inf" },
96 { 1.0, -1022, 2046, " inf" },
97 { 1.0, 1025, SKIP
, " inf" },
98 { -1.0, 1024, SKIP
, " -inf" },
99 { -1.0, 1023, 1, " -inf" },
100 { -1.0, -1022, 2046, " -inf" },
101 { -1.0, 1025, SKIP
, " -inf" },
105 struct ldexp_test ldexp_denormal
[] = {
106 { 1.0, -1023, SKIP
, "1.1125369292536006915451e-308" },
107 { 1.0, -1022, -1, "1.1125369292536006915451e-308" },
108 { 1.0, 1023, -2046, "1.1125369292536006915451e-308" },
109 { 1.0, -1024, SKIP
, "5.5626846462680034577256e-309" },
110 { 1.0, -1074, SKIP
, "4.9406564584124654417657e-324" },
111 { -1.0, -1023, SKIP
, "-1.1125369292536006915451e-308" },
112 { -1.0, -1022, -1, "-1.1125369292536006915451e-308" },
113 { -1.0, 1023, -2046, "-1.1125369292536006915451e-308" },
114 { -1.0, -1024, SKIP
, "-5.5626846462680034577256e-309" },
115 { -1.0, -1074, SKIP
, "-4.9406564584124654417657e-324" },
119 struct ldexp_test ldexp_underflow
[] = {
120 { 1.0, -1075, SKIP
, " 0" },
121 { 1.0, -1074, -1, " 0" },
122 { 1.0, 1023, -2098, " 0" },
123 { 1.0, -1076, SKIP
, " 0" },
124 { -1.0, -1075, SKIP
, " -0" },
125 { -1.0, -1074, -1, " -0" },
126 { -1.0, 1023, -2098, " -0" },
127 { -1.0, -1076, SKIP
, " -0" },
131 struct ldexp_test ldexp_denormal_large
[] = {
132 { 1.0, -1028, 1024, " 0.0625" },
133 { 1.0, -1028, 1025, " 0.125" },
134 { 1.0, -1028, 1026, " 0.25" },
135 { 1.0, -1028, 1027, " 0.5" },
136 { 1.0, -1028, 1028, " 1" },
137 { 1.0, -1028, 1029, " 2" },
138 { 1.0, -1028, 1030, " 4" },
139 { 1.0, -1028, 1040, " 4096" },
140 { 1.0, -1028, 1050, " 4194304" },
141 { 1.0, -1028, 1060, " 4294967296" },
142 { 1.0, -1028, 1100, " 4722366482869645213696" },
143 { 1.0, -1028, 1200, "5.9863107065073783529623e+51" },
144 { 1.0, -1028, 1300, "7.5885503602567541832791e+81" },
145 { 1.0, -1028, 1400, "9.6196304190416209014353e+111" },
146 { 1.0, -1028, 1500, "1.2194330274671844653834e+142" },
147 { 1.0, -1028, 1600, "1.5458150092069033378781e+172" },
148 { 1.0, -1028, 1700, "1.9595533242629369747791e+202" },
149 { 1.0, -1028, 1800, "2.4840289476811342962384e+232" },
150 { 1.0, -1028, 1900, "3.1488807865122869393369e+262" },
151 { 1.0, -1028, 2000, "3.9916806190694396233127e+292" },
152 { 1.0, -1028, 2046, "2.808895523222368605827e+306" },
153 { 1.0, -1028, 2047, "5.6177910464447372116541e+306" },
154 { 1.0, -1028, 2048, "1.1235582092889474423308e+307" },
155 { 1.0, -1028, 2049, "2.2471164185778948846616e+307" },
156 { 1.0, -1028, 2050, "4.4942328371557897693233e+307" },
157 { 1.0, -1028, 2051, "8.9884656743115795386465e+307" },
162 run_test(struct ldexp_test
*table
)
168 for (i
= 0; table
->result
!= NULL
; table
++, i
++) {
170 v
= ldexp(table
->x
, table
->exp1
);
172 if (table
->exp2
== SKIP
)
175 v
= ldexp(v
, table
->exp2
);
177 (void)snprintf(outbuf
, sizeof(outbuf
), FORMAT
, v
);
179 ATF_CHECK_STREQ_MSG(table
->result
, outbuf
,
180 "Entry %zu:\n\tExp: \"%s\"\n\tAct: \"%s\"",
181 i
, table
->result
, outbuf
);
189 ATF_TC_HEAD(ldexp_exp2
, tc
)
191 atf_tc_set_md_var(tc
, "descr", "Test ldexp(x, n) == x * exp2(n)");
194 ATF_TC_BODY(ldexp_exp2
, tc
)
197 const double n
[] = { 1, 2, 3, 10, 50, 100 };
198 const double eps
= 1.0e-40;
199 const double x
= 12.0;
203 for (i
= 0; i
< __arraycount(n
); i
++) {
207 if (fabs(y
- (x
* exp2(n
[i
]))) > eps
) {
208 atf_tc_fail_nonfatal("ldexp(%0.01f, %0.01f) "
209 "!= %0.01f * exp2(%0.01f)", x
, n
[i
], x
, n
[i
]);
216 ATF_TC_HEAD(ldexp_nan
, tc
)
218 atf_tc_set_md_var(tc
, "descr", "Test ldexp(NaN) == NaN");
221 ATF_TC_BODY(ldexp_nan
, tc
)
224 const double x
= 0.0L / 0.0L;
228 ATF_REQUIRE(isnan(x
) != 0);
230 for (i
= 0; i
< __arraycount(exps
); i
++) {
231 y
= ldexp(x
, exps
[i
]);
232 ATF_CHECK(isnan(y
) != 0);
237 ATF_TC(ldexp_inf_neg
);
238 ATF_TC_HEAD(ldexp_inf_neg
, tc
)
240 atf_tc_set_md_var(tc
, "descr", "Test ldexp(-Inf) == -Inf");
243 ATF_TC_BODY(ldexp_inf_neg
, tc
)
246 const double x
= -1.0L / 0.0L;
249 for (i
= 0; i
< __arraycount(exps
); i
++)
250 ATF_CHECK(ldexp(x
, exps
[i
]) == x
);
254 ATF_TC(ldexp_inf_pos
);
255 ATF_TC_HEAD(ldexp_inf_pos
, tc
)
257 atf_tc_set_md_var(tc
, "descr", "Test ldexp(+Inf) == +Inf");
260 ATF_TC_BODY(ldexp_inf_pos
, tc
)
263 const double x
= 1.0L / 0.0L;
266 for (i
= 0; i
< __arraycount(exps
); i
++)
267 ATF_CHECK(ldexp(x
, exps
[i
]) == x
);
271 ATF_TC(ldexp_zero_neg
);
272 ATF_TC_HEAD(ldexp_zero_neg
, tc
)
274 atf_tc_set_md_var(tc
, "descr", "Test ldexp(-0.0) == -0.0");
277 ATF_TC_BODY(ldexp_zero_neg
, tc
)
280 const double x
= -0.0L;
284 ATF_REQUIRE(signbit(x
) != 0);
286 for (i
= 0; i
< __arraycount(exps
); i
++) {
287 y
= ldexp(x
, exps
[i
]);
289 ATF_CHECK(signbit(y
) != 0);
294 ATF_TC(ldexp_zero_pos
);
295 ATF_TC_HEAD(ldexp_zero_pos
, tc
)
297 atf_tc_set_md_var(tc
, "descr", "Test ldexp(+0.0) == +0.0");
300 ATF_TC_BODY(ldexp_zero_pos
, tc
)
303 const double x
= 0.0L;
307 ATF_REQUIRE(signbit(x
) == 0);
309 for (i
= 0; i
< __arraycount(exps
); i
++) {
310 y
= ldexp(x
, exps
[i
]);
312 ATF_CHECK(signbit(y
) == 0);
321 ATF_TC(ldexpf_exp2f
);
322 ATF_TC_HEAD(ldexpf_exp2f
, tc
)
324 atf_tc_set_md_var(tc
, "descr", "Test ldexpf(x, n) == x * exp2f(n)");
327 ATF_TC_BODY(ldexpf_exp2f
, tc
)
330 const float n
[] = { 1, 2, 3, 10, 50, 100 };
331 const float eps
= 1.0e-9;
332 const float x
= 12.0;
336 for (i
= 0; i
< __arraycount(n
); i
++) {
340 if (fabsf(y
- (x
* exp2f(n
[i
]))) > eps
) {
341 atf_tc_fail_nonfatal("ldexpf(%0.01f, %0.01f) "
342 "!= %0.01f * exp2f(%0.01f)", x
, n
[i
], x
, n
[i
]);
349 ATF_TC_HEAD(ldexpf_nan
, tc
)
351 atf_tc_set_md_var(tc
, "descr", "Test ldexpf(NaN) == NaN");
354 ATF_TC_BODY(ldexpf_nan
, tc
)
357 const float x
= 0.0L / 0.0L;
361 ATF_REQUIRE(isnan(x
) != 0);
363 for (i
= 0; i
< __arraycount(exps
); i
++) {
364 y
= ldexpf(x
, exps
[i
]);
365 ATF_CHECK(isnan(y
) != 0);
370 ATF_TC(ldexpf_inf_neg
);
371 ATF_TC_HEAD(ldexpf_inf_neg
, tc
)
373 atf_tc_set_md_var(tc
, "descr", "Test ldexpf(-Inf) == -Inf");
376 ATF_TC_BODY(ldexpf_inf_neg
, tc
)
379 const float x
= -1.0L / 0.0L;
382 for (i
= 0; i
< __arraycount(exps
); i
++)
383 ATF_CHECK(ldexpf(x
, exps
[i
]) == x
);
387 ATF_TC(ldexpf_inf_pos
);
388 ATF_TC_HEAD(ldexpf_inf_pos
, tc
)
390 atf_tc_set_md_var(tc
, "descr", "Test ldexpf(+Inf) == +Inf");
393 ATF_TC_BODY(ldexpf_inf_pos
, tc
)
396 const float x
= 1.0L / 0.0L;
399 for (i
= 0; i
< __arraycount(exps
); i
++)
400 ATF_CHECK(ldexpf(x
, exps
[i
]) == x
);
404 ATF_TC(ldexpf_zero_neg
);
405 ATF_TC_HEAD(ldexpf_zero_neg
, tc
)
407 atf_tc_set_md_var(tc
, "descr", "Test ldexpf(-0.0) == -0.0");
410 ATF_TC_BODY(ldexpf_zero_neg
, tc
)
413 const float x
= -0.0L;
417 ATF_REQUIRE(signbit(x
) != 0);
419 for (i
= 0; i
< __arraycount(exps
); i
++) {
420 y
= ldexpf(x
, exps
[i
]);
422 ATF_CHECK(signbit(y
) != 0);
427 ATF_TC(ldexpf_zero_pos
);
428 ATF_TC_HEAD(ldexpf_zero_pos
, tc
)
430 atf_tc_set_md_var(tc
, "descr", "Test ldexpf(+0.0) == +0.0");
433 ATF_TC_BODY(ldexpf_zero_pos
, tc
)
436 const float x
= 0.0L;
440 ATF_REQUIRE(signbit(x
) == 0);
442 for (i
= 0; i
< __arraycount(exps
); i
++) {
443 y
= ldexpf(x
, exps
[i
]);
445 ATF_CHECK(signbit(y
) == 0);
450 #define TEST(name, desc) \
452 ATF_TC_HEAD(name, tc) \
455 atf_tc_set_md_var(tc, "descr", \
456 "Test ldexp(3) for " ___STRING(desc)); \
458 ATF_TC_BODY(name, tc) \
460 const char *machine; \
462 machine = atf_config_get("atf_machine"); \
463 if (strcmp("vax", machine) == 0) \
464 atf_tc_skip("Test not valid for %s", machine); \
468 TEST(ldexp_basic
, basics
)
469 TEST(ldexp_zero
, zero
)
470 TEST(ldexp_infinity
, infinity
)
471 TEST(ldexp_overflow
, overflow
)
472 TEST(ldexp_denormal
, denormal
)
473 TEST(ldexp_denormal_large
, large
)
474 TEST(ldexp_underflow
, underflow
)
479 ATF_TP_ADD_TC(tp
, ldexp_basic
);
480 ATF_TP_ADD_TC(tp
, ldexp_zero
);
481 ATF_TP_ADD_TC(tp
, ldexp_infinity
);
482 ATF_TP_ADD_TC(tp
, ldexp_overflow
);
483 ATF_TP_ADD_TC(tp
, ldexp_denormal
);
484 ATF_TP_ADD_TC(tp
, ldexp_underflow
);
485 ATF_TP_ADD_TC(tp
, ldexp_denormal_large
);
487 ATF_TP_ADD_TC(tp
, ldexp_exp2
);
488 ATF_TP_ADD_TC(tp
, ldexp_nan
);
489 ATF_TP_ADD_TC(tp
, ldexp_inf_neg
);
490 ATF_TP_ADD_TC(tp
, ldexp_inf_pos
);
491 ATF_TP_ADD_TC(tp
, ldexp_zero_neg
);
492 ATF_TP_ADD_TC(tp
, ldexp_zero_pos
);
494 ATF_TP_ADD_TC(tp
, ldexpf_exp2f
);
495 ATF_TP_ADD_TC(tp
, ldexpf_nan
);
496 ATF_TP_ADD_TC(tp
, ldexpf_inf_neg
);
497 ATF_TP_ADD_TC(tp
, ldexpf_inf_pos
);
498 ATF_TP_ADD_TC(tp
, ldexpf_zero_neg
);
499 ATF_TP_ADD_TC(tp
, ldexpf_zero_pos
);
501 return atf_no_error();