Avoid test failures on IRIX MIPS.
[gnulib.git] / tests / test-frexpl.c
1 /* Test of splitting a 'long double' into fraction and mantissa.
2    Copyright (C) 2007 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 2, or (at your option)
7    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, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
19
20 #include <config.h>
21
22 #include <math.h>
23
24 #include <float.h>
25 #include <stdlib.h>
26
27 #include "fpucw.h"
28 #include "isnanl-nolibm.h"
29
30 #define ASSERT(expr) if (!(expr)) abort ();
31
32 /* On MIPS IRIX machines, LDBL_MIN_EXP is -1021, but the smallest reliable
33    exponent for 'long double' is -964.  For exponents below that, the
34    precision may be truncated to the precision used for 'double'.  */
35 #ifdef __sgi
36 # define MIN_NORMAL_EXP (LDBL_MIN_EXP + 57)
37 #else
38 # define MIN_NORMAL_EXP LDBL_MIN_EXP
39 #endif
40
41 static long double
42 my_ldexp (long double x, int d)
43 {
44   for (; d > 0; d--)
45     x *= 2.0L;
46   for (; d < 0; d++)
47     x *= 0.5L;
48   return x;
49 }
50
51 int
52 main ()
53 {
54   int i;
55   long double x;
56   DECL_LONG_DOUBLE_ROUNDING
57
58   BEGIN_LONG_DOUBLE_ROUNDING ();
59
60   { /* NaN.  */
61     int exp = -9999;
62     long double mantissa;
63     x = 0.0L / 0.0L;
64     mantissa = frexpl (x, &exp);
65     ASSERT (isnanl (mantissa));
66   }
67
68   { /* Positive infinity.  */
69     int exp = -9999;
70     long double mantissa;
71     x = 1.0L / 0.0L;
72     mantissa = frexpl (x, &exp);
73     ASSERT (mantissa == x);
74   }
75
76   { /* Negative infinity.  */
77     int exp = -9999;
78     long double mantissa;
79     x = -1.0L / 0.0L;
80     mantissa = frexpl (x, &exp);
81     ASSERT (mantissa == x);
82   }
83
84   { /* Positive zero.  */
85     int exp = -9999;
86     long double mantissa;
87     x = 0.0L;
88     mantissa = frexpl (x, &exp);
89     ASSERT (exp == 0);
90     ASSERT (mantissa == x);
91   }
92
93   { /* Negative zero.  */
94     int exp = -9999;
95     long double mantissa;
96     x = -0.0L;
97     mantissa = frexpl (x, &exp);
98     ASSERT (exp == 0);
99     ASSERT (mantissa == x);
100   }
101
102   for (i = 1, x = 1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
103     {
104       int exp = -9999;
105       long double mantissa = frexpl (x, &exp);
106       ASSERT (exp == i);
107       ASSERT (mantissa == 0.5L);
108     }
109   for (i = 1, x = 1.0L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
110     {
111       int exp = -9999;
112       long double mantissa = frexpl (x, &exp);
113       ASSERT (exp == i);
114       ASSERT (mantissa == 0.5L);
115     }
116   for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
117     {
118       int exp = -9999;
119       long double mantissa = frexpl (x, &exp);
120       ASSERT (exp == i);
121       ASSERT (mantissa == 0.5L);
122     }
123
124   for (i = 1, x = -1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
125     {
126       int exp = -9999;
127       long double mantissa = frexpl (x, &exp);
128       ASSERT (exp == i);
129       ASSERT (mantissa == -0.5L);
130     }
131   for (i = 1, x = -1.0L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
132     {
133       int exp = -9999;
134       long double mantissa = frexpl (x, &exp);
135       ASSERT (exp == i);
136       ASSERT (mantissa == -0.5L);
137     }
138   for (; i >= LDBL_MIN_EXP - 100 && x < 0.0L; i--, x *= 0.5L)
139     {
140       int exp = -9999;
141       long double mantissa = frexpl (x, &exp);
142       ASSERT (exp == i);
143       ASSERT (mantissa == -0.5L);
144     }
145
146   for (i = 1, x = 1.01L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
147     {
148       int exp = -9999;
149       long double mantissa = frexpl (x, &exp);
150       ASSERT (exp == i);
151       ASSERT (mantissa == 0.505L);
152     }
153   for (i = 1, x = 1.01L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
154     {
155       int exp = -9999;
156       long double mantissa = frexpl (x, &exp);
157       ASSERT (exp == i);
158       ASSERT (mantissa == 0.505L);
159     }
160   for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
161     {
162       int exp = -9999;
163       long double mantissa = frexpl (x, &exp);
164       ASSERT (exp == i);
165       ASSERT (mantissa >= 0.5L);
166       ASSERT (mantissa < 1.0L);
167       ASSERT (mantissa == my_ldexp (x, - exp));
168     }
169
170   for (i = 1, x = 1.73205L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
171     {
172       int exp = -9999;
173       long double mantissa = frexpl (x, &exp);
174       ASSERT (exp == i);
175       ASSERT (mantissa == 0.866025L);
176     }
177   for (i = 1, x = 1.73205L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
178     {
179       int exp = -9999;
180       long double mantissa = frexpl (x, &exp);
181       ASSERT (exp == i);
182       ASSERT (mantissa == 0.866025L);
183     }
184   for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
185     {
186       int exp = -9999;
187       long double mantissa = frexpl (x, &exp);
188       ASSERT (exp == i || exp == i + 1);
189       ASSERT (mantissa >= 0.5L);
190       ASSERT (mantissa < 1.0L);
191       ASSERT (mantissa == my_ldexp (x, - exp));
192     }
193
194   return 0;
195 }