blob: b4be555018bc285780ab7675cacbaaeaf611e52d [file] [log] [blame]
Austin Schuhbb1338c2024-06-15 19:31:16 -07001/* 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
7Copyright 2000, 2001, 2003 Free Software Foundation, Inc.
8
9This file is part of the GNU MP Library.
10
11The GNU MP Library is free software; you can redistribute it and/or modify
12it 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
18or
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
24or both in parallel, as here.
25
26The GNU MP Library is distributed in the hope that it will be useful, but
27WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
28or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
29for more details.
30
31You should have received copies of the GNU General Public License and the
32GNU Lesser General Public License along with the GNU MP Library. If not,
33see https://www.gnu.org/licenses/. */
34
35#include <stdlib.h>
36
37#include <signal.h>
38
39#include "gmp-impl.h"
40
41int 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. */
47void
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. */
63void
64__gmp_sqrt_of_negative (void)
65{
66 __gmp_exception (GMP_ERROR_SQRT_OF_NEGATIVE);
67}
68void
69__gmp_divide_by_zero (void)
70{
71 __gmp_exception (GMP_ERROR_DIVISION_BY_ZERO);
72}