Remove building with NOCRYPTO option
[minix.git] / tests / lib / libm / t_ldexp.c
blobeaf8a4bfdb189d46f6a7a0741969fde0f6d8ca85
1 /* $NetBSD: t_ldexp.c,v 1.14 2014/11/04 00:20:19 justin Exp $ */
3 /*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jukka Ruohonen.
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.
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.14 2014/11/04 00:20:19 justin Exp $");
34 #include <sys/param.h>
36 #include <atf-c.h>
38 #include <math.h>
39 #include <limits.h>
40 #include <stdio.h>
41 #include <string.h>
43 #define SKIP 9999
44 #define FORMAT "%23.23lg"
46 static const int exps[] = { 0, 1, -1, 100, -100 };
48 struct ldexp_test {
49 double x;
50 int exp1;
51 int exp2;
52 const char *result;
55 struct ldexp_test ldexp_basic[] = {
56 { 1.0, 5, SKIP, " 32" },
57 { 1.0, 1022, SKIP, "4.4942328371557897693233e+307" },
58 { 1.0, 1023, -1, "4.4942328371557897693233e+307" },
59 { 1.0, 1023, SKIP, "8.9884656743115795386465e+307" },
60 { 1.0, 1022, 1, "8.9884656743115795386465e+307" },
61 { 1.0, -1022, 2045, "8.9884656743115795386465e+307" },
62 { 1.0, -5, SKIP, " 0.03125" },
63 { 1.0, -1021, SKIP, "4.4501477170144027661805e-308" },
64 { 1.0, -1022, 1, "4.4501477170144027661805e-308" },
65 { 1.0, -1022, SKIP, "2.2250738585072013830902e-308" },
66 { 1.0, -1021, -1, "2.2250738585072013830902e-308" },
67 { 1.0, 1023, -2045, "2.2250738585072013830902e-308" },
68 { 1.0, 1023, -1023, " 1" },
69 { 1.0, -1022, 1022, " 1" },
70 { 0, 0, 0, NULL }
73 struct ldexp_test ldexp_zero[] = {
74 { 0.0, -1, SKIP, " 0" },
75 { 0.0, 0, SKIP, " 0" },
76 { 0.0, 1, SKIP, " 0" },
77 { 0.0, 1024, SKIP, " 0" },
78 { 0.0, 1025, SKIP, " 0" },
79 { 0.0, -1023, SKIP, " 0" },
80 { 0.0, -1024, SKIP, " 0" },
81 { 0, 0, 0, NULL }
84 struct ldexp_test ldexp_infinity[] = {
85 { 1.0, 1024, -1, " inf" },
86 { 1.0, 1024, 0, " inf" },
87 { 1.0, 1024, 1, " inf" },
88 { -1.0, 1024, -1, " -inf" },
89 { -1.0, 1024, 0, " -inf" },
90 { -1.0, 1024, 1, " -inf" },
91 { 0, 0, 0, NULL }
94 struct ldexp_test ldexp_overflow[] = {
95 { 1.0, 1024, SKIP, " inf" },
96 { 1.0, 1023, 1, " inf" },
97 { 1.0, -1022, 2046, " inf" },
98 { 1.0, 1025, SKIP, " inf" },
99 { -1.0, 1024, SKIP, " -inf" },
100 { -1.0, 1023, 1, " -inf" },
101 { -1.0, -1022, 2046, " -inf" },
102 { -1.0, 1025, SKIP, " -inf" },
103 { 0, 0, 0, NULL }
106 struct ldexp_test ldexp_denormal[] = {
107 { 1.0, -1023, SKIP, "1.1125369292536006915451e-308" },
108 { 1.0, -1022, -1, "1.1125369292536006915451e-308" },
109 { 1.0, 1023, -2046, "1.1125369292536006915451e-308" },
110 { 1.0, -1024, SKIP, "5.5626846462680034577256e-309" },
111 { 1.0, -1074, SKIP, "4.9406564584124654417657e-324" },
112 { -1.0, -1023, SKIP, "-1.1125369292536006915451e-308" },
113 { -1.0, -1022, -1, "-1.1125369292536006915451e-308" },
114 { -1.0, 1023, -2046, "-1.1125369292536006915451e-308" },
115 { -1.0, -1024, SKIP, "-5.5626846462680034577256e-309" },
116 { -1.0, -1074, SKIP, "-4.9406564584124654417657e-324" },
117 { 0, 0, 0, NULL }
120 struct ldexp_test ldexp_underflow[] = {
121 { 1.0, -1075, SKIP, " 0" },
122 { 1.0, -1074, -1, " 0" },
123 { 1.0, 1023, -2098, " 0" },
124 { 1.0, -1076, SKIP, " 0" },
125 { -1.0, -1075, SKIP, " -0" },
126 { -1.0, -1074, -1, " -0" },
127 { -1.0, 1023, -2098, " -0" },
128 { -1.0, -1076, SKIP, " -0" },
129 { 0, 0, 0, NULL }
132 struct ldexp_test ldexp_denormal_large[] = {
133 { 1.0, -1028, 1024, " 0.0625" },
134 { 1.0, -1028, 1025, " 0.125" },
135 { 1.0, -1028, 1026, " 0.25" },
136 { 1.0, -1028, 1027, " 0.5" },
137 { 1.0, -1028, 1028, " 1" },
138 { 1.0, -1028, 1029, " 2" },
139 { 1.0, -1028, 1030, " 4" },
140 { 1.0, -1028, 1040, " 4096" },
141 { 1.0, -1028, 1050, " 4194304" },
142 { 1.0, -1028, 1060, " 4294967296" },
143 { 1.0, -1028, 1100, " 4722366482869645213696" },
144 { 1.0, -1028, 1200, "5.9863107065073783529623e+51" },
145 { 1.0, -1028, 1300, "7.5885503602567541832791e+81" },
146 { 1.0, -1028, 1400, "9.6196304190416209014353e+111" },
147 { 1.0, -1028, 1500, "1.2194330274671844653834e+142" },
148 { 1.0, -1028, 1600, "1.5458150092069033378781e+172" },
149 { 1.0, -1028, 1700, "1.9595533242629369747791e+202" },
150 { 1.0, -1028, 1800, "2.4840289476811342962384e+232" },
151 { 1.0, -1028, 1900, "3.1488807865122869393369e+262" },
152 { 1.0, -1028, 2000, "3.9916806190694396233127e+292" },
153 { 1.0, -1028, 2046, "2.808895523222368605827e+306" },
154 { 1.0, -1028, 2047, "5.6177910464447372116541e+306" },
155 { 1.0, -1028, 2048, "1.1235582092889474423308e+307" },
156 { 1.0, -1028, 2049, "2.2471164185778948846616e+307" },
157 { 1.0, -1028, 2050, "4.4942328371557897693233e+307" },
158 { 1.0, -1028, 2051, "8.9884656743115795386465e+307" },
159 { 0, 0, 0, NULL }
162 static void
163 run_test(struct ldexp_test *table)
165 char outbuf[64];
166 size_t i;
167 double v;
169 for (i = 0; table->result != NULL; table++, i++) {
171 v = ldexp(table->x, table->exp1);
173 if (table->exp2 == SKIP)
174 continue;
176 v = ldexp(v, table->exp2);
178 (void)snprintf(outbuf, sizeof(outbuf), FORMAT, v);
180 ATF_CHECK_STREQ_MSG(table->result, outbuf,
181 "Entry %zu:\n\tExp: \"%s\"\n\tAct: \"%s\"",
182 i, table->result, outbuf);
187 * ldexp(3)
189 ATF_TC(ldexp_exp2);
190 ATF_TC_HEAD(ldexp_exp2, tc)
192 atf_tc_set_md_var(tc, "descr", "Test ldexp(x, n) == x * exp2(n)");
195 ATF_TC_BODY(ldexp_exp2, tc)
197 const double n[] = { 1, 2, 3, 10, 50, 100 };
198 #if __DBL_MIN_10_EXP__ <= -40
199 const double eps = 1.0e-40;
200 #else
201 const double eps = __DBL_MIN__*4.0;
202 #endif
203 const double x = 12.0;
204 double y;
205 size_t i;
207 for (i = 0; i < __arraycount(n); i++) {
209 y = ldexp(x, n[i]);
211 if (fabs(y - (x * exp2(n[i]))) > eps) {
212 atf_tc_fail_nonfatal("ldexp(%0.01f, %0.01f) "
213 "!= %0.01f * exp2(%0.01f)", x, n[i], x, n[i]);
218 ATF_TC(ldexp_nan);
219 ATF_TC_HEAD(ldexp_nan, tc)
221 atf_tc_set_md_var(tc, "descr", "Test ldexp(NaN) == NaN");
224 ATF_TC_BODY(ldexp_nan, tc)
226 const double x = 0.0L / 0.0L;
227 double y;
228 size_t i;
230 ATF_REQUIRE(isnan(x) != 0);
232 for (i = 0; i < __arraycount(exps); i++) {
233 y = ldexp(x, exps[i]);
234 ATF_CHECK(isnan(y) != 0);
238 ATF_TC(ldexp_inf_neg);
239 ATF_TC_HEAD(ldexp_inf_neg, tc)
241 atf_tc_set_md_var(tc, "descr", "Test ldexp(-Inf) == -Inf");
244 ATF_TC_BODY(ldexp_inf_neg, tc)
246 const double x = -1.0L / 0.0L;
247 size_t i;
249 for (i = 0; i < __arraycount(exps); i++)
250 ATF_CHECK(ldexp(x, exps[i]) == x);
253 ATF_TC(ldexp_inf_pos);
254 ATF_TC_HEAD(ldexp_inf_pos, tc)
256 atf_tc_set_md_var(tc, "descr", "Test ldexp(+Inf) == +Inf");
259 ATF_TC_BODY(ldexp_inf_pos, tc)
261 const double x = 1.0L / 0.0L;
262 size_t i;
264 for (i = 0; i < __arraycount(exps); i++)
265 ATF_CHECK(ldexp(x, exps[i]) == x);
268 ATF_TC(ldexp_zero_neg);
269 ATF_TC_HEAD(ldexp_zero_neg, tc)
271 atf_tc_set_md_var(tc, "descr", "Test ldexp(-0.0) == -0.0");
274 ATF_TC_BODY(ldexp_zero_neg, tc)
276 const double x = -0.0L;
277 double y;
278 size_t i;
280 ATF_REQUIRE(signbit(x) != 0);
282 for (i = 0; i < __arraycount(exps); i++) {
283 y = ldexp(x, exps[i]);
284 ATF_CHECK(x == y);
285 ATF_CHECK(signbit(y) != 0);
289 ATF_TC(ldexp_zero_pos);
290 ATF_TC_HEAD(ldexp_zero_pos, tc)
292 atf_tc_set_md_var(tc, "descr", "Test ldexp(+0.0) == +0.0");
295 ATF_TC_BODY(ldexp_zero_pos, tc)
297 const double x = 0.0L;
298 double y;
299 size_t i;
301 ATF_REQUIRE(signbit(x) == 0);
303 for (i = 0; i < __arraycount(exps); i++) {
304 y = ldexp(x, exps[i]);
305 ATF_CHECK(x == y);
306 ATF_CHECK(signbit(y) == 0);
311 * ldexpf(3)
314 ATF_TC(ldexpf_exp2f);
315 ATF_TC_HEAD(ldexpf_exp2f, tc)
317 atf_tc_set_md_var(tc, "descr", "Test ldexpf(x, n) == x * exp2f(n)");
320 ATF_TC_BODY(ldexpf_exp2f, tc)
322 const float n[] = { 1, 2, 3, 10, 50, 100 };
323 const float eps = 1.0e-9;
324 const float x = 12.0;
325 float y;
326 size_t i;
328 for (i = 0; i < __arraycount(n); i++) {
330 y = ldexpf(x, n[i]);
332 if (fabsf(y - (x * exp2f(n[i]))) > eps) {
333 atf_tc_fail_nonfatal("ldexpf(%0.01f, %0.01f) "
334 "!= %0.01f * exp2f(%0.01f)", x, n[i], x, n[i]);
339 ATF_TC(ldexpf_nan);
340 ATF_TC_HEAD(ldexpf_nan, tc)
342 atf_tc_set_md_var(tc, "descr", "Test ldexpf(NaN) == NaN");
345 ATF_TC_BODY(ldexpf_nan, tc)
347 const float x = 0.0L / 0.0L;
348 float y;
349 size_t i;
351 ATF_REQUIRE(isnan(x) != 0);
353 for (i = 0; i < __arraycount(exps); i++) {
354 y = ldexpf(x, exps[i]);
355 ATF_CHECK(isnan(y) != 0);
359 ATF_TC(ldexpf_inf_neg);
360 ATF_TC_HEAD(ldexpf_inf_neg, tc)
362 atf_tc_set_md_var(tc, "descr", "Test ldexpf(-Inf) == -Inf");
365 ATF_TC_BODY(ldexpf_inf_neg, tc)
367 const float x = -1.0L / 0.0L;
368 size_t i;
370 for (i = 0; i < __arraycount(exps); i++)
371 ATF_CHECK(ldexpf(x, exps[i]) == x);
374 ATF_TC(ldexpf_inf_pos);
375 ATF_TC_HEAD(ldexpf_inf_pos, tc)
377 atf_tc_set_md_var(tc, "descr", "Test ldexpf(+Inf) == +Inf");
380 ATF_TC_BODY(ldexpf_inf_pos, tc)
382 const float x = 1.0L / 0.0L;
383 size_t i;
385 for (i = 0; i < __arraycount(exps); i++)
386 ATF_CHECK(ldexpf(x, exps[i]) == x);
389 ATF_TC(ldexpf_zero_neg);
390 ATF_TC_HEAD(ldexpf_zero_neg, tc)
392 atf_tc_set_md_var(tc, "descr", "Test ldexpf(-0.0) == -0.0");
395 ATF_TC_BODY(ldexpf_zero_neg, tc)
397 const float x = -0.0L;
398 float y;
399 size_t i;
401 ATF_REQUIRE(signbit(x) != 0);
403 for (i = 0; i < __arraycount(exps); i++) {
404 y = ldexpf(x, exps[i]);
405 ATF_CHECK(x == y);
406 ATF_CHECK(signbit(y) != 0);
410 ATF_TC(ldexpf_zero_pos);
411 ATF_TC_HEAD(ldexpf_zero_pos, tc)
413 atf_tc_set_md_var(tc, "descr", "Test ldexpf(+0.0) == +0.0");
416 ATF_TC_BODY(ldexpf_zero_pos, tc)
418 const float x = 0.0L;
419 float y;
420 size_t i;
422 ATF_REQUIRE(signbit(x) == 0);
424 for (i = 0; i < __arraycount(exps); i++) {
425 y = ldexpf(x, exps[i]);
426 ATF_CHECK(x == y);
427 ATF_CHECK(signbit(y) == 0);
431 #define TEST(name, desc) \
432 ATF_TC(name); \
433 ATF_TC_HEAD(name, tc) \
436 atf_tc_set_md_var(tc, "descr", \
437 "Test ldexp(3) for " ___STRING(desc)); \
439 ATF_TC_BODY(name, tc) \
441 if (strcmp("vax", MACHINE_ARCH) == 0) \
442 atf_tc_skip("Test not valid for " MACHINE_ARCH); \
443 run_test(name); \
446 TEST(ldexp_basic, basics)
447 TEST(ldexp_zero, zero)
448 TEST(ldexp_infinity, infinity)
449 TEST(ldexp_overflow, overflow)
450 TEST(ldexp_denormal, denormal)
451 TEST(ldexp_denormal_large, large)
452 TEST(ldexp_underflow, underflow)
454 ATF_TP_ADD_TCS(tp)
457 ATF_TP_ADD_TC(tp, ldexp_basic);
458 ATF_TP_ADD_TC(tp, ldexp_zero);
459 ATF_TP_ADD_TC(tp, ldexp_infinity);
460 ATF_TP_ADD_TC(tp, ldexp_overflow);
461 ATF_TP_ADD_TC(tp, ldexp_denormal);
462 ATF_TP_ADD_TC(tp, ldexp_underflow);
463 ATF_TP_ADD_TC(tp, ldexp_denormal_large);
465 ATF_TP_ADD_TC(tp, ldexp_exp2);
466 ATF_TP_ADD_TC(tp, ldexp_nan);
467 ATF_TP_ADD_TC(tp, ldexp_inf_neg);
468 ATF_TP_ADD_TC(tp, ldexp_inf_pos);
469 ATF_TP_ADD_TC(tp, ldexp_zero_neg);
470 ATF_TP_ADD_TC(tp, ldexp_zero_pos);
472 ATF_TP_ADD_TC(tp, ldexpf_exp2f);
473 ATF_TP_ADD_TC(tp, ldexpf_nan);
474 ATF_TP_ADD_TC(tp, ldexpf_inf_neg);
475 ATF_TP_ADD_TC(tp, ldexpf_inf_pos);
476 ATF_TP_ADD_TC(tp, ldexpf_zero_neg);
477 ATF_TP_ADD_TC(tp, ldexpf_zero_pos);
479 return atf_no_error();