xref: /minix3/tests/lib/libm/t_ldexp.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /* $NetBSD: t_ldexp.c,v 1.14 2014/11/04 00:20:19 justin Exp $ */
211be35a1SLionel Sambuc 
311be35a1SLionel Sambuc /*-
411be35a1SLionel Sambuc  * Copyright (c) 2011 The NetBSD Foundation, Inc.
511be35a1SLionel Sambuc  * All rights reserved.
611be35a1SLionel Sambuc  *
711be35a1SLionel Sambuc  * This code is derived from software contributed to The NetBSD Foundation
811be35a1SLionel Sambuc  * by Jukka Ruohonen.
911be35a1SLionel Sambuc  *
1011be35a1SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
1111be35a1SLionel Sambuc  * modification, are permitted provided that the following conditions
1211be35a1SLionel Sambuc  * are met:
1311be35a1SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
1411be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
1511be35a1SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
1611be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
1711be35a1SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
1811be35a1SLionel Sambuc  *
1911be35a1SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2011be35a1SLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2111be35a1SLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2211be35a1SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2311be35a1SLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2411be35a1SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2511be35a1SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2611be35a1SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2711be35a1SLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2811be35a1SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2911be35a1SLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
3011be35a1SLionel Sambuc  */
3111be35a1SLionel Sambuc #include <sys/cdefs.h>
32*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: t_ldexp.c,v 1.14 2014/11/04 00:20:19 justin Exp $");
33*0a6a1f1dSLionel Sambuc 
34*0a6a1f1dSLionel Sambuc #include <sys/param.h>
3511be35a1SLionel Sambuc 
3611be35a1SLionel Sambuc #include <atf-c.h>
3711be35a1SLionel Sambuc 
3811be35a1SLionel Sambuc #include <math.h>
3911be35a1SLionel Sambuc #include <limits.h>
4011be35a1SLionel Sambuc #include <stdio.h>
4111be35a1SLionel Sambuc #include <string.h>
4211be35a1SLionel Sambuc 
4311be35a1SLionel Sambuc #define SKIP	9999
4411be35a1SLionel Sambuc #define FORMAT  "%23.23lg"
4511be35a1SLionel Sambuc 
4611be35a1SLionel Sambuc static const int exps[] = { 0, 1, -1, 100, -100 };
4711be35a1SLionel Sambuc 
4811be35a1SLionel Sambuc struct ldexp_test {
4911be35a1SLionel Sambuc 	double	    x;
5011be35a1SLionel Sambuc 	int	    exp1;
5111be35a1SLionel Sambuc 	int	    exp2;
5211be35a1SLionel Sambuc 	const char *result;
5311be35a1SLionel Sambuc };
5411be35a1SLionel Sambuc 
5511be35a1SLionel Sambuc struct ldexp_test ldexp_basic[] = {
5611be35a1SLionel Sambuc 	{ 1.0,	5,	SKIP,	"                     32" },
5711be35a1SLionel Sambuc 	{ 1.0,	1022,	SKIP,	"4.4942328371557897693233e+307" },
5811be35a1SLionel Sambuc 	{ 1.0,	1023,	-1,	"4.4942328371557897693233e+307" },
5911be35a1SLionel Sambuc 	{ 1.0,	1023,	SKIP,	"8.9884656743115795386465e+307" },
6011be35a1SLionel Sambuc 	{ 1.0,	1022,	1,	"8.9884656743115795386465e+307" },
6111be35a1SLionel Sambuc 	{ 1.0,	-1022,	2045,	"8.9884656743115795386465e+307" },
6211be35a1SLionel Sambuc 	{ 1.0,	-5,	SKIP,	"                0.03125" },
6311be35a1SLionel Sambuc 	{ 1.0,	-1021,	SKIP,	"4.4501477170144027661805e-308" },
6411be35a1SLionel Sambuc 	{ 1.0,	-1022,	1,	"4.4501477170144027661805e-308" },
6511be35a1SLionel Sambuc 	{ 1.0,	-1022,	SKIP,	"2.2250738585072013830902e-308" },
6611be35a1SLionel Sambuc 	{ 1.0,	-1021,	-1,	"2.2250738585072013830902e-308" },
6711be35a1SLionel Sambuc 	{ 1.0,	1023,	-2045,	"2.2250738585072013830902e-308" },
6811be35a1SLionel Sambuc 	{ 1.0,	1023,	-1023,	"                      1" },
6911be35a1SLionel Sambuc 	{ 1.0,	-1022,	1022,	"                      1" },
7011be35a1SLionel Sambuc 	{ 0,	0,	0,	NULL }
7111be35a1SLionel Sambuc };
7211be35a1SLionel Sambuc 
7311be35a1SLionel Sambuc struct ldexp_test ldexp_zero[] = {
7411be35a1SLionel Sambuc 	{ 0.0,	-1,	SKIP,	"                      0" },
7511be35a1SLionel Sambuc 	{ 0.0,	0,	SKIP,	"                      0" },
7611be35a1SLionel Sambuc 	{ 0.0,	1,	SKIP,	"                      0" },
7711be35a1SLionel Sambuc 	{ 0.0,	1024,	SKIP,	"                      0" },
7811be35a1SLionel Sambuc 	{ 0.0,	1025,	SKIP,	"                      0" },
7911be35a1SLionel Sambuc 	{ 0.0,	-1023,	SKIP,	"                      0" },
8011be35a1SLionel Sambuc 	{ 0.0,	-1024,	SKIP,	"                      0" },
8111be35a1SLionel Sambuc 	{ 0,	0,	0,	NULL }
8211be35a1SLionel Sambuc };
8311be35a1SLionel Sambuc 
8411be35a1SLionel Sambuc struct ldexp_test ldexp_infinity[] = {
8511be35a1SLionel Sambuc 	{ 1.0,	1024,	-1,	"                    inf" },
8611be35a1SLionel Sambuc 	{ 1.0,	1024,	0,	"                    inf" },
8711be35a1SLionel Sambuc 	{ 1.0,	1024,	1,	"                    inf" },
8811be35a1SLionel Sambuc 	{ -1.0,	1024,	-1,	"                   -inf" },
8911be35a1SLionel Sambuc 	{ -1.0,	1024,	0,	"                   -inf" },
9011be35a1SLionel Sambuc 	{ -1.0,	1024,	1,	"                   -inf" },
9111be35a1SLionel Sambuc 	{ 0,	0,	0,	NULL }
9211be35a1SLionel Sambuc };
9311be35a1SLionel Sambuc 
9411be35a1SLionel Sambuc struct ldexp_test ldexp_overflow[] = {
9511be35a1SLionel Sambuc 	{ 1.0,	1024,	SKIP,	"                    inf" },
9611be35a1SLionel Sambuc 	{ 1.0,	1023,	1,	"                    inf" },
9711be35a1SLionel Sambuc 	{ 1.0,	-1022,	2046,	"                    inf" },
9811be35a1SLionel Sambuc 	{ 1.0,	1025,	SKIP,	"                    inf" },
9911be35a1SLionel Sambuc 	{ -1.0,	1024,	SKIP,	"                   -inf" },
10011be35a1SLionel Sambuc 	{ -1.0,	1023,	1,	"                   -inf" },
10111be35a1SLionel Sambuc 	{ -1.0,	-1022,	2046,	"                   -inf" },
10211be35a1SLionel Sambuc 	{ -1.0,	1025,	SKIP,	"                   -inf" },
10311be35a1SLionel Sambuc 	{ 0,	0,	0,	NULL }
10411be35a1SLionel Sambuc };
10511be35a1SLionel Sambuc 
10611be35a1SLionel Sambuc struct ldexp_test ldexp_denormal[] = {
10711be35a1SLionel Sambuc 	{ 1.0,	-1023,	SKIP,	"1.1125369292536006915451e-308" },
10811be35a1SLionel Sambuc 	{ 1.0,	-1022,	-1,	"1.1125369292536006915451e-308" },
10911be35a1SLionel Sambuc 	{ 1.0,	1023,	-2046,	"1.1125369292536006915451e-308" },
11011be35a1SLionel Sambuc 	{ 1.0,	-1024,	SKIP,	"5.5626846462680034577256e-309" },
11111be35a1SLionel Sambuc 	{ 1.0,	-1074,	SKIP,	"4.9406564584124654417657e-324" },
11211be35a1SLionel Sambuc 	{ -1.0,	-1023,	SKIP,	"-1.1125369292536006915451e-308" },
11311be35a1SLionel Sambuc 	{ -1.0,	-1022,	-1,	"-1.1125369292536006915451e-308" },
11411be35a1SLionel Sambuc 	{ -1.0,	1023,	-2046,	"-1.1125369292536006915451e-308" },
11511be35a1SLionel Sambuc 	{ -1.0,	-1024,	SKIP,	"-5.5626846462680034577256e-309" },
11611be35a1SLionel Sambuc 	{ -1.0,	-1074,	SKIP,	"-4.9406564584124654417657e-324" },
11711be35a1SLionel Sambuc 	{ 0,	0,	0,	NULL }
11811be35a1SLionel Sambuc };
11911be35a1SLionel Sambuc 
12011be35a1SLionel Sambuc struct ldexp_test ldexp_underflow[] = {
12111be35a1SLionel Sambuc 	{ 1.0,	-1075,	SKIP,	"                      0" },
12211be35a1SLionel Sambuc 	{ 1.0,	-1074,	-1,	"                      0" },
12311be35a1SLionel Sambuc 	{ 1.0,	1023,	-2098,	"                      0" },
12411be35a1SLionel Sambuc 	{ 1.0,	-1076,	SKIP,	"                      0" },
12511be35a1SLionel Sambuc 	{ -1.0,	-1075,	SKIP,	"                     -0" },
12611be35a1SLionel Sambuc 	{ -1.0,	-1074,	-1,	"                     -0" },
12711be35a1SLionel Sambuc 	{ -1.0,	1023,	-2098,	"                     -0" },
12811be35a1SLionel Sambuc 	{ -1.0,	-1076,	SKIP,	"                     -0" },
12911be35a1SLionel Sambuc 	{ 0,	0,	0,	NULL }
13011be35a1SLionel Sambuc };
13111be35a1SLionel Sambuc 
13211be35a1SLionel Sambuc struct ldexp_test ldexp_denormal_large[] = {
13311be35a1SLionel Sambuc 	{ 1.0,	-1028,	1024,	"                 0.0625" },
13411be35a1SLionel Sambuc 	{ 1.0,	-1028,	1025,	"                  0.125" },
13511be35a1SLionel Sambuc 	{ 1.0,	-1028,	1026,	"                   0.25" },
13611be35a1SLionel Sambuc 	{ 1.0,	-1028,	1027,	"                    0.5" },
13711be35a1SLionel Sambuc 	{ 1.0,	-1028,	1028,	"                      1" },
13811be35a1SLionel Sambuc 	{ 1.0,	-1028,	1029,	"                      2" },
13911be35a1SLionel Sambuc 	{ 1.0,	-1028,	1030,	"                      4" },
14011be35a1SLionel Sambuc 	{ 1.0,	-1028,	1040,	"                   4096" },
14111be35a1SLionel Sambuc 	{ 1.0,	-1028,	1050,	"                4194304" },
14211be35a1SLionel Sambuc 	{ 1.0,	-1028,	1060,	"             4294967296" },
14311be35a1SLionel Sambuc 	{ 1.0,	-1028,	1100,	" 4722366482869645213696" },
14411be35a1SLionel Sambuc 	{ 1.0,	-1028,	1200,	"5.9863107065073783529623e+51" },
14511be35a1SLionel Sambuc 	{ 1.0,	-1028,	1300,	"7.5885503602567541832791e+81" },
14611be35a1SLionel Sambuc 	{ 1.0,	-1028,	1400,	"9.6196304190416209014353e+111" },
14711be35a1SLionel Sambuc 	{ 1.0,	-1028,	1500,	"1.2194330274671844653834e+142" },
14811be35a1SLionel Sambuc 	{ 1.0,	-1028,	1600,	"1.5458150092069033378781e+172" },
14911be35a1SLionel Sambuc 	{ 1.0,	-1028,	1700,	"1.9595533242629369747791e+202" },
15011be35a1SLionel Sambuc 	{ 1.0,	-1028,	1800,	"2.4840289476811342962384e+232" },
15111be35a1SLionel Sambuc 	{ 1.0,	-1028,	1900,	"3.1488807865122869393369e+262" },
15211be35a1SLionel Sambuc 	{ 1.0,	-1028,	2000,	"3.9916806190694396233127e+292" },
15311be35a1SLionel Sambuc 	{ 1.0,	-1028,	2046,	"2.808895523222368605827e+306" },
15411be35a1SLionel Sambuc 	{ 1.0,	-1028,	2047,	"5.6177910464447372116541e+306" },
15511be35a1SLionel Sambuc 	{ 1.0,	-1028,	2048,	"1.1235582092889474423308e+307" },
15611be35a1SLionel Sambuc 	{ 1.0,	-1028,	2049,	"2.2471164185778948846616e+307" },
15711be35a1SLionel Sambuc 	{ 1.0,	-1028,	2050,	"4.4942328371557897693233e+307" },
15811be35a1SLionel Sambuc 	{ 1.0,	-1028,	2051,	"8.9884656743115795386465e+307" },
15911be35a1SLionel Sambuc 	{ 0,	0,	0,	NULL }
16011be35a1SLionel Sambuc };
16111be35a1SLionel Sambuc 
16211be35a1SLionel Sambuc static void
run_test(struct ldexp_test * table)16311be35a1SLionel Sambuc run_test(struct ldexp_test *table)
16411be35a1SLionel Sambuc {
16511be35a1SLionel Sambuc 	char outbuf[64];
16611be35a1SLionel Sambuc 	size_t i;
16711be35a1SLionel Sambuc 	double v;
16811be35a1SLionel Sambuc 
16911be35a1SLionel Sambuc 	for (i = 0; table->result != NULL; table++, i++) {
17011be35a1SLionel Sambuc 
17111be35a1SLionel Sambuc 		v = ldexp(table->x, table->exp1);
17211be35a1SLionel Sambuc 
17311be35a1SLionel Sambuc 		if (table->exp2 == SKIP)
17411be35a1SLionel Sambuc 			continue;
17511be35a1SLionel Sambuc 
17611be35a1SLionel Sambuc 		v = ldexp(v, table->exp2);
17711be35a1SLionel Sambuc 
17811be35a1SLionel Sambuc 		(void)snprintf(outbuf, sizeof(outbuf), FORMAT, v);
17911be35a1SLionel Sambuc 
18011be35a1SLionel Sambuc 		ATF_CHECK_STREQ_MSG(table->result, outbuf,
18111be35a1SLionel Sambuc 			    "Entry %zu:\n\tExp: \"%s\"\n\tAct: \"%s\"",
18211be35a1SLionel Sambuc 			    i, table->result, outbuf);
18311be35a1SLionel Sambuc 	}
18411be35a1SLionel Sambuc }
18511be35a1SLionel Sambuc 
18611be35a1SLionel Sambuc /*
18711be35a1SLionel Sambuc  * ldexp(3)
18811be35a1SLionel Sambuc  */
18911be35a1SLionel Sambuc ATF_TC(ldexp_exp2);
ATF_TC_HEAD(ldexp_exp2,tc)19011be35a1SLionel Sambuc ATF_TC_HEAD(ldexp_exp2, tc)
19111be35a1SLionel Sambuc {
19211be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test ldexp(x, n) == x * exp2(n)");
19311be35a1SLionel Sambuc }
19411be35a1SLionel Sambuc 
ATF_TC_BODY(ldexp_exp2,tc)19511be35a1SLionel Sambuc ATF_TC_BODY(ldexp_exp2, tc)
19611be35a1SLionel Sambuc {
19711be35a1SLionel Sambuc 	const double n[] = { 1, 2, 3, 10, 50, 100 };
198*0a6a1f1dSLionel Sambuc #if __DBL_MIN_10_EXP__ <= -40
19911be35a1SLionel Sambuc 	const double eps = 1.0e-40;
200*0a6a1f1dSLionel Sambuc #else
201*0a6a1f1dSLionel Sambuc 	const double eps = __DBL_MIN__*4.0;
202*0a6a1f1dSLionel Sambuc #endif
20311be35a1SLionel Sambuc 	const double x = 12.0;
20411be35a1SLionel Sambuc 	double y;
20511be35a1SLionel Sambuc 	size_t i;
20611be35a1SLionel Sambuc 
20711be35a1SLionel Sambuc 	for (i = 0; i < __arraycount(n); i++) {
20811be35a1SLionel Sambuc 
20911be35a1SLionel Sambuc 		y = ldexp(x, n[i]);
21011be35a1SLionel Sambuc 
21111be35a1SLionel Sambuc 		if (fabs(y - (x * exp2(n[i]))) > eps) {
21211be35a1SLionel Sambuc 			atf_tc_fail_nonfatal("ldexp(%0.01f, %0.01f) "
21311be35a1SLionel Sambuc 			    "!= %0.01f * exp2(%0.01f)", x, n[i], x, n[i]);
21411be35a1SLionel Sambuc 		}
21511be35a1SLionel Sambuc 	}
21611be35a1SLionel Sambuc }
21711be35a1SLionel Sambuc 
21811be35a1SLionel Sambuc ATF_TC(ldexp_nan);
ATF_TC_HEAD(ldexp_nan,tc)21911be35a1SLionel Sambuc ATF_TC_HEAD(ldexp_nan, tc)
22011be35a1SLionel Sambuc {
22111be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test ldexp(NaN) == NaN");
22211be35a1SLionel Sambuc }
22311be35a1SLionel Sambuc 
ATF_TC_BODY(ldexp_nan,tc)22411be35a1SLionel Sambuc ATF_TC_BODY(ldexp_nan, tc)
22511be35a1SLionel Sambuc {
22611be35a1SLionel Sambuc 	const double x = 0.0L / 0.0L;
22711be35a1SLionel Sambuc 	double y;
22811be35a1SLionel Sambuc 	size_t i;
22911be35a1SLionel Sambuc 
23011be35a1SLionel Sambuc 	ATF_REQUIRE(isnan(x) != 0);
23111be35a1SLionel Sambuc 
23211be35a1SLionel Sambuc 	for (i = 0; i < __arraycount(exps); i++) {
23311be35a1SLionel Sambuc 		y = ldexp(x, exps[i]);
23411be35a1SLionel Sambuc 		ATF_CHECK(isnan(y) != 0);
23511be35a1SLionel Sambuc 	}
23611be35a1SLionel Sambuc }
23711be35a1SLionel Sambuc 
23811be35a1SLionel Sambuc ATF_TC(ldexp_inf_neg);
ATF_TC_HEAD(ldexp_inf_neg,tc)23911be35a1SLionel Sambuc ATF_TC_HEAD(ldexp_inf_neg, tc)
24011be35a1SLionel Sambuc {
24111be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test ldexp(-Inf) == -Inf");
24211be35a1SLionel Sambuc }
24311be35a1SLionel Sambuc 
ATF_TC_BODY(ldexp_inf_neg,tc)24411be35a1SLionel Sambuc ATF_TC_BODY(ldexp_inf_neg, tc)
24511be35a1SLionel Sambuc {
24611be35a1SLionel Sambuc 	const double x = -1.0L / 0.0L;
24711be35a1SLionel Sambuc 	size_t i;
24811be35a1SLionel Sambuc 
24911be35a1SLionel Sambuc 	for (i = 0; i < __arraycount(exps); i++)
25011be35a1SLionel Sambuc 		ATF_CHECK(ldexp(x, exps[i]) == x);
25111be35a1SLionel Sambuc }
25211be35a1SLionel Sambuc 
25311be35a1SLionel Sambuc ATF_TC(ldexp_inf_pos);
ATF_TC_HEAD(ldexp_inf_pos,tc)25411be35a1SLionel Sambuc ATF_TC_HEAD(ldexp_inf_pos, tc)
25511be35a1SLionel Sambuc {
25611be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test ldexp(+Inf) == +Inf");
25711be35a1SLionel Sambuc }
25811be35a1SLionel Sambuc 
ATF_TC_BODY(ldexp_inf_pos,tc)25911be35a1SLionel Sambuc ATF_TC_BODY(ldexp_inf_pos, tc)
26011be35a1SLionel Sambuc {
26111be35a1SLionel Sambuc 	const double x = 1.0L / 0.0L;
26211be35a1SLionel Sambuc 	size_t i;
26311be35a1SLionel Sambuc 
26411be35a1SLionel Sambuc 	for (i = 0; i < __arraycount(exps); i++)
26511be35a1SLionel Sambuc 		ATF_CHECK(ldexp(x, exps[i]) == x);
26611be35a1SLionel Sambuc }
26711be35a1SLionel Sambuc 
26811be35a1SLionel Sambuc ATF_TC(ldexp_zero_neg);
ATF_TC_HEAD(ldexp_zero_neg,tc)26911be35a1SLionel Sambuc ATF_TC_HEAD(ldexp_zero_neg, tc)
27011be35a1SLionel Sambuc {
27111be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test ldexp(-0.0) == -0.0");
27211be35a1SLionel Sambuc }
27311be35a1SLionel Sambuc 
ATF_TC_BODY(ldexp_zero_neg,tc)27411be35a1SLionel Sambuc ATF_TC_BODY(ldexp_zero_neg, tc)
27511be35a1SLionel Sambuc {
27611be35a1SLionel Sambuc 	const double x = -0.0L;
27711be35a1SLionel Sambuc 	double y;
27811be35a1SLionel Sambuc 	size_t i;
27911be35a1SLionel Sambuc 
28011be35a1SLionel Sambuc 	ATF_REQUIRE(signbit(x) != 0);
28111be35a1SLionel Sambuc 
28211be35a1SLionel Sambuc 	for (i = 0; i < __arraycount(exps); i++) {
28311be35a1SLionel Sambuc 		y = ldexp(x, exps[i]);
28411be35a1SLionel Sambuc 		ATF_CHECK(x == y);
28511be35a1SLionel Sambuc 		ATF_CHECK(signbit(y) != 0);
28611be35a1SLionel Sambuc 	}
28711be35a1SLionel Sambuc }
28811be35a1SLionel Sambuc 
28911be35a1SLionel Sambuc ATF_TC(ldexp_zero_pos);
ATF_TC_HEAD(ldexp_zero_pos,tc)29011be35a1SLionel Sambuc ATF_TC_HEAD(ldexp_zero_pos, tc)
29111be35a1SLionel Sambuc {
29211be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test ldexp(+0.0) == +0.0");
29311be35a1SLionel Sambuc }
29411be35a1SLionel Sambuc 
ATF_TC_BODY(ldexp_zero_pos,tc)29511be35a1SLionel Sambuc ATF_TC_BODY(ldexp_zero_pos, tc)
29611be35a1SLionel Sambuc {
29711be35a1SLionel Sambuc 	const double x = 0.0L;
29811be35a1SLionel Sambuc 	double y;
29911be35a1SLionel Sambuc 	size_t i;
30011be35a1SLionel Sambuc 
30111be35a1SLionel Sambuc 	ATF_REQUIRE(signbit(x) == 0);
30211be35a1SLionel Sambuc 
30311be35a1SLionel Sambuc 	for (i = 0; i < __arraycount(exps); i++) {
30411be35a1SLionel Sambuc 		y = ldexp(x, exps[i]);
30511be35a1SLionel Sambuc 		ATF_CHECK(x == y);
30611be35a1SLionel Sambuc 		ATF_CHECK(signbit(y) == 0);
30711be35a1SLionel Sambuc 	}
30811be35a1SLionel Sambuc }
30911be35a1SLionel Sambuc 
31011be35a1SLionel Sambuc /*
31111be35a1SLionel Sambuc  * ldexpf(3)
31211be35a1SLionel Sambuc  */
31311be35a1SLionel Sambuc 
31411be35a1SLionel Sambuc ATF_TC(ldexpf_exp2f);
ATF_TC_HEAD(ldexpf_exp2f,tc)31511be35a1SLionel Sambuc ATF_TC_HEAD(ldexpf_exp2f, tc)
31611be35a1SLionel Sambuc {
31711be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test ldexpf(x, n) == x * exp2f(n)");
31811be35a1SLionel Sambuc }
31911be35a1SLionel Sambuc 
ATF_TC_BODY(ldexpf_exp2f,tc)32011be35a1SLionel Sambuc ATF_TC_BODY(ldexpf_exp2f, tc)
32111be35a1SLionel Sambuc {
32211be35a1SLionel Sambuc 	const float n[] = { 1, 2, 3, 10, 50, 100 };
32311be35a1SLionel Sambuc 	const float eps = 1.0e-9;
32411be35a1SLionel Sambuc 	const float x = 12.0;
32511be35a1SLionel Sambuc 	float y;
32611be35a1SLionel Sambuc 	size_t i;
32711be35a1SLionel Sambuc 
32811be35a1SLionel Sambuc 	for (i = 0; i < __arraycount(n); i++) {
32911be35a1SLionel Sambuc 
33011be35a1SLionel Sambuc 		y = ldexpf(x, n[i]);
33111be35a1SLionel Sambuc 
33211be35a1SLionel Sambuc 		if (fabsf(y - (x * exp2f(n[i]))) > eps) {
33311be35a1SLionel Sambuc 			atf_tc_fail_nonfatal("ldexpf(%0.01f, %0.01f) "
33411be35a1SLionel Sambuc 			    "!= %0.01f * exp2f(%0.01f)", x, n[i], x, n[i]);
33511be35a1SLionel Sambuc 		}
33611be35a1SLionel Sambuc 	}
33711be35a1SLionel Sambuc }
33811be35a1SLionel Sambuc 
33911be35a1SLionel Sambuc ATF_TC(ldexpf_nan);
ATF_TC_HEAD(ldexpf_nan,tc)34011be35a1SLionel Sambuc ATF_TC_HEAD(ldexpf_nan, tc)
34111be35a1SLionel Sambuc {
34211be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test ldexpf(NaN) == NaN");
34311be35a1SLionel Sambuc }
34411be35a1SLionel Sambuc 
ATF_TC_BODY(ldexpf_nan,tc)34511be35a1SLionel Sambuc ATF_TC_BODY(ldexpf_nan, tc)
34611be35a1SLionel Sambuc {
34711be35a1SLionel Sambuc 	const float x = 0.0L / 0.0L;
34811be35a1SLionel Sambuc 	float y;
34911be35a1SLionel Sambuc 	size_t i;
35011be35a1SLionel Sambuc 
35111be35a1SLionel Sambuc 	ATF_REQUIRE(isnan(x) != 0);
35211be35a1SLionel Sambuc 
35311be35a1SLionel Sambuc 	for (i = 0; i < __arraycount(exps); i++) {
35411be35a1SLionel Sambuc 		y = ldexpf(x, exps[i]);
35511be35a1SLionel Sambuc 		ATF_CHECK(isnan(y) != 0);
35611be35a1SLionel Sambuc 	}
35711be35a1SLionel Sambuc }
35811be35a1SLionel Sambuc 
35911be35a1SLionel Sambuc ATF_TC(ldexpf_inf_neg);
ATF_TC_HEAD(ldexpf_inf_neg,tc)36011be35a1SLionel Sambuc ATF_TC_HEAD(ldexpf_inf_neg, tc)
36111be35a1SLionel Sambuc {
36211be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test ldexpf(-Inf) == -Inf");
36311be35a1SLionel Sambuc }
36411be35a1SLionel Sambuc 
ATF_TC_BODY(ldexpf_inf_neg,tc)36511be35a1SLionel Sambuc ATF_TC_BODY(ldexpf_inf_neg, tc)
36611be35a1SLionel Sambuc {
36711be35a1SLionel Sambuc 	const float x = -1.0L / 0.0L;
36811be35a1SLionel Sambuc 	size_t i;
36911be35a1SLionel Sambuc 
37011be35a1SLionel Sambuc 	for (i = 0; i < __arraycount(exps); i++)
37111be35a1SLionel Sambuc 		ATF_CHECK(ldexpf(x, exps[i]) == x);
37211be35a1SLionel Sambuc }
37311be35a1SLionel Sambuc 
37411be35a1SLionel Sambuc ATF_TC(ldexpf_inf_pos);
ATF_TC_HEAD(ldexpf_inf_pos,tc)37511be35a1SLionel Sambuc ATF_TC_HEAD(ldexpf_inf_pos, tc)
37611be35a1SLionel Sambuc {
37711be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test ldexpf(+Inf) == +Inf");
37811be35a1SLionel Sambuc }
37911be35a1SLionel Sambuc 
ATF_TC_BODY(ldexpf_inf_pos,tc)38011be35a1SLionel Sambuc ATF_TC_BODY(ldexpf_inf_pos, tc)
38111be35a1SLionel Sambuc {
38211be35a1SLionel Sambuc 	const float x = 1.0L / 0.0L;
38311be35a1SLionel Sambuc 	size_t i;
38411be35a1SLionel Sambuc 
38511be35a1SLionel Sambuc 	for (i = 0; i < __arraycount(exps); i++)
38611be35a1SLionel Sambuc 		ATF_CHECK(ldexpf(x, exps[i]) == x);
38711be35a1SLionel Sambuc }
38811be35a1SLionel Sambuc 
38911be35a1SLionel Sambuc ATF_TC(ldexpf_zero_neg);
ATF_TC_HEAD(ldexpf_zero_neg,tc)39011be35a1SLionel Sambuc ATF_TC_HEAD(ldexpf_zero_neg, tc)
39111be35a1SLionel Sambuc {
39211be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test ldexpf(-0.0) == -0.0");
39311be35a1SLionel Sambuc }
39411be35a1SLionel Sambuc 
ATF_TC_BODY(ldexpf_zero_neg,tc)39511be35a1SLionel Sambuc ATF_TC_BODY(ldexpf_zero_neg, tc)
39611be35a1SLionel Sambuc {
39711be35a1SLionel Sambuc 	const float x = -0.0L;
39811be35a1SLionel Sambuc 	float y;
39911be35a1SLionel Sambuc 	size_t i;
40011be35a1SLionel Sambuc 
40111be35a1SLionel Sambuc 	ATF_REQUIRE(signbit(x) != 0);
40211be35a1SLionel Sambuc 
40311be35a1SLionel Sambuc 	for (i = 0; i < __arraycount(exps); i++) {
40411be35a1SLionel Sambuc 		y = ldexpf(x, exps[i]);
40511be35a1SLionel Sambuc 		ATF_CHECK(x == y);
40611be35a1SLionel Sambuc 		ATF_CHECK(signbit(y) != 0);
40711be35a1SLionel Sambuc 	}
40811be35a1SLionel Sambuc }
40911be35a1SLionel Sambuc 
41011be35a1SLionel Sambuc ATF_TC(ldexpf_zero_pos);
ATF_TC_HEAD(ldexpf_zero_pos,tc)41111be35a1SLionel Sambuc ATF_TC_HEAD(ldexpf_zero_pos, tc)
41211be35a1SLionel Sambuc {
41311be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test ldexpf(+0.0) == +0.0");
41411be35a1SLionel Sambuc }
41511be35a1SLionel Sambuc 
ATF_TC_BODY(ldexpf_zero_pos,tc)41611be35a1SLionel Sambuc ATF_TC_BODY(ldexpf_zero_pos, tc)
41711be35a1SLionel Sambuc {
41811be35a1SLionel Sambuc 	const float x = 0.0L;
41911be35a1SLionel Sambuc 	float y;
42011be35a1SLionel Sambuc 	size_t i;
42111be35a1SLionel Sambuc 
42211be35a1SLionel Sambuc 	ATF_REQUIRE(signbit(x) == 0);
42311be35a1SLionel Sambuc 
42411be35a1SLionel Sambuc 	for (i = 0; i < __arraycount(exps); i++) {
42511be35a1SLionel Sambuc 		y = ldexpf(x, exps[i]);
42611be35a1SLionel Sambuc 		ATF_CHECK(x == y);
42711be35a1SLionel Sambuc 		ATF_CHECK(signbit(y) == 0);
42811be35a1SLionel Sambuc 	}
42911be35a1SLionel Sambuc }
43011be35a1SLionel Sambuc 
43111be35a1SLionel Sambuc #define TEST(name, desc)						\
43211be35a1SLionel Sambuc 	ATF_TC(name);							\
43311be35a1SLionel Sambuc 	ATF_TC_HEAD(name, tc)						\
43411be35a1SLionel Sambuc 	{								\
43511be35a1SLionel Sambuc 									\
43611be35a1SLionel Sambuc 		atf_tc_set_md_var(tc, "descr",				\
43711be35a1SLionel Sambuc 		    "Test ldexp(3) for " ___STRING(desc));		\
43811be35a1SLionel Sambuc 	}								\
43911be35a1SLionel Sambuc 	ATF_TC_BODY(name, tc)						\
44011be35a1SLionel Sambuc 	{								\
441*0a6a1f1dSLionel Sambuc 		if (strcmp("vax", MACHINE_ARCH) == 0)			\
442*0a6a1f1dSLionel Sambuc 			atf_tc_skip("Test not valid for " MACHINE_ARCH); \
44311be35a1SLionel Sambuc 		run_test(name);						\
44411be35a1SLionel Sambuc 	}
44511be35a1SLionel Sambuc 
TEST(ldexp_basic,basics)44611be35a1SLionel Sambuc TEST(ldexp_basic, basics)
44711be35a1SLionel Sambuc TEST(ldexp_zero, zero)
44811be35a1SLionel Sambuc TEST(ldexp_infinity, infinity)
44911be35a1SLionel Sambuc TEST(ldexp_overflow, overflow)
45011be35a1SLionel Sambuc TEST(ldexp_denormal, denormal)
45111be35a1SLionel Sambuc TEST(ldexp_denormal_large, large)
45211be35a1SLionel Sambuc TEST(ldexp_underflow, underflow)
45311be35a1SLionel Sambuc 
45411be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
45511be35a1SLionel Sambuc {
45611be35a1SLionel Sambuc 
45711be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, ldexp_basic);
45811be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, ldexp_zero);
45911be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, ldexp_infinity);
46011be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, ldexp_overflow);
46111be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, ldexp_denormal);
46211be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, ldexp_underflow);
46311be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, ldexp_denormal_large);
46411be35a1SLionel Sambuc 
46511be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, ldexp_exp2);
46611be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, ldexp_nan);
46711be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, ldexp_inf_neg);
46811be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, ldexp_inf_pos);
46911be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, ldexp_zero_neg);
47011be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, ldexp_zero_pos);
47111be35a1SLionel Sambuc 
47211be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, ldexpf_exp2f);
47311be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, ldexpf_nan);
47411be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, ldexpf_inf_neg);
47511be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, ldexpf_inf_pos);
47611be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, ldexpf_zero_neg);
47711be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, ldexpf_zero_pos);
47811be35a1SLionel Sambuc 
47911be35a1SLionel Sambuc 	return atf_no_error();
48011be35a1SLionel Sambuc }
481