Work around DEC C 6.4 compiler bug.
[gnulib.git] / lib / isnan.c
1 /* Test for NaN that does not need libm.
2    Copyright (C) 2007 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License along
15    with this program; if not, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
19
20 #include <config.h>
21
22 #include <float.h>
23 #include <string.h>
24
25 #include "float+.h"
26
27 #ifdef USE_LONG_DOUBLE
28 # define FUNC rpl_isnanl
29 # define DOUBLE long double
30 # define MAX_EXP LDBL_MAX_EXP
31 # define MIN_EXP LDBL_MIN_EXP
32 # if defined LDBL_EXPBIT0_WORD && defined LDBL_EXPBIT0_BIT
33 #  define KNOWN_EXPBIT0_LOCATION
34 #  define EXPBIT0_WORD LDBL_EXPBIT0_WORD
35 #  define EXPBIT0_BIT LDBL_EXPBIT0_BIT
36 # endif
37 # define SIZE SIZEOF_LDBL
38 # define L_(literal) literal##L
39 #else
40 # define FUNC rpl_isnan
41 # define DOUBLE double
42 # define MAX_EXP DBL_MAX_EXP
43 # define MIN_EXP DBL_MIN_EXP
44 # if defined DBL_EXPBIT0_WORD && defined DBL_EXPBIT0_BIT
45 #  define KNOWN_EXPBIT0_LOCATION
46 #  define EXPBIT0_WORD DBL_EXPBIT0_WORD
47 #  define EXPBIT0_BIT DBL_EXPBIT0_BIT
48 # endif
49 # define SIZE SIZEOF_DBL
50 # define L_(literal) literal
51 #endif
52
53 #define EXP_MASK ((MAX_EXP - MIN_EXP) | 7)
54
55 #define NWORDS \
56   ((sizeof (DOUBLE) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
57 typedef union { DOUBLE value; unsigned int word[NWORDS]; } memory_double;
58
59 int
60 FUNC (DOUBLE x)
61 {
62 #ifdef KNOWN_EXPBIT0_LOCATION
63   /* Be careful to not do any floating-point operation on x, such as x == x,
64      because x may be a signaling NaN.  */
65 # if defined __SUNPRO_C || defined __DECC
66   /* The Sun C 5.0 compilers and the Compaq (ex-DEC) 6.4 compilers don't
67      recognize the initializers as constant expressions.  The latter compiler
68      also fails when constant-folding 0.0 / 0.0 even when constant-folding is
69      not required.  */
70   static DOUBLE zero = L_(0.0);
71   memory_double nan;
72   DOUBLE plus_inf = L_(1.0) / L_(0.0);
73   DOUBLE minus_inf = -L_(1.0) / L_(0.0);
74   nan.value = zero / zero;
75 # else
76   static memory_double nan = { L_(0.0) / L_(0.0) };
77   static DOUBLE plus_inf = L_(1.0) / L_(0.0);
78   static DOUBLE minus_inf = -L_(1.0) / L_(0.0);
79 # endif
80   {
81     memory_double m;
82
83     /* A NaN can be recognized through its exponent.  But exclude +Infinity and
84        -Infinity, which have the same exponent.  */
85     m.value = x;
86     if (((m.word[EXPBIT0_WORD] ^ nan.word[EXPBIT0_WORD])
87          & (EXP_MASK << EXPBIT0_BIT))
88         == 0)
89       return (memcmp (&m.value, &plus_inf, SIZE) != 0
90               && memcmp (&m.value, &minus_inf, SIZE) != 0);
91     else
92       return 0;
93   }
94 #else
95   /* The configuration did not find sufficient information.  Give up about
96      the signaling NaNs, handle only the quiet NaNs.  */
97   if (x == x)
98     return 0;
99   else
100     return 1;
101 #endif
102 }