tst-setjmp-fp.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* Test that setjmp/longjmp do not save and restore floating-point
  2. exceptions and rounding modes.
  3. Copyright (C) 2013-2026 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. <https://www.gnu.org/licenses/>. */
  16. #include <fenv.h>
  17. #include <setjmp.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. static jmp_buf __attribute__ ((unused)) env;
  21. static int result = 0;
  22. #if defined FE_TONEAREST && defined FE_TOWARDZERO
  23. static int expected_rounding_mode = FE_TONEAREST;
  24. static void
  25. change_rounding_mode (void)
  26. {
  27. if (fesetround (FE_TOWARDZERO) == 0)
  28. expected_rounding_mode = FE_TOWARDZERO;
  29. else
  30. puts ("fesetround (FE_TOWARDZERO) failed, continuing test");
  31. longjmp (env, 1);
  32. }
  33. #endif
  34. #ifdef FE_INVALID
  35. static int expected_exceptions = 0;
  36. static void
  37. raise_exception (void)
  38. {
  39. if (feraiseexcept (FE_INVALID) == 0)
  40. expected_exceptions = FE_INVALID;
  41. else
  42. puts ("feraiseexcept (FE_INVALID) failed, continuing test");
  43. longjmp (env, 1);
  44. }
  45. #endif
  46. static int
  47. do_test (void)
  48. {
  49. #if defined FE_TONEAREST && defined FE_TOWARDZERO
  50. if (fesetround (FE_TONEAREST) == 0)
  51. {
  52. if (setjmp (env) == 0)
  53. change_rounding_mode ();
  54. else
  55. {
  56. if (fegetround () == expected_rounding_mode)
  57. puts ("PASS: longjmp preserved rounding mode");
  58. else
  59. {
  60. puts ("FAIL: longjmp changed rounding mode");
  61. result = 1;
  62. }
  63. }
  64. }
  65. else
  66. puts ("fesetround (FE_TONEAREST) failed, not testing rounding modes");
  67. #else
  68. puts ("rounding mode test not supported");
  69. #endif
  70. #ifdef FE_INVALID
  71. if (feclearexcept (FE_ALL_EXCEPT) == 0)
  72. {
  73. if (setjmp (env) == 0)
  74. raise_exception ();
  75. else
  76. {
  77. if (fetestexcept (FE_INVALID) == expected_exceptions)
  78. puts ("PASS: longjmp preserved exceptions");
  79. else
  80. {
  81. puts ("FAIL: longjmp changed exceptions");
  82. result = 1;
  83. }
  84. }
  85. }
  86. else
  87. puts ("feclearexcept (FE_ALL_EXCEPT) failed, not testing exceptions");
  88. #else
  89. puts ("exception test not supported");
  90. #endif
  91. return result;
  92. }
  93. #define TEST_FUNCTION do_test ()
  94. #include "../test-skeleton.c"