2 * Copyright (c) 2004 David Schultz <das@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * Test the correctness and C99-compliance of various fenv.h features.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/types.h>
46 #include "test-utils.h"
48 #define NEXCEPTS (sizeof(std_excepts) / sizeof(std_excepts[0]))
50 static const int std_excepts[] = {
58 /* init_exceptsets() initializes this to the power set of std_excepts[] */
59 static int std_except_sets[1 << NEXCEPTS];
61 #pragma STDC FENV_ACCESS ON
64 * Initialize std_except_sets[] to the power set of std_excepts[]
71 for (i = 0; i < 1 << NEXCEPTS; i++) {
72 for (sr = i, j = 0; sr != 0; sr >>= 1, j++)
73 std_except_sets[i] |= std_excepts[j] & ((~sr & 1) - 1);
78 * Raise a floating-point exception without relying on the standard
79 * library routines, which we are trying to test.
81 * XXX We can't raise an {over,under}flow without also raising an
85 raiseexcept(int excepts)
90 * With a compiler that supports the FENV_ACCESS pragma
91 * properly, simple expressions like '0.0 / 0.0' should
92 * be sufficient to generate traps. Unfortunately, we
93 * need to bring a volatile variable into the equation
94 * to prevent incorrect optimizations.
96 if (excepts & FE_INVALID) {
100 if (excepts & FE_DIVBYZERO) {
104 if (excepts & FE_OVERFLOW) {
108 if (excepts & FE_UNDERFLOW) {
112 if (excepts & FE_INEXACT) {
118 * On the x86 (and some other architectures?) the FPU and
119 * integer units are decoupled. We need to execute an FWAIT
120 * or a floating-point instruction to get synchronous exceptions.
127 * Determine the current rounding mode without relying on the fenv
128 * routines. This function may raise an inexact exception.
136 * This test works just as well with 0.0 - 0.0, except on ia64
137 * where 0.0 - 0.0 gives the wrong sign when rounding downwards.
141 if (copysign(1.0, d) < 0.0)
142 return (FE_DOWNWARD);
145 if (d + (DBL_EPSILON * 3.0 / 4.0) == 1.0)
146 return (FE_TOWARDZERO);
147 if (d + (DBL_EPSILON * 1.0 / 4.0) > 1.0)
150 return (FE_TONEAREST);
154 trap_handler(int sig)
157 assert(sig == SIGFPE);
162 * This tests checks the default FP environment, so it must be first.
163 * The memcmp() test below may be too much to ask for, since there
164 * could be multiple machine-specific default environments.
169 #ifndef NO_STRICT_DFL_ENV
176 * Compare the fields that the AMD [1] and Intel [2] specs say will be
177 * set once fnstenv returns.
179 * Not all amd64 capable processors implement the fnstenv instruction
180 * by zero'ing out the env.__x87.__other field (example: AMD Opteron
181 * 6308). The AMD64/x64 specs aren't explicit on what the
182 * env.__x87.__other field will contain after fnstenv is executed, so
183 * the values in env.__x87.__other could be filled with arbitrary
184 * data depending on how the CPU implements fnstenv.
186 * 1. http://support.amd.com/TechDocs/26569_APM_v5.pdf
187 * 2. http://www.intel.com/Assets/en_US/PDF/manual/253666.pdf
189 assert(memcmp(&env.__mxcsr, &FE_DFL_ENV->__mxcsr,
190 sizeof(env.__mxcsr)) == 0);
191 assert(memcmp(&env.__x87.__control, &FE_DFL_ENV->__x87.__control,
192 sizeof(env.__x87.__control)) == 0);
193 assert(memcmp(&env.__x87.__status, &FE_DFL_ENV->__x87.__status,
194 sizeof(env.__x87.__status)) == 0);
195 assert(memcmp(&env.__x87.__tag, &FE_DFL_ENV->__x87.__tag,
196 sizeof(env.__x87.__tag)) == 0);
198 assert(memcmp(&env, FE_DFL_ENV, sizeof(env)) == 0);
202 assert(fetestexcept(FE_ALL_EXCEPT) == 0);
206 * Test fetestexcept() and feclearexcept().
209 test_fetestclearexcept(void)
213 for (i = 0; i < 1 << NEXCEPTS; i++)
214 assert(fetestexcept(std_except_sets[i]) == 0);
215 for (i = 0; i < 1 << NEXCEPTS; i++) {
216 excepts = std_except_sets[i];
218 /* FE_ALL_EXCEPT might be special-cased, as on i386. */
219 raiseexcept(excepts);
220 assert(fetestexcept(excepts) == excepts);
221 assert(feclearexcept(FE_ALL_EXCEPT) == 0);
222 assert(fetestexcept(FE_ALL_EXCEPT) == 0);
224 raiseexcept(excepts);
225 assert(fetestexcept(excepts) == excepts);
226 if ((excepts & (FE_UNDERFLOW | FE_OVERFLOW)) != 0) {
227 excepts |= FE_INEXACT;
228 assert((fetestexcept(ALL_STD_EXCEPT) | FE_INEXACT) ==
231 assert(fetestexcept(ALL_STD_EXCEPT) == excepts);
233 assert(feclearexcept(excepts) == 0);
234 assert(fetestexcept(ALL_STD_EXCEPT) == 0);
239 * Test fegetexceptflag() and fesetexceptflag().
241 * Prerequisites: fetestexcept(), feclearexcept()
244 test_fegsetexceptflag(void)
249 assert(fetestexcept(FE_ALL_EXCEPT) == 0);
250 for (i = 0; i < 1 << NEXCEPTS; i++) {
251 excepts = std_except_sets[i];
253 assert(fegetexceptflag(&flag, excepts) == 0);
254 raiseexcept(ALL_STD_EXCEPT);
255 assert(fesetexceptflag(&flag, excepts) == 0);
256 assert(fetestexcept(ALL_STD_EXCEPT) ==
257 (ALL_STD_EXCEPT ^ excepts));
259 assert(fegetexceptflag(&flag, FE_ALL_EXCEPT) == 0);
260 assert(feclearexcept(FE_ALL_EXCEPT) == 0);
261 assert(fesetexceptflag(&flag, excepts) == 0);
262 assert(fetestexcept(ALL_STD_EXCEPT) == 0);
263 assert(fesetexceptflag(&flag, ALL_STD_EXCEPT ^ excepts) == 0);
264 assert(fetestexcept(ALL_STD_EXCEPT) ==
265 (ALL_STD_EXCEPT ^ excepts));
267 assert(feclearexcept(FE_ALL_EXCEPT) == 0);
272 * Test feraiseexcept().
274 * Prerequisites: fetestexcept(), feclearexcept()
277 test_feraiseexcept(void)
281 for (i = 0; i < 1 << NEXCEPTS; i++) {
282 excepts = std_except_sets[i];
284 assert(fetestexcept(FE_ALL_EXCEPT) == 0);
285 assert(feraiseexcept(excepts) == 0);
286 if ((excepts & (FE_UNDERFLOW | FE_OVERFLOW)) != 0) {
287 excepts |= FE_INEXACT;
288 assert((fetestexcept(ALL_STD_EXCEPT) | FE_INEXACT) ==
291 assert(fetestexcept(ALL_STD_EXCEPT) == excepts);
293 assert(feclearexcept(FE_ALL_EXCEPT) == 0);
295 assert(feraiseexcept(FE_INVALID | FE_DIVBYZERO) == 0);
296 assert(fetestexcept(ALL_STD_EXCEPT) == (FE_INVALID | FE_DIVBYZERO));
297 assert(feraiseexcept(FE_OVERFLOW | FE_UNDERFLOW | FE_INEXACT) == 0);
298 assert(fetestexcept(ALL_STD_EXCEPT) == ALL_STD_EXCEPT);
299 assert(feclearexcept(FE_ALL_EXCEPT) == 0);
303 * Test fegetround() and fesetround().
306 test_fegsetround(void)
309 assert(fegetround() == FE_TONEAREST);
310 assert(getround() == FE_TONEAREST);
311 assert(FLT_ROUNDS == 1);
313 assert(fesetround(FE_DOWNWARD) == 0);
314 assert(fegetround() == FE_DOWNWARD);
315 assert(getround() == FE_DOWNWARD);
316 assert(FLT_ROUNDS == 3);
318 assert(fesetround(FE_UPWARD) == 0);
319 assert(getround() == FE_UPWARD);
320 assert(fegetround() == FE_UPWARD);
321 assert(FLT_ROUNDS == 2);
323 assert(fesetround(FE_TOWARDZERO) == 0);
324 assert(getround() == FE_TOWARDZERO);
325 assert(fegetround() == FE_TOWARDZERO);
326 assert(FLT_ROUNDS == 0);
328 assert(fesetround(FE_TONEAREST) == 0);
329 assert(getround() == FE_TONEAREST);
330 assert(FLT_ROUNDS == 1);
332 assert(feclearexcept(FE_ALL_EXCEPT) == 0);
336 * Test fegetenv() and fesetenv().
338 * Prerequisites: fetestexcept(), feclearexcept(), fegetround(), fesetround()
346 for (i = 0; i < 1 << NEXCEPTS; i++) {
347 excepts = std_except_sets[i];
349 assert(fetestexcept(FE_ALL_EXCEPT) == 0);
350 assert(fegetround() == FE_TONEAREST);
351 assert(fegetenv(&env1) == 0);
354 * fe[gs]etenv() should be able to save and restore
355 * exception flags without the spurious inexact
356 * exceptions that afflict raiseexcept().
358 raiseexcept(excepts);
359 if ((excepts & (FE_UNDERFLOW | FE_OVERFLOW)) != 0 &&
360 (excepts & FE_INEXACT) == 0)
361 assert(feclearexcept(FE_INEXACT) == 0);
363 fesetround(FE_DOWNWARD);
364 assert(fegetenv(&env2) == 0);
365 assert(fesetenv(&env1) == 0);
366 assert(fetestexcept(FE_ALL_EXCEPT) == 0);
367 assert(fegetround() == FE_TONEAREST);
369 assert(fesetenv(&env2) == 0);
372 * Some platforms like powerpc may set extra exception bits. Since
373 * only standard exceptions are tested, mask against ALL_STD_EXCEPT
375 assert((fetestexcept(FE_ALL_EXCEPT) & ALL_STD_EXCEPT) == excepts);
377 assert(fegetround() == FE_DOWNWARD);
378 assert(fesetenv(&env1) == 0);
379 assert(fetestexcept(FE_ALL_EXCEPT) == 0);
380 assert(fegetround() == FE_TONEAREST);
385 * Test fegetexcept(), fedisableexcept(), and feenableexcept().
387 * Prerequisites: fetestexcept(), feraiseexcept()
392 struct sigaction act;
393 int except, pass, raise, status;
396 assert((fegetexcept() & ALL_STD_EXCEPT) == 0);
397 assert((feenableexcept(FE_INVALID|FE_OVERFLOW) & ALL_STD_EXCEPT) == 0);
398 assert((feenableexcept(FE_UNDERFLOW) & ALL_STD_EXCEPT) ==
399 (FE_INVALID | FE_OVERFLOW));
400 assert((fedisableexcept(FE_OVERFLOW) & ALL_STD_EXCEPT) ==
401 (FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW));
402 assert((fegetexcept() & ALL_STD_EXCEPT) == (FE_INVALID | FE_UNDERFLOW));
403 assert((fedisableexcept(FE_ALL_EXCEPT) & ALL_STD_EXCEPT) ==
404 (FE_INVALID | FE_UNDERFLOW));
405 assert((fegetexcept() & ALL_STD_EXCEPT) == 0);
407 sigemptyset(&act.sa_mask);
409 act.sa_handler = trap_handler;
410 for (pass = 0; pass < 2; pass++) {
411 for (i = 0; i < NEXCEPTS; i++) {
412 except = std_excepts[i];
413 /* over/underflow may also raise inexact */
414 if (except == FE_INEXACT)
415 raise = FE_DIVBYZERO | FE_INVALID;
417 raise = ALL_STD_EXCEPT ^ except;
420 * We need to fork a child process because
421 * there isn't a portable way to recover from
422 * a floating-point exception.
426 assert((fegetexcept() & ALL_STD_EXCEPT) == 0);
427 assert((feenableexcept(except)
428 & ALL_STD_EXCEPT) == 0);
429 assert(fegetexcept() == except);
431 assert(feraiseexcept(raise) == 0);
432 assert(fetestexcept(ALL_STD_EXCEPT) == raise);
434 assert(sigaction(SIGFPE, &act, NULL) == 0);
439 feraiseexcept(except);
444 default: /* parent */
445 assert(wait(&status) > 0);
447 * Avoid assert() here so that it's possible
448 * to examine a failed child's core dump.
450 if (!WIFEXITED(status))
451 errx(1, "child aborted\n");
452 assert(WEXITSTATUS(status) == 0);
459 assert(fetestexcept(FE_ALL_EXCEPT) == 0);
463 * Test feholdexcept() and feupdateenv().
465 * Prerequisites: fetestexcept(), fegetround(), fesetround(),
466 * fedisableexcept(), feenableexcept()
469 test_feholdupdate(void)
473 struct sigaction act;
474 int except, pass, status, raise;
477 sigemptyset(&act.sa_mask);
479 act.sa_handler = trap_handler;
480 for (pass = 0; pass < 2; pass++) {
481 for (i = 0; i < NEXCEPTS; i++) {
482 except = std_excepts[i];
483 /* over/underflow may also raise inexact */
484 if (except == FE_INEXACT)
485 raise = FE_DIVBYZERO | FE_INVALID;
487 raise = ALL_STD_EXCEPT ^ except;
490 * We need to fork a child process because
491 * there isn't a portable way to recover from
492 * a floating-point exception.
497 * We don't want to cause a fatal exception in
498 * the child until the second pass, so we can
499 * check other properties of feupdateenv().
502 assert((feenableexcept(except) &
503 ALL_STD_EXCEPT) == 0);
505 assert(fesetround(FE_DOWNWARD) == 0);
506 assert(feholdexcept(&env) == 0);
507 assert(fetestexcept(FE_ALL_EXCEPT) == 0);
509 assert(fesetround(FE_UPWARD) == 0);
512 assert(sigaction(SIGFPE, &act, NULL) ==
514 assert(feupdateenv(&env) == 0);
515 assert(fegetround() == FE_DOWNWARD);
516 assert(fetestexcept(ALL_STD_EXCEPT) ==
521 default: /* parent */
522 assert(wait(&status) > 0);
524 * Avoid assert() here so that it's possible
525 * to examine a failed child's core dump.
527 if (!WIFEXITED(status))
528 errx(1, "child aborted\n");
529 assert(WEXITSTATUS(status) == 0);
536 assert(fetestexcept(FE_ALL_EXCEPT) == 0);
546 printf("ok 1 - fenv\n");
547 test_fetestclearexcept();
548 printf("ok 2 - fenv\n");
549 test_fegsetexceptflag();
550 printf("ok 3 - fenv\n");
551 test_feraiseexcept();
552 printf("ok 4 - fenv\n");
554 printf("ok 5 - fenv\n");
556 printf("ok 6 - fenv\n");
558 printf("ok 7 - fenv\n");
560 printf("ok 8 - fenv\n");