Improve name: "count-one-bits" is better than "popcount".
[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 <stdio.h>
26 #include <stdlib.h>
27
28 #include "fpucw.h"
29 #include "isnanl-nolibm.h"
30
31 #define ASSERT(expr) \
32   do                                                                         \
33     {                                                                        \
34       if (!(expr))                                                           \
35         {                                                                    \
36           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
37           abort ();                                                          \
38         }                                                                    \
39     }                                                                        \
40   while (0)
41
42 /* On MIPS IRIX machines, LDBL_MIN_EXP is -1021, but the smallest reliable
43    exponent for 'long double' is -964.  Similarly, on PowerPC machines,
44    LDBL_MIN_EXP is -1021, but the smallest reliable exponent for 'long double'
45    is -968.  For exponents below that, the precision may be truncated to the
46    precision used for 'double'.  */
47 #ifdef __sgi
48 # define MIN_NORMAL_EXP (LDBL_MIN_EXP + 57)
49 #elif defined __ppc || defined __ppc__ || defined __powerpc || defined __powerpc__
50 # define MIN_NORMAL_EXP (LDBL_MIN_EXP + 53)
51 #else
52 # define MIN_NORMAL_EXP LDBL_MIN_EXP
53 #endif
54
55 static long double
56 my_ldexp (long double x, int d)
57 {
58   for (; d > 0; d--)
59     x *= 2.0L;
60   for (; d < 0; d++)
61     x *= 0.5L;
62   return x;
63 }
64
65 int
66 main ()
67 {
68   int i;
69   long double x;
70   DECL_LONG_DOUBLE_ROUNDING
71
72   BEGIN_LONG_DOUBLE_ROUNDING ();
73
74   { /* NaN.  */
75     int exp = -9999;
76     long double mantissa;
77     x = 0.0L / 0.0L;
78     mantissa = frexpl (x, &exp);
79     ASSERT (isnanl (mantissa));
80   }
81
82   { /* Positive infinity.  */
83     int exp = -9999;
84     long double mantissa;
85     x = 1.0L / 0.0L;
86     mantissa = frexpl (x, &exp);
87     ASSERT (mantissa == x);
88   }
89
90   { /* Negative infinity.  */
91     int exp = -9999;
92     long double mantissa;
93     x = -1.0L / 0.0L;
94     mantissa = frexpl (x, &exp);
95     ASSERT (mantissa == x);
96   }
97
98   { /* Positive zero.  */
99     int exp = -9999;
100     long double mantissa;
101     x = 0.0L;
102     mantissa = frexpl (x, &exp);
103     ASSERT (exp == 0);
104     ASSERT (mantissa == x);
105     ASSERT (!signbit (mantissa));
106   }
107
108   { /* Negative zero.  */
109     int exp = -9999;
110     long double mantissa;
111     x = -0.0L;
112     mantissa = frexpl (x, &exp);
113     ASSERT (exp == 0);
114     ASSERT (mantissa == x);
115     ASSERT (signbit (mantissa));
116   }
117
118   for (i = 1, x = 1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
119     {
120       int exp = -9999;
121       long double mantissa = frexpl (x, &exp);
122       ASSERT (exp == i);
123       ASSERT (mantissa == 0.5L);
124     }
125   for (i = 1, x = 1.0L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
126     {
127       int exp = -9999;
128       long double mantissa = frexpl (x, &exp);
129       ASSERT (exp == i);
130       ASSERT (mantissa == 0.5L);
131     }
132   for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
133     {
134       int exp = -9999;
135       long double mantissa = frexpl (x, &exp);
136       ASSERT (exp == i);
137       ASSERT (mantissa == 0.5L);
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.01L; 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.505L);
168     }
169   for (i = 1, x = 1.01L; 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.505L);
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       ASSERT (mantissa < 1.0L);
183       ASSERT (mantissa == my_ldexp (x, - exp));
184     }
185
186   for (i = 1, x = 1.73205L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
187     {
188       int exp = -9999;
189       long double mantissa = frexpl (x, &exp);
190       ASSERT (exp == i);
191       ASSERT (mantissa == 0.866025L);
192     }
193   for (i = 1, x = 1.73205L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
194     {
195       int exp = -9999;
196       long double mantissa = frexpl (x, &exp);
197       ASSERT (exp == i);
198       ASSERT (mantissa == 0.866025L);
199     }
200   for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
201     {
202       int exp = -9999;
203       long double mantissa = frexpl (x, &exp);
204       ASSERT (exp == i || exp == i + 1);
205       ASSERT (mantissa >= 0.5L);
206       ASSERT (mantissa < 1.0L);
207       ASSERT (mantissa == my_ldexp (x, - exp));
208     }
209
210   return 0;
211 }