Recognize non-IEEE numbers on i386, x86_64, ia64.
[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 #elif ! defined USE_FLOAT
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 #else /* defined USE_FLOAT */
52 # define FUNC rpl_isnanf
53 # define DOUBLE float
54 # define MAX_EXP FLT_MAX_EXP
55 # define MIN_EXP FLT_MIN_EXP
56 # if defined FLT_EXPBIT0_WORD && defined FLT_EXPBIT0_BIT
57 #  define KNOWN_EXPBIT0_LOCATION
58 #  define EXPBIT0_WORD FLT_EXPBIT0_WORD
59 #  define EXPBIT0_BIT FLT_EXPBIT0_BIT
60 # endif
61 # define SIZE SIZEOF_FLT
62 # define L_(literal) literal##f
63 #endif
64
65 #define EXP_MASK ((MAX_EXP - MIN_EXP) | 7)
66
67 #define NWORDS \
68   ((sizeof (DOUBLE) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
69 typedef union { DOUBLE value; unsigned int word[NWORDS]; } memory_double;
70
71 int
72 FUNC (DOUBLE x)
73 {
74 #ifdef KNOWN_EXPBIT0_LOCATION
75 # 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_))
76   /* Special CPU dependent code is needed to treat bit patterns outside the
77      IEEE 754 specification (such as Pseudo-NaNs, Pseudo-Infinities,
78      Pseudo-Zeroes, Unnormalized Numbers, and Pseudo-Denormals) as NaNs.
79      These bit patterns are:
80        - exponent = 0x0001..0x7FFF, mantissa bit 63 = 0,
81        - exponent = 0x0000, mantissa bit 63 = 1.
82      The NaN bit pattern is:
83        - exponent = 0x7FFF, mantissa >= 0x8000000000000001.  */
84   memory_double m;
85   unsigned int exponent;
86
87   m.value = x;
88   exponent = (m.word[EXPBIT0_WORD] >> EXPBIT0_BIT) & EXP_MASK;
89 #  ifdef WORDS_BIGENDIAN
90   /* Big endian: EXPBIT0_WORD = 0, EXPBIT0_BIT = 16.  */
91   if (exponent == 0)
92     return 1 & (m.word[0] >> 15);
93   else if (exponent == EXP_MASK)
94     return (((m.word[0] ^ 0x8000U) << 16) | m.word[1] | (m.word[2] >> 16)) != 0;
95   else
96     return 1 & ~(m.word[0] >> 15);
97 #  else
98   /* Little endian: EXPBIT0_WORD = 2, EXPBIT0_BIT = 0.  */
99   if (exponent == 0)
100     return (m.word[1] >> 31);
101   else if (exponent == EXP_MASK)
102     return ((m.word[1] ^ 0x80000000U) | m.word[0]) != 0;
103   else
104     return (m.word[1] >> 31) ^ 1;
105 #  endif
106 # else
107   /* Be careful to not do any floating-point operation on x, such as x == x,
108      because x may be a signaling NaN.  */
109 #  if defined __SUNPRO_C || defined __DECC || (defined __sgi && !defined __GNUC__)
110   /* The Sun C 5.0 compilers and the Compaq (ex-DEC) 6.4 compilers don't
111      recognize the initializers as constant expressions.  The latter compiler
112      also fails when constant-folding 0.0 / 0.0 even when constant-folding is
113      not required.  The SGI MIPSpro C compiler complains about "floating-point
114      operation result is out of range".  */
115   static DOUBLE zero = L_(0.0);
116   memory_double nan;
117   DOUBLE plus_inf = L_(1.0) / L_(0.0);
118   DOUBLE minus_inf = -L_(1.0) / L_(0.0);
119   nan.value = zero / zero;
120 #  else
121   static memory_double nan = { L_(0.0) / L_(0.0) };
122   static DOUBLE plus_inf = L_(1.0) / L_(0.0);
123   static DOUBLE minus_inf = -L_(1.0) / L_(0.0);
124 #  endif
125   {
126     memory_double m;
127
128     /* A NaN can be recognized through its exponent.  But exclude +Infinity and
129        -Infinity, which have the same exponent.  */
130     m.value = x;
131     if (((m.word[EXPBIT0_WORD] ^ nan.word[EXPBIT0_WORD])
132          & (EXP_MASK << EXPBIT0_BIT))
133         == 0)
134       return (memcmp (&m.value, &plus_inf, SIZE) != 0
135               && memcmp (&m.value, &minus_inf, SIZE) != 0);
136     else
137       return 0;
138   }
139 # endif
140 #else
141   /* The configuration did not find sufficient information.  Give up about
142      the signaling NaNs, handle only the quiet NaNs.  */
143   if (x == x)
144     return 0;
145   else
146     return 1;
147 #endif
148 }