1 /* Test of splitting a 'long double' into fraction and mantissa.
2 Copyright (C) 2007-2008 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. */
28 #include "isnanl-nolibm.h"
30 /* Avoid some warnings from "gcc -Wshadow".
31 This file doesn't use the exp() function. */
35 #define ASSERT(expr) \
40 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
47 /* On MIPS IRIX machines, LDBL_MIN_EXP is -1021, but the smallest reliable
48 exponent for 'long double' is -964. Similarly, on PowerPC machines,
49 LDBL_MIN_EXP is -1021, but the smallest reliable exponent for 'long double'
50 is -968. For exponents below that, the precision may be truncated to the
51 precision used for 'double'. */
53 # define MIN_NORMAL_EXP (LDBL_MIN_EXP + 57)
54 #elif defined __ppc || defined __ppc__ || defined __powerpc || defined __powerpc__
55 # define MIN_NORMAL_EXP (LDBL_MIN_EXP + 53)
57 # define MIN_NORMAL_EXP LDBL_MIN_EXP
60 /* On HP-UX 10.20, negating 0.0L does not yield -0.0L.
61 So we use minus_zero instead.
62 Note that the expression -LDBL_MIN * LDBL_MIN does not work on other
63 platforms, such as when cross-compiling to PowerPC on MacOS X 10.5. */
64 #if defined __hpux || defined __sgi
65 long double minus_zero = -LDBL_MIN * LDBL_MIN;
67 long double minus_zero = -0.0L;
71 my_ldexp (long double x, int d)
85 DECL_LONG_DOUBLE_ROUNDING
87 BEGIN_LONG_DOUBLE_ROUNDING ();
93 mantissa = frexpl (x, &exp);
94 ASSERT (isnanl (mantissa));
97 { /* Positive infinity. */
101 mantissa = frexpl (x, &exp);
102 ASSERT (mantissa == x);
105 { /* Negative infinity. */
107 long double mantissa;
109 mantissa = frexpl (x, &exp);
110 ASSERT (mantissa == x);
113 { /* Positive zero. */
115 long double mantissa;
117 mantissa = frexpl (x, &exp);
119 ASSERT (mantissa == x);
120 ASSERT (!signbit (mantissa));
123 { /* Negative zero. */
125 long double mantissa;
127 mantissa = frexpl (x, &exp);
129 ASSERT (mantissa == x);
130 ASSERT (signbit (mantissa));
133 for (i = 1, x = 1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
136 long double mantissa = frexpl (x, &exp);
138 ASSERT (mantissa == 0.5L);
140 for (i = 1, x = 1.0L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
143 long double mantissa = frexpl (x, &exp);
145 ASSERT (mantissa == 0.5L);
147 for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
150 long double mantissa = frexpl (x, &exp);
152 ASSERT (mantissa == 0.5L);
155 for (i = 1, x = -1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
158 long double mantissa = frexpl (x, &exp);
160 ASSERT (mantissa == -0.5L);
162 for (i = 1, x = -1.0L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
165 long double mantissa = frexpl (x, &exp);
167 ASSERT (mantissa == -0.5L);
169 for (; i >= LDBL_MIN_EXP - 100 && x < 0.0L; i--, x *= 0.5L)
172 long double mantissa = frexpl (x, &exp);
174 ASSERT (mantissa == -0.5L);
177 for (i = 1, x = 1.01L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
180 long double mantissa = frexpl (x, &exp);
182 ASSERT (mantissa == 0.505L);
184 for (i = 1, x = 1.01L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
187 long double mantissa = frexpl (x, &exp);
189 ASSERT (mantissa == 0.505L);
191 for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
194 long double mantissa = frexpl (x, &exp);
196 ASSERT (mantissa >= 0.5L);
197 ASSERT (mantissa < 1.0L);
198 ASSERT (mantissa == my_ldexp (x, - exp));
201 for (i = 1, x = 1.73205L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
204 long double mantissa = frexpl (x, &exp);
206 ASSERT (mantissa == 0.866025L);
208 for (i = 1, x = 1.73205L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
211 long double mantissa = frexpl (x, &exp);
213 ASSERT (mantissa == 0.866025L);
215 for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
218 long double mantissa = frexpl (x, &exp);
219 ASSERT (exp == i || exp == i + 1);
220 ASSERT (mantissa >= 0.5L);
221 ASSERT (mantissa < 1.0L);
222 ASSERT (mantissa == my_ldexp (x, - exp));