Avoid some warnings from "gcc -Wshadow".
[gnulib.git] / tests / test-frexpl.c
1 /* Test of splitting a 'long double' into fraction and mantissa.
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 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 #define exp exponent
33
34 #define ASSERT(expr) \
35   do                                                                         \
36     {                                                                        \
37       if (!(expr))                                                           \
38         {                                                                    \
39           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
40           abort ();                                                          \
41         }                                                                    \
42     }                                                                        \
43   while (0)
44
45 /* On MIPS IRIX machines, LDBL_MIN_EXP is -1021, but the smallest reliable
46    exponent for 'long double' is -964.  Similarly, on PowerPC machines,
47    LDBL_MIN_EXP is -1021, but the smallest reliable exponent for 'long double'
48    is -968.  For exponents below that, the precision may be truncated to the
49    precision used for 'double'.  */
50 #ifdef __sgi
51 # define MIN_NORMAL_EXP (LDBL_MIN_EXP + 57)
52 #elif defined __ppc || defined __ppc__ || defined __powerpc || defined __powerpc__
53 # define MIN_NORMAL_EXP (LDBL_MIN_EXP + 53)
54 #else
55 # define MIN_NORMAL_EXP LDBL_MIN_EXP
56 #endif
57
58 static long double
59 my_ldexp (long double x, int d)
60 {
61   for (; d > 0; d--)
62     x *= 2.0L;
63   for (; d < 0; d++)
64     x *= 0.5L;
65   return x;
66 }
67
68 int
69 main ()
70 {
71   int i;
72   long double x;
73   DECL_LONG_DOUBLE_ROUNDING
74
75   BEGIN_LONG_DOUBLE_ROUNDING ();
76
77   { /* NaN.  */
78     int exp = -9999;
79     long double mantissa;
80     x = 0.0L / 0.0L;
81     mantissa = frexpl (x, &exp);
82     ASSERT (isnanl (mantissa));
83   }
84
85   { /* Positive infinity.  */
86     int exp = -9999;
87     long double mantissa;
88     x = 1.0L / 0.0L;
89     mantissa = frexpl (x, &exp);
90     ASSERT (mantissa == x);
91   }
92
93   { /* Negative infinity.  */
94     int exp = -9999;
95     long double mantissa;
96     x = -1.0L / 0.0L;
97     mantissa = frexpl (x, &exp);
98     ASSERT (mantissa == x);
99   }
100
101   { /* Positive zero.  */
102     int exp = -9999;
103     long double mantissa;
104     x = 0.0L;
105     mantissa = frexpl (x, &exp);
106     ASSERT (exp == 0);
107     ASSERT (mantissa == x);
108     ASSERT (!signbit (mantissa));
109   }
110
111   { /* Negative zero.  */
112     int exp = -9999;
113     long double mantissa;
114     x = -0.0L;
115     mantissa = frexpl (x, &exp);
116     ASSERT (exp == 0);
117     ASSERT (mantissa == x);
118     ASSERT (signbit (mantissa));
119   }
120
121   for (i = 1, x = 1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
122     {
123       int exp = -9999;
124       long double mantissa = frexpl (x, &exp);
125       ASSERT (exp == i);
126       ASSERT (mantissa == 0.5L);
127     }
128   for (i = 1, x = 1.0L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
129     {
130       int exp = -9999;
131       long double mantissa = frexpl (x, &exp);
132       ASSERT (exp == i);
133       ASSERT (mantissa == 0.5L);
134     }
135   for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
136     {
137       int exp = -9999;
138       long double mantissa = frexpl (x, &exp);
139       ASSERT (exp == i);
140       ASSERT (mantissa == 0.5L);
141     }
142
143   for (i = 1, x = -1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
144     {
145       int exp = -9999;
146       long double mantissa = frexpl (x, &exp);
147       ASSERT (exp == i);
148       ASSERT (mantissa == -0.5L);
149     }
150   for (i = 1, x = -1.0L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
151     {
152       int exp = -9999;
153       long double mantissa = frexpl (x, &exp);
154       ASSERT (exp == i);
155       ASSERT (mantissa == -0.5L);
156     }
157   for (; i >= LDBL_MIN_EXP - 100 && x < 0.0L; i--, x *= 0.5L)
158     {
159       int exp = -9999;
160       long double mantissa = frexpl (x, &exp);
161       ASSERT (exp == i);
162       ASSERT (mantissa == -0.5L);
163     }
164
165   for (i = 1, x = 1.01L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
166     {
167       int exp = -9999;
168       long double mantissa = frexpl (x, &exp);
169       ASSERT (exp == i);
170       ASSERT (mantissa == 0.505L);
171     }
172   for (i = 1, x = 1.01L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
173     {
174       int exp = -9999;
175       long double mantissa = frexpl (x, &exp);
176       ASSERT (exp == i);
177       ASSERT (mantissa == 0.505L);
178     }
179   for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
180     {
181       int exp = -9999;
182       long double mantissa = frexpl (x, &exp);
183       ASSERT (exp == i);
184       ASSERT (mantissa >= 0.5L);
185       ASSERT (mantissa < 1.0L);
186       ASSERT (mantissa == my_ldexp (x, - exp));
187     }
188
189   for (i = 1, x = 1.73205L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
190     {
191       int exp = -9999;
192       long double mantissa = frexpl (x, &exp);
193       ASSERT (exp == i);
194       ASSERT (mantissa == 0.866025L);
195     }
196   for (i = 1, x = 1.73205L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
197     {
198       int exp = -9999;
199       long double mantissa = frexpl (x, &exp);
200       ASSERT (exp == i);
201       ASSERT (mantissa == 0.866025L);
202     }
203   for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
204     {
205       int exp = -9999;
206       long double mantissa = frexpl (x, &exp);
207       ASSERT (exp == i || exp == i + 1);
208       ASSERT (mantissa >= 0.5L);
209       ASSERT (mantissa < 1.0L);
210       ASSERT (mantissa == my_ldexp (x, - exp));
211     }
212
213   return 0;
214 }