test-nearbyint-except-2.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* Test nearbyint functions do not disable exception traps (bug 19228).
  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. #if __UCLIBC_HAS_FENV__
  21. #ifndef FE_INEXACT
  22. # define FE_INEXACT 0
  23. #endif
  24. #define TEST_FUNC(NAME, FLOAT, SUFFIX) \
  25. static int \
  26. NAME (void) \
  27. { \
  28. int result = 0; \
  29. volatile FLOAT a, b __attribute__ ((unused)); \
  30. a = 1.5; \
  31. /* nearbyint must work when traps on "inexact" are enabled. */ \
  32. b = nearbyint ## SUFFIX (a); \
  33. /* And it must have left those traps enabled. */ \
  34. if (fegetexcept () == FE_INEXACT) \
  35. puts ("PASS: " #FLOAT); \
  36. else \
  37. { \
  38. puts ("FAIL: " #FLOAT); \
  39. result = 1; \
  40. } \
  41. return result; \
  42. }
  43. TEST_FUNC (float_test, float, f)
  44. TEST_FUNC (double_test, double, )
  45. #ifndef NO_LONG_DOUBLE
  46. TEST_FUNC (ldouble_test, long double, l)
  47. #endif
  48. #endif
  49. static int
  50. do_test (void)
  51. {
  52. #if __UCLIBC_HAS_FENV__
  53. if (feenableexcept (FE_INEXACT) == -1)
  54. {
  55. puts ("enabling FE_INEXACT traps failed, cannot test");
  56. return 77;
  57. }
  58. int result = float_test ();
  59. feenableexcept (FE_INEXACT);
  60. result |= double_test ();
  61. #ifndef NO_LONG_DOUBLE
  62. feenableexcept (FE_INEXACT);
  63. result |= ldouble_test ();
  64. #endif
  65. return result;
  66. #else
  67. return 23;
  68. #endif
  69. }
  70. #define TEST_FUNCTION do_test ()
  71. #include "../test-skeleton.c"