tests: IRIX 6.2 cc can't compile -0.0 into .data
[gnulib.git] / tests / test-frexpl.c
1 /* Test of splitting a 'long double' into fraction and mantissa.
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 Bruno Haible <bruno@clisp.org>, 2007.  */
18
19 #include <config.h>
20
21 #include <math.h>
22
23 #include <float.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 #include "fpucw.h"
28 #include "isnanl-nolibm.h"
29
30 /* Avoid some warnings from "gcc -Wshadow".
31    This file doesn't use the exp() function.  */
32 #undef exp
33 #define exp exponent
34
35 #define ASSERT(expr) \
36   do                                                                         \
37     {                                                                        \
38       if (!(expr))                                                           \
39         {                                                                    \
40           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
41           fflush (stderr);                                                   \
42           abort ();                                                          \
43         }                                                                    \
44     }                                                                        \
45   while (0)
46
47 /* On MIPS IRIX machines, LDBL_MIN_EXP is -1021, but the smallest reliable
48    exponent for 'long double' is -964.  Similarly, on PowerPC machines,
49    LDBL_MIN_EXP is -1021, but the smallest reliable exponent for 'long double'
50    is -968.  For exponents below that, the precision may be truncated to the
51    precision used for 'double'.  */
52 #ifdef __sgi
53 # define MIN_NORMAL_EXP (LDBL_MIN_EXP + 57)
54 #elif defined __ppc || defined __ppc__ || defined __powerpc || defined __powerpc__
55 # define MIN_NORMAL_EXP (LDBL_MIN_EXP + 53)
56 #else
57 # define MIN_NORMAL_EXP LDBL_MIN_EXP
58 #endif
59
60 /* On HP-UX 10.20, negating 0.0L does not yield -0.0L.
61    So we use minus_zero instead.
62    IRIX cc can't put -0.0L into .data, but can compute at runtime.
63    Note that the expression -LDBL_MIN * LDBL_MIN does not work on other
64    platforms, such as when cross-compiling to PowerPC on MacOS X 10.5.  */
65 #if defined __hpux || defined __sgi
66 static long double
67 compute_minus_zero (void)
68 {
69   return -LDBL_MIN * LDBL_MIN;
70 }
71 # define minus_zero compute_minus_zero ()
72 #else
73 long double minus_zero = -0.0L;
74 #endif
75
76 static long double
77 my_ldexp (long double x, int d)
78 {
79   for (; d > 0; d--)
80     x *= 2.0L;
81   for (; d < 0; d++)
82     x *= 0.5L;
83   return x;
84 }
85
86 int
87 main ()
88 {
89   int i;
90   long double x;
91   DECL_LONG_DOUBLE_ROUNDING
92
93   BEGIN_LONG_DOUBLE_ROUNDING ();
94
95   { /* NaN.  */
96     int exp = -9999;
97     long double mantissa;
98     x = 0.0L / 0.0L;
99     mantissa = frexpl (x, &exp);
100     ASSERT (isnanl (mantissa));
101   }
102
103   { /* Positive infinity.  */
104     int exp = -9999;
105     long double mantissa;
106     x = 1.0L / 0.0L;
107     mantissa = frexpl (x, &exp);
108     ASSERT (mantissa == x);
109   }
110
111   { /* Negative infinity.  */
112     int exp = -9999;
113     long double mantissa;
114     x = -1.0L / 0.0L;
115     mantissa = frexpl (x, &exp);
116     ASSERT (mantissa == x);
117   }
118
119   { /* Positive zero.  */
120     int exp = -9999;
121     long double mantissa;
122     x = 0.0L;
123     mantissa = frexpl (x, &exp);
124     ASSERT (exp == 0);
125     ASSERT (mantissa == x);
126     ASSERT (!signbit (mantissa));
127   }
128
129   { /* Negative zero.  */
130     int exp = -9999;
131     long double mantissa;
132     x = minus_zero;
133     mantissa = frexpl (x, &exp);
134     ASSERT (exp == 0);
135     ASSERT (mantissa == x);
136     ASSERT (signbit (mantissa));
137   }
138
139   for (i = 1, x = 1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
140     {
141       int exp = -9999;
142       long double mantissa = frexpl (x, &exp);
143       ASSERT (exp == i);
144       ASSERT (mantissa == 0.5L);
145     }
146   for (i = 1, x = 1.0L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
147     {
148       int exp = -9999;
149       long double mantissa = frexpl (x, &exp);
150       ASSERT (exp == i);
151       ASSERT (mantissa == 0.5L);
152     }
153   for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
154     {
155       int exp = -9999;
156       long double mantissa = frexpl (x, &exp);
157       ASSERT (exp == i);
158       ASSERT (mantissa == 0.5L);
159     }
160
161   for (i = 1, x = -1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
162     {
163       int exp = -9999;
164       long double mantissa = frexpl (x, &exp);
165       ASSERT (exp == i);
166       ASSERT (mantissa == -0.5L);
167     }
168   for (i = 1, x = -1.0L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
169     {
170       int exp = -9999;
171       long double mantissa = frexpl (x, &exp);
172       ASSERT (exp == i);
173       ASSERT (mantissa == -0.5L);
174     }
175   for (; i >= LDBL_MIN_EXP - 100 && x < 0.0L; i--, x *= 0.5L)
176     {
177       int exp = -9999;
178       long double mantissa = frexpl (x, &exp);
179       ASSERT (exp == i);
180       ASSERT (mantissa == -0.5L);
181     }
182
183   for (i = 1, x = 1.01L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
184     {
185       int exp = -9999;
186       long double mantissa = frexpl (x, &exp);
187       ASSERT (exp == i);
188       ASSERT (mantissa == 0.505L);
189     }
190   for (i = 1, x = 1.01L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
191     {
192       int exp = -9999;
193       long double mantissa = frexpl (x, &exp);
194       ASSERT (exp == i);
195       ASSERT (mantissa == 0.505L);
196     }
197   for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
198     {
199       int exp = -9999;
200       long double mantissa = frexpl (x, &exp);
201       ASSERT (exp == i);
202       ASSERT (mantissa >= 0.5L);
203       ASSERT (mantissa < 1.0L);
204       ASSERT (mantissa == my_ldexp (x, - exp));
205     }
206
207   for (i = 1, x = 1.73205L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
208     {
209       int exp = -9999;
210       long double mantissa = frexpl (x, &exp);
211       ASSERT (exp == i);
212       ASSERT (mantissa == 0.866025L);
213     }
214   for (i = 1, x = 1.73205L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
215     {
216       int exp = -9999;
217       long double mantissa = frexpl (x, &exp);
218       ASSERT (exp == i);
219       ASSERT (mantissa == 0.866025L);
220     }
221   for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
222     {
223       int exp = -9999;
224       long double mantissa = frexpl (x, &exp);
225       ASSERT (exp == i || exp == i + 1);
226       ASSERT (mantissa >= 0.5L);
227       ASSERT (mantissa < 1.0L);
228       ASSERT (mantissa == my_ldexp (x, - exp));
229     }
230
231   return 0;
232 }