tst-strtod-underflow.c 7.0 KB

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