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 #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
35 # define L_(literal) literal##L
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
46 # define L_(literal) literal
49 #define EXP_MASK ((MAX_EXP - MIN_EXP) | 7)
52 ((sizeof (DOUBLE) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
53 typedef union { DOUBLE value; unsigned int word[NWORDS]; } memory_double;
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);
66 /* A NaN can be recognized through its exponent. But exclude +Infinity and
67 -Infinity, which have the same exponent. */
69 if (((m.word[EXPBIT0_WORD] ^ nan.word[EXPBIT0_WORD])
70 & (EXP_MASK << EXPBIT0_BIT))
72 return (memcmp (&m.value, &plus_inf, sizeof (DOUBLE)) != 0
73 && memcmp (&m.value, &minus_inf, sizeof (DOUBLE)) != 0);
77 /* The configuration did not find sufficient information. Give up about
78 the signaling NaNs, handle only the quiet NaNs. */