xref: /netbsd-src/external/lgpl3/gmp/dist/invalid.c (revision 72c7faa4dbb41dbb0238d6b4a109da0d4b236dd4)
1 /* __gmp_invalid_operation -- invalid floating point operation.
2 
3    THE FUNCTIONS IN THIS FILE ARE FOR INTERNAL USE ONLY.  THEY'RE ALMOST
4    CERTAIN TO BE SUBJECT TO INCOMPATIBLE CHANGES OR DISAPPEAR COMPLETELY IN
5    FUTURE GNU MP RELEASES.
6 
7 Copyright 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 "config.h"
36 
37 #include <signal.h>
38 #include <stdlib.h>
39 
40 #if HAVE_UNISTD_H
41 #include <unistd.h>  /* for getpid */
42 #endif
43 
44 #include "gmp-impl.h"
45 
46 
47 /* Incidentally, kill is not available on mingw, but that's ok, it has raise
48    and we'll be using that.  */
49 #if ! HAVE_RAISE
50 #define raise(sig)   kill (getpid(), sig)
51 #endif
52 
53 
54 /* __gmp_invalid_operation is for an invalid floating point operation, like
55    mpz_set_d on a NaN or Inf.  It's done as a subroutine to minimize code in
56    places raising an exception.
57 
58    feraiseexcept(FE_INVALID) is not used here, since unfortunately on most
59    systems it would require libm.
60 
61    Alternatives:
62 
63    It might be possible to check whether a hardware "invalid operation" trap
64    is enabled or not before raising a signal.  This would require all
65    callers to be prepared to continue with some bogus result.  Bogus returns
66    are bad, but presumably an application disabling the trap is prepared for
67    that.
68 
69    On some systems (eg. BSD) the signal handler can find out the reason for
70    a SIGFPE (overflow, invalid, div-by-zero, etc).  Perhaps we could get
71    that into our raise too.
72 
73    i386 GLIBC implements feraiseexcept(FE_INVALID) with an asm fdiv 0/0.
74    That would both respect the exceptions mask and give a reason code in a
75    BSD signal.  */
76 
77 void
__gmp_invalid_operation(void)78 __gmp_invalid_operation (void)
79 {
80   raise (SIGFPE);
81   abort ();
82 }
83