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