1 /* Test of isnand() substitute.
2 Copyright (C) 2007-2010 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 3 of the License, or
7 (at your option) any later version.
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
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by Ben Pfaff <blp@cs.stanford.edu>, from code by Bruno
18 Haible <bruno@clisp.org>. */
24 /* isnan must be a macro. */
26 # error missing declaration
35 /* HP cc on HP-UX 10.20 has a bug with the constant expression -0.0f.
36 So we use -zero instead. */
39 /* HP cc on HP-UX 10.20 has a bug with the constant expression -0.0.
40 So we use -zero instead. */
43 /* On HP-UX 10.20, negating 0.0L does not yield -0.0L.
44 So we use minus_zerol instead.
45 IRIX cc can't put -0.0L into .data, but can compute at runtime.
46 Note that the expression -LDBL_MIN * LDBL_MIN does not work on other
47 platforms, such as when cross-compiling to PowerPC on MacOS X 10.5. */
48 #if defined __hpux || defined __sgi
50 compute_minus_zerol (void)
52 return -LDBL_MIN * LDBL_MIN;
54 # define minus_zerol compute_minus_zerol ()
56 long double minus_zerol = -0.0L;
63 ASSERT (!isnan (3.141f));
64 ASSERT (!isnan (3.141e30f));
65 ASSERT (!isnan (3.141e-30f));
66 ASSERT (!isnan (-2.718f));
67 ASSERT (!isnan (-2.718e30f));
68 ASSERT (!isnan (-2.718e-30f));
69 ASSERT (!isnan (0.0f));
70 ASSERT (!isnan (-zerof));
71 /* Infinite values. */
72 ASSERT (!isnan (1.0f / 0.0f));
73 ASSERT (!isnan (-1.0f / 0.0f));
75 ASSERT (isnan (NaNf ()));
76 #if defined FLT_EXPBIT0_WORD && defined FLT_EXPBIT0_BIT
80 ((sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
81 typedef union { float value; unsigned int word[NWORDSF]; } memory_float;
84 # if FLT_EXPBIT0_BIT > 0
85 m.word[FLT_EXPBIT0_WORD] ^= (unsigned int) 1 << (FLT_EXPBIT0_BIT - 1);
87 m.word[FLT_EXPBIT0_WORD + (FLT_EXPBIT0_WORD < NWORDSF / 2 ? 1 : - 1)]
88 ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
90 if (FLT_EXPBIT0_WORD < NWORDSF / 2)
91 m.word[FLT_EXPBIT0_WORD + 1] |= (unsigned int) 1 << FLT_EXPBIT0_BIT;
93 m.word[0] |= (unsigned int) 1;
94 ASSERT (isnan (m.value));
103 ASSERT (!isnan (3.141));
104 ASSERT (!isnan (3.141e30));
105 ASSERT (!isnan (3.141e-30));
106 ASSERT (!isnan (-2.718));
107 ASSERT (!isnan (-2.718e30));
108 ASSERT (!isnan (-2.718e-30));
109 ASSERT (!isnan (0.0));
110 ASSERT (!isnan (-zerod));
111 /* Infinite values. */
112 ASSERT (!isnan (1.0 / 0.0));
113 ASSERT (!isnan (-1.0 / 0.0));
115 ASSERT (isnan (NaNd ()));
116 #if defined DBL_EXPBIT0_WORD && defined DBL_EXPBIT0_BIT
117 /* Signalling NaN. */
120 ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
121 typedef union { double value; unsigned int word[NWORDSD]; } memory_double;
124 # if DBL_EXPBIT0_BIT > 0
125 m.word[DBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (DBL_EXPBIT0_BIT - 1);
127 m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDSD / 2 ? 1 : - 1)]
128 ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
130 m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDSD / 2 ? 1 : - 1)]
131 |= (unsigned int) 1 << DBL_EXPBIT0_BIT;
132 ASSERT (isnan (m.value));
138 test_long_double (void)
141 ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
142 typedef union { unsigned int word[NWORDSL]; long double value; }
146 ASSERT (!isnan (3.141L));
147 ASSERT (!isnan (3.141e30L));
148 ASSERT (!isnan (3.141e-30L));
149 ASSERT (!isnan (-2.718L));
150 ASSERT (!isnan (-2.718e30L));
151 ASSERT (!isnan (-2.718e-30L));
152 ASSERT (!isnan (0.0L));
153 ASSERT (!isnan (minus_zerol));
154 /* Infinite values. */
155 ASSERT (!isnan (1.0L / 0.0L));
156 ASSERT (!isnan (-1.0L / 0.0L));
158 ASSERT (isnan (NaNl ()));
160 #if defined LDBL_EXPBIT0_WORD && defined LDBL_EXPBIT0_BIT
161 /* A bit pattern that is different from a Quiet NaN. With a bit of luck,
162 it's a Signalling NaN. */
164 memory_long_double m;
166 # if LDBL_EXPBIT0_BIT > 0
167 m.word[LDBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (LDBL_EXPBIT0_BIT - 1);
169 m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDSL / 2 ? 1 : - 1)]
170 ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
172 m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDSL / 2 ? 1 : - 1)]
173 |= (unsigned int) 1 << LDBL_EXPBIT0_BIT;
174 ASSERT (isnan (m.value));
178 #if ((defined __ia64 && LDBL_MANT_DIG == 64) || (defined __x86_64__ || defined __amd64__) || (defined __i386 || defined __i386__ || defined _I386 || defined _M_IX86 || defined _X86_))
179 /* Representation of an 80-bit 'long double' as an initializer for a sequence
180 of 'unsigned int' words. */
181 # ifdef WORDS_BIGENDIAN
182 # define LDBL80_WORDS(exponent,manthi,mantlo) \
183 { ((unsigned int) (exponent) << 16) | ((unsigned int) (manthi) >> 16), \
184 ((unsigned int) (manthi) << 16) | (unsigned int) (mantlo) >> 16), \
185 (unsigned int) (mantlo) << 16 \
188 # define LDBL80_WORDS(exponent,manthi,mantlo) \
189 { mantlo, manthi, exponent }
192 static memory_long_double x =
193 { LDBL80_WORDS (0xFFFF, 0xC3333333, 0x00000000) };
194 ASSERT (isnan (x.value));
197 /* Signalling NaN. */
198 static memory_long_double x =
199 { LDBL80_WORDS (0xFFFF, 0x83333333, 0x00000000) };
200 ASSERT (isnan (x.value));
202 /* The isnan function should recognize Pseudo-NaNs, Pseudo-Infinities,
203 Pseudo-Zeroes, Unnormalized Numbers, and Pseudo-Denormals, as defined in
204 Intel IA-64 Architecture Software Developer's Manual, Volume 1:
205 Application Architecture.
206 Table 5-2 "Floating-Point Register Encodings"
207 Figure 5-6 "Memory to Floating-Point Register Data Translation"
210 static memory_long_double x =
211 { LDBL80_WORDS (0xFFFF, 0x40000001, 0x00000000) };
212 ASSERT (isnan (x.value));
214 { /* Pseudo-Infinity. */
215 static memory_long_double x =
216 { LDBL80_WORDS (0xFFFF, 0x00000000, 0x00000000) };
217 ASSERT (isnan (x.value));
220 static memory_long_double x =
221 { LDBL80_WORDS (0x4004, 0x00000000, 0x00000000) };
222 ASSERT (isnan (x.value));
224 { /* Unnormalized number. */
225 static memory_long_double x =
226 { LDBL80_WORDS (0x4000, 0x63333333, 0x00000000) };
227 ASSERT (isnan (x.value));
229 { /* Pseudo-Denormal. */
230 static memory_long_double x =
231 { LDBL80_WORDS (0x0000, 0x83333333, 0x00000000) };
232 ASSERT (isnan (x.value));