New module 'isnan-nolibm'.
[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 #define DBL_EXP_MASK ((DBL_MAX_EXP - DBL_MIN_EXP) | 7)
26
27 #define NWORDS \
28   ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
29 typedef union { double value; unsigned int word[NWORDS]; } memory_double;
30
31 int
32 rpl_isnan (double x)
33 {
34 #if defined DBL_EXPBIT0_WORD && defined DBL_EXPBIT0_BIT
35   /* Be careful to not do any floating-point operation on x, such as x == x,
36      because x may be a signaling NaN.  */
37   static memory_double nan = { 0.0 / 0.0 };
38   static double plus_inf = 1.0 / 0.0;
39   static double minus_inf = -1.0 / 0.0;
40   memory_double m;
41
42   /* A NaN can be recognized through its exponent.  But exclude +Infinity and
43      -Infinity, which have the same exponent.  */
44   m.value = x;
45   if ((((m.word[DBL_EXPBIT0_WORD] >> DBL_EXPBIT0_BIT)
46         ^ (nan.word[DBL_EXPBIT0_WORD] >> DBL_EXPBIT0_BIT))
47        & DBL_EXP_MASK)
48       == 0)
49     return (memcmp (&m.value, &plus_inf, sizeof (double)) != 0
50             && memcmp (&m.value, &minus_inf, sizeof (double)) != 0);
51   else
52     return 0;
53 #else
54   /* The configuration did not find sufficient information.  Give up about
55      the signaling NaNs, handle only the quiet NaNs.  */
56   if (x == x)
57     return 0;
58   else
59     return 1;
60 #endif
61 }