xref: /minix3/tests/lib/libm/t_fenv.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /* $NetBSD: t_fenv.c,v 1.2 2014/12/29 19:51:53 martin Exp $ */
2*0a6a1f1dSLionel Sambuc 
3*0a6a1f1dSLionel Sambuc /*-
4*0a6a1f1dSLionel Sambuc  * Copyright (c) 2014 The NetBSD Foundation, Inc.
5*0a6a1f1dSLionel Sambuc  * All rights reserved.
6*0a6a1f1dSLionel Sambuc  *
7*0a6a1f1dSLionel Sambuc  * This code is derived from software contributed to The NetBSD Foundation
8*0a6a1f1dSLionel Sambuc  * by Martin Husemann.
9*0a6a1f1dSLionel Sambuc  *
10*0a6a1f1dSLionel Sambuc  * Redistribution and use in source and binary forms, with or without
11*0a6a1f1dSLionel Sambuc  * modification, are permitted provided that the following conditions
12*0a6a1f1dSLionel Sambuc  * are met:
13*0a6a1f1dSLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
14*0a6a1f1dSLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
15*0a6a1f1dSLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16*0a6a1f1dSLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17*0a6a1f1dSLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18*0a6a1f1dSLionel Sambuc  *
19*0a6a1f1dSLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*0a6a1f1dSLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*0a6a1f1dSLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*0a6a1f1dSLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*0a6a1f1dSLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*0a6a1f1dSLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*0a6a1f1dSLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*0a6a1f1dSLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*0a6a1f1dSLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*0a6a1f1dSLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*0a6a1f1dSLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
30*0a6a1f1dSLionel Sambuc  */
31*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
32*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: t_fenv.c,v 1.2 2014/12/29 19:51:53 martin Exp $");
33*0a6a1f1dSLionel Sambuc 
34*0a6a1f1dSLionel Sambuc #include <atf-c.h>
35*0a6a1f1dSLionel Sambuc 
36*0a6a1f1dSLionel Sambuc #ifdef HAVE_FENV_H
37*0a6a1f1dSLionel Sambuc 
38*0a6a1f1dSLionel Sambuc #include <ieeefp.h>
39*0a6a1f1dSLionel Sambuc #include <stdlib.h>
40*0a6a1f1dSLionel Sambuc #include <fenv.h>
41*0a6a1f1dSLionel Sambuc 
42*0a6a1f1dSLionel Sambuc 
43*0a6a1f1dSLionel Sambuc #if __arm__ && !__SOFTFP__
44*0a6a1f1dSLionel Sambuc 	/*
45*0a6a1f1dSLionel Sambuc 	 * Some NEON fpus do not implement IEEE exception handling,
46*0a6a1f1dSLionel Sambuc 	 * skip these tests if running on them and compiled for
47*0a6a1f1dSLionel Sambuc 	 * hard float.
48*0a6a1f1dSLionel Sambuc 	 */
49*0a6a1f1dSLionel Sambuc #define	FPU_EXC_PREREQ()						\
50*0a6a1f1dSLionel Sambuc 	if (0 == fpsetmask(fpsetmask(FP_X_INV)))			\
51*0a6a1f1dSLionel Sambuc 		atf_tc_skip("FPU does not implement exception handling");
52*0a6a1f1dSLionel Sambuc 
53*0a6a1f1dSLionel Sambuc 	/*
54*0a6a1f1dSLionel Sambuc 	 * Same as above: some don't allow configuring the rounding mode.
55*0a6a1f1dSLionel Sambuc 	 */
56*0a6a1f1dSLionel Sambuc #define	FPU_RND_PREREQ()						\
57*0a6a1f1dSLionel Sambuc 	if (0 == fpsetround(fpsetround(FP_RZ)))				\
58*0a6a1f1dSLionel Sambuc 		atf_tc_skip("FPU does not implement configurable "	\
59*0a6a1f1dSLionel Sambuc 		    "rounding modes");
60*0a6a1f1dSLionel Sambuc #endif
61*0a6a1f1dSLionel Sambuc 
62*0a6a1f1dSLionel Sambuc #ifndef FPU_EXC_PREREQ
63*0a6a1f1dSLionel Sambuc #define	FPU_EXC_PREREQ()	/* nothing */
64*0a6a1f1dSLionel Sambuc #endif
65*0a6a1f1dSLionel Sambuc #ifndef FPU_RND_PREREQ
66*0a6a1f1dSLionel Sambuc #define	FPU_RND_PREREQ()	/* nothing */
67*0a6a1f1dSLionel Sambuc #endif
68*0a6a1f1dSLionel Sambuc 
69*0a6a1f1dSLionel Sambuc 
70*0a6a1f1dSLionel Sambuc ATF_TC(fegetround);
71*0a6a1f1dSLionel Sambuc 
ATF_TC_HEAD(fegetround,tc)72*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(fegetround, tc)
73*0a6a1f1dSLionel Sambuc {
74*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
75*0a6a1f1dSLionel Sambuc 	    "verify the fegetround() function agrees with the legacy "
76*0a6a1f1dSLionel Sambuc 	    "fpsetround");
77*0a6a1f1dSLionel Sambuc }
78*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(fegetround,tc)79*0a6a1f1dSLionel Sambuc ATF_TC_BODY(fegetround, tc)
80*0a6a1f1dSLionel Sambuc {
81*0a6a1f1dSLionel Sambuc 	FPU_RND_PREREQ();
82*0a6a1f1dSLionel Sambuc 
83*0a6a1f1dSLionel Sambuc 	fpsetround(FP_RZ);
84*0a6a1f1dSLionel Sambuc 	ATF_CHECK(fegetround() == FE_TOWARDZERO);
85*0a6a1f1dSLionel Sambuc 	fpsetround(FP_RM);
86*0a6a1f1dSLionel Sambuc 	ATF_CHECK(fegetround() == FE_DOWNWARD);
87*0a6a1f1dSLionel Sambuc 	fpsetround(FP_RN);
88*0a6a1f1dSLionel Sambuc 	ATF_CHECK(fegetround() == FE_TONEAREST);
89*0a6a1f1dSLionel Sambuc 	fpsetround(FP_RP);
90*0a6a1f1dSLionel Sambuc 	ATF_CHECK(fegetround() == FE_UPWARD);
91*0a6a1f1dSLionel Sambuc }
92*0a6a1f1dSLionel Sambuc 
93*0a6a1f1dSLionel Sambuc ATF_TC(fesetround);
94*0a6a1f1dSLionel Sambuc 
ATF_TC_HEAD(fesetround,tc)95*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(fesetround, tc)
96*0a6a1f1dSLionel Sambuc {
97*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
98*0a6a1f1dSLionel Sambuc 	    "verify the fesetround() function agrees with the legacy "
99*0a6a1f1dSLionel Sambuc 	    "fpgetround");
100*0a6a1f1dSLionel Sambuc }
101*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(fesetround,tc)102*0a6a1f1dSLionel Sambuc ATF_TC_BODY(fesetround, tc)
103*0a6a1f1dSLionel Sambuc {
104*0a6a1f1dSLionel Sambuc 	FPU_RND_PREREQ();
105*0a6a1f1dSLionel Sambuc 
106*0a6a1f1dSLionel Sambuc 	fesetround(FE_TOWARDZERO);
107*0a6a1f1dSLionel Sambuc 	ATF_CHECK(fpgetround() == FP_RZ);
108*0a6a1f1dSLionel Sambuc 	fesetround(FE_DOWNWARD);
109*0a6a1f1dSLionel Sambuc 	ATF_CHECK(fpgetround() == FP_RM);
110*0a6a1f1dSLionel Sambuc 	fesetround(FE_TONEAREST);
111*0a6a1f1dSLionel Sambuc 	ATF_CHECK(fpgetround() == FP_RN);
112*0a6a1f1dSLionel Sambuc 	fesetround(FE_UPWARD);
113*0a6a1f1dSLionel Sambuc 	ATF_CHECK(fpgetround() == FP_RP);
114*0a6a1f1dSLionel Sambuc }
115*0a6a1f1dSLionel Sambuc 
116*0a6a1f1dSLionel Sambuc ATF_TC(fegetexcept);
117*0a6a1f1dSLionel Sambuc 
ATF_TC_HEAD(fegetexcept,tc)118*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(fegetexcept, tc)
119*0a6a1f1dSLionel Sambuc {
120*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
121*0a6a1f1dSLionel Sambuc 	    "verify the fegetexcept() function agrees with the legacy "
122*0a6a1f1dSLionel Sambuc 	    "fpsetmask()");
123*0a6a1f1dSLionel Sambuc }
124*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(fegetexcept,tc)125*0a6a1f1dSLionel Sambuc ATF_TC_BODY(fegetexcept, tc)
126*0a6a1f1dSLionel Sambuc {
127*0a6a1f1dSLionel Sambuc 	FPU_EXC_PREREQ();
128*0a6a1f1dSLionel Sambuc 
129*0a6a1f1dSLionel Sambuc 	fpsetmask(0);
130*0a6a1f1dSLionel Sambuc 	ATF_CHECK(fegetexcept() == 0);
131*0a6a1f1dSLionel Sambuc 
132*0a6a1f1dSLionel Sambuc 	fpsetmask(FP_X_INV|FP_X_DZ|FP_X_OFL|FP_X_UFL|FP_X_IMP);
133*0a6a1f1dSLionel Sambuc 	ATF_CHECK(fegetexcept() == (FE_INVALID|FE_DIVBYZERO|FE_OVERFLOW
134*0a6a1f1dSLionel Sambuc 	    |FE_UNDERFLOW|FE_INEXACT));
135*0a6a1f1dSLionel Sambuc 
136*0a6a1f1dSLionel Sambuc 	fpsetmask(FP_X_INV);
137*0a6a1f1dSLionel Sambuc 	ATF_CHECK(fegetexcept() == FE_INVALID);
138*0a6a1f1dSLionel Sambuc 
139*0a6a1f1dSLionel Sambuc 	fpsetmask(FP_X_DZ);
140*0a6a1f1dSLionel Sambuc 	ATF_CHECK(fegetexcept() == FE_DIVBYZERO);
141*0a6a1f1dSLionel Sambuc 
142*0a6a1f1dSLionel Sambuc 	fpsetmask(FP_X_OFL);
143*0a6a1f1dSLionel Sambuc 	ATF_CHECK(fegetexcept() == FE_OVERFLOW);
144*0a6a1f1dSLionel Sambuc 
145*0a6a1f1dSLionel Sambuc 	fpsetmask(FP_X_UFL);
146*0a6a1f1dSLionel Sambuc 	ATF_CHECK(fegetexcept() == FE_UNDERFLOW);
147*0a6a1f1dSLionel Sambuc 
148*0a6a1f1dSLionel Sambuc 	fpsetmask(FP_X_IMP);
149*0a6a1f1dSLionel Sambuc 	ATF_CHECK(fegetexcept() == FE_INEXACT);
150*0a6a1f1dSLionel Sambuc }
151*0a6a1f1dSLionel Sambuc 
152*0a6a1f1dSLionel Sambuc ATF_TC(feenableexcept);
153*0a6a1f1dSLionel Sambuc 
ATF_TC_HEAD(feenableexcept,tc)154*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(feenableexcept, tc)
155*0a6a1f1dSLionel Sambuc {
156*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
157*0a6a1f1dSLionel Sambuc 	    "verify the feenableexcept() function agrees with the legacy "
158*0a6a1f1dSLionel Sambuc 	    "fpgetmask()");
159*0a6a1f1dSLionel Sambuc }
160*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(feenableexcept,tc)161*0a6a1f1dSLionel Sambuc ATF_TC_BODY(feenableexcept, tc)
162*0a6a1f1dSLionel Sambuc {
163*0a6a1f1dSLionel Sambuc 	FPU_EXC_PREREQ();
164*0a6a1f1dSLionel Sambuc 
165*0a6a1f1dSLionel Sambuc 	fedisableexcept(FE_ALL_EXCEPT);
166*0a6a1f1dSLionel Sambuc 	ATF_CHECK(fpgetmask() == 0);
167*0a6a1f1dSLionel Sambuc 
168*0a6a1f1dSLionel Sambuc 	feenableexcept(FE_UNDERFLOW);
169*0a6a1f1dSLionel Sambuc 	ATF_CHECK(fpgetmask() == FP_X_UFL);
170*0a6a1f1dSLionel Sambuc 
171*0a6a1f1dSLionel Sambuc 	fedisableexcept(FE_ALL_EXCEPT);
172*0a6a1f1dSLionel Sambuc 	feenableexcept(FE_OVERFLOW);
173*0a6a1f1dSLionel Sambuc 	ATF_CHECK(fpgetmask() == FP_X_OFL);
174*0a6a1f1dSLionel Sambuc 
175*0a6a1f1dSLionel Sambuc 	fedisableexcept(FE_ALL_EXCEPT);
176*0a6a1f1dSLionel Sambuc 	feenableexcept(FE_DIVBYZERO);
177*0a6a1f1dSLionel Sambuc 	ATF_CHECK(fpgetmask() == FP_X_DZ);
178*0a6a1f1dSLionel Sambuc 
179*0a6a1f1dSLionel Sambuc 	fedisableexcept(FE_ALL_EXCEPT);
180*0a6a1f1dSLionel Sambuc 	feenableexcept(FE_INEXACT);
181*0a6a1f1dSLionel Sambuc 	ATF_CHECK(fpgetmask() == FP_X_IMP);
182*0a6a1f1dSLionel Sambuc 
183*0a6a1f1dSLionel Sambuc 	fedisableexcept(FE_ALL_EXCEPT);
184*0a6a1f1dSLionel Sambuc 	feenableexcept(FE_INVALID);
185*0a6a1f1dSLionel Sambuc 	ATF_CHECK(fpgetmask() == FP_X_INV);
186*0a6a1f1dSLionel Sambuc }
187*0a6a1f1dSLionel Sambuc 
ATF_TP_ADD_TCS(tp)188*0a6a1f1dSLionel Sambuc ATF_TP_ADD_TCS(tp)
189*0a6a1f1dSLionel Sambuc {
190*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, fegetround);
191*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, fesetround);
192*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, fegetexcept);
193*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, feenableexcept);
194*0a6a1f1dSLionel Sambuc 
195*0a6a1f1dSLionel Sambuc 	return atf_no_error();
196*0a6a1f1dSLionel Sambuc }
197*0a6a1f1dSLionel Sambuc 
198*0a6a1f1dSLionel Sambuc #else	/* no fenv.h support */
199*0a6a1f1dSLionel Sambuc 
200*0a6a1f1dSLionel Sambuc ATF_TC(t_nofenv);
201*0a6a1f1dSLionel Sambuc 
ATF_TC_HEAD(t_nofenv,tc)202*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(t_nofenv, tc)
203*0a6a1f1dSLionel Sambuc {
204*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
205*0a6a1f1dSLionel Sambuc 	    "dummy test case - no fenv.h support");
206*0a6a1f1dSLionel Sambuc }
207*0a6a1f1dSLionel Sambuc 
208*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(t_nofenv,tc)209*0a6a1f1dSLionel Sambuc ATF_TC_BODY(t_nofenv, tc)
210*0a6a1f1dSLionel Sambuc {
211*0a6a1f1dSLionel Sambuc 	atf_tc_skip("no fenv.h support on this architecture");
212*0a6a1f1dSLionel Sambuc }
213*0a6a1f1dSLionel Sambuc 
ATF_TP_ADD_TCS(tp)214*0a6a1f1dSLionel Sambuc ATF_TP_ADD_TCS(tp)
215*0a6a1f1dSLionel Sambuc {
216*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, t_nofenv);
217*0a6a1f1dSLionel Sambuc 	return atf_no_error();
218*0a6a1f1dSLionel Sambuc }
219*0a6a1f1dSLionel Sambuc 
220*0a6a1f1dSLionel Sambuc #endif
221