Branch
Hash :
f3684e41
Author :
Date :
2025-06-15T17:25:22
fenv-environment tests: Fix compilation errors on Linux/sh4. Reported by Collin Funk in <https://lists.gnu.org/archive/html/bug-gnulib/2025-06/msg00124.html>. * tests/test-fenv-env-2.c (FE_UPWARD, FE_DOWNWARD): Define as aliases. * tests/test-fenv-env-3.c: Likewise. * tests/test-fenv-env-4.c: Likewise. * tests/test-fenv-env-5.c: Likewise.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
/* Test of controlling the floating-point environment.
Copyright (C) 2023-2025 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
/* Written by Bruno Haible <bruno@clisp.org>, 2023. */
#include <config.h>
/* Specification. */
#include <fenv.h>
#include "macros.h"
/* Test the combination of feholdexcept() with feupdateenv(). */
/* Some CPUs don't support FE_UPWARD and FE_DOWNWARD in hardware. */
#if defined __sh__
# ifndef FE_UPWARD
# define FE_UPWARD FE_TOWARDZERO
# define FE_DOWNWARD FE_TONEAREST
# endif
#endif
/* On *BSD/powerpc systems, raising FE_INVALID also sets FE_VXSOFT. */
#ifndef FE_VXSOFT
# define FE_VXSOFT 0
#endif
int
main ()
{
#if defined __GLIBC__ && defined __arm__ && defined __SOFTFP__
fputs ("Skipping test: no floating-point environment exists on this machine\n", stderr);
return 77;
#else
fenv_t env1, env2;
/* Get to a known initial state. */
ASSERT (feclearexcept (FE_ALL_EXCEPT) == 0);
/* Save the current environment in env1. */
ASSERT (feholdexcept (&env1) == 0);
/* Modify the current environment. */
fesetround (FE_UPWARD);
int supports_tracking = (feraiseexcept (FE_INVALID | FE_OVERFLOW | FE_INEXACT) == 0);
int supports_trapping = (feenableexcept (FE_DIVBYZERO) != -1);
/* Save the current environment in env2. */
ASSERT (feholdexcept (&env2) == 0);
/* Check that the exception flags are cleared. */
ASSERT (fetestexcept (FE_ALL_EXCEPT) == 0);
/* Check that the exception trap bits are cleared. */
ASSERT (fegetexcept () == 0);
/* Go back to env1. */
ASSERT (feupdateenv (&env1) == 0);
/* Check that the rounding direction has been restored. */
ASSERT (fegetround () == FE_TONEAREST);
/* Check that the exception flags are the union of the saved and of the
current exception flags. (The saved exception flags and the current
exception flags both happen to be none in this case.) */
ASSERT (fetestexcept (FE_ALL_EXCEPT) == 0);
/* Check that the exception trap bits have been restored. */
ASSERT (fegetexcept () == 0);
/* Modify the rounding direction, the exception flags, and the exception
trap bits again. */
fesetround (FE_DOWNWARD);
ASSERT (fegetround () == FE_DOWNWARD);
feclearexcept (FE_OVERFLOW);
feraiseexcept (FE_UNDERFLOW | FE_INEXACT);
ASSERT (fetestexcept (FE_ALL_EXCEPT) == (supports_tracking ? FE_UNDERFLOW | FE_INEXACT : 0));
feenableexcept (FE_INVALID);
ASSERT (fegetexcept () == (supports_trapping ? FE_INVALID : 0));
/* Go back to env2. */
ASSERT (feupdateenv (&env2) == 0);
/* Check that the rounding direction has been restored. */
ASSERT (fegetround () == FE_UPWARD);
/* Check that the exception flags are the union of the saved and of the
current exception flags. */
if (supports_tracking)
ASSERT ((fetestexcept (FE_ALL_EXCEPT) & ~FE_VXSOFT) == (FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW | FE_INEXACT));
else
ASSERT (fetestexcept (FE_ALL_EXCEPT) == 0);
/* Check that the exception trap bits have been restored. */
ASSERT (fegetexcept () == (supports_trapping ? FE_DIVBYZERO : 0));
/* ======================================================================== */
/* FE_DFL_ENV */
/* Go back to the default environment. */
ASSERT (feupdateenv (FE_DFL_ENV) == 0);
/* Check that the rounding direction has been restored,
whereas the exception flags are unmodified. */
ASSERT (fegetround () == FE_TONEAREST);
if (supports_tracking)
ASSERT ((fetestexcept (FE_ALL_EXCEPT) & ~FE_VXSOFT) == (FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW | FE_INEXACT));
else
ASSERT (fetestexcept (FE_ALL_EXCEPT) == 0);
/* Enable trapping on FE_INVALID. */
feclearexcept (FE_INVALID);
feenableexcept (FE_INVALID);
ASSERT (fetestexcept (FE_ALL_EXCEPT) == (supports_tracking ? FE_OVERFLOW | FE_UNDERFLOW | FE_INEXACT : 0));
/* Go back to the default environment. */
ASSERT (feupdateenv (FE_DFL_ENV) == 0);
/* Check that the rounding direction has been restored,
whereas the exception flags are unmodified. */
ASSERT (fegetround () == FE_TONEAREST);
ASSERT (fetestexcept (FE_ALL_EXCEPT) == (supports_tracking ? FE_OVERFLOW | FE_UNDERFLOW | FE_INEXACT : 0));
/* Check that it has trapping on FE_INVALID disabled. */
ASSERT (fegetexcept () == 0);
{
double volatile a;
_GL_UNUSED double volatile b;
a = 0; b = a / a;
}
/* ======================================================================== */
/* Check that feholdexcept, unlike fegetenv, disables trapping. */
/* musl libc does not support floating-point exception trapping, even where
the hardware supports it. See
<https://wiki.musl-libc.org/functional-differences-from-glibc.html> */
# if !MUSL_LIBC || GNULIB_FEENABLEEXCEPT
/* Enable trapping on FE_INVALID. */
feclearexcept (FE_INVALID);
if (feenableexcept (FE_INVALID) != -1)
{
/* Call feholdexcept. */
ASSERT (feholdexcept (&env1) == 0);
/* Check that it has disabled trapping on FE_INVALID. */
ASSERT (fegetexcept () == 0);
{
double volatile a;
_GL_UNUSED double volatile b;
a = 0; b = a / a;
}
}
# endif
return test_exit_status;
#endif
}