gettext: Sync with gettext 0.23.
[gnulib.git] / tests / test-fenv-round.c
bloba4812ed9b7ad58712e8bc8689a1feb46e35366b1
1 /* Test of floating-point rounding direction.
2 Copyright (C) 2023-2024 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. */
19 #include <config.h>
21 /* Specification. */
22 #include <fenv.h>
24 #include "macros.h"
26 /* Check the FE_TOWARDZERO rounding direction. */
27 _GL_UNUSED static void
28 test_towardzero ()
31 float volatile a, b, q, p;
33 a = 1; b = 3; q = a / b; p = b * q;
34 ASSERT (p < a);
36 a = -1; b = 3; q = a / b; p = b * q;
37 ASSERT (p > a);
40 double volatile a, b, q, p;
42 a = 1; b = 3; q = a / b; p = b * q;
43 ASSERT (p < a);
45 a = -1; b = 3; q = a / b; p = b * q;
46 ASSERT (p > a);
48 #if !(((defined __FreeBSD__ || defined __OpenBSD__) && defined __aarch64__) || (defined __NetBSD__ && defined __sparcv9 /* sparc64 */) || (defined __OpenBSD__ && defined __mips64))
50 long double volatile a, b, q, p;
52 a = 1; b = 3; q = a / b; p = b * q;
53 ASSERT (p < a);
55 a = -1; b = 3; q = a / b; p = b * q;
56 ASSERT (p > a);
58 #endif
61 /* Check the FE_UPWARD rounding direction. */
62 _GL_UNUSED static void
63 test_upward ()
66 float volatile a, b, q, p;
68 a = 1; b = 3; q = a / b; p = b * q;
69 ASSERT (p > a);
71 a = -1; b = 3; q = a / b; p = b * q;
72 ASSERT (p > a);
75 double volatile a, b, q, p;
77 a = 1; b = 3; q = a / b; p = b * q;
78 ASSERT (p > a);
80 a = -1; b = 3; q = a / b; p = b * q;
81 ASSERT (p > a);
83 #if !(((defined __FreeBSD__ || defined __OpenBSD__) && defined __aarch64__) || (defined __NetBSD__ && defined __sparcv9 /* sparc64 */) || (defined __OpenBSD__ && defined __mips64))
85 long double volatile a, b, q, p;
87 a = 1; b = 3; q = a / b; p = b * q;
88 ASSERT (p > a);
90 a = -1; b = 3; q = a / b; p = b * q;
91 ASSERT (p > a);
93 #endif
96 /* Check the FE_DOWNWARD rounding direction. */
97 _GL_UNUSED static void
98 test_downward ()
101 float volatile a, b, q, p;
103 a = 1; b = 3; q = a / b; p = b * q;
104 ASSERT (p < a);
106 a = -1; b = 3; q = a / b; p = b * q;
107 ASSERT (p < a);
110 double volatile a, b, q, p;
112 a = 1; b = 3; q = a / b; p = b * q;
113 ASSERT (p < a);
115 a = -1; b = 3; q = a / b; p = b * q;
116 ASSERT (p < a);
118 #if !(((defined __FreeBSD__ || defined __OpenBSD__) && defined __aarch64__) || (defined __NetBSD__ && defined __sparcv9 /* sparc64 */) || (defined __OpenBSD__ && defined __mips64))
120 long double volatile a, b, q, p;
122 a = 1; b = 3; q = a / b; p = b * q;
123 ASSERT (p < a);
125 a = -1; b = 3; q = a / b; p = b * q;
126 ASSERT (p < a);
128 #endif
131 /* Check the FE_TONEAREST rounding direction. */
132 static void
133 test_tonearest ()
136 float volatile a, b, q, p;
138 a = 1; b = 3; q = a / b; p = b * q;
139 ASSERT (p == a);
141 a = -1; b = 3; q = a / b; p = b * q;
142 ASSERT (p == a);
145 double volatile a, b, q, p;
147 a = 1; b = 3; q = a / b; p = b * q;
148 ASSERT (p == a);
150 a = -1; b = 3; q = a / b; p = b * q;
151 ASSERT (p == a);
154 long double volatile a, b, q, p;
156 a = 1; b = 3; q = a / b; p = b * q;
157 ASSERT (p == a);
159 a = -1; b = 3; q = a / b; p = b * q;
160 ASSERT (p == a);
165 main ()
167 /* Check that the default rounding direction is FE_TONEAREST. */
168 test_tonearest ();
170 /* On alpha, this test works only when compiled with the GCC option
171 '-mfp-rounding-mode=d'. */
172 #if !((defined __arm__ && defined __SOFTFP__) || defined __alpha)
173 # if defined FE_TOWARDZERO
174 ASSERT (fesetround (FE_TOWARDZERO) == 0);
175 test_towardzero ();
176 ASSERT (fegetround () == FE_TOWARDZERO);
177 # endif
179 # if defined FE_UPWARD
180 ASSERT (fesetround (FE_UPWARD) == 0);
181 test_upward ();
182 ASSERT (fegetround () == FE_UPWARD);
183 # endif
185 # if defined FE_DOWNWARD
186 ASSERT (fesetround (FE_DOWNWARD) == 0);
187 test_downward ();
188 ASSERT (fegetround () == FE_DOWNWARD);
189 # endif
190 #endif
192 ASSERT (fesetround (FE_TONEAREST) == 0);
193 test_tonearest ();
194 ASSERT (fegetround () == FE_TONEAREST);
196 return test_exit_status;