lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* Test that setjmp/longjmp do not save and restore floating-point |
| 2 | exceptions and rounding modes. |
| 3 | Copyright (C) 2013-2015 Free Software Foundation, Inc. |
| 4 | This file is part of the GNU C Library. |
| 5 | |
| 6 | The GNU C Library is free software; you can redistribute it and/or |
| 7 | modify it under the terms of the GNU Lesser General Public |
| 8 | License as published by the Free Software Foundation; either |
| 9 | version 2.1 of the License, or (at your option) any later version. |
| 10 | |
| 11 | The GNU C Library is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | Lesser General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU Lesser General Public |
| 17 | License along with the GNU C Library; if not, see |
| 18 | <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #include <fenv.h> |
| 21 | #include <setjmp.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | |
| 25 | static jmp_buf __attribute__ ((unused)) env; |
| 26 | static int result = 0; |
| 27 | |
| 28 | #if defined FE_TONEAREST && defined FE_TOWARDZERO |
| 29 | static int expected_rounding_mode = FE_TONEAREST; |
| 30 | |
| 31 | static void |
| 32 | change_rounding_mode (void) |
| 33 | { |
| 34 | if (fesetround (FE_TOWARDZERO) == 0) |
| 35 | expected_rounding_mode = FE_TOWARDZERO; |
| 36 | else |
| 37 | puts ("fesetround (FE_TOWARDZERO) failed, continuing test"); |
| 38 | longjmp (env, 1); |
| 39 | } |
| 40 | #endif |
| 41 | |
| 42 | #ifdef FE_INVALID |
| 43 | static int expected_exceptions = 0; |
| 44 | |
| 45 | static void |
| 46 | raise_exception (void) |
| 47 | { |
| 48 | if (feraiseexcept (FE_INVALID) == 0) |
| 49 | expected_exceptions = FE_INVALID; |
| 50 | else |
| 51 | puts ("feraiseexcept (FE_INVALID) failed, continuing test"); |
| 52 | longjmp (env, 1); |
| 53 | } |
| 54 | #endif |
| 55 | |
| 56 | static int |
| 57 | do_test (void) |
| 58 | { |
| 59 | #if defined FE_TONEAREST && defined FE_TOWARDZERO |
| 60 | if (fesetround (FE_TONEAREST) == 0) |
| 61 | { |
| 62 | if (setjmp (env) == 0) |
| 63 | change_rounding_mode (); |
| 64 | else |
| 65 | { |
| 66 | if (fegetround () == expected_rounding_mode) |
| 67 | puts ("PASS: longjmp preserved rounding mode"); |
| 68 | else |
| 69 | { |
| 70 | puts ("FAIL: longjmp changed rounding mode"); |
| 71 | result = 1; |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | else |
| 76 | puts ("fesetround (FE_TONEAREST) failed, not testing rounding modes"); |
| 77 | #else |
| 78 | puts ("rounding mode test not supported"); |
| 79 | #endif |
| 80 | #ifdef FE_INVALID |
| 81 | if (feclearexcept (FE_ALL_EXCEPT) == 0) |
| 82 | { |
| 83 | if (setjmp (env) == 0) |
| 84 | raise_exception (); |
| 85 | else |
| 86 | { |
| 87 | if (fetestexcept (FE_INVALID) == expected_exceptions) |
| 88 | puts ("PASS: longjmp preserved exceptions"); |
| 89 | else |
| 90 | { |
| 91 | puts ("FAIL: longjmp changed exceptions"); |
| 92 | result = 1; |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | else |
| 97 | puts ("feclearexcept (FE_ALL_EXCEPT) failed, not testing exceptions"); |
| 98 | #else |
| 99 | puts ("exception test not supported"); |
| 100 | #endif |
| 101 | return result; |
| 102 | } |
| 103 | |
| 104 | #define TEST_FUNCTION do_test () |
| 105 | #include "../test-skeleton.c" |