1 /* Test of controlling the floating-point environment.
2 Copyright (C) 2023-2025 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2023. */
26 /* Test the combination of fegetenv() with fesetenv(). */
28 /* On *BSD/powerpc systems, raising FE_INVALID also sets FE_VXSOFT. */
36 #if defined __GLIBC__ && defined __arm__ && defined __SOFTFP__
37 fputs ("Skipping test: no floating-point environment exists on this machine\n", stderr
);
42 /* Get to a known initial state. */
43 ASSERT (feclearexcept (FE_ALL_EXCEPT
) == 0);
45 /* Save the current environment in env1. */
46 ASSERT (fegetenv (&env1
) == 0);
48 /* Modify the current environment. */
49 fesetround (FE_UPWARD
);
50 int supports_tracking
= (feraiseexcept (FE_INVALID
| FE_OVERFLOW
| FE_INEXACT
) == 0);
51 int supports_trapping
= (feenableexcept (FE_DIVBYZERO
) != -1);
53 /* Save the current environment in env2. */
54 ASSERT (fegetenv (&env2
) == 0);
56 /* Check that the exception flags are unmodified. */
57 if (supports_tracking
)
58 ASSERT ((fetestexcept (FE_ALL_EXCEPT
) & ~FE_VXSOFT
) == (FE_INVALID
| FE_OVERFLOW
| FE_INEXACT
));
60 ASSERT (fetestexcept (FE_ALL_EXCEPT
) == 0);
61 /* Check that the exception trap bits are unmodified. */
62 ASSERT (fegetexcept () == (supports_trapping
? FE_DIVBYZERO
: 0));
64 /* Go back to env1. */
65 ASSERT (fesetenv (&env1
) == 0);
67 /* Check that the rounding direction has been restored. */
68 ASSERT (fegetround () == FE_TONEAREST
);
69 /* Check that the exception flags have been restored. */
70 ASSERT (fetestexcept (FE_ALL_EXCEPT
) == 0);
71 /* Check that the exception trap bits have been restored. */
72 ASSERT (fegetexcept () == 0);
74 /* Modify the rounding direction, the exception flags, and the exception
76 fesetround (FE_DOWNWARD
);
77 ASSERT (fegetround () == FE_DOWNWARD
);
78 feclearexcept (FE_OVERFLOW
);
79 feraiseexcept (FE_UNDERFLOW
| FE_INEXACT
);
80 ASSERT (fetestexcept (FE_ALL_EXCEPT
) == (supports_tracking
? FE_UNDERFLOW
| FE_INEXACT
: 0));
81 feenableexcept (FE_INVALID
);
82 ASSERT (fegetexcept () == (supports_trapping
? FE_INVALID
: 0));
84 /* Go back to env2. */
85 ASSERT (fesetenv (&env2
) == 0);
87 /* Check that the rounding direction has been restored. */
88 ASSERT (fegetround () == FE_UPWARD
);
89 /* Check that the exception flags have been restored. */
90 if (supports_tracking
)
91 ASSERT ((fetestexcept (FE_ALL_EXCEPT
) & ~FE_VXSOFT
) == (FE_INVALID
| FE_OVERFLOW
| FE_INEXACT
));
93 ASSERT (fetestexcept (FE_ALL_EXCEPT
) == 0);
94 /* Check that the exception trap bits have been restored. */
95 ASSERT (fegetexcept () == (supports_trapping
? FE_DIVBYZERO
: 0));
97 /* ======================================================================== */
100 /* Enable trapping on FE_INVALID. */
101 feclearexcept (FE_INVALID
);
102 feenableexcept (FE_INVALID
);
103 ASSERT (fetestexcept (FE_ALL_EXCEPT
) == (supports_tracking
? FE_OVERFLOW
| FE_INEXACT
: 0));
105 /* Go back to the default environment. */
106 ASSERT (fesetenv (FE_DFL_ENV
) == 0);
108 /* Check its contents. */
109 ASSERT (fegetround () == FE_TONEAREST
);
110 ASSERT (fetestexcept (FE_ALL_EXCEPT
) == 0);
112 /* Check that it has trapping on FE_INVALID disabled. */
113 ASSERT (fegetexcept () == 0);
116 _GL_UNUSED
double volatile b
;
120 /* ======================================================================== */
121 /* Check that fesetenv restores the trapping behaviour. */
123 /* Enable trapping on FE_INVALID. */
124 feclearexcept (FE_INVALID
);
125 feenableexcept (FE_INVALID
);
127 /* Go back to env1. */
128 ASSERT (fesetenv (&env1
) == 0);
130 /* Check that it has disabled trapping on FE_INVALID. */
131 ASSERT (fegetexcept () == 0);
134 _GL_UNUSED
double volatile b
;
138 return test_exit_status
;