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. */
28 #include "isnanl-nolibm.h"
31 /* Avoid some warnings from "gcc -Wshadow".
32 This file doesn't use the exp() function. */
36 #define ASSERT(expr) \
41 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
48 /* On MIPS IRIX machines, LDBL_MIN_EXP is -1021, but the smallest reliable
49 exponent for 'long double' is -964. Similarly, on PowerPC machines,
50 LDBL_MIN_EXP is -1021, but the smallest reliable exponent for 'long double'
51 is -968. For exponents below that, the precision may be truncated to the
52 precision used for 'double'. */
54 # define MIN_NORMAL_EXP (LDBL_MIN_EXP + 57)
55 #elif defined __ppc || defined __ppc__ || defined __powerpc || defined __powerpc__
56 # define MIN_NORMAL_EXP (LDBL_MIN_EXP + 53)
58 # define MIN_NORMAL_EXP LDBL_MIN_EXP
61 /* On HP-UX 10.20, negating 0.0L does not yield -0.0L.
62 So we use minus_zero instead.
63 IRIX cc can't put -0.0L into .data, but can compute at runtime.
64 Note that the expression -LDBL_MIN * LDBL_MIN does not work on other
65 platforms, such as when cross-compiling to PowerPC on MacOS X 10.5. */
66 #if defined __hpux || defined __sgi
68 compute_minus_zero (void)
70 return -LDBL_MIN * LDBL_MIN;
72 # define minus_zero compute_minus_zero ()
74 long double minus_zero = -0.0L;
78 my_ldexp (long double x, int d)
92 DECL_LONG_DOUBLE_ROUNDING
94 BEGIN_LONG_DOUBLE_ROUNDING ();
100 mantissa = frexpl (x, &exp);
101 ASSERT (isnanl (mantissa));
104 { /* Positive infinity. */
106 long double mantissa;
108 mantissa = frexpl (x, &exp);
109 ASSERT (mantissa == x);
112 { /* Negative infinity. */
114 long double mantissa;
116 mantissa = frexpl (x, &exp);
117 ASSERT (mantissa == x);
120 { /* Positive zero. */
122 long double mantissa;
124 mantissa = frexpl (x, &exp);
126 ASSERT (mantissa == x);
127 ASSERT (!signbit (mantissa));
130 { /* Negative zero. */
132 long double mantissa;
134 mantissa = frexpl (x, &exp);
136 ASSERT (mantissa == x);
137 ASSERT (signbit (mantissa));
140 for (i = 1, x = 1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
143 long double mantissa = frexpl (x, &exp);
145 ASSERT (mantissa == 0.5L);
147 for (i = 1, x = 1.0L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
150 long double mantissa = frexpl (x, &exp);
152 ASSERT (mantissa == 0.5L);
154 for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
157 long double mantissa = frexpl (x, &exp);
159 ASSERT (mantissa == 0.5L);
162 for (i = 1, x = -1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
165 long double mantissa = frexpl (x, &exp);
167 ASSERT (mantissa == -0.5L);
169 for (i = 1, x = -1.0L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
172 long double mantissa = frexpl (x, &exp);
174 ASSERT (mantissa == -0.5L);
176 for (; i >= LDBL_MIN_EXP - 100 && x < 0.0L; i--, x *= 0.5L)
179 long double mantissa = frexpl (x, &exp);
181 ASSERT (mantissa == -0.5L);
184 for (i = 1, x = 1.01L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
187 long double mantissa = frexpl (x, &exp);
189 ASSERT (mantissa == 0.505L);
191 for (i = 1, x = 1.01L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
194 long double mantissa = frexpl (x, &exp);
196 ASSERT (mantissa == 0.505L);
198 for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
201 long double mantissa = frexpl (x, &exp);
203 ASSERT (mantissa >= 0.5L);
204 ASSERT (mantissa < 1.0L);
205 ASSERT (mantissa == my_ldexp (x, - exp));
208 for (i = 1, x = 1.73205L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
211 long double mantissa = frexpl (x, &exp);
213 ASSERT (mantissa == 0.866025L);
215 for (i = 1, x = 1.73205L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
218 long double mantissa = frexpl (x, &exp);
220 ASSERT (mantissa == 0.866025L);
222 for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
225 long double mantissa = frexpl (x, &exp);
226 ASSERT (exp == i || exp == i + 1);
227 ASSERT (mantissa >= 0.5L);
228 ASSERT (mantissa < 1.0L);
229 ASSERT (mantissa == my_ldexp (x, - exp));