1 /* Test for NaN that does not need libm.
2 Copyright (C) 2007 Free Software Foundation, Inc.
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)
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.
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. */
18 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
25 #define DBL_EXP_MASK ((DBL_MAX_EXP - DBL_MIN_EXP) | 7)
28 ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
29 typedef union { double value; unsigned int word[NWORDS]; } memory_double;
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;
42 /* A NaN can be recognized through its exponent. But exclude +Infinity and
43 -Infinity, which have the same exponent. */
45 if ((((m.word[DBL_EXPBIT0_WORD] >> DBL_EXPBIT0_BIT)
46 ^ (nan.word[DBL_EXPBIT0_WORD] >> DBL_EXPBIT0_BIT))
49 return (memcmp (&m.value, &plus_inf, sizeof (double)) != 0
50 && memcmp (&m.value, &minus_inf, sizeof (double)) != 0);
54 /* The configuration did not find sufficient information. Give up about
55 the signaling NaNs, handle only the quiet NaNs. */