test-nearbyint-except.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* Test nearbyint functions do not clear exceptions (bug 15491).
  2. Copyright (C) 2015-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <fenv.h>
  16. #include <math.h>
  17. #include <stdbool.h>
  18. #include <stdio.h>
  19. #include <math-tests.h>
  20. #ifndef FE_INVALID
  21. # define FE_INVALID 0
  22. #endif
  23. static bool any_supported = false;
  24. #define TEST_FUNC(NAME, FLOAT, SUFFIX) \
  25. static int \
  26. NAME (void) \
  27. { \
  28. int result = 0; \
  29. if (!EXCEPTION_TESTS (FLOAT)) \
  30. return 0; \
  31. any_supported = true; \
  32. volatile FLOAT a, b __attribute__ ((unused)); \
  33. a = 1.0; \
  34. /* nearbyint must not clear already-raised exceptions. */ \
  35. feraiseexcept (FE_ALL_EXCEPT); \
  36. b = nearbyint ## SUFFIX (a); \
  37. if (fetestexcept (FE_ALL_EXCEPT) == FE_ALL_EXCEPT) \
  38. puts ("PASS: " #FLOAT); \
  39. else \
  40. { \
  41. puts ("FAIL: " #FLOAT); \
  42. result = 1; \
  43. } \
  44. /* But it mustn't lose exceptions from sNaN arguments. */ \
  45. if (SNAN_TESTS (FLOAT)) \
  46. { \
  47. static volatile FLOAT snan = __builtin_nans ## SUFFIX (""); \
  48. volatile FLOAT c __attribute__ ((unused)); \
  49. feclearexcept (FE_ALL_EXCEPT); \
  50. c = nearbyint ## SUFFIX (snan); \
  51. if (fetestexcept (FE_INVALID) == FE_INVALID) \
  52. puts ("PASS: " #FLOAT " sNaN"); \
  53. else \
  54. { \
  55. puts ("FAIL: " #FLOAT " sNaN"); \
  56. result = 1; \
  57. } \
  58. } \
  59. return result; \
  60. }
  61. TEST_FUNC (float_test, float, f)
  62. TEST_FUNC (double_test, double, )
  63. TEST_FUNC (ldouble_test, long double, l)
  64. static int
  65. do_test (void)
  66. {
  67. int result = float_test ();
  68. result |= double_test ();
  69. result |= ldouble_test ();
  70. if (!any_supported)
  71. return 77;
  72. return result;
  73. }
  74. #define TEST_FUNCTION do_test ()
  75. #include "../test-skeleton.c"