Merge isnan and isnanl into a single source file.
[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 #ifdef USE_LONG_DOUBLE
26 # define FUNC rpl_isnanl
27 # define DOUBLE long double
28 # define MAX_EXP LDBL_MAX_EXP
29 # define MIN_EXP LDBL_MIN_EXP
30 # if defined LDBL_EXPBIT0_WORD && defined LDBL_EXPBIT0_BIT
31 #  define KNOWN_EXPBIT0_LOCATION
32 #  define EXPBIT0_WORD LDBL_EXPBIT0_WORD
33 #  define EXPBIT0_BIT LDBL_EXPBIT0_BIT
34 # endif
35 # define L_(literal) literal##L
36 #else
37 # define FUNC rpl_isnan
38 # define DOUBLE double
39 # define MAX_EXP DBL_MAX_EXP
40 # define MIN_EXP DBL_MIN_EXP
41 # if defined DBL_EXPBIT0_WORD && defined DBL_EXPBIT0_BIT
42 #  define KNOWN_EXPBIT0_LOCATION
43 #  define EXPBIT0_WORD DBL_EXPBIT0_WORD
44 #  define EXPBIT0_BIT DBL_EXPBIT0_BIT
45 # endif
46 # define L_(literal) literal
47 #endif
48
49 #define EXP_MASK ((MAX_EXP - MIN_EXP) | 7)
50
51 #define NWORDS \
52   ((sizeof (DOUBLE) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
53 typedef union { DOUBLE value; unsigned int word[NWORDS]; } memory_double;
54
55 int
56 FUNC (DOUBLE x)
57 {
58 #ifdef KNOWN_EXPBIT0_LOCATION
59   /* Be careful to not do any floating-point operation on x, such as x == x,
60      because x may be a signaling NaN.  */
61   static memory_double nan = { L_(0.0) / L_(0.0) };
62   static DOUBLE plus_inf = L_(1.0) / L_(0.0);
63   static DOUBLE minus_inf = -L_(1.0) / L_(0.0);
64   memory_double m;
65
66   /* A NaN can be recognized through its exponent.  But exclude +Infinity and
67      -Infinity, which have the same exponent.  */
68   m.value = x;
69   if (((m.word[EXPBIT0_WORD] ^ nan.word[EXPBIT0_WORD])
70        & (EXP_MASK << EXPBIT0_BIT))
71       == 0)
72     return (memcmp (&m.value, &plus_inf, sizeof (DOUBLE)) != 0
73             && memcmp (&m.value, &minus_inf, sizeof (DOUBLE)) != 0);
74   else
75     return 0;
76 #else
77   /* The configuration did not find sufficient information.  Give up about
78      the signaling NaNs, handle only the quiet NaNs.  */
79   if (x == x)
80     return 0;
81   else
82     return 1;
83 #endif
84 }