xref: /minix3/tests/lib/libc/gen/t_fpsetround.c (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc /* $NetBSD: t_fpsetround.c,v 1.6 2011/10/01 17:46:10 christos Exp $ */
2*11be35a1SLionel Sambuc 
3*11be35a1SLionel Sambuc /*-
4*11be35a1SLionel Sambuc  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5*11be35a1SLionel Sambuc  * All rights reserved.
6*11be35a1SLionel Sambuc  *
7*11be35a1SLionel Sambuc  * This code is derived from software contributed to The NetBSD Foundation
8*11be35a1SLionel Sambuc  * by Christos Zoulas.
9*11be35a1SLionel Sambuc  *
10*11be35a1SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
11*11be35a1SLionel Sambuc  * modification, are permitted provided that the following conditions
12*11be35a1SLionel Sambuc  * are met:
13*11be35a1SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
14*11be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
15*11be35a1SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16*11be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17*11be35a1SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18*11be35a1SLionel Sambuc  * 3. All advertising materials mentioning features or use of this software
19*11be35a1SLionel Sambuc  *    must display the following acknowledgement:
20*11be35a1SLionel Sambuc  *        This product includes software developed by the NetBSD
21*11be35a1SLionel Sambuc  *        Foundation, Inc. and its contributors.
22*11be35a1SLionel Sambuc  * 4. Neither the name of The NetBSD Foundation nor the names of its
23*11be35a1SLionel Sambuc  *    contributors may be used to endorse or promote products derived
24*11be35a1SLionel Sambuc  *    from this software without specific prior written permission.
25*11be35a1SLionel Sambuc  *
26*11be35a1SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27*11be35a1SLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28*11be35a1SLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29*11be35a1SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30*11be35a1SLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31*11be35a1SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32*11be35a1SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33*11be35a1SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34*11be35a1SLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35*11be35a1SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36*11be35a1SLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
37*11be35a1SLionel Sambuc  */
38*11be35a1SLionel Sambuc #include <sys/cdefs.h>
39*11be35a1SLionel Sambuc __RCSID("$NetBSD: t_fpsetround.c,v 1.6 2011/10/01 17:46:10 christos Exp $");
40*11be35a1SLionel Sambuc 
41*11be35a1SLionel Sambuc #include <float.h>
42*11be35a1SLionel Sambuc #include <math.h>
43*11be35a1SLionel Sambuc #include <stdlib.h>
44*11be35a1SLionel Sambuc #include <string.h>
45*11be35a1SLionel Sambuc #include <stdio.h>
46*11be35a1SLionel Sambuc 
47*11be35a1SLionel Sambuc #include <atf-c.h>
48*11be35a1SLionel Sambuc 
49*11be35a1SLionel Sambuc ATF_TC(fpsetround_basic);
ATF_TC_HEAD(fpsetround_basic,tc)50*11be35a1SLionel Sambuc ATF_TC_HEAD(fpsetround_basic, tc)
51*11be35a1SLionel Sambuc {
52*11be35a1SLionel Sambuc 
53*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
54*11be35a1SLionel Sambuc 	    "Minimal testing of fpgetround(3) and fpsetround(3)");
55*11be35a1SLionel Sambuc }
56*11be35a1SLionel Sambuc 
57*11be35a1SLionel Sambuc #ifdef _FLOAT_IEEE754
58*11be35a1SLionel Sambuc #include <ieeefp.h>
59*11be35a1SLionel Sambuc 
60*11be35a1SLionel Sambuc static const struct {
61*11be35a1SLionel Sambuc 	const char *n;
62*11be35a1SLionel Sambuc 	int rm;
63*11be35a1SLionel Sambuc 	int rf;
64*11be35a1SLionel Sambuc } rnd[] = {
65*11be35a1SLionel Sambuc 	{ "RN", FP_RN, 1 },
66*11be35a1SLionel Sambuc 	{ "RP", FP_RP, 2 },
67*11be35a1SLionel Sambuc 	{ "RM", FP_RM, 3 },
68*11be35a1SLionel Sambuc 	{ "RZ", FP_RZ, 0 },
69*11be35a1SLionel Sambuc 
70*11be35a1SLionel Sambuc };
71*11be35a1SLionel Sambuc 
72*11be35a1SLionel Sambuc static const struct {
73*11be35a1SLionel Sambuc 	const char *n;
74*11be35a1SLionel Sambuc 	int v[4];
75*11be35a1SLionel Sambuc } tst[] = {	/*  RN  RP  RM  RZ */
76*11be35a1SLionel Sambuc 	{  "1.1", {  1,  1,  2,  1 } },
77*11be35a1SLionel Sambuc 	{  "1.5", {  1,  2,  2,  1 } },
78*11be35a1SLionel Sambuc 	{  "1.9", {  1,  2,  2,  1 } },
79*11be35a1SLionel Sambuc 	{  "2.1", {  2,  2,  3,  2 } },
80*11be35a1SLionel Sambuc 	{  "2.5", {  2,  2,  3,  2 } },
81*11be35a1SLionel Sambuc 	{  "2.9", {  2,  3,  3,  2 } },
82*11be35a1SLionel Sambuc 	{ "-1.1", { -1, -1, -1, -2 } },
83*11be35a1SLionel Sambuc 	{ "-1.5", { -1, -2, -1, -2 } },
84*11be35a1SLionel Sambuc 	{ "-1.9", { -1, -2, -1, -2 } },
85*11be35a1SLionel Sambuc 	{ "-2.1", { -2, -2, -2, -3 } },
86*11be35a1SLionel Sambuc 	{ "-2.5", { -2, -2, -2, -3 } },
87*11be35a1SLionel Sambuc 	{ "-2.9", { -2, -3, -2, -3 } },
88*11be35a1SLionel Sambuc };
89*11be35a1SLionel Sambuc 
90*11be35a1SLionel Sambuc static const char *
getname(int r)91*11be35a1SLionel Sambuc getname(int r)
92*11be35a1SLionel Sambuc {
93*11be35a1SLionel Sambuc 	for (size_t i = 0; i < __arraycount(rnd); i++)
94*11be35a1SLionel Sambuc 		if (rnd[i].rm == r)
95*11be35a1SLionel Sambuc 			return rnd[i].n;
96*11be35a1SLionel Sambuc 	return "*unknown*";
97*11be35a1SLionel Sambuc }
98*11be35a1SLionel Sambuc 
99*11be35a1SLionel Sambuc static void
test(int r)100*11be35a1SLionel Sambuc test(int r)
101*11be35a1SLionel Sambuc {
102*11be35a1SLionel Sambuc 	int did = 0;
103*11be35a1SLionel Sambuc 	for (size_t i = 0; i < __arraycount(tst); i++) {
104*11be35a1SLionel Sambuc 		double d = strtod(tst[i].n, NULL);
105*11be35a1SLionel Sambuc 		int g = (int)rint(d);
106*11be35a1SLionel Sambuc 		int e = tst[i].v[r];
107*11be35a1SLionel Sambuc 		ATF_CHECK_EQ(g, e);
108*11be35a1SLionel Sambuc 		if (g != e) {
109*11be35a1SLionel Sambuc 			if (!did) {
110*11be35a1SLionel Sambuc 				fprintf(stderr, "Mode Value Result Expected\n");
111*11be35a1SLionel Sambuc 				did = 1;
112*11be35a1SLionel Sambuc 			}
113*11be35a1SLionel Sambuc 			fprintf(stderr, "%4.4s %-5.5s %6d %8d\n", rnd[r].n,
114*11be35a1SLionel Sambuc 			    tst[i].n, (int)rint(d), tst[i].v[r]);
115*11be35a1SLionel Sambuc 		}
116*11be35a1SLionel Sambuc 	}
117*11be35a1SLionel Sambuc }
118*11be35a1SLionel Sambuc #endif
119*11be35a1SLionel Sambuc 
120*11be35a1SLionel Sambuc 
ATF_TC_BODY(fpsetround_basic,tc)121*11be35a1SLionel Sambuc ATF_TC_BODY(fpsetround_basic, tc)
122*11be35a1SLionel Sambuc {
123*11be35a1SLionel Sambuc 
124*11be35a1SLionel Sambuc #ifndef _FLOAT_IEEE754
125*11be35a1SLionel Sambuc 	atf_tc_skip("Test not applicable on this architecture.");
126*11be35a1SLionel Sambuc #else
127*11be35a1SLionel Sambuc 	int r;
128*11be35a1SLionel Sambuc 
129*11be35a1SLionel Sambuc 	ATF_CHECK_EQ(r = fpgetround(), FP_RN);
130*11be35a1SLionel Sambuc 	if (FP_RN != r)
131*11be35a1SLionel Sambuc 		fprintf(stderr, "default expected=%s got=%s\n", getname(FP_RN),
132*11be35a1SLionel Sambuc 		    getname(r));
133*11be35a1SLionel Sambuc 	ATF_CHECK_EQ(FLT_ROUNDS, 1);
134*11be35a1SLionel Sambuc 
135*11be35a1SLionel Sambuc 	for (size_t i = 0; i < __arraycount(rnd); i++) {
136*11be35a1SLionel Sambuc 		const size_t j = (i + 1) & 3;
137*11be35a1SLionel Sambuc 		const int o = rnd[i].rm;
138*11be35a1SLionel Sambuc 		const int n = rnd[j].rm;
139*11be35a1SLionel Sambuc 
140*11be35a1SLionel Sambuc 		ATF_CHECK_EQ(r = fpsetround(n), o);
141*11be35a1SLionel Sambuc 		if (o != r)
142*11be35a1SLionel Sambuc 			fprintf(stderr, "set %s expected=%s got=%s\n",
143*11be35a1SLionel Sambuc 			    getname(n), getname(o), getname(r));
144*11be35a1SLionel Sambuc 		ATF_CHECK_EQ(r = fpgetround(), n);
145*11be35a1SLionel Sambuc 		if (n != r)
146*11be35a1SLionel Sambuc 			fprintf(stderr, "get expected=%s got=%s\n", getname(n),
147*11be35a1SLionel Sambuc 			    getname(r));
148*11be35a1SLionel Sambuc 		ATF_CHECK_EQ(r = FLT_ROUNDS, rnd[j].rf);
149*11be35a1SLionel Sambuc 		if (r != rnd[j].rf)
150*11be35a1SLionel Sambuc 			fprintf(stderr, "rounds expected=%x got=%x\n",
151*11be35a1SLionel Sambuc 			    rnd[j].rf, r);
152*11be35a1SLionel Sambuc 		test(r);
153*11be35a1SLionel Sambuc 	}
154*11be35a1SLionel Sambuc #endif /* _FLOAT_IEEE754 */
155*11be35a1SLionel Sambuc }
156*11be35a1SLionel Sambuc 
ATF_TP_ADD_TCS(tp)157*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
158*11be35a1SLionel Sambuc {
159*11be35a1SLionel Sambuc 
160*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, fpsetround_basic);
161*11be35a1SLionel Sambuc 
162*11be35a1SLionel Sambuc 	return atf_no_error();
163*11be35a1SLionel Sambuc }
164