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 *));
31 #include "isnanl-nolibm.h"
34 /* Avoid some warnings from "gcc -Wshadow".
35 This file doesn't use the exp() function. */
39 #define ASSERT(expr) \
44 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
51 /* On MIPS IRIX machines, LDBL_MIN_EXP is -1021, but the smallest reliable
52 exponent for 'long double' is -964. Similarly, on PowerPC machines,
53 LDBL_MIN_EXP is -1021, but the smallest reliable exponent for 'long double'
54 is -968. For exponents below that, the precision may be truncated to the
55 precision used for 'double'. */
57 # define MIN_NORMAL_EXP (LDBL_MIN_EXP + 57)
58 #elif defined __ppc || defined __ppc__ || defined __powerpc || defined __powerpc__
59 # define MIN_NORMAL_EXP (LDBL_MIN_EXP + 53)
61 # define MIN_NORMAL_EXP LDBL_MIN_EXP
64 /* On HP-UX 10.20, negating 0.0L does not yield -0.0L.
65 So we use minus_zero instead.
66 IRIX cc can't put -0.0L into .data, but can compute at runtime.
67 Note that the expression -LDBL_MIN * LDBL_MIN does not work on other
68 platforms, such as when cross-compiling to PowerPC on MacOS X 10.5. */
69 #if defined __hpux || defined __sgi
71 compute_minus_zero (void)
73 return -LDBL_MIN * LDBL_MIN;
75 # define minus_zero compute_minus_zero ()
77 long double minus_zero = -0.0L;
81 my_ldexp (long double x, int d)
95 DECL_LONG_DOUBLE_ROUNDING
97 BEGIN_LONG_DOUBLE_ROUNDING ();
101 long double mantissa;
103 mantissa = frexpl (x, &exp);
104 ASSERT (isnanl (mantissa));
107 { /* Positive infinity. */
109 long double mantissa;
111 mantissa = frexpl (x, &exp);
112 ASSERT (mantissa == x);
115 { /* Negative infinity. */
117 long double mantissa;
119 mantissa = frexpl (x, &exp);
120 ASSERT (mantissa == x);
123 { /* Positive zero. */
125 long double mantissa;
127 mantissa = frexpl (x, &exp);
129 ASSERT (mantissa == x);
130 ASSERT (!signbit (mantissa));
133 { /* Negative zero. */
135 long double mantissa;
137 mantissa = frexpl (x, &exp);
139 ASSERT (mantissa == x);
140 ASSERT (signbit (mantissa));
143 for (i = 1, x = 1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
146 long double mantissa = frexpl (x, &exp);
148 ASSERT (mantissa == 0.5L);
150 for (i = 1, x = 1.0L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
153 long double mantissa = frexpl (x, &exp);
155 ASSERT (mantissa == 0.5L);
157 for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
160 long double mantissa = frexpl (x, &exp);
162 ASSERT (mantissa == 0.5L);
165 for (i = 1, x = -1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
168 long double mantissa = frexpl (x, &exp);
170 ASSERT (mantissa == -0.5L);
172 for (i = 1, x = -1.0L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
175 long double mantissa = frexpl (x, &exp);
177 ASSERT (mantissa == -0.5L);
179 for (; i >= LDBL_MIN_EXP - 100 && x < 0.0L; i--, x *= 0.5L)
182 long double mantissa = frexpl (x, &exp);
184 ASSERT (mantissa == -0.5L);
187 for (i = 1, x = 1.01L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
190 long double mantissa = frexpl (x, &exp);
192 ASSERT (mantissa == 0.505L);
194 for (i = 1, x = 1.01L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
197 long double mantissa = frexpl (x, &exp);
199 ASSERT (mantissa == 0.505L);
201 for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
204 long double mantissa = frexpl (x, &exp);
206 ASSERT (mantissa >= 0.5L);
207 ASSERT (mantissa < 1.0L);
208 ASSERT (mantissa == my_ldexp (x, - exp));
211 for (i = 1, x = 1.73205L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
214 long double mantissa = frexpl (x, &exp);
216 ASSERT (mantissa == 0.866025L);
218 for (i = 1, x = 1.73205L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
221 long double mantissa = frexpl (x, &exp);
223 ASSERT (mantissa == 0.866025L);
225 for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
228 long double mantissa = frexpl (x, &exp);
229 ASSERT (exp == i || exp == i + 1);
230 ASSERT (mantissa >= 0.5L);
231 ASSERT (mantissa < 1.0L);
232 ASSERT (mantissa == my_ldexp (x, - exp));