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
7Copyright 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 the GNU Lesser General Public License as published by
13the Free Software Foundation; either version 3 of the License, or (at your
14option) any later version.
15
16The GNU MP Library is distributed in the hope that it will be useful, but
17WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
19License for more details.
20
21You should have received a copy of the GNU Lesser General Public License
22along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
23
24#include "config.h"
25
26#include <signal.h>
27#include <stdlib.h>
28
29#if HAVE_UNISTD_H
30#include <unistd.h>  /* for getpid */
31#endif
32
33#include "gmp.h"
34#include "gmp-impl.h"
35
36
37/* Incidentally, kill is not available on mingw, but that's ok, it has raise
38   and we'll be using that.  */
39#if ! HAVE_RAISE
40#define raise(sig)   kill (getpid(), sig)
41#endif
42
43
44/* __gmp_invalid_operation is for an invalid floating point operation, like
45   mpz_set_d on a NaN or Inf.  It's done as a subroutine to minimize code in
46   places raising an exception.
47
48   feraiseexcept(FE_INVALID) is not used here, since unfortunately on most
49   systems it would require libm.
50
51   Alternatives:
52
53   It might be possible to check whether a hardware "invalid operation" trap
54   is enabled or not before raising a signal.  This would require all
55   callers to be prepared to continue with some bogus result.  Bogus returns
56   are bad, but presumably an application disabling the trap is prepared for
57   that.
58
59   On some systems (eg. BSD) the signal handler can find out the reason for
60   a SIGFPE (overflow, invalid, div-by-zero, etc).  Perhaps we could get
61   that into our raise too.
62
63   i386 GLIBC implements feraiseexcept(FE_INVALID) with an asm fdiv 0/0.
64   That would both respect the exceptions mask and give a reason code in a
65   BSD signal.  */
66
67void
68__gmp_invalid_operation (void)
69{
70  raise (SIGFPE);
71  abort ();
72}
73