1 /* Test of isfinite() 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, 2007, using Bruno Haible's code as a
29 #define ASSERT(expr) \
34 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
43 long double zerol = 0.0L;
49 ASSERT (isfinite (0.0f));
50 /* Subnormal values. */
51 ASSERT (isfinite (FLT_MIN / 2));
52 ASSERT (isfinite (-FLT_MIN / 2));
54 ASSERT (isfinite (3.141f));
55 ASSERT (isfinite (3.141e30f));
56 ASSERT (isfinite (3.141e-30f));
57 ASSERT (isfinite (-2.718f));
58 ASSERT (isfinite (-2.718e30f));
59 ASSERT (isfinite (-2.718e-30f));
60 /* Infinite values. */
61 ASSERT (!isfinite (1.0f / 0.0f));
62 ASSERT (!isfinite (-1.0f / 0.0f));
64 ASSERT (!isfinite (zerof / zerof));
65 #if defined FLT_EXPBIT0_WORD && defined FLT_EXPBIT0_BIT
69 ((sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
70 typedef union { float value; unsigned int word[NWORDS]; } memory_float;
72 m.value = zerof / zerof;
73 # if FLT_EXPBIT0_BIT > 0
74 m.word[FLT_EXPBIT0_WORD] ^= (unsigned int) 1 << (FLT_EXPBIT0_BIT - 1);
76 m.word[FLT_EXPBIT0_WORD + (FLT_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
77 ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
79 if (FLT_EXPBIT0_WORD < NWORDS / 2)
80 m.word[FLT_EXPBIT0_WORD + 1] |= (unsigned int) 1 << FLT_EXPBIT0_BIT;
82 m.word[0] |= (unsigned int) 1;
83 ASSERT (!isfinite (m.value));
93 ASSERT (isfinite (0.0));
94 /* Subnormal values. */
95 ASSERT (isfinite (DBL_MIN / 2));
96 ASSERT (isfinite (-DBL_MIN / 2));
98 ASSERT (isfinite (3.141));
99 ASSERT (isfinite (3.141e30));
100 ASSERT (isfinite (3.141e-30));
101 ASSERT (isfinite (-2.718));
102 ASSERT (isfinite (-2.718e30));
103 ASSERT (isfinite (-2.718e-30));
104 /* Infinite values. */
105 ASSERT (!isfinite (1.0 / 0.0));
106 ASSERT (!isfinite (-1.0 / 0.0));
108 ASSERT (!isfinite (zerod / zerod));
109 #if defined DBL_EXPBIT0_WORD && defined DBL_EXPBIT0_BIT
110 /* Signalling NaN. */
113 ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
114 typedef union { double value; unsigned int word[NWORDS]; } memory_double;
116 m.value = zerod / zerod;
117 # if DBL_EXPBIT0_BIT > 0
118 m.word[DBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (DBL_EXPBIT0_BIT - 1);
120 m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
121 ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
123 m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
124 |= (unsigned int) 1 << DBL_EXPBIT0_BIT;
125 ASSERT (!isfinite (m.value));
135 ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
136 typedef union { unsigned int word[NWORDS]; long double value; }
140 ASSERT (isfinite (0.0L));
141 /* Subnormal values. */
142 ASSERT (isfinite (LDBL_MIN / 2));
143 ASSERT (isfinite (-LDBL_MIN / 2));
145 ASSERT (isfinite (3.141L));
146 ASSERT (isfinite (3.141e30L));
147 ASSERT (isfinite (3.141e-30L));
148 ASSERT (isfinite (-2.718L));
149 ASSERT (isfinite (-2.718e30L));
150 ASSERT (isfinite (-2.718e-30L));
151 /* Infinite values. */
152 ASSERT (!isfinite (1.0L / 0.0L));
153 ASSERT (!isfinite (-1.0L / 0.0L));
155 ASSERT (!isfinite (zerol / zerol));
157 #if defined LDBL_EXPBIT0_WORD && defined LDBL_EXPBIT0_BIT
158 /* A bit pattern that is different from a Quiet NaN. With a bit of luck,
159 it's a Signalling NaN. */
161 memory_long_double m;
162 m.value = zerol / zerol;
163 # if LDBL_EXPBIT0_BIT > 0
164 m.word[LDBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (LDBL_EXPBIT0_BIT - 1);
166 m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
167 ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
169 m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
170 |= (unsigned int) 1 << LDBL_EXPBIT0_BIT;
171 ASSERT (!isfinite (m.value));
175 #if ((defined __ia64 && LDBL_MANT_DIG == 64) || (defined __x86_64__ || defined __amd64__) || (defined __i386 || defined __i386__ || defined _I386 || defined _M_IX86 || defined _X86_))
176 /* Representation of an 80-bit 'long double' as an initializer for a sequence
177 of 'unsigned int' words. */
178 # ifdef WORDS_BIGENDIAN
179 # define LDBL80_WORDS(exponent,manthi,mantlo) \
180 { ((unsigned int) (exponent) << 16) | ((unsigned int) (manthi) >> 16), \
181 ((unsigned int) (manthi) << 16) | (unsigned int) (mantlo) >> 16), \
182 (unsigned int) (mantlo) << 16 \
185 # define LDBL80_WORDS(exponent,manthi,mantlo) \
186 { mantlo, manthi, exponent }
189 static memory_long_double x =
190 { LDBL80_WORDS (0xFFFF, 0xC3333333, 0x00000000) };
191 ASSERT (!isfinite (x.value));
194 /* Signalling NaN. */
195 static memory_long_double x =
196 { LDBL80_WORDS (0xFFFF, 0x83333333, 0x00000000) };
197 ASSERT (!isfinite (x.value));
199 /* The isnanl function should recognize Pseudo-NaNs, Pseudo-Infinities,
200 Pseudo-Zeroes, Unnormalized Numbers, and Pseudo-Denormals, as defined in
201 Intel IA-64 Architecture Software Developer's Manual, Volume 1:
202 Application Architecture.
203 Table 5-2 "Floating-Point Register Encodings"
204 Figure 5-6 "Memory to Floating-Point Register Data Translation"
207 static memory_long_double x =
208 { LDBL80_WORDS (0xFFFF, 0x40000001, 0x00000000) };
209 ASSERT (!isfinite (x.value));
211 { /* Pseudo-Infinity. */
212 static memory_long_double x =
213 { LDBL80_WORDS (0xFFFF, 0x00000000, 0x00000000) };
214 ASSERT (!isfinite (x.value));
217 static memory_long_double x =
218 { LDBL80_WORDS (0x4004, 0x00000000, 0x00000000) };
219 ASSERT (!isfinite (x.value));
221 { /* Unnormalized number. */
222 static memory_long_double x =
223 { LDBL80_WORDS (0x4000, 0x63333333, 0x00000000) };
224 ASSERT (!isfinite (x.value));
226 { /* Pseudo-Denormal. */
227 static memory_long_double x =
228 { LDBL80_WORDS (0x0000, 0x83333333, 0x00000000) };
229 ASSERT (!isfinite (x.value));