test-nearbyint-except.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* Test nearbyint functions do not clear exceptions (bug 15491).
  2. Copyright (C) 2015-2016 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. #if __UCLIBC_HAS_FENV__
  16. #include <fenv.h>
  17. #endif
  18. #include <math.h>
  19. #include <stdio.h>
  20. #include "math-tests.h"
  21. #if __UCLIBC_HAS_FENV__
  22. #ifndef FE_INVALID
  23. # define FE_INVALID 0
  24. #endif
  25. #define TEST_FUNC(NAME, FLOAT, SUFFIX) \
  26. static int \
  27. NAME (void) \
  28. { \
  29. int result = 0; \
  30. volatile FLOAT a, b __attribute__ ((unused)); \
  31. a = 1.0; \
  32. /* nearbyint must not clear already-raised exceptions. */ \
  33. feraiseexcept (FE_ALL_EXCEPT); \
  34. b = nearbyint ## SUFFIX (a); \
  35. if (fetestexcept (FE_ALL_EXCEPT) == FE_ALL_EXCEPT) \
  36. puts ("PASS: " #FLOAT); \
  37. else \
  38. { \
  39. puts ("FAIL: " #FLOAT); \
  40. result = 1; \
  41. } \
  42. /* But it mustn't lose exceptions from sNaN arguments. */ \
  43. if (SNAN_TESTS (FLOAT) && EXCEPTION_TESTS (FLOAT)) \
  44. { \
  45. static volatile FLOAT snan = __builtin_nans ## SUFFIX (""); \
  46. volatile FLOAT c __attribute__ ((unused)); \
  47. feclearexcept (FE_ALL_EXCEPT); \
  48. c = nearbyint ## SUFFIX (snan); \
  49. if (fetestexcept (FE_INVALID) == FE_INVALID) \
  50. puts ("PASS: " #FLOAT " sNaN"); \
  51. else \
  52. { \
  53. puts ("FAIL: " #FLOAT " sNaN"); \
  54. result = 1; \
  55. } \
  56. } \
  57. return result; \
  58. }
  59. TEST_FUNC (float_test, float, f)
  60. TEST_FUNC (double_test, double, )
  61. #ifndef NO_LONG_DOUBLE
  62. TEST_FUNC (ldouble_test, long double, l)
  63. #endif
  64. #endif
  65. static int
  66. do_test (void)
  67. {
  68. #if __UCLIBC_HAS_FENV__
  69. int result = float_test ();
  70. result |= double_test ();
  71. #ifndef NO_LONG_DOUBLE
  72. result |= ldouble_test ();
  73. #endif
  74. return result;
  75. #else
  76. return 23;
  77. #endif
  78. }
  79. #define TEST_FUNCTION do_test ()
  80. #include "../test-skeleton.c"