1 /* NaNs and Infinity in floating-point numbers.
2    Copyright (C) 2015-2019 Free Software Foundation, Inc.
3 
4    This file was written by Daiki Ueno <ueno@gnu.org>, 2015.
5 
6    This program is free software: you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10 
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
18 
19 /* Replacement 'trionan.c', using Gnulib functions.  */
20 
21 #include "config.h"
22 #include <float.h>
23 #include <math.h>
24 
25 /* Copied from gnulib/tests/infinity.h.  */
26 
27 /* Infinityd () returns a 'double' +Infinity.  */
28 
29 /* The Microsoft MSVC 9 compiler chokes on the expression 1.0 / 0.0.  */
30 #if defined _MSC_VER
31 static double
Infinityd()32 Infinityd ()
33 {
34   static double zero = 0.0;
35   return 1.0 / zero;
36 }
37 #else
38 # define Infinityd() (1.0 / 0.0)
39 #endif
40 
41 /* Copied from gnulib/tests/nan.h.  */
42 
43 /* NaNd () returns a 'double' not-a-number.  */
44 
45 /* The Compaq (ex-DEC) C 6.4 compiler and the Microsoft MSVC 9 compiler choke
46    on the expression 0.0 / 0.0.  */
47 #if defined __DECC || defined _MSC_VER
48 static double
NaNd()49 NaNd ()
50 {
51   static double zero = 0.0;
52   return zero / zero;
53 }
54 #else
55 # define NaNd() (0.0 / 0.0)
56 #endif
57 
58 /* Copied from gnulib/tests/minus-zero.h.  */
59 
60 /* minus_zerod represents the value -0.0.  */
61 
62 /* HP cc on HP-UX 10.20 has a bug with the constant expression -0.0.
63    ICC 10.0 has a bug when optimizing the expression -zero.
64    The expression -DBL_MIN * DBL_MIN does not work when cross-compiling
65    to PowerPC on Mac OS X 10.5.  */
66 #if defined __hpux || defined __sgi || defined __ICC
67 static double
compute_minus_zerod(void)68 compute_minus_zerod (void)
69 {
70   return -DBL_MIN * DBL_MIN;
71 }
72 # define minus_zerod compute_minus_zerod ()
73 #else
74 static double minus_zerod = -0.0;
75 #endif
76 
77 #undef INFINITY
78 #undef NAN
79 
80 #define INFINITY Infinityd()
81 #define NAN NaNd()
82 
83 #define trio_pinf() INFINITY
84 #define trio_ninf() -INFINITY
85 #define trio_nan() NAN
86 #define trio_nzero() minus_zerod
87 
88 #define trio_isnan(x) isnan(x)
89 #define trio_isinf(x) isinf(x)
90 #define trio_signbit(x) signbit(x)
91