ignore-value: remove dependency on stdint
[gnulib.git] / tests / test-isfinite.c
blobed87ccf97e88f9942e9253ef132b5d31af41d750
1 /* Test of isfinite() substitute.
2 Copyright (C) 2007-2011 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
18 template. */
20 #include <config.h>
22 #include <math.h>
24 /* isfinite must be a macro. */
25 #ifndef isfinite
26 # error missing declaration
27 #endif
29 #include <float.h>
30 #include <limits.h>
32 #include "macros.h"
34 float zerof = 0.0f;
35 double zerod = 0.0;
36 long double zerol = 0.0L;
38 static void
39 test_isfinitef ()
41 /* Zero. */
42 ASSERT (isfinite (0.0f));
43 /* Subnormal values. */
44 ASSERT (isfinite (FLT_MIN / 2));
45 ASSERT (isfinite (-FLT_MIN / 2));
46 /* Finite values. */
47 ASSERT (isfinite (3.141f));
48 ASSERT (isfinite (3.141e30f));
49 ASSERT (isfinite (3.141e-30f));
50 ASSERT (isfinite (-2.718f));
51 ASSERT (isfinite (-2.718e30f));
52 ASSERT (isfinite (-2.718e-30f));
53 /* Infinite values. */
54 ASSERT (!isfinite (1.0f / 0.0f));
55 ASSERT (!isfinite (-1.0f / 0.0f));
56 /* Quiet NaN. */
57 ASSERT (!isfinite (zerof / zerof));
58 #if defined FLT_EXPBIT0_WORD && defined FLT_EXPBIT0_BIT
59 /* Signalling NaN. */
61 #define NWORDS \
62 ((sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
63 typedef union { float value; unsigned int word[NWORDS]; } memory_float;
64 memory_float m;
65 m.value = zerof / zerof;
66 # if FLT_EXPBIT0_BIT > 0
67 m.word[FLT_EXPBIT0_WORD] ^= (unsigned int) 1 << (FLT_EXPBIT0_BIT - 1);
68 # else
69 m.word[FLT_EXPBIT0_WORD + (FLT_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
70 ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
71 # endif
72 if (FLT_EXPBIT0_WORD < NWORDS / 2)
73 m.word[FLT_EXPBIT0_WORD + 1] |= (unsigned int) 1 << FLT_EXPBIT0_BIT;
74 else
75 m.word[0] |= (unsigned int) 1;
76 ASSERT (!isfinite (m.value));
77 #undef NWORDS
79 #endif
82 static void
83 test_isfinited ()
85 /* Zero. */
86 ASSERT (isfinite (0.0));
87 /* Subnormal values. */
88 ASSERT (isfinite (DBL_MIN / 2));
89 ASSERT (isfinite (-DBL_MIN / 2));
90 /* Finite values. */
91 ASSERT (isfinite (3.141));
92 ASSERT (isfinite (3.141e30));
93 ASSERT (isfinite (3.141e-30));
94 ASSERT (isfinite (-2.718));
95 ASSERT (isfinite (-2.718e30));
96 ASSERT (isfinite (-2.718e-30));
97 /* Infinite values. */
98 ASSERT (!isfinite (1.0 / 0.0));
99 ASSERT (!isfinite (-1.0 / 0.0));
100 /* Quiet NaN. */
101 ASSERT (!isfinite (zerod / zerod));
102 #if defined DBL_EXPBIT0_WORD && defined DBL_EXPBIT0_BIT
103 /* Signalling NaN. */
105 #define NWORDS \
106 ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
107 typedef union { double value; unsigned int word[NWORDS]; } memory_double;
108 memory_double m;
109 m.value = zerod / zerod;
110 # if DBL_EXPBIT0_BIT > 0
111 m.word[DBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (DBL_EXPBIT0_BIT - 1);
112 # else
113 m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
114 ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
115 # endif
116 m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
117 |= (unsigned int) 1 << DBL_EXPBIT0_BIT;
118 ASSERT (!isfinite (m.value));
119 #undef NWORDS
121 #endif
124 static void
125 test_isfinitel ()
127 #define NWORDS \
128 ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
129 typedef union { unsigned int word[NWORDS]; long double value; }
130 memory_long_double;
132 /* Zero. */
133 ASSERT (isfinite (0.0L));
134 /* Subnormal values. */
135 ASSERT (isfinite (LDBL_MIN / 2));
136 ASSERT (isfinite (-LDBL_MIN / 2));
137 /* Finite values. */
138 ASSERT (isfinite (3.141L));
139 ASSERT (isfinite (3.141e30L));
140 ASSERT (isfinite (3.141e-30L));
141 ASSERT (isfinite (-2.718L));
142 ASSERT (isfinite (-2.718e30L));
143 ASSERT (isfinite (-2.718e-30L));
144 /* Infinite values. */
145 ASSERT (!isfinite (1.0L / 0.0L));
146 ASSERT (!isfinite (-1.0L / 0.0L));
147 /* Quiet NaN. */
148 ASSERT (!isfinite (zerol / zerol));
150 #if defined LDBL_EXPBIT0_WORD && defined LDBL_EXPBIT0_BIT
151 /* A bit pattern that is different from a Quiet NaN. With a bit of luck,
152 it's a Signalling NaN. */
154 memory_long_double m;
155 m.value = zerol / zerol;
156 # if LDBL_EXPBIT0_BIT > 0
157 m.word[LDBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (LDBL_EXPBIT0_BIT - 1);
158 # else
159 m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
160 ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
161 # endif
162 m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
163 |= (unsigned int) 1 << LDBL_EXPBIT0_BIT;
164 ASSERT (!isfinite (m.value));
166 #endif
168 #if ((defined __ia64 && LDBL_MANT_DIG == 64) || (defined __x86_64__ || defined __amd64__) || (defined __i386 || defined __i386__ || defined _I386 || defined _M_IX86 || defined _X86_))
169 /* Representation of an 80-bit 'long double' as an initializer for a sequence
170 of 'unsigned int' words. */
171 # ifdef WORDS_BIGENDIAN
172 # define LDBL80_WORDS(exponent,manthi,mantlo) \
173 { ((unsigned int) (exponent) << 16) | ((unsigned int) (manthi) >> 16), \
174 ((unsigned int) (manthi) << 16) | (unsigned int) (mantlo) >> 16), \
175 (unsigned int) (mantlo) << 16 \
177 # else
178 # define LDBL80_WORDS(exponent,manthi,mantlo) \
179 { mantlo, manthi, exponent }
180 # endif
181 { /* Quiet NaN. */
182 static memory_long_double x =
183 { LDBL80_WORDS (0xFFFF, 0xC3333333, 0x00000000) };
184 ASSERT (!isfinite (x.value));
187 /* Signalling NaN. */
188 static memory_long_double x =
189 { LDBL80_WORDS (0xFFFF, 0x83333333, 0x00000000) };
190 ASSERT (!isfinite (x.value));
192 /* The isnanl function should recognize Pseudo-NaNs, Pseudo-Infinities,
193 Pseudo-Zeroes, Unnormalized Numbers, and Pseudo-Denormals, as defined in
194 Intel IA-64 Architecture Software Developer's Manual, Volume 1:
195 Application Architecture.
196 Table 5-2 "Floating-Point Register Encodings"
197 Figure 5-6 "Memory to Floating-Point Register Data Translation"
199 { /* Pseudo-NaN. */
200 static memory_long_double x =
201 { LDBL80_WORDS (0xFFFF, 0x40000001, 0x00000000) };
202 ASSERT (!isfinite (x.value));
204 { /* Pseudo-Infinity. */
205 static memory_long_double x =
206 { LDBL80_WORDS (0xFFFF, 0x00000000, 0x00000000) };
207 ASSERT (!isfinite (x.value));
209 { /* Pseudo-Zero. */
210 static memory_long_double x =
211 { LDBL80_WORDS (0x4004, 0x00000000, 0x00000000) };
212 ASSERT (!isfinite (x.value));
214 { /* Unnormalized number. */
215 static memory_long_double x =
216 { LDBL80_WORDS (0x4000, 0x63333333, 0x00000000) };
217 ASSERT (!isfinite (x.value));
219 { /* Pseudo-Denormal. */
220 static memory_long_double x =
221 { LDBL80_WORDS (0x0000, 0x83333333, 0x00000000) };
222 ASSERT (!isfinite (x.value));
224 #endif
226 #undef NWORDS
230 main ()
232 test_isfinitef ();
233 test_isfinited ();
234 test_isfinitel ();
235 return 0;