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. */
34 #define ASSERT(expr) \
39 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
46 /* On MIPS IRIX machines, LDBL_MIN_EXP is -1021, but the smallest reliable
47 exponent for 'long double' is -964. Similarly, on PowerPC machines,
48 LDBL_MIN_EXP is -1021, but the smallest reliable exponent for 'long double'
49 is -968. For exponents below that, the precision may be truncated to the
50 precision used for 'double'. */
52 # define MIN_NORMAL_EXP (LDBL_MIN_EXP + 57)
53 #elif defined __ppc || defined __ppc__ || defined __powerpc || defined __powerpc__
54 # define MIN_NORMAL_EXP (LDBL_MIN_EXP + 53)
56 # define MIN_NORMAL_EXP LDBL_MIN_EXP
60 my_ldexp (long double x, int d)
74 DECL_LONG_DOUBLE_ROUNDING
76 BEGIN_LONG_DOUBLE_ROUNDING ();
82 mantissa = frexpl (x, &exp);
83 ASSERT (isnanl (mantissa));
86 { /* Positive infinity. */
90 mantissa = frexpl (x, &exp);
91 ASSERT (mantissa == x);
94 { /* Negative infinity. */
98 mantissa = frexpl (x, &exp);
99 ASSERT (mantissa == x);
102 { /* Positive zero. */
104 long double mantissa;
106 mantissa = frexpl (x, &exp);
108 ASSERT (mantissa == x);
109 ASSERT (!signbit (mantissa));
112 { /* Negative zero. */
114 long double mantissa;
116 mantissa = frexpl (x, &exp);
118 ASSERT (mantissa == x);
119 ASSERT (signbit (mantissa));
122 for (i = 1, x = 1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
125 long double mantissa = frexpl (x, &exp);
127 ASSERT (mantissa == 0.5L);
129 for (i = 1, x = 1.0L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
132 long double mantissa = frexpl (x, &exp);
134 ASSERT (mantissa == 0.5L);
136 for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
139 long double mantissa = frexpl (x, &exp);
141 ASSERT (mantissa == 0.5L);
144 for (i = 1, x = -1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
147 long double mantissa = frexpl (x, &exp);
149 ASSERT (mantissa == -0.5L);
151 for (i = 1, x = -1.0L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
154 long double mantissa = frexpl (x, &exp);
156 ASSERT (mantissa == -0.5L);
158 for (; i >= LDBL_MIN_EXP - 100 && x < 0.0L; i--, x *= 0.5L)
161 long double mantissa = frexpl (x, &exp);
163 ASSERT (mantissa == -0.5L);
166 for (i = 1, x = 1.01L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
169 long double mantissa = frexpl (x, &exp);
171 ASSERT (mantissa == 0.505L);
173 for (i = 1, x = 1.01L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
176 long double mantissa = frexpl (x, &exp);
178 ASSERT (mantissa == 0.505L);
180 for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
183 long double mantissa = frexpl (x, &exp);
185 ASSERT (mantissa >= 0.5L);
186 ASSERT (mantissa < 1.0L);
187 ASSERT (mantissa == my_ldexp (x, - exp));
190 for (i = 1, x = 1.73205L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
193 long double mantissa = frexpl (x, &exp);
195 ASSERT (mantissa == 0.866025L);
197 for (i = 1, x = 1.73205L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
200 long double mantissa = frexpl (x, &exp);
202 ASSERT (mantissa == 0.866025L);
204 for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
207 long double mantissa = frexpl (x, &exp);
208 ASSERT (exp == i || exp == i + 1);
209 ASSERT (mantissa >= 0.5L);
210 ASSERT (mantissa < 1.0L);
211 ASSERT (mantissa == my_ldexp (x, - exp));