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