1 /* Test of isinf() substitute.
2    Copyright (C) 2007-2020 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 <https://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 
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
test_isinff()40 test_isinff ()
41 {
42   /* Zero. */
43   ASSERT (!isinf (0.0f));
44   /* Subnormal values. */
45   ASSERT (!isinf (FLT_MIN / 2));
46   ASSERT (!isinf (-FLT_MIN / 2));
47   /* Finite values.  */
48   ASSERT (!isinf (3.141f));
49   ASSERT (!isinf (3.141e30f));
50   ASSERT (!isinf (3.141e-30f));
51   ASSERT (!isinf (-2.718f));
52   ASSERT (!isinf (-2.718e30f));
53   ASSERT (!isinf (-2.718e-30f));
54   ASSERT (!isinf (FLT_MAX));
55   ASSERT (!isinf (-FLT_MAX));
56   /* Infinite values.  */
57   ASSERT (isinf (Infinityf ()));
58   ASSERT (isinf (- Infinityf ()));
59   /* Quiet NaN.  */
60   ASSERT (!isinf (zerof / zerof));
61 #if defined FLT_EXPBIT0_WORD && defined FLT_EXPBIT0_BIT
62   /* Signalling NaN.  */
63   {
64     #define NWORDS \
65       ((sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
66     typedef union { float value; unsigned int word[NWORDS]; } memory_float;
67     memory_float m;
68     m.value = zerof / zerof;
69 # if FLT_EXPBIT0_BIT > 0
70     m.word[FLT_EXPBIT0_WORD] ^= (unsigned int) 1 << (FLT_EXPBIT0_BIT - 1);
71 # else
72     m.word[FLT_EXPBIT0_WORD + (FLT_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
73       ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
74 # endif
75     if (FLT_EXPBIT0_WORD < NWORDS / 2)
76       m.word[FLT_EXPBIT0_WORD + 1] |= (unsigned int) 1 << FLT_EXPBIT0_BIT;
77     else
78       m.word[0] |= (unsigned int) 1;
79     ASSERT (!isinf (m.value));
80     #undef NWORDS
81   }
82 #endif
83 }
84 
85 static void
test_isinfd()86 test_isinfd ()
87 {
88   /* Zero. */
89   ASSERT (!isinf (0.0));
90   /* Subnormal values. */
91   ASSERT (!isinf (DBL_MIN / 2));
92   ASSERT (!isinf (-DBL_MIN / 2));
93   /* Finite values. */
94   ASSERT (!isinf (3.141));
95   ASSERT (!isinf (3.141e30));
96   ASSERT (!isinf (3.141e-30));
97   ASSERT (!isinf (-2.718));
98   ASSERT (!isinf (-2.718e30));
99   ASSERT (!isinf (-2.718e-30));
100   ASSERT (!isinf (DBL_MAX));
101   ASSERT (!isinf (-DBL_MAX));
102   /* Infinite values.  */
103   ASSERT (isinf (Infinityd ()));
104   ASSERT (isinf (- Infinityd ()));
105   /* Quiet NaN.  */
106   ASSERT (!isinf (zerod / zerod));
107 #if defined DBL_EXPBIT0_WORD && defined DBL_EXPBIT0_BIT
108   /* Signalling NaN.  */
109   {
110     #define NWORDS \
111       ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
112     typedef union { double value; unsigned int word[NWORDS]; } memory_double;
113     memory_double m;
114     m.value = zerod / zerod;
115 # if DBL_EXPBIT0_BIT > 0
116     m.word[DBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (DBL_EXPBIT0_BIT - 1);
117 # else
118     m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
119       ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
120 # endif
121     m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
122       |= (unsigned int) 1 << DBL_EXPBIT0_BIT;
123     ASSERT (!isinf (m.value));
124     #undef NWORDS
125   }
126 #endif
127 }
128 
129 static void
test_isinfl()130 test_isinfl ()
131 {
132   #define NWORDS \
133     ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
134   typedef union { unsigned int word[NWORDS]; long double value; }
135           memory_long_double;
136 
137   /* Zero. */
138   ASSERT (!isinf (0.0L));
139   /* Subnormal values. */
140   ASSERT (!isinf (LDBL_MIN / 2));
141   ASSERT (!isinf (-LDBL_MIN / 2));
142   /* Finite values. */
143   ASSERT (!isinf (3.141L));
144   ASSERT (!isinf (3.141e30L));
145   ASSERT (!isinf (3.141e-30L));
146   ASSERT (!isinf (-2.718L));
147   ASSERT (!isinf (-2.718e30L));
148   ASSERT (!isinf (-2.718e-30L));
149   ASSERT (!isinf (LDBL_MAX));
150   ASSERT (!isinf (-LDBL_MAX));
151   /* Infinite values.  */
152   ASSERT (isinf (Infinityl ()));
153   ASSERT (isinf (- Infinityl ()));
154   /* Quiet NaN.  */
155   ASSERT (!isinf (zerol / zerol));
156 
157 #if defined LDBL_EXPBIT0_WORD && defined LDBL_EXPBIT0_BIT
158   /* A bit pattern that is different from a Quiet NaN.  With a bit of luck,
159      it's a Signalling NaN.  */
160   {
161 #if defined __powerpc__ && LDBL_MANT_DIG == 106
162     /* This is PowerPC "double double", a pair of two doubles.  Inf and Nan are
163        represented as the corresponding 64-bit IEEE values in the first double;
164        the second is ignored.  Manipulate only the first double.  */
165     #undef NWORDS
166     #define NWORDS \
167       ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
168 #endif
169 
170     memory_long_double m;
171     m.value = zerol / zerol;
172 # if LDBL_EXPBIT0_BIT > 0
173     m.word[LDBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (LDBL_EXPBIT0_BIT - 1);
174 # else
175     m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
176       ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
177 # endif
178     m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
179       |= (unsigned int) 1 << LDBL_EXPBIT0_BIT;
180     ASSERT (!isinf (m.value));
181   }
182 #endif
183 
184 #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
185 /* Representation of an 80-bit 'long double' as an initializer for a sequence
186    of 'unsigned int' words.  */
187 # ifdef WORDS_BIGENDIAN
188 #  define LDBL80_WORDS(exponent,manthi,mantlo) \
189      { ((unsigned int) (exponent) << 16) | ((unsigned int) (manthi) >> 16), \
190        ((unsigned int) (manthi) << 16) | ((unsigned int) (mantlo) >> 16),   \
191        (unsigned int) (mantlo) << 16                                        \
192      }
193 # else
194 #  define LDBL80_WORDS(exponent,manthi,mantlo) \
195      { mantlo, manthi, exponent }
196 # endif
197   { /* Quiet NaN.  */
198     static memory_long_double x =
199       { LDBL80_WORDS (0xFFFF, 0xC3333333, 0x00000000) };
200     ASSERT (!isinf (x.value));
201   }
202   {
203     /* Signalling NaN.  */
204     static memory_long_double x =
205       { LDBL80_WORDS (0xFFFF, 0x83333333, 0x00000000) };
206     ASSERT (!isinf (x.value));
207   }
208   /* isinf should return something for noncanonical values.  */
209   { /* Pseudo-NaN.  */
210     static memory_long_double x =
211       { LDBL80_WORDS (0xFFFF, 0x40000001, 0x00000000) };
212     ASSERT (isinf (x.value) || !isinf (x.value));
213   }
214   { /* Pseudo-Infinity.  */
215     static memory_long_double x =
216       { LDBL80_WORDS (0xFFFF, 0x00000000, 0x00000000) };
217     ASSERT (isinf (x.value) || !isinf (x.value));
218   }
219   { /* Pseudo-Zero.  */
220     static memory_long_double x =
221       { LDBL80_WORDS (0x4004, 0x00000000, 0x00000000) };
222     ASSERT (isinf (x.value) || !isinf (x.value));
223   }
224   { /* Unnormalized number.  */
225     static memory_long_double x =
226       { LDBL80_WORDS (0x4000, 0x63333333, 0x00000000) };
227     ASSERT (isinf (x.value) || !isinf (x.value));
228   }
229   { /* Pseudo-Denormal.  */
230     static memory_long_double x =
231       { LDBL80_WORDS (0x0000, 0x83333333, 0x00000000) };
232     ASSERT (isinf (x.value) || !isinf (x.value));
233   }
234 #endif
235 
236   #undef NWORDS
237 }
238 
239 int
main()240 main ()
241 {
242   test_isinff ();
243   test_isinfd ();
244   test_isinfl ();
245   return 0;
246 }
247