use _GL_ATTRIBUTE_CONST and _GL_ATTRIBUTE_PURE
[gnulib.git] / lib / isnan.c
1 /* Test for NaN that does not need libm.
2    Copyright (C) 2007-2011 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 3 of the License, or
7    (at your option) 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
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
18
19 #include <config.h>
20
21 /* Specification.  */
22 #ifdef USE_LONG_DOUBLE
23 /* Specification found in math.h or isnanl-nolibm.h.  */
24 extern int rpl_isnanl (long double x);
25 #elif ! defined USE_FLOAT
26 /* Specification found in math.h or isnand-nolibm.h.  */
27 extern int rpl_isnand (double x);
28 #else /* defined USE_FLOAT */
29 /* Specification found in math.h or isnanf-nolibm.h.  */
30 extern int rpl_isnanf (float x);
31 #endif
32
33 #include <float.h>
34 #include <string.h>
35
36 #include "float+.h"
37
38 #ifdef USE_LONG_DOUBLE
39 # define FUNC rpl_isnanl
40 # define DOUBLE long double
41 # define MAX_EXP LDBL_MAX_EXP
42 # define MIN_EXP LDBL_MIN_EXP
43 # if defined LDBL_EXPBIT0_WORD && defined LDBL_EXPBIT0_BIT
44 #  define KNOWN_EXPBIT0_LOCATION
45 #  define EXPBIT0_WORD LDBL_EXPBIT0_WORD
46 #  define EXPBIT0_BIT LDBL_EXPBIT0_BIT
47 # endif
48 # define SIZE SIZEOF_LDBL
49 # define L_(literal) literal##L
50 #elif ! defined USE_FLOAT
51 # define FUNC rpl_isnand
52 # define DOUBLE double
53 # define MAX_EXP DBL_MAX_EXP
54 # define MIN_EXP DBL_MIN_EXP
55 # if defined DBL_EXPBIT0_WORD && defined DBL_EXPBIT0_BIT
56 #  define KNOWN_EXPBIT0_LOCATION
57 #  define EXPBIT0_WORD DBL_EXPBIT0_WORD
58 #  define EXPBIT0_BIT DBL_EXPBIT0_BIT
59 # endif
60 # define SIZE SIZEOF_DBL
61 # define L_(literal) literal
62 #else /* defined USE_FLOAT */
63 # define FUNC rpl_isnanf
64 # define DOUBLE float
65 # define MAX_EXP FLT_MAX_EXP
66 # define MIN_EXP FLT_MIN_EXP
67 # if defined FLT_EXPBIT0_WORD && defined FLT_EXPBIT0_BIT
68 #  define KNOWN_EXPBIT0_LOCATION
69 #  define EXPBIT0_WORD FLT_EXPBIT0_WORD
70 #  define EXPBIT0_BIT FLT_EXPBIT0_BIT
71 # endif
72 # define SIZE SIZEOF_FLT
73 # define L_(literal) literal##f
74 #endif
75
76 #define EXP_MASK ((MAX_EXP - MIN_EXP) | 7)
77
78 #define NWORDS \
79   ((sizeof (DOUBLE) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
80 typedef union { DOUBLE value; unsigned int word[NWORDS]; } memory_double;
81
82 /* The attribute __const__ was added in gcc 2.95.  */
83 #undef _GL_ATTRIBUTE_CONST
84 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 95)
85 # define _GL_ATTRIBUTE_CONST __attribute__ ((__const__))
86 #else
87 # define _GL_ATTRIBUTE_CONST /* empty */
88 #endif
89
90 int _GL_ATTRIBUTE_CONST
91 FUNC (DOUBLE x)
92 {
93 #ifdef KNOWN_EXPBIT0_LOCATION
94 # if defined USE_LONG_DOUBLE && ((defined __ia64 && LDBL_MANT_DIG == 64) || (defined __x86_64__ || defined __amd64__) || (defined __i386 || defined __i386__ || defined _I386 || defined _M_IX86 || defined _X86_))
95   /* Special CPU dependent code is needed to treat bit patterns outside the
96      IEEE 754 specification (such as Pseudo-NaNs, Pseudo-Infinities,
97      Pseudo-Zeroes, Unnormalized Numbers, and Pseudo-Denormals) as NaNs.
98      These bit patterns are:
99        - exponent = 0x0001..0x7FFF, mantissa bit 63 = 0,
100        - exponent = 0x0000, mantissa bit 63 = 1.
101      The NaN bit pattern is:
102        - exponent = 0x7FFF, mantissa >= 0x8000000000000001.  */
103   memory_double m;
104   unsigned int exponent;
105
106   m.value = x;
107   exponent = (m.word[EXPBIT0_WORD] >> EXPBIT0_BIT) & EXP_MASK;
108 #  ifdef WORDS_BIGENDIAN
109   /* Big endian: EXPBIT0_WORD = 0, EXPBIT0_BIT = 16.  */
110   if (exponent == 0)
111     return 1 & (m.word[0] >> 15);
112   else if (exponent == EXP_MASK)
113     return (((m.word[0] ^ 0x8000U) << 16) | m.word[1] | (m.word[2] >> 16)) != 0;
114   else
115     return 1 & ~(m.word[0] >> 15);
116 #  else
117   /* Little endian: EXPBIT0_WORD = 2, EXPBIT0_BIT = 0.  */
118   if (exponent == 0)
119     return (m.word[1] >> 31);
120   else if (exponent == EXP_MASK)
121     return ((m.word[1] ^ 0x80000000U) | m.word[0]) != 0;
122   else
123     return (m.word[1] >> 31) ^ 1;
124 #  endif
125 # else
126   /* Be careful to not do any floating-point operation on x, such as x == x,
127      because x may be a signaling NaN.  */
128 #  if defined __TINYC__ || defined __SUNPRO_C || defined __DECC \
129       || (defined __sgi && !defined __GNUC__) || defined __ICC
130   /* The Sun C 5.0, Intel ICC 10.0, and Compaq (ex-DEC) 6.4 compilers don't
131      recognize the initializers as constant expressions.  The latter compiler
132      also fails when constant-folding 0.0 / 0.0 even when constant-folding is
133      not required.  The SGI MIPSpro C compiler complains about "floating-point
134      operation result is out of range".  */
135   static DOUBLE zero = L_(0.0);
136   memory_double nan;
137   DOUBLE plus_inf = L_(1.0) / L_(0.0);
138   DOUBLE minus_inf = -L_(1.0) / L_(0.0);
139   nan.value = zero / zero;
140 #  else
141   static memory_double nan = { L_(0.0) / L_(0.0) };
142   static DOUBLE plus_inf = L_(1.0) / L_(0.0);
143   static DOUBLE minus_inf = -L_(1.0) / L_(0.0);
144 #  endif
145   {
146     memory_double m;
147
148     /* A NaN can be recognized through its exponent.  But exclude +Infinity and
149        -Infinity, which have the same exponent.  */
150     m.value = x;
151     if (((m.word[EXPBIT0_WORD] ^ nan.word[EXPBIT0_WORD])
152          & (EXP_MASK << EXPBIT0_BIT))
153         == 0)
154       return (memcmp (&m.value, &plus_inf, SIZE) != 0
155               && memcmp (&m.value, &minus_inf, SIZE) != 0);
156     else
157       return 0;
158   }
159 # endif
160 #else
161   /* The configuration did not find sufficient information.  Give up about
162      the signaling NaNs, handle only the quiet NaNs.  */
163   if (x == x)
164     {
165 # if defined USE_LONG_DOUBLE && ((defined __ia64 && LDBL_MANT_DIG == 64) || (defined __x86_64__ || defined __amd64__) || (defined __i386 || defined __i386__ || defined _I386 || defined _M_IX86 || defined _X86_))
166       /* Detect any special bit patterns that pass ==; see comment above.  */
167       memory_double m1;
168       memory_double m2;
169
170       memset (&m1.value, 0, SIZE);
171       memset (&m2.value, 0, SIZE);
172       m1.value = x;
173       m2.value = x + (x ? 0.0L : -0.0L);
174       if (memcmp (&m1.value, &m2.value, SIZE) != 0)
175         return 1;
176 # endif
177       return 0;
178     }
179   else
180     return 1;
181 #endif
182 }