1 /* Test for strtod handling of arguments that may cause floating-point
3 Copyright (C) 2012-2017 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
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.
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.
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 <http://www.gnu.org/licenses/>. */
30 /* Result is exact or outside the subnormal range. */
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. */
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
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
,
59 enum underflow_case c
;
62 static const struct test tests
[] =
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
},
92 /* Return whether to expect underflow from a particular testcase, in a
93 given rounding mode. */
96 expect_underflow (enum underflow_case c
, int rm
)
98 if (c
== UNDERFLOW_NONE
)
100 if (c
== UNDERFLOW_ALWAYS
)
102 if (TININESS_AFTER_ROUNDING
)
108 return (c
== UNDERFLOW_EXCEPT_UPWARD
109 || c
== UNDERFLOW_ONLY_DOWNWARD_ZERO
);
119 return (c
== UNDERFLOW_EXCEPT_DOWNWARD
120 || c
== UNDERFLOW_ONLY_UPWARD_ZERO
);
124 return (c
== UNDERFLOW_EXCEPT_UPWARD
125 || c
== UNDERFLOW_EXCEPT_DOWNWARD
);
132 static bool support_underflow_exception
= false;
133 volatile double d
= DBL_MIN
;
137 test_in_one_mode (const char *s
, enum underflow_case c
, int rm
,
138 const char *mode_name
)
141 feclearexcept (FE_ALL_EXCEPT
);
143 double d
= strtod (s
, NULL
);
144 int got_errno
= errno
;
146 bool got_fe_underflow
= fetestexcept (FE_UNDERFLOW
) != 0;
148 bool got_fe_underflow
= false;
150 printf ("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
)
155 puts ("FAIL: errno neither 0 nor ERANGE");
158 else if (this_expect_underflow
!= (errno
== ERANGE
))
160 puts ("FAIL: underflow from errno differs from expectations");
163 if (support_underflow_exception
&& got_fe_underflow
!= this_expect_underflow
)
165 puts ("FAIL: underflow from exceptions differs from expectations");
174 int save_round_mode
__attribute__ ((unused
)) = fegetround ();
177 const int fe_tonearest
= FE_TONEAREST
;
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"
185 feclearexcept (FE_ALL_EXCEPT
);
187 if (fetestexcept (FE_UNDERFLOW
))
188 support_underflow_exception
= true;
190 puts ("underflow exception not supported at runtime, only testing errno");
192 for (size_t i
= 0; i
< sizeof (tests
) / sizeof (tests
[0]); i
++)
194 result
|= test_in_one_mode (tests
[i
].s
, tests
[i
].c
, fe_tonearest
,
195 "default rounding mode");
197 if (!fesetround (FE_DOWNWARD
))
199 result
|= test_in_one_mode (tests
[i
].s
, tests
[i
].c
, FE_DOWNWARD
,
201 fesetround (save_round_mode
);
205 if (!fesetround (FE_TOWARDZERO
))
207 result
|= test_in_one_mode (tests
[i
].s
, tests
[i
].c
, FE_TOWARDZERO
,
209 fesetround (save_round_mode
);
213 if (!fesetround (FE_UPWARD
))
215 result
|= test_in_one_mode (tests
[i
].s
, tests
[i
].c
, FE_UPWARD
,
217 fesetround (save_round_mode
);
224 #define TEST_FUNCTION do_test ()
225 #include "../test-skeleton.c"