Austin Schuh | bb1338c | 2024-06-15 19:31:16 -0700 | [diff] [blame] | 1 | /* gmp_errno, __gmp_exception -- exception handling and reporting. |
| 2 | |
| 3 | THE FUNCTIONS IN THIS FILE, APART FROM gmp_errno, ARE FOR INTERNAL USE |
| 4 | ONLY. THEY'RE ALMOST CERTAIN TO BE SUBJECT TO INCOMPATIBLE CHANGES OR |
| 5 | DISAPPEAR COMPLETELY IN FUTURE GNU MP RELEASES. |
| 6 | |
| 7 | Copyright 2000, 2001, 2003 Free Software Foundation, Inc. |
| 8 | |
| 9 | This file is part of the GNU MP Library. |
| 10 | |
| 11 | The GNU MP Library is free software; you can redistribute it and/or modify |
| 12 | it under the terms of either: |
| 13 | |
| 14 | * the GNU Lesser General Public License as published by the Free |
| 15 | Software Foundation; either version 3 of the License, or (at your |
| 16 | option) any later version. |
| 17 | |
| 18 | or |
| 19 | |
| 20 | * the GNU General Public License as published by the Free Software |
| 21 | Foundation; either version 2 of the License, or (at your option) any |
| 22 | later version. |
| 23 | |
| 24 | or both in parallel, as here. |
| 25 | |
| 26 | The GNU MP Library is distributed in the hope that it will be useful, but |
| 27 | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 28 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 29 | for more details. |
| 30 | |
| 31 | You should have received copies of the GNU General Public License and the |
| 32 | GNU Lesser General Public License along with the GNU MP Library. If not, |
| 33 | see https://www.gnu.org/licenses/. */ |
| 34 | |
| 35 | #include <stdlib.h> |
| 36 | |
| 37 | #include <signal.h> |
| 38 | |
| 39 | #include "gmp-impl.h" |
| 40 | |
| 41 | int gmp_errno = 0; |
| 42 | |
| 43 | |
| 44 | /* Use SIGFPE on systems which have it. Otherwise, deliberate divide |
| 45 | by zero, which triggers an exception on most systems. On those |
| 46 | where it doesn't, for example power and powerpc, use abort instead. */ |
| 47 | void |
| 48 | __gmp_exception (int error_bit) |
| 49 | { |
| 50 | gmp_errno |= error_bit; |
| 51 | #ifdef SIGFPE |
| 52 | raise (SIGFPE); |
| 53 | #else |
| 54 | __gmp_junk = 10 / __gmp_0; |
| 55 | #endif |
| 56 | abort (); |
| 57 | } |
| 58 | |
| 59 | |
| 60 | /* These functions minimize the amount of code required in functions raising |
| 61 | exceptions. Since they're "noreturn" and don't take any parameters, a |
| 62 | test and call might even come out as a simple conditional jump. */ |
| 63 | void |
| 64 | __gmp_sqrt_of_negative (void) |
| 65 | { |
| 66 | __gmp_exception (GMP_ERROR_SQRT_OF_NEGATIVE); |
| 67 | } |
| 68 | void |
| 69 | __gmp_divide_by_zero (void) |
| 70 | { |
| 71 | __gmp_exception (GMP_ERROR_DIVISION_BY_ZERO); |
| 72 | } |