xref: /netbsd-src/tests/lib/libm/t_ldexp.c (revision a536ee5124e62c9a0051a252f7833dc8f50f44c9)
1 /* $NetBSD: t_ldexp.c,v 1.10 2011/09/19 05:40:38 jruoho 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.10 2011/09/19 05:40:38 jruoho Exp $");
33 
34 #include <atf-c.h>
35 #include <atf-c/config.h>
36 
37 #include <math.h>
38 #include <limits.h>
39 #include <stdio.h>
40 #include <string.h>
41 
42 #define SKIP	9999
43 #define FORMAT  "%23.23lg"
44 
45 static const int exps[] = { 0, 1, -1, 100, -100 };
46 
47 struct ldexp_test {
48 	double	    x;
49 	int	    exp1;
50 	int	    exp2;
51 	const char *result;
52 };
53 
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" },
69 	{ 0,	0,	0,	NULL }
70 };
71 
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" },
80 	{ 0,	0,	0,	NULL }
81 };
82 
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" },
90 	{ 0,	0,	0,	NULL }
91 };
92 
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" },
102 	{ 0,	0,	0,	NULL }
103 };
104 
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" },
116 	{ 0,	0,	0,	NULL }
117 };
118 
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" },
128 	{ 0,	0,	0,	NULL }
129 };
130 
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" },
158 	{ 0,	0,	0,	NULL }
159 };
160 
161 static void
162 run_test(struct ldexp_test *table)
163 {
164 	char outbuf[64];
165 	size_t i;
166 	double v;
167 
168 	for (i = 0; table->result != NULL; table++, i++) {
169 
170 		v = ldexp(table->x, table->exp1);
171 
172 		if (table->exp2 == SKIP)
173 			continue;
174 
175 		v = ldexp(v, table->exp2);
176 
177 		(void)snprintf(outbuf, sizeof(outbuf), FORMAT, v);
178 
179 		ATF_CHECK_STREQ_MSG(table->result, outbuf,
180 			    "Entry %zu:\n\tExp: \"%s\"\n\tAct: \"%s\"",
181 			    i, table->result, outbuf);
182 	}
183 }
184 
185 /*
186  * ldexp(3)
187  */
188 ATF_TC(ldexp_exp2);
189 ATF_TC_HEAD(ldexp_exp2, tc)
190 {
191 	atf_tc_set_md_var(tc, "descr", "Test ldexp(x, n) == x * exp2(n)");
192 }
193 
194 ATF_TC_BODY(ldexp_exp2, tc)
195 {
196 #ifndef __vax__
197 	const double n[] = { 1, 2, 3, 10, 50, 100 };
198 	const double eps = 1.0e-40;
199 	const double x = 12.0;
200 	double y;
201 	size_t i;
202 
203 	for (i = 0; i < __arraycount(n); i++) {
204 
205 		y = ldexp(x, n[i]);
206 
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]);
210 		}
211 	}
212 #endif
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 #ifndef __vax__
224 	const double x = 0.0L / 0.0L;
225 	double y;
226 	size_t i;
227 
228 	ATF_REQUIRE(isnan(x) != 0);
229 
230 	for (i = 0; i < __arraycount(exps); i++) {
231 		y = ldexp(x, exps[i]);
232 		ATF_CHECK(isnan(y) != 0);
233 	}
234 #endif
235 }
236 
237 ATF_TC(ldexp_inf_neg);
238 ATF_TC_HEAD(ldexp_inf_neg, tc)
239 {
240 	atf_tc_set_md_var(tc, "descr", "Test ldexp(-Inf) == -Inf");
241 }
242 
243 ATF_TC_BODY(ldexp_inf_neg, tc)
244 {
245 #ifndef __vax__
246 	const double x = -1.0L / 0.0L;
247 	size_t i;
248 
249 	for (i = 0; i < __arraycount(exps); i++)
250 		ATF_CHECK(ldexp(x, exps[i]) == x);
251 #endif
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 #ifndef __vax__
263 	const double x = 1.0L / 0.0L;
264 	size_t i;
265 
266 	for (i = 0; i < __arraycount(exps); i++)
267 		ATF_CHECK(ldexp(x, exps[i]) == x);
268 #endif
269 }
270 
271 ATF_TC(ldexp_zero_neg);
272 ATF_TC_HEAD(ldexp_zero_neg, tc)
273 {
274 	atf_tc_set_md_var(tc, "descr", "Test ldexp(-0.0) == -0.0");
275 }
276 
277 ATF_TC_BODY(ldexp_zero_neg, tc)
278 {
279 #ifndef __vax__
280 	const double x = -0.0L;
281 	double y;
282 	size_t i;
283 
284 	ATF_REQUIRE(signbit(x) != 0);
285 
286 	for (i = 0; i < __arraycount(exps); i++) {
287 		y = ldexp(x, exps[i]);
288 		ATF_CHECK(x == y);
289 		ATF_CHECK(signbit(y) != 0);
290 	}
291 #endif
292 }
293 
294 ATF_TC(ldexp_zero_pos);
295 ATF_TC_HEAD(ldexp_zero_pos, tc)
296 {
297 	atf_tc_set_md_var(tc, "descr", "Test ldexp(+0.0) == +0.0");
298 }
299 
300 ATF_TC_BODY(ldexp_zero_pos, tc)
301 {
302 #ifndef __vax__
303 	const double x = 0.0L;
304 	double y;
305 	size_t i;
306 
307 	ATF_REQUIRE(signbit(x) == 0);
308 
309 	for (i = 0; i < __arraycount(exps); i++) {
310 		y = ldexp(x, exps[i]);
311 		ATF_CHECK(x == y);
312 		ATF_CHECK(signbit(y) == 0);
313 	}
314 #endif
315 }
316 
317 /*
318  * ldexpf(3)
319  */
320 
321 ATF_TC(ldexpf_exp2f);
322 ATF_TC_HEAD(ldexpf_exp2f, tc)
323 {
324 	atf_tc_set_md_var(tc, "descr", "Test ldexpf(x, n) == x * exp2f(n)");
325 }
326 
327 ATF_TC_BODY(ldexpf_exp2f, tc)
328 {
329 #ifndef __vax__
330 	const float n[] = { 1, 2, 3, 10, 50, 100 };
331 	const float eps = 1.0e-9;
332 	const float x = 12.0;
333 	float y;
334 	size_t i;
335 
336 	for (i = 0; i < __arraycount(n); i++) {
337 
338 		y = ldexpf(x, n[i]);
339 
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]);
343 		}
344 	}
345 #endif
346 }
347 
348 ATF_TC(ldexpf_nan);
349 ATF_TC_HEAD(ldexpf_nan, tc)
350 {
351 	atf_tc_set_md_var(tc, "descr", "Test ldexpf(NaN) == NaN");
352 }
353 
354 ATF_TC_BODY(ldexpf_nan, tc)
355 {
356 #ifndef __vax__
357 	const float x = 0.0L / 0.0L;
358 	float y;
359 	size_t i;
360 
361 	ATF_REQUIRE(isnan(x) != 0);
362 
363 	for (i = 0; i < __arraycount(exps); i++) {
364 		y = ldexpf(x, exps[i]);
365 		ATF_CHECK(isnan(y) != 0);
366 	}
367 #endif
368 }
369 
370 ATF_TC(ldexpf_inf_neg);
371 ATF_TC_HEAD(ldexpf_inf_neg, tc)
372 {
373 	atf_tc_set_md_var(tc, "descr", "Test ldexpf(-Inf) == -Inf");
374 }
375 
376 ATF_TC_BODY(ldexpf_inf_neg, tc)
377 {
378 #ifndef __vax__
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 #endif
385 }
386 
387 ATF_TC(ldexpf_inf_pos);
388 ATF_TC_HEAD(ldexpf_inf_pos, tc)
389 {
390 	atf_tc_set_md_var(tc, "descr", "Test ldexpf(+Inf) == +Inf");
391 }
392 
393 ATF_TC_BODY(ldexpf_inf_pos, tc)
394 {
395 #ifndef __vax__
396 	const float x = 1.0L / 0.0L;
397 	size_t i;
398 
399 	for (i = 0; i < __arraycount(exps); i++)
400 		ATF_CHECK(ldexpf(x, exps[i]) == x);
401 #endif
402 }
403 
404 ATF_TC(ldexpf_zero_neg);
405 ATF_TC_HEAD(ldexpf_zero_neg, tc)
406 {
407 	atf_tc_set_md_var(tc, "descr", "Test ldexpf(-0.0) == -0.0");
408 }
409 
410 ATF_TC_BODY(ldexpf_zero_neg, tc)
411 {
412 #ifndef __vax__
413 	const float x = -0.0L;
414 	float y;
415 	size_t i;
416 
417 	ATF_REQUIRE(signbit(x) != 0);
418 
419 	for (i = 0; i < __arraycount(exps); i++) {
420 		y = ldexpf(x, exps[i]);
421 		ATF_CHECK(x == y);
422 		ATF_CHECK(signbit(y) != 0);
423 	}
424 #endif
425 }
426 
427 ATF_TC(ldexpf_zero_pos);
428 ATF_TC_HEAD(ldexpf_zero_pos, tc)
429 {
430 	atf_tc_set_md_var(tc, "descr", "Test ldexpf(+0.0) == +0.0");
431 }
432 
433 ATF_TC_BODY(ldexpf_zero_pos, tc)
434 {
435 #ifndef __vax__
436 	const float x = 0.0L;
437 	float y;
438 	size_t i;
439 
440 	ATF_REQUIRE(signbit(x) == 0);
441 
442 	for (i = 0; i < __arraycount(exps); i++) {
443 		y = ldexpf(x, exps[i]);
444 		ATF_CHECK(x == y);
445 		ATF_CHECK(signbit(y) == 0);
446 	}
447 #endif
448 }
449 
450 #define TEST(name, desc)						\
451 	ATF_TC(name);							\
452 	ATF_TC_HEAD(name, tc)						\
453 	{								\
454 									\
455 		atf_tc_set_md_var(tc, "descr",				\
456 		    "Test ldexp(3) for " ___STRING(desc));		\
457 	}								\
458 	ATF_TC_BODY(name, tc)						\
459 	{								\
460 		const char *machine;					\
461 									\
462 		machine = atf_config_get("atf_machine");		\
463 		if (strcmp("vax", machine) == 0)			\
464 			atf_tc_skip("Test not valid for %s", machine);	\
465 		run_test(name);						\
466 	}
467 
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)
475 
476 ATF_TP_ADD_TCS(tp)
477 {
478 
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);
486 
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);
493 
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);
500 
501 	return atf_no_error();
502 }
503