1 /* Test of splitting a 'long double' into fraction and mantissa.
2 Copyright (C) 2007-2009 Free Software Foundation, Inc.
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.
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.
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/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
23 #include "signature.h"
24 SIGNATURE_CHECK (frexpl, long double, (long double, int *));
29 #include "isnanl-nolibm.h"
33 /* Avoid some warnings from "gcc -Wshadow".
34 This file doesn't use the exp() function. */
38 /* On MIPS IRIX machines, LDBL_MIN_EXP is -1021, but the smallest reliable
39 exponent for 'long double' is -964. Similarly, on PowerPC machines,
40 LDBL_MIN_EXP is -1021, but the smallest reliable exponent for 'long double'
41 is -968. For exponents below that, the precision may be truncated to the
42 precision used for 'double'. */
44 # define MIN_NORMAL_EXP (LDBL_MIN_EXP + 57)
45 #elif defined __ppc || defined __ppc__ || defined __powerpc || defined __powerpc__
46 # define MIN_NORMAL_EXP (LDBL_MIN_EXP + 53)
48 # define MIN_NORMAL_EXP LDBL_MIN_EXP
51 /* On HP-UX 10.20, negating 0.0L does not yield -0.0L.
52 So we use minus_zero instead.
53 IRIX cc can't put -0.0L into .data, but can compute at runtime.
54 Note that the expression -LDBL_MIN * LDBL_MIN does not work on other
55 platforms, such as when cross-compiling to PowerPC on MacOS X 10.5. */
56 #if defined __hpux || defined __sgi
58 compute_minus_zero (void)
60 return -LDBL_MIN * LDBL_MIN;
62 # define minus_zero compute_minus_zero ()
64 long double minus_zero = -0.0L;
68 my_ldexp (long double x, int d)
82 DECL_LONG_DOUBLE_ROUNDING
84 BEGIN_LONG_DOUBLE_ROUNDING ();
90 mantissa = frexpl (x, &exp);
91 ASSERT (isnanl (mantissa));
94 { /* Positive infinity. */
98 mantissa = frexpl (x, &exp);
99 ASSERT (mantissa == x);
102 { /* Negative infinity. */
104 long double mantissa;
106 mantissa = frexpl (x, &exp);
107 ASSERT (mantissa == x);
110 { /* Positive zero. */
112 long double mantissa;
114 mantissa = frexpl (x, &exp);
116 ASSERT (mantissa == x);
117 ASSERT (!signbit (mantissa));
120 { /* Negative zero. */
122 long double mantissa;
124 mantissa = frexpl (x, &exp);
126 ASSERT (mantissa == x);
127 ASSERT (signbit (mantissa));
130 for (i = 1, x = 1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
133 long double mantissa = frexpl (x, &exp);
135 ASSERT (mantissa == 0.5L);
137 for (i = 1, x = 1.0L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
140 long double mantissa = frexpl (x, &exp);
142 ASSERT (mantissa == 0.5L);
144 for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
147 long double mantissa = frexpl (x, &exp);
149 ASSERT (mantissa == 0.5L);
152 for (i = 1, x = -1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
155 long double mantissa = frexpl (x, &exp);
157 ASSERT (mantissa == -0.5L);
159 for (i = 1, x = -1.0L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
162 long double mantissa = frexpl (x, &exp);
164 ASSERT (mantissa == -0.5L);
166 for (; i >= LDBL_MIN_EXP - 100 && x < 0.0L; i--, x *= 0.5L)
169 long double mantissa = frexpl (x, &exp);
171 ASSERT (mantissa == -0.5L);
174 for (i = 1, x = 1.01L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
177 long double mantissa = frexpl (x, &exp);
179 ASSERT (mantissa == 0.505L);
181 for (i = 1, x = 1.01L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
184 long double mantissa = frexpl (x, &exp);
186 ASSERT (mantissa == 0.505L);
188 for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
191 long double mantissa = frexpl (x, &exp);
193 ASSERT (mantissa >= 0.5L);
194 ASSERT (mantissa < 1.0L);
195 ASSERT (mantissa == my_ldexp (x, - exp));
198 for (i = 1, x = 1.73205L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
201 long double mantissa = frexpl (x, &exp);
203 ASSERT (mantissa == 0.866025L);
205 for (i = 1, x = 1.73205L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
208 long double mantissa = frexpl (x, &exp);
210 ASSERT (mantissa == 0.866025L);
212 for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
215 long double mantissa = frexpl (x, &exp);
216 ASSERT (exp == i || exp == i + 1);
217 ASSERT (mantissa >= 0.5L);
218 ASSERT (mantissa < 1.0L);
219 ASSERT (mantissa == my_ldexp (x, - exp));