1 /* Test of isnand() substitute.
2 Copyright (C) 2007-2008 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>. */
31 #define ASSERT(expr) \
36 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
43 /* HP cc on HP-UX 10.20 has a bug with the constant expression -0.0f.
44 So we use -zero instead. */
47 /* HP cc on HP-UX 10.20 has a bug with the constant expression -0.0.
48 So we use -zero instead. */
51 /* HP cc on HP-UX 10.20 has a bug with the constant expression -0.0L.
52 So we use -zero instead. */
53 long double zerol = 0.0L;
59 ASSERT (!isnan (3.141f));
60 ASSERT (!isnan (3.141e30f));
61 ASSERT (!isnan (3.141e-30f));
62 ASSERT (!isnan (-2.718f));
63 ASSERT (!isnan (-2.718e30f));
64 ASSERT (!isnan (-2.718e-30f));
65 ASSERT (!isnan (0.0f));
66 ASSERT (!isnan (-zerof));
67 /* Infinite values. */
68 ASSERT (!isnan (1.0f / 0.0f));
69 ASSERT (!isnan (-1.0f / 0.0f));
71 ASSERT (isnan (NaNf ()));
72 #if defined FLT_EXPBIT0_WORD && defined FLT_EXPBIT0_BIT
76 ((sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
77 typedef union { float value; unsigned int word[NWORDSF]; } memory_float;
80 # if FLT_EXPBIT0_BIT > 0
81 m.word[FLT_EXPBIT0_WORD] ^= (unsigned int) 1 << (FLT_EXPBIT0_BIT - 1);
83 m.word[FLT_EXPBIT0_WORD + (FLT_EXPBIT0_WORD < NWORDSF / 2 ? 1 : - 1)]
84 ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
86 if (FLT_EXPBIT0_WORD < NWORDSF / 2)
87 m.word[FLT_EXPBIT0_WORD + 1] |= (unsigned int) 1 << FLT_EXPBIT0_BIT;
89 m.word[0] |= (unsigned int) 1;
90 ASSERT (isnan (m.value));
99 ASSERT (!isnan (3.141));
100 ASSERT (!isnan (3.141e30));
101 ASSERT (!isnan (3.141e-30));
102 ASSERT (!isnan (-2.718));
103 ASSERT (!isnan (-2.718e30));
104 ASSERT (!isnan (-2.718e-30));
105 ASSERT (!isnan (0.0));
106 ASSERT (!isnan (-zerod));
107 /* Infinite values. */
108 ASSERT (!isnan (1.0 / 0.0));
109 ASSERT (!isnan (-1.0 / 0.0));
111 ASSERT (isnan (NaNd ()));
112 #if defined DBL_EXPBIT0_WORD && defined DBL_EXPBIT0_BIT
113 /* Signalling NaN. */
116 ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
117 typedef union { double value; unsigned int word[NWORDSD]; } memory_double;
120 # if DBL_EXPBIT0_BIT > 0
121 m.word[DBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (DBL_EXPBIT0_BIT - 1);
123 m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDSD / 2 ? 1 : - 1)]
124 ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
126 m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDSD / 2 ? 1 : - 1)]
127 |= (unsigned int) 1 << DBL_EXPBIT0_BIT;
128 ASSERT (isnan (m.value));
134 test_long_double (void)
137 ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
138 typedef union { unsigned int word[NWORDSL]; long double value; }
142 ASSERT (!isnan (3.141L));
143 ASSERT (!isnan (3.141e30L));
144 ASSERT (!isnan (3.141e-30L));
145 ASSERT (!isnan (-2.718L));
146 ASSERT (!isnan (-2.718e30L));
147 ASSERT (!isnan (-2.718e-30L));
148 ASSERT (!isnan (0.0L));
149 ASSERT (!isnan (-zerol));
150 /* Infinite values. */
151 ASSERT (!isnan (1.0L / 0.0L));
152 ASSERT (!isnan (-1.0L / 0.0L));
154 ASSERT (isnan (0.0L / 0.0L));
156 #if defined LDBL_EXPBIT0_WORD && defined LDBL_EXPBIT0_BIT
157 /* A bit pattern that is different from a Quiet NaN. With a bit of luck,
158 it's a Signalling NaN. */
160 memory_long_double m;
161 m.value = 0.0L / 0.0L;
162 # if LDBL_EXPBIT0_BIT > 0
163 m.word[LDBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (LDBL_EXPBIT0_BIT - 1);
165 m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDSL / 2 ? 1 : - 1)]
166 ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
168 m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDSL / 2 ? 1 : - 1)]
169 |= (unsigned int) 1 << LDBL_EXPBIT0_BIT;
170 ASSERT (isnan (m.value));
174 #if ((defined __ia64 && LDBL_MANT_DIG == 64) || (defined __x86_64__ || defined __amd64__) || (defined __i386 || defined __i386__ || defined _I386 || defined _M_IX86 || defined _X86_))
175 /* Representation of an 80-bit 'long double' as an initializer for a sequence
176 of 'unsigned int' words. */
177 # ifdef WORDS_BIGENDIAN
178 # define LDBL80_WORDS(exponent,manthi,mantlo) \
179 { ((unsigned int) (exponent) << 16) | ((unsigned int) (manthi) >> 16), \
180 ((unsigned int) (manthi) << 16) | (unsigned int) (mantlo) >> 16), \
181 (unsigned int) (mantlo) << 16 \
184 # define LDBL80_WORDS(exponent,manthi,mantlo) \
185 { mantlo, manthi, exponent }
188 static memory_long_double x =
189 { LDBL80_WORDS (0xFFFF, 0xC3333333, 0x00000000) };
190 ASSERT (isnan (x.value));
193 /* Signalling NaN. */
194 static memory_long_double x =
195 { LDBL80_WORDS (0xFFFF, 0x83333333, 0x00000000) };
196 ASSERT (isnan (x.value));
198 /* The isnan function should recognize Pseudo-NaNs, Pseudo-Infinities,
199 Pseudo-Zeroes, Unnormalized Numbers, and Pseudo-Denormals, as defined in
200 Intel IA-64 Architecture Software Developer's Manual, Volume 1:
201 Application Architecture.
202 Table 5-2 "Floating-Point Register Encodings"
203 Figure 5-6 "Memory to Floating-Point Register Data Translation"
206 static memory_long_double x =
207 { LDBL80_WORDS (0xFFFF, 0x40000001, 0x00000000) };
208 ASSERT (isnan (x.value));
210 { /* Pseudo-Infinity. */
211 static memory_long_double x =
212 { LDBL80_WORDS (0xFFFF, 0x00000000, 0x00000000) };
213 ASSERT (isnan (x.value));
216 static memory_long_double x =
217 { LDBL80_WORDS (0x4004, 0x00000000, 0x00000000) };
218 ASSERT (isnan (x.value));
220 { /* Unnormalized number. */
221 static memory_long_double x =
222 { LDBL80_WORDS (0x4000, 0x63333333, 0x00000000) };
223 ASSERT (isnan (x.value));
225 { /* Pseudo-Denormal. */
226 static memory_long_double x =
227 { LDBL80_WORDS (0x0000, 0x83333333, 0x00000000) };
228 ASSERT (isnan (x.value));