xref: /netbsd-src/external/lgpl3/gmp/dist/invalid.c (revision eceb233b9bd0dfebb902ed73b531ae6964fa3f9b)
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.h"
45 #include "gmp-impl.h"
46 
47 
48 /* Incidentally, kill is not available on mingw, but that's ok, it has raise
49    and we'll be using that.  */
50 #if ! HAVE_RAISE
51 #define raise(sig)   kill (getpid(), sig)
52 #endif
53 
54 
55 /* __gmp_invalid_operation is for an invalid floating point operation, like
56    mpz_set_d on a NaN or Inf.  It's done as a subroutine to minimize code in
57    places raising an exception.
58 
59    feraiseexcept(FE_INVALID) is not used here, since unfortunately on most
60    systems it would require libm.
61 
62    Alternatives:
63 
64    It might be possible to check whether a hardware "invalid operation" trap
65    is enabled or not before raising a signal.  This would require all
66    callers to be prepared to continue with some bogus result.  Bogus returns
67    are bad, but presumably an application disabling the trap is prepared for
68    that.
69 
70    On some systems (eg. BSD) the signal handler can find out the reason for
71    a SIGFPE (overflow, invalid, div-by-zero, etc).  Perhaps we could get
72    that into our raise too.
73 
74    i386 GLIBC implements feraiseexcept(FE_INVALID) with an asm fdiv 0/0.
75    That would both respect the exceptions mask and give a reason code in a
76    BSD signal.  */
77 
78 void
79 __gmp_invalid_operation (void)
80 {
81   raise (SIGFPE);
82   abort ();
83 }
84