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