Refactor common macros used in tests.
[gnulib.git] / tests / test-isnan.c
1 /* Test of isnand() 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 <blp@cs.stanford.edu>, from code by Bruno
18    Haible <bruno@clisp.org>.  */
19
20 #include <config.h>
21
22 #include <math.h>
23
24 /* isnan must be a macro.  */
25 #ifndef isnan
26 # error missing declaration
27 #endif
28
29 #include <float.h>
30 #include <limits.h>
31
32 #include "nan.h"
33 #include "macros.h"
34
35 /* HP cc on HP-UX 10.20 has a bug with the constant expression -0.0f.
36    So we use -zero instead.  */
37 float zerof = 0.0f;
38
39 /* HP cc on HP-UX 10.20 has a bug with the constant expression -0.0.
40    So we use -zero instead.  */
41 double zerod = 0.0;
42
43 /* On HP-UX 10.20, negating 0.0L does not yield -0.0L.
44    So we use minus_zerol instead.
45    IRIX cc can't put -0.0L into .data, but can compute at runtime.
46    Note that the expression -LDBL_MIN * LDBL_MIN does not work on other
47    platforms, such as when cross-compiling to PowerPC on MacOS X 10.5.  */
48 #if defined __hpux || defined __sgi
49 static long double
50 compute_minus_zerol (void)
51 {
52   return -LDBL_MIN * LDBL_MIN;
53 }
54 # define minus_zerol compute_minus_zerol ()
55 #else
56 long double minus_zerol = -0.0L;
57 #endif
58
59 static void
60 test_float (void)
61 {
62   /* Finite values.  */
63   ASSERT (!isnan (3.141f));
64   ASSERT (!isnan (3.141e30f));
65   ASSERT (!isnan (3.141e-30f));
66   ASSERT (!isnan (-2.718f));
67   ASSERT (!isnan (-2.718e30f));
68   ASSERT (!isnan (-2.718e-30f));
69   ASSERT (!isnan (0.0f));
70   ASSERT (!isnan (-zerof));
71   /* Infinite values.  */
72   ASSERT (!isnan (1.0f / 0.0f));
73   ASSERT (!isnan (-1.0f / 0.0f));
74   /* Quiet NaN.  */
75   ASSERT (isnan (NaNf ()));
76 #if defined FLT_EXPBIT0_WORD && defined FLT_EXPBIT0_BIT
77   /* Signalling NaN.  */
78   {
79     #define NWORDSF \
80       ((sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
81     typedef union { float value; unsigned int word[NWORDSF]; } memory_float;
82     memory_float m;
83     m.value = NaNf ();
84 # if FLT_EXPBIT0_BIT > 0
85     m.word[FLT_EXPBIT0_WORD] ^= (unsigned int) 1 << (FLT_EXPBIT0_BIT - 1);
86 # else
87     m.word[FLT_EXPBIT0_WORD + (FLT_EXPBIT0_WORD < NWORDSF / 2 ? 1 : - 1)]
88       ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
89 # endif
90     if (FLT_EXPBIT0_WORD < NWORDSF / 2)
91       m.word[FLT_EXPBIT0_WORD + 1] |= (unsigned int) 1 << FLT_EXPBIT0_BIT;
92     else
93       m.word[0] |= (unsigned int) 1;
94     ASSERT (isnan (m.value));
95   }
96 #endif
97 }
98
99 static void
100 test_double (void)
101 {
102   /* Finite values.  */
103   ASSERT (!isnan (3.141));
104   ASSERT (!isnan (3.141e30));
105   ASSERT (!isnan (3.141e-30));
106   ASSERT (!isnan (-2.718));
107   ASSERT (!isnan (-2.718e30));
108   ASSERT (!isnan (-2.718e-30));
109   ASSERT (!isnan (0.0));
110   ASSERT (!isnan (-zerod));
111   /* Infinite values.  */
112   ASSERT (!isnan (1.0 / 0.0));
113   ASSERT (!isnan (-1.0 / 0.0));
114   /* Quiet NaN.  */
115   ASSERT (isnan (NaNd ()));
116 #if defined DBL_EXPBIT0_WORD && defined DBL_EXPBIT0_BIT
117   /* Signalling NaN.  */
118   {
119     #define NWORDSD \
120       ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
121     typedef union { double value; unsigned int word[NWORDSD]; } memory_double;
122     memory_double m;
123     m.value = NaNd ();
124 # if DBL_EXPBIT0_BIT > 0
125     m.word[DBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (DBL_EXPBIT0_BIT - 1);
126 # else
127     m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDSD / 2 ? 1 : - 1)]
128       ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
129 # endif
130     m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDSD / 2 ? 1 : - 1)]
131       |= (unsigned int) 1 << DBL_EXPBIT0_BIT;
132     ASSERT (isnan (m.value));
133   }
134 #endif
135 }
136
137 static void
138 test_long_double (void)
139 {
140   #define NWORDSL \
141     ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
142   typedef union { unsigned int word[NWORDSL]; long double value; }
143           memory_long_double;
144
145   /* Finite values.  */
146   ASSERT (!isnan (3.141L));
147   ASSERT (!isnan (3.141e30L));
148   ASSERT (!isnan (3.141e-30L));
149   ASSERT (!isnan (-2.718L));
150   ASSERT (!isnan (-2.718e30L));
151   ASSERT (!isnan (-2.718e-30L));
152   ASSERT (!isnan (0.0L));
153   ASSERT (!isnan (minus_zerol));
154   /* Infinite values.  */
155   ASSERT (!isnan (1.0L / 0.0L));
156   ASSERT (!isnan (-1.0L / 0.0L));
157   /* Quiet NaN.  */
158   ASSERT (isnan (NaNl ()));
159
160 #if defined LDBL_EXPBIT0_WORD && defined LDBL_EXPBIT0_BIT
161   /* A bit pattern that is different from a Quiet NaN.  With a bit of luck,
162      it's a Signalling NaN.  */
163   {
164     memory_long_double m;
165     m.value = NaNl ();
166 # if LDBL_EXPBIT0_BIT > 0
167     m.word[LDBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (LDBL_EXPBIT0_BIT - 1);
168 # else
169     m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDSL / 2 ? 1 : - 1)]
170       ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
171 # endif
172     m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDSL / 2 ? 1 : - 1)]
173       |= (unsigned int) 1 << LDBL_EXPBIT0_BIT;
174     ASSERT (isnan (m.value));
175   }
176 #endif
177
178 #if ((defined __ia64 && LDBL_MANT_DIG == 64) || (defined __x86_64__ || defined __amd64__) || (defined __i386 || defined __i386__ || defined _I386 || defined _M_IX86 || defined _X86_))
179 /* Representation of an 80-bit 'long double' as an initializer for a sequence
180    of 'unsigned int' words.  */
181 # ifdef WORDS_BIGENDIAN
182 #  define LDBL80_WORDS(exponent,manthi,mantlo) \
183      { ((unsigned int) (exponent) << 16) | ((unsigned int) (manthi) >> 16), \
184        ((unsigned int) (manthi) << 16) | (unsigned int) (mantlo) >> 16),    \
185        (unsigned int) (mantlo) << 16                                        \
186      }
187 # else
188 #  define LDBL80_WORDS(exponent,manthi,mantlo) \
189      { mantlo, manthi, exponent }
190 # endif
191   { /* Quiet NaN.  */
192     static memory_long_double x =
193       { LDBL80_WORDS (0xFFFF, 0xC3333333, 0x00000000) };
194     ASSERT (isnan (x.value));
195   }
196   {
197     /* Signalling NaN.  */
198     static memory_long_double x =
199       { LDBL80_WORDS (0xFFFF, 0x83333333, 0x00000000) };
200     ASSERT (isnan (x.value));
201   }
202   /* The isnan function should recognize Pseudo-NaNs, Pseudo-Infinities,
203      Pseudo-Zeroes, Unnormalized Numbers, and Pseudo-Denormals, as defined in
204        Intel IA-64 Architecture Software Developer's Manual, Volume 1:
205        Application Architecture.
206        Table 5-2 "Floating-Point Register Encodings"
207        Figure 5-6 "Memory to Floating-Point Register Data Translation"
208    */
209   { /* Pseudo-NaN.  */
210     static memory_long_double x =
211       { LDBL80_WORDS (0xFFFF, 0x40000001, 0x00000000) };
212     ASSERT (isnan (x.value));
213   }
214   { /* Pseudo-Infinity.  */
215     static memory_long_double x =
216       { LDBL80_WORDS (0xFFFF, 0x00000000, 0x00000000) };
217     ASSERT (isnan (x.value));
218   }
219   { /* Pseudo-Zero.  */
220     static memory_long_double x =
221       { LDBL80_WORDS (0x4004, 0x00000000, 0x00000000) };
222     ASSERT (isnan (x.value));
223   }
224   { /* Unnormalized number.  */
225     static memory_long_double x =
226       { LDBL80_WORDS (0x4000, 0x63333333, 0x00000000) };
227     ASSERT (isnan (x.value));
228   }
229   { /* Pseudo-Denormal.  */
230     static memory_long_double x =
231       { LDBL80_WORDS (0x0000, 0x83333333, 0x00000000) };
232     ASSERT (isnan (x.value));
233   }
234 #endif
235 }
236
237 int
238 main ()
239 {
240   test_float ();
241   test_double ();
242   test_long_double ();
243   return 0;
244 }