t_fenv.c revision 1.14 1 /* $NetBSD: t_fenv.c,v 1.14 2024/02/19 23:19:10 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.14 2024/02/19 23:19:10 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 double z1, z2;
132
133 switch (feround) {
134 case FE_TONEAREST:
135 z1 = -1;
136 z2 = 1 + 2*ulp1;
137 break;
138 case FE_TOWARDZERO:
139 z1 = -1 + ulp1/2;
140 z2 = 1 + ulp1;
141 break;
142 case FE_UPWARD:
143 z1 = -1 + ulp1/2;
144 z2 = 1 + 2*ulp1;
145 break;
146 case FE_DOWNWARD:
147 z1 = -1;
148 z2 = 1 + ulp1;
149 break;
150 default:
151 atf_tc_fail("unknown rounding mode %d (%s)", feround, name);
152 }
153
154 ATF_CHECK_EQ_MSG(y1, z1, "%s[-1 + ulp(1)/4] expected=%a actual=%a",
155 name, y1, z1);
156 ATF_CHECK_EQ_MSG(y2, z2, "%s[1 + 3*(ulp(1)/2)] expected=%a actual=%a",
157 name, y2, z2);
158 }
159
160 ATF_TC(fegetround);
161
162 ATF_TC_HEAD(fegetround, tc)
163 {
164 atf_tc_set_md_var(tc, "descr",
165 "verify the fegetround() function agrees with the legacy "
166 "fpsetround");
167 }
168
169 ATF_TC_BODY(fegetround, tc)
170 {
171 FPU_RND_PREREQ();
172
173 checkrounding(FE_TONEAREST, "FE_TONEAREST");
174
175 fpsetround(FP_RZ);
176 ATF_CHECK_EQ_MSG(fegetround(), FE_TOWARDZERO,
177 "fegetround()=%d FE_TOWARDZERO=%d",
178 fegetround(), FE_TOWARDZERO);
179 checkfltrounds();
180 checkrounding(FE_TOWARDZERO, "FE_TOWARDZERO");
181
182 fpsetround(FP_RM);
183 ATF_CHECK_EQ_MSG(fegetround(), FE_DOWNWARD,
184 "fegetround()=%d FE_DOWNWARD=%d",
185 fegetround(), FE_DOWNWARD);
186 checkfltrounds();
187 checkrounding(FE_DOWNWARD, "FE_DOWNWARD");
188
189 fpsetround(FP_RN);
190 ATF_CHECK_EQ_MSG(fegetround(), FE_TONEAREST,
191 "fegetround()=%d FE_TONEAREST=%d",
192 fegetround(), FE_TONEAREST);
193 checkfltrounds();
194 checkrounding(FE_TONEAREST, "FE_TONEAREST");
195
196 fpsetround(FP_RP);
197 ATF_CHECK_EQ_MSG(fegetround(), FE_UPWARD,
198 "fegetround()=%d FE_UPWARD=%d",
199 fegetround(), FE_UPWARD);
200 checkfltrounds();
201 checkrounding(FE_UPWARD, "FE_UPWARD");
202 }
203
204 ATF_TC(fesetround);
205
206 ATF_TC_HEAD(fesetround, tc)
207 {
208 atf_tc_set_md_var(tc, "descr",
209 "verify the fesetround() function agrees with the legacy "
210 "fpgetround");
211 }
212
213 ATF_TC_BODY(fesetround, tc)
214 {
215 FPU_RND_PREREQ();
216
217 checkrounding(FE_TONEAREST, "FE_TONEAREST");
218
219 fesetround(FE_TOWARDZERO);
220 ATF_CHECK_EQ_MSG(fpgetround(), FP_RZ,
221 "fpgetround()=%d FP_RZ=%d",
222 (int)fpgetround(), (int)FP_RZ);
223 checkfltrounds();
224 checkrounding(FE_TOWARDZERO, "FE_TOWARDZERO");
225
226 fesetround(FE_DOWNWARD);
227 ATF_CHECK_EQ_MSG(fpgetround(), FP_RM,
228 "fpgetround()=%d FP_RM=%d",
229 (int)fpgetround(), (int)FP_RM);
230 checkfltrounds();
231 checkrounding(FE_DOWNWARD, "FE_DOWNWARD");
232
233 fesetround(FE_TONEAREST);
234 ATF_CHECK_EQ_MSG(fpgetround(), FP_RN,
235 "fpgetround()=%d FP_RN=%d",
236 (int)fpgetround(), (int)FP_RN);
237 checkfltrounds();
238 checkrounding(FE_TONEAREST, "FE_TONEAREST");
239
240 fesetround(FE_UPWARD);
241 ATF_CHECK_EQ_MSG(fpgetround(), FP_RP,
242 "fpgetround()=%d FP_RP=%d",
243 (int)fpgetround(), (int)FP_RP);
244 checkfltrounds();
245 checkrounding(FE_UPWARD, "FE_UPWARD");
246 }
247
248 ATF_TC(fegetexcept);
249
250 ATF_TC_HEAD(fegetexcept, tc)
251 {
252 atf_tc_set_md_var(tc, "descr",
253 "verify the fegetexcept() function agrees with the legacy "
254 "fpsetmask()");
255 }
256
257 ATF_TC_BODY(fegetexcept, tc)
258 {
259 FPU_EXC_PREREQ();
260
261 fpsetmask(0);
262 ATF_CHECK_EQ_MSG(fegetexcept(), 0,
263 "fegetexcept()=%d",
264 fegetexcept());
265
266 fpsetmask(FP_X_INV|FP_X_DZ|FP_X_OFL|FP_X_UFL|FP_X_IMP);
267 ATF_CHECK(fegetexcept() == (FE_INVALID|FE_DIVBYZERO|FE_OVERFLOW
268 |FE_UNDERFLOW|FE_INEXACT));
269
270 fpsetmask(FP_X_INV);
271 ATF_CHECK_EQ_MSG(fegetexcept(), FE_INVALID,
272 "fegetexcept()=%d FE_INVALID=%d",
273 fegetexcept(), FE_INVALID);
274
275 fpsetmask(FP_X_DZ);
276 ATF_CHECK_EQ_MSG(fegetexcept(), FE_DIVBYZERO,
277 "fegetexcept()=%d FE_DIVBYZERO=%d",
278 fegetexcept(), FE_DIVBYZERO);
279
280 fpsetmask(FP_X_OFL);
281 ATF_CHECK_EQ_MSG(fegetexcept(), FE_OVERFLOW,
282 "fegetexcept()=%d FE_OVERFLOW=%d",
283 fegetexcept(), FE_OVERFLOW);
284
285 fpsetmask(FP_X_UFL);
286 ATF_CHECK_EQ_MSG(fegetexcept(), FE_UNDERFLOW,
287 "fegetexcept()=%d FE_UNDERFLOW=%d",
288 fegetexcept(), FE_UNDERFLOW);
289
290 fpsetmask(FP_X_IMP);
291 ATF_CHECK_EQ_MSG(fegetexcept(), FE_INEXACT,
292 "fegetexcept()=%d FE_INEXACT=%d",
293 fegetexcept(), FE_INEXACT);
294 }
295
296 ATF_TC(feenableexcept);
297
298 ATF_TC_HEAD(feenableexcept, tc)
299 {
300 atf_tc_set_md_var(tc, "descr",
301 "verify the feenableexcept() function agrees with the legacy "
302 "fpgetmask()");
303 }
304
305 ATF_TC_BODY(feenableexcept, tc)
306 {
307 FPU_EXC_PREREQ();
308
309 fedisableexcept(FE_ALL_EXCEPT);
310 ATF_CHECK_EQ_MSG(fpgetmask(), 0,
311 "fpgetmask()=%d",
312 (int)fpgetmask());
313
314 feenableexcept(FE_UNDERFLOW);
315 ATF_CHECK_EQ_MSG(fpgetmask(), FP_X_UFL,
316 "fpgetmask()=%d FP_X_UFL=%d",
317 (int)fpgetmask(), (int)FP_X_UFL);
318
319 fedisableexcept(FE_ALL_EXCEPT);
320 feenableexcept(FE_OVERFLOW);
321 ATF_CHECK_EQ_MSG(fpgetmask(), FP_X_OFL,
322 "fpgetmask()=%d FP_X_OFL=%d",
323 (int)fpgetmask(), (int)FP_X_OFL);
324
325 fedisableexcept(FE_ALL_EXCEPT);
326 feenableexcept(FE_DIVBYZERO);
327 ATF_CHECK_EQ_MSG(fpgetmask(), FP_X_DZ,
328 "fpgetmask()=%d FP_X_DZ=%d",
329 (int)fpgetmask(), (int)FP_X_DZ);
330
331 fedisableexcept(FE_ALL_EXCEPT);
332 feenableexcept(FE_INEXACT);
333 ATF_CHECK_EQ_MSG(fpgetmask(), FP_X_IMP,
334 "fpgetmask()=%d FP_X_IMP=%d",
335 (int)fpgetmask(), (int)FP_X_IMP);
336
337 fedisableexcept(FE_ALL_EXCEPT);
338 feenableexcept(FE_INVALID);
339 ATF_CHECK_EQ_MSG(fpgetmask(), FP_X_INV,
340 "fpgetmask()=%d FP_X_INV=%d",
341 (int)fpgetmask(), (int)FP_X_INV);
342 }
343
344 ATF_TC(fetestexcept_trap);
345 ATF_TC_HEAD(fetestexcept_trap, tc)
346 {
347 atf_tc_set_md_var(tc, "descr",
348 "Verify fetestexcept doesn't affect the trapped excpetions");
349 }
350 ATF_TC_BODY(fetestexcept_trap, tc)
351 {
352 int except;
353
354 fedisableexcept(FE_ALL_EXCEPT);
355 ATF_CHECK_EQ_MSG((except = fegetexcept()), 0,
356 "fegetexcept()=0x%x", except);
357
358 (void)fetestexcept(FE_ALL_EXCEPT);
359 ATF_CHECK_EQ_MSG((except = fegetexcept()), 0,
360 "fegetexcept()=0x%x", except);
361
362 feenableexcept(FE_ALL_EXCEPT);
363 ATF_CHECK_EQ_MSG((except = fegetexcept()), FE_ALL_EXCEPT,
364 "fegetexcept()=0x%x FE_ALL_EXCEPT=0x%x", except, FE_ALL_EXCEPT);
365
366 (void)fetestexcept(FE_ALL_EXCEPT);
367 #ifdef __x86_64__
368 atf_tc_expect_fail("PR port-amd64/57949");
369 #endif
370 ATF_CHECK_EQ_MSG((except = fegetexcept()), FE_ALL_EXCEPT,
371 "fegetexcept()=0x%x FE_ALL_EXCEPT=0x%x", except, FE_ALL_EXCEPT);
372 }
373
374 ATF_TP_ADD_TCS(tp)
375 {
376 ATF_TP_ADD_TC(tp, fegetround);
377 ATF_TP_ADD_TC(tp, fesetround);
378 ATF_TP_ADD_TC(tp, fegetexcept);
379 ATF_TP_ADD_TC(tp, feenableexcept);
380 ATF_TP_ADD_TC(tp, fetestexcept_trap);
381
382 return atf_no_error();
383 }
384
385 #else /* no fenv.h support */
386
387 ATF_TC(t_nofenv);
388
389 ATF_TC_HEAD(t_nofenv, tc)
390 {
391 atf_tc_set_md_var(tc, "descr",
392 "dummy test case - no fenv.h support");
393 }
394
395
396 ATF_TC_BODY(t_nofenv, tc)
397 {
398 atf_tc_skip("no fenv.h support on this architecture");
399 }
400
401 ATF_TP_ADD_TCS(tp)
402 {
403 ATF_TP_ADD_TC(tp, t_nofenv);
404 return atf_no_error();
405 }
406
407 #endif
408