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