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