t_fenv.c revision 1.12 1 /* $NetBSD: t_fenv.c,v 1.12 2023/11/06 13:48:00 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 2014 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Martin Husemann.
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_fenv.c,v 1.12 2023/11/06 13:48:00 riastradh Exp $");
33
34 #include <atf-c.h>
35
36 #include <fenv.h>
37 #ifdef __HAVE_FENV
38
39 /* XXXGCC gcc lacks #pragma STDC FENV_ACCESS */
40 /* XXXclang clang lacks #pragma STDC FENV_ACCESS on some ports */
41 #if !defined(__GNUC__)
42 #pragma STDC FENV_ACCESS ON
43 #endif
44
45 #include <float.h>
46 #include <ieeefp.h>
47 #include <stdlib.h>
48
49 #if FLT_RADIX != 2
50 #error This test assumes binary floating-point arithmetic.
51 #endif
52
53 #if (__arm__ && !__SOFTFP__) || __aarch64__
54 /*
55 * Some NEON fpus do not trap on IEEE 754 FP exceptions.
56 * Skip these tests if running on them and compiled for
57 * hard float.
58 */
59 #define FPU_EXC_PREREQ() \
60 if (0 == fpsetmask(fpsetmask(FP_X_INV))) \
61 atf_tc_skip("FPU does not implement traps on FP exceptions");
62
63 /*
64 * Same as above: some don't allow configuring the rounding mode.
65 */
66 #define FPU_RND_PREREQ() \
67 if (0 == fpsetround(fpsetround(FP_RZ))) \
68 atf_tc_skip("FPU does not implement configurable " \
69 "rounding modes");
70 #endif
71
72 #ifndef FPU_EXC_PREREQ
73 #define FPU_EXC_PREREQ() /* nothing */
74 #endif
75 #ifndef FPU_RND_PREREQ
76 #define FPU_RND_PREREQ() /* nothing */
77 #endif
78
79
80 static int
81 feround_to_fltrounds(int feround)
82 {
83
84 /*
85 * C99, Sec. 5.2.4.2.2 Characteristics of floating types
86 * <float.h>, p. 24, clause 7
87 */
88 switch (feround) {
89 case FE_TOWARDZERO:
90 return 0;
91 case FE_TONEAREST:
92 return 1;
93 case FE_UPWARD:
94 return 2;
95 case FE_DOWNWARD:
96 return 3;
97 default:
98 return -1;
99 }
100 }
101
102 static void
103 checkfltrounds(void)
104 {
105 int feround = fegetround();
106 int expected = feround_to_fltrounds(feround);
107
108 ATF_CHECK_EQ_MSG(FLT_ROUNDS, expected,
109 "FLT_ROUNDS=%d expected=%d fegetround()=%d",
110 FLT_ROUNDS, expected, feround);
111 }
112
113 static void
114 checkrounding(int feround, const char *name)
115 {
116 volatile double ulp1 = DBL_EPSILON;
117
118 /*
119 * XXX These must be volatile to force rounding to double when
120 * the intermediate quantities are evaluated in long double
121 * precision, e.g. on 32-bit x86 with x87 long double. Under
122 * the C standard (C99, C11, C17, &c.), cast and assignment
123 * operators are required to remove all extra range and
124 * precision, i.e., round double to long double. But we build
125 * this code with -std=gnu99, which diverges from this
126 * requirement -- unless you add a volatile qualifier.
127 */
128 volatile double y1 = -1 + ulp1/4;
129 volatile double y2 = 1 + 3*(ulp1/2);
130
131 switch (feround) {
132 case FE_TONEAREST: {
133 double z1 = -1;
134 double z2 = 1 + 2*ulp1;
135 ATF_CHECK_EQ_MSG(y1, z1, "%s[-1 + ulp(1)/4]"
136 " expected=%a actual=%a",
137 name, y1, z1);
138 ATF_CHECK_EQ_MSG(y2, z2, "%s[1 + 3*(ulp(1)/2)]"
139 " expected=%a actual=%a",
140 name, y2, z2);
141 break;
142 }
143 case FE_TOWARDZERO: {
144 double z1 = -1 + ulp1/2;
145 double z2 = 1 + ulp1;
146 ATF_CHECK_EQ_MSG(y1, z1, "%s[-1 + ulp(1)/4]"
147 " expected=%a actual=%a",
148 name, y1, z1);
149 ATF_CHECK_EQ_MSG(y2, z2, "%s[1 + 3*(ulp(1)/2)]"
150 " expected=%a actual=%a",
151 name, y2, z2);
152 break;
153 }
154 case FE_UPWARD: {
155 double z1 = -1 + ulp1/2;
156 double z2 = 1 + 2*ulp1;
157 ATF_CHECK_EQ_MSG(y1, z1, "%s[-1 + ulp(1)/4]"
158 " expected=%a actual=%a",
159 name, y1, z1);
160 ATF_CHECK_EQ_MSG(y2, z2, "%s[1 + 3*(ulp(1)/2)]"
161 " expected=%a actual=%a",
162 name, y2, z2);
163 break;
164 }
165 case FE_DOWNWARD: {
166 double z1 = -1;
167 double z2 = 1 + ulp1;
168 ATF_CHECK_EQ_MSG(y1, z1, "%s[-1 + ulp(1)/4]"
169 " expected=%a actual=%a",
170 name, y1, z1);
171 ATF_CHECK_EQ_MSG(y2, z2, "%s[1 + 3*(ulp(1)/2)]"
172 " expected=%a actual=%a",
173 name, y2, z2);
174 break;
175 }
176 }
177 }
178
179 ATF_TC(fegetround);
180
181 ATF_TC_HEAD(fegetround, tc)
182 {
183 atf_tc_set_md_var(tc, "descr",
184 "verify the fegetround() function agrees with the legacy "
185 "fpsetround");
186 }
187
188 ATF_TC_BODY(fegetround, tc)
189 {
190 FPU_RND_PREREQ();
191
192 checkrounding(FE_TONEAREST, "FE_TONEAREST");
193
194 fpsetround(FP_RZ);
195 ATF_CHECK_EQ_MSG(fegetround(), FE_TOWARDZERO,
196 "fegetround()=%d FE_TOWARDZERO=%d",
197 fegetround(), FE_TOWARDZERO);
198 checkfltrounds();
199 checkrounding(FE_TOWARDZERO, "FE_TOWARDZERO");
200
201 fpsetround(FP_RM);
202 ATF_CHECK_EQ_MSG(fegetround(), FE_DOWNWARD,
203 "fegetround()=%d FE_DOWNWARD=%d",
204 fegetround(), FE_DOWNWARD);
205 checkfltrounds();
206 checkrounding(FE_DOWNWARD, "FE_DOWNWARD");
207
208 fpsetround(FP_RN);
209 ATF_CHECK_EQ_MSG(fegetround(), FE_TONEAREST,
210 "fegetround()=%d FE_TONEAREST=%d",
211 fegetround(), FE_TONEAREST);
212 checkfltrounds();
213 checkrounding(FE_TONEAREST, "FE_TONEAREST");
214
215 fpsetround(FP_RP);
216 ATF_CHECK_EQ_MSG(fegetround(), FE_UPWARD,
217 "fegetround()=%d FE_UPWARD=%d",
218 fegetround(), FE_UPWARD);
219 checkfltrounds();
220 checkrounding(FE_UPWARD, "FE_UPWARD");
221 }
222
223 ATF_TC(fesetround);
224
225 ATF_TC_HEAD(fesetround, tc)
226 {
227 atf_tc_set_md_var(tc, "descr",
228 "verify the fesetround() function agrees with the legacy "
229 "fpgetround");
230 }
231
232 ATF_TC_BODY(fesetround, tc)
233 {
234 FPU_RND_PREREQ();
235
236 checkrounding(FE_TONEAREST, "FE_TONEAREST");
237
238 fesetround(FE_TOWARDZERO);
239 ATF_CHECK_EQ_MSG(fpgetround(), FP_RZ,
240 "fpgetround()=%d FP_RZ=%d",
241 (int)fpgetround(), (int)FP_RZ);
242 checkfltrounds();
243 checkrounding(FE_TOWARDZERO, "FE_TOWARDZERO");
244
245 fesetround(FE_DOWNWARD);
246 ATF_CHECK_EQ_MSG(fpgetround(), FP_RM,
247 "fpgetround()=%d FP_RM=%d",
248 (int)fpgetround(), (int)FP_RM);
249 checkfltrounds();
250 checkrounding(FE_DOWNWARD, "FE_DOWNWARD");
251
252 fesetround(FE_TONEAREST);
253 ATF_CHECK_EQ_MSG(fpgetround(), FP_RN,
254 "fpgetround()=%d FP_RN=%d",
255 (int)fpgetround(), (int)FP_RN);
256 checkfltrounds();
257 checkrounding(FE_TONEAREST, "FE_TONEAREST");
258
259 fesetround(FE_UPWARD);
260 ATF_CHECK_EQ_MSG(fpgetround(), FP_RP,
261 "fpgetround()=%d FP_RP=%d",
262 (int)fpgetround(), (int)FP_RP);
263 checkfltrounds();
264 checkrounding(FE_UPWARD, "FE_UPWARD");
265 }
266
267 ATF_TC(fegetexcept);
268
269 ATF_TC_HEAD(fegetexcept, tc)
270 {
271 atf_tc_set_md_var(tc, "descr",
272 "verify the fegetexcept() function agrees with the legacy "
273 "fpsetmask()");
274 }
275
276 ATF_TC_BODY(fegetexcept, tc)
277 {
278 FPU_EXC_PREREQ();
279
280 fpsetmask(0);
281 ATF_CHECK_EQ_MSG(fegetexcept(), 0,
282 "fegetexcept()=%d",
283 fegetexcept());
284
285 fpsetmask(FP_X_INV|FP_X_DZ|FP_X_OFL|FP_X_UFL|FP_X_IMP);
286 ATF_CHECK(fegetexcept() == (FE_INVALID|FE_DIVBYZERO|FE_OVERFLOW
287 |FE_UNDERFLOW|FE_INEXACT));
288
289 fpsetmask(FP_X_INV);
290 ATF_CHECK_EQ_MSG(fegetexcept(), FE_INVALID,
291 "fegetexcept()=%d FE_INVALID=%d",
292 fegetexcept(), FE_INVALID);
293
294 fpsetmask(FP_X_DZ);
295 ATF_CHECK_EQ_MSG(fegetexcept(), FE_DIVBYZERO,
296 "fegetexcept()=%d FE_DIVBYZERO=%d",
297 fegetexcept(), FE_DIVBYZERO);
298
299 fpsetmask(FP_X_OFL);
300 ATF_CHECK_EQ_MSG(fegetexcept(), FE_OVERFLOW,
301 "fegetexcept()=%d FE_OVERFLOW=%d",
302 fegetexcept(), FE_OVERFLOW);
303
304 fpsetmask(FP_X_UFL);
305 ATF_CHECK_EQ_MSG(fegetexcept(), FE_UNDERFLOW,
306 "fegetexcept()=%d FE_UNDERFLOW=%d",
307 fegetexcept(), FE_UNDERFLOW);
308
309 fpsetmask(FP_X_IMP);
310 ATF_CHECK_EQ_MSG(fegetexcept(), FE_INEXACT,
311 "fegetexcept()=%d FE_INEXACT=%d",
312 fegetexcept(), FE_INEXACT);
313 }
314
315 ATF_TC(feenableexcept);
316
317 ATF_TC_HEAD(feenableexcept, tc)
318 {
319 atf_tc_set_md_var(tc, "descr",
320 "verify the feenableexcept() function agrees with the legacy "
321 "fpgetmask()");
322 }
323
324 ATF_TC_BODY(feenableexcept, tc)
325 {
326 FPU_EXC_PREREQ();
327
328 fedisableexcept(FE_ALL_EXCEPT);
329 ATF_CHECK_EQ_MSG(fpgetmask(), 0,
330 "fpgetmask()=%d",
331 (int)fpgetmask());
332
333 feenableexcept(FE_UNDERFLOW);
334 ATF_CHECK_EQ_MSG(fpgetmask(), FP_X_UFL,
335 "fpgetmask()=%d FP_X_UFL=%d",
336 (int)fpgetmask(), (int)FP_X_UFL);
337
338 fedisableexcept(FE_ALL_EXCEPT);
339 feenableexcept(FE_OVERFLOW);
340 ATF_CHECK_EQ_MSG(fpgetmask(), FP_X_OFL,
341 "fpgetmask()=%d FP_X_OFL=%d",
342 (int)fpgetmask(), (int)FP_X_OFL);
343
344 fedisableexcept(FE_ALL_EXCEPT);
345 feenableexcept(FE_DIVBYZERO);
346 ATF_CHECK_EQ_MSG(fpgetmask(), FP_X_DZ,
347 "fpgetmask()=%d FP_X_DZ=%d",
348 (int)fpgetmask(), (int)FP_X_DZ);
349
350 fedisableexcept(FE_ALL_EXCEPT);
351 feenableexcept(FE_INEXACT);
352 ATF_CHECK_EQ_MSG(fpgetmask(), FP_X_IMP,
353 "fpgetmask()=%d FP_X_IMP=%d",
354 (int)fpgetmask(), (int)FP_X_IMP);
355
356 fedisableexcept(FE_ALL_EXCEPT);
357 feenableexcept(FE_INVALID);
358 ATF_CHECK_EQ_MSG(fpgetmask(), FP_X_INV,
359 "fpgetmask()=%d FP_X_INV=%d",
360 (int)fpgetmask(), (int)FP_X_INV);
361 }
362
363 ATF_TP_ADD_TCS(tp)
364 {
365 ATF_TP_ADD_TC(tp, fegetround);
366 ATF_TP_ADD_TC(tp, fesetround);
367 ATF_TP_ADD_TC(tp, fegetexcept);
368 ATF_TP_ADD_TC(tp, feenableexcept);
369
370 return atf_no_error();
371 }
372
373 #else /* no fenv.h support */
374
375 ATF_TC(t_nofenv);
376
377 ATF_TC_HEAD(t_nofenv, tc)
378 {
379 atf_tc_set_md_var(tc, "descr",
380 "dummy test case - no fenv.h support");
381 }
382
383
384 ATF_TC_BODY(t_nofenv, tc)
385 {
386 atf_tc_skip("no fenv.h support on this architecture");
387 }
388
389 ATF_TP_ADD_TCS(tp)
390 {
391 ATF_TP_ADD_TC(tp, t_nofenv);
392 return atf_no_error();
393 }
394
395 #endif
396