1 | /* Test for strtod handling of arguments that may cause floating-point |
2 | underflow. |
3 | Copyright (C) 2012-2022 Free Software Foundation, Inc. |
4 | This file is part of the GNU C Library. |
5 | |
6 | The GNU C Library is free software; you can redistribute it and/or |
7 | modify it under the terms of the GNU Lesser General Public |
8 | License as published by the Free Software Foundation; either |
9 | version 2.1 of the License, or (at your option) any later version. |
10 | |
11 | The GNU C Library is distributed in the hope that it will be useful, |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | Lesser General Public License for more details. |
15 | |
16 | You should have received a copy of the GNU Lesser General Public |
17 | License along with the GNU C Library; if not, see |
18 | <https://www.gnu.org/licenses/>. */ |
19 | |
20 | #include <errno.h> |
21 | #include <fenv.h> |
22 | #include <float.h> |
23 | #include <stdbool.h> |
24 | #include <stdio.h> |
25 | #include <stdlib.h> |
26 | #include <tininess.h> |
27 | |
28 | enum underflow_case |
29 | { |
30 | /* Result is exact or outside the subnormal range. */ |
31 | UNDERFLOW_NONE, |
32 | /* Result has magnitude at most half way between the largest |
33 | subnormal value and the smallest positive normal value, and is |
34 | not exact, so underflows in all rounding modes and independent |
35 | of how tininess is detected. */ |
36 | UNDERFLOW_ALWAYS, |
37 | /* Result is positive, with magnitude larger than half way between |
38 | the largest subnormal value and the least positive normal |
39 | value, but would underflow when rounded to nearest to normal |
40 | precision, so underflows after rounding in all modes except |
41 | rounding upward. */ |
42 | UNDERFLOW_EXCEPT_UPWARD, |
43 | /* Likewise, for a negative result, underflowing after rounding |
44 | except when rounding downward. */ |
45 | UNDERFLOW_EXCEPT_DOWNWARD, |
46 | /* Result is positive, with magnitude at least three quarters of |
47 | the way from the largest subnormal value to the smallest |
48 | positive normal value, so underflows after rounding only when |
49 | rounding downward or toward zero. */ |
50 | UNDERFLOW_ONLY_DOWNWARD_ZERO, |
51 | /* Likewise, for a negative result, underflowing after rounding |
52 | only when rounding upward or toward zero. */ |
53 | UNDERFLOW_ONLY_UPWARD_ZERO, |
54 | }; |
55 | |
56 | struct test |
57 | { |
58 | const char *s; |
59 | enum underflow_case c; |
60 | }; |
61 | |
62 | static const struct test tests[] = |
63 | { |
64 | { "0x1p-1022" , UNDERFLOW_NONE }, |
65 | { "-0x1p-1022" , UNDERFLOW_NONE }, |
66 | { "0x0p-10000000000000000000000000" , UNDERFLOW_NONE }, |
67 | { "-0x0p-10000000000000000000000000" , UNDERFLOW_NONE }, |
68 | { "0x1p-10000000000000000000000000" , UNDERFLOW_ALWAYS }, |
69 | { "-0x1p-10000000000000000000000000" , UNDERFLOW_ALWAYS }, |
70 | { "0x1.000000000000000000001p-1022" , UNDERFLOW_NONE }, |
71 | { "-0x1.000000000000000000001p-1022" , UNDERFLOW_NONE }, |
72 | { "0x1p-1075" , UNDERFLOW_ALWAYS }, |
73 | { "-0x1p-1075" , UNDERFLOW_ALWAYS }, |
74 | { "0x1p-1023" , UNDERFLOW_NONE }, |
75 | { "-0x1p-1023" , UNDERFLOW_NONE }, |
76 | { "0x1p-1074" , UNDERFLOW_NONE }, |
77 | { "-0x1p-1074" , UNDERFLOW_NONE }, |
78 | { "0x1.ffffffffffffep-1023" , UNDERFLOW_NONE }, |
79 | { "-0x1.ffffffffffffep-1023" , UNDERFLOW_NONE }, |
80 | { "0x1.fffffffffffffp-1023" , UNDERFLOW_ALWAYS }, |
81 | { "-0x1.fffffffffffffp-1023" , UNDERFLOW_ALWAYS }, |
82 | { "0x1.fffffffffffff0001p-1023" , UNDERFLOW_EXCEPT_UPWARD }, |
83 | { "-0x1.fffffffffffff0001p-1023" , UNDERFLOW_EXCEPT_DOWNWARD }, |
84 | { "0x1.fffffffffffff7fffp-1023" , UNDERFLOW_EXCEPT_UPWARD }, |
85 | { "-0x1.fffffffffffff7fffp-1023" , UNDERFLOW_EXCEPT_DOWNWARD }, |
86 | { "0x1.fffffffffffff8p-1023" , UNDERFLOW_ONLY_DOWNWARD_ZERO }, |
87 | { "-0x1.fffffffffffff8p-1023" , UNDERFLOW_ONLY_UPWARD_ZERO }, |
88 | { "0x1.fffffffffffffffffp-1023" , UNDERFLOW_ONLY_DOWNWARD_ZERO }, |
89 | { "-0x1.fffffffffffffffffp-1023" , UNDERFLOW_ONLY_UPWARD_ZERO }, |
90 | }; |
91 | |
92 | /* Return whether to expect underflow from a particular testcase, in a |
93 | given rounding mode. */ |
94 | |
95 | static bool |
96 | expect_underflow (enum underflow_case c, int rm) |
97 | { |
98 | if (c == UNDERFLOW_NONE) |
99 | return false; |
100 | if (c == UNDERFLOW_ALWAYS) |
101 | return true; |
102 | if (TININESS_AFTER_ROUNDING) |
103 | { |
104 | switch (rm) |
105 | { |
106 | #ifdef FE_DOWNWARD |
107 | case FE_DOWNWARD: |
108 | return (c == UNDERFLOW_EXCEPT_UPWARD |
109 | || c == UNDERFLOW_ONLY_DOWNWARD_ZERO); |
110 | #endif |
111 | |
112 | #ifdef FE_TOWARDZERO |
113 | case FE_TOWARDZERO: |
114 | return true; |
115 | #endif |
116 | |
117 | #ifdef FE_UPWARD |
118 | case FE_UPWARD: |
119 | return (c == UNDERFLOW_EXCEPT_DOWNWARD |
120 | || c == UNDERFLOW_ONLY_UPWARD_ZERO); |
121 | #endif |
122 | |
123 | default: |
124 | return (c == UNDERFLOW_EXCEPT_UPWARD |
125 | || c == UNDERFLOW_EXCEPT_DOWNWARD); |
126 | } |
127 | } |
128 | else |
129 | return true; |
130 | } |
131 | |
132 | static bool support_underflow_exception = false; |
133 | volatile double d = DBL_MIN; |
134 | volatile double dd; |
135 | |
136 | static int |
137 | test_in_one_mode (const char *s, enum underflow_case c, int rm, |
138 | const char *mode_name) |
139 | { |
140 | int result = 0; |
141 | feclearexcept (FE_ALL_EXCEPT); |
142 | errno = 0; |
143 | double d = strtod (s, NULL); |
144 | int got_errno = errno; |
145 | #ifdef FE_UNDERFLOW |
146 | bool got_fe_underflow = fetestexcept (FE_UNDERFLOW) != 0; |
147 | #else |
148 | bool got_fe_underflow = false; |
149 | #endif |
150 | printf (format: "strtod (%s) (%s) returned %a, errno = %d, %sunderflow exception\n" , |
151 | s, mode_name, d, got_errno, got_fe_underflow ? "" : "no " ); |
152 | bool this_expect_underflow = expect_underflow (c, rm); |
153 | if (got_errno != 0 && got_errno != ERANGE) |
154 | { |
155 | puts (s: "FAIL: errno neither 0 nor ERANGE" ); |
156 | result = 1; |
157 | } |
158 | else if (this_expect_underflow != (errno == ERANGE)) |
159 | { |
160 | puts (s: "FAIL: underflow from errno differs from expectations" ); |
161 | result = 1; |
162 | } |
163 | if (support_underflow_exception && got_fe_underflow != this_expect_underflow) |
164 | { |
165 | puts (s: "FAIL: underflow from exceptions differs from expectations" ); |
166 | result = 1; |
167 | } |
168 | return result; |
169 | } |
170 | |
171 | static int |
172 | do_test (void) |
173 | { |
174 | int save_round_mode __attribute__ ((unused)) = fegetround (); |
175 | int result = 0; |
176 | #ifdef FE_TONEAREST |
177 | const int fe_tonearest = FE_TONEAREST; |
178 | #else |
179 | const int fe_tonearest = 0; |
180 | # if defined FE_DOWNWARD || defined FE_TOWARDZERO || defined FE_UPWARD |
181 | # error "FE_TONEAREST not defined, but another rounding mode is" |
182 | # endif |
183 | #endif |
184 | #ifdef FE_UNDERFLOW |
185 | feclearexcept (FE_ALL_EXCEPT); |
186 | dd = d * d; |
187 | if (fetestexcept (FE_UNDERFLOW)) |
188 | support_underflow_exception = true; |
189 | else |
190 | puts (s: "underflow exception not supported at runtime, only testing errno" ); |
191 | #endif |
192 | for (size_t i = 0; i < sizeof (tests) / sizeof (tests[0]); i++) |
193 | { |
194 | result |= test_in_one_mode (s: tests[i].s, c: tests[i].c, rm: fe_tonearest, |
195 | mode_name: "default rounding mode" ); |
196 | #ifdef FE_DOWNWARD |
197 | if (!fesetround (FE_DOWNWARD)) |
198 | { |
199 | result |= test_in_one_mode (s: tests[i].s, c: tests[i].c, FE_DOWNWARD, |
200 | mode_name: "FE_DOWNWARD" ); |
201 | fesetround (rounding_direction: save_round_mode); |
202 | } |
203 | #endif |
204 | #ifdef FE_TOWARDZERO |
205 | if (!fesetround (FE_TOWARDZERO)) |
206 | { |
207 | result |= test_in_one_mode (s: tests[i].s, c: tests[i].c, FE_TOWARDZERO, |
208 | mode_name: "FE_TOWARDZERO" ); |
209 | fesetround (rounding_direction: save_round_mode); |
210 | } |
211 | #endif |
212 | #ifdef FE_UPWARD |
213 | if (!fesetround (FE_UPWARD)) |
214 | { |
215 | result |= test_in_one_mode (s: tests[i].s, c: tests[i].c, FE_UPWARD, |
216 | mode_name: "FE_UPWARD" ); |
217 | fesetround (rounding_direction: save_round_mode); |
218 | } |
219 | #endif |
220 | } |
221 | return result; |
222 | } |
223 | |
224 | #define TEST_FUNCTION do_test () |
225 | #include "../test-skeleton.c" |
226 | |