test-fenv-clear-main.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* Test fesetenv (FE_DFL_ENV) and fesetenv (FE_NOMASK_ENV) clear
  2. exceptions (bug 19181).
  3. Copyright (C) 2015-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 <fenv.h>
  17. #include <float.h>
  18. #include <stdio.h>
  19. volatile float fa = 1.0f, fb = 0.0f, fc = FLT_MAX, fr;
  20. volatile long double lda = 1.0L, ldb = 0.0L, ldc = LDBL_MAX, ldr;
  21. static void
  22. raise_exceptions (void)
  23. {
  24. /* Raise exceptions both with feraiseexcept and arithmetic to allow
  25. for case of multiple floating-point units with separate
  26. exceptions state. */
  27. feraiseexcept (FE_ALL_EXCEPT);
  28. fr = fb / fb;
  29. fr = fa / fb;
  30. fr = fc * fc;
  31. fr = fa / fc / fc;
  32. ldr = ldb / ldb;
  33. ldr = lda / ldb;
  34. ldr = ldc * ldc;
  35. ldr = lda / ldc / ldc;
  36. }
  37. static __attribute__ ((noinline)) int
  38. run_tests (void)
  39. {
  40. int result = 0;
  41. raise_exceptions ();
  42. if (fesetenv (FE_DFL_ENV) == 0)
  43. {
  44. puts ("PASS: fesetenv (FE_DFL_ENV)");
  45. if (fetestexcept (FE_ALL_EXCEPT) == 0)
  46. puts ("PASS: fesetenv (FE_DFL_ENV) clearing exceptions");
  47. else
  48. {
  49. puts ("FAIL: fesetenv (FE_DFL_ENV) clearing exceptions");
  50. result = 1;
  51. }
  52. }
  53. else
  54. {
  55. puts ("FAIL: fesetenv (FE_DFL_ENV)");
  56. result = 1;
  57. }
  58. #ifdef FE_NOMASK_ENV
  59. raise_exceptions ();
  60. if (fesetenv (FE_NOMASK_ENV) == 0)
  61. {
  62. if (fetestexcept (FE_ALL_EXCEPT) == 0)
  63. puts ("PASS: fesetenv (FE_NOMASK_ENV) clearing exceptions");
  64. else
  65. {
  66. puts ("FAIL: fesetenv (FE_NOMASK_ENV) clearing exceptions");
  67. result = 1;
  68. }
  69. }
  70. else
  71. puts ("fesetenv (FE_NOMASK_ENV) failed, cannot test");
  72. #endif
  73. return result;
  74. }
  75. static int
  76. do_test (void)
  77. {
  78. CHECK_CAN_TEST;
  79. return run_tests ();
  80. }
  81. #define TEST_FUNCTION do_test ()
  82. #include "../test-skeleton.c"