1 /* Test of splitting a 'long double' into fraction and mantissa.
2 Copyright (C) 2007 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 #define ASSERT(expr) \
35 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
41 /* On MIPS IRIX machines, LDBL_MIN_EXP is -1021, but the smallest reliable
42 exponent for 'long double' is -964. Similarly, on PowerPC machines,
43 LDBL_MIN_EXP is -1021, but the smallest reliable exponent for 'long double'
44 is -968. For exponents below that, the precision may be truncated to the
45 precision used for 'double'. */
47 # define MIN_NORMAL_EXP (LDBL_MIN_EXP + 57)
48 #elif defined __ppc || defined __ppc__ || defined __powerpc || defined __powerpc__
49 # define MIN_NORMAL_EXP (LDBL_MIN_EXP + 53)
51 # define MIN_NORMAL_EXP LDBL_MIN_EXP
55 my_ldexp (long double x, int d)
69 DECL_LONG_DOUBLE_ROUNDING
71 BEGIN_LONG_DOUBLE_ROUNDING ();
77 mantissa = frexpl (x, &exp);
78 ASSERT (isnanl (mantissa));
81 { /* Positive infinity. */
85 mantissa = frexpl (x, &exp);
86 ASSERT (mantissa == x);
89 { /* Negative infinity. */
93 mantissa = frexpl (x, &exp);
94 ASSERT (mantissa == x);
97 { /* Positive zero. */
101 mantissa = frexpl (x, &exp);
103 ASSERT (mantissa == x);
104 ASSERT (!signbit (mantissa));
107 { /* Negative zero. */
109 long double mantissa;
111 mantissa = frexpl (x, &exp);
113 ASSERT (mantissa == x);
114 ASSERT (signbit (mantissa));
117 for (i = 1, x = 1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
120 long double mantissa = frexpl (x, &exp);
122 ASSERT (mantissa == 0.5L);
124 for (i = 1, x = 1.0L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
127 long double mantissa = frexpl (x, &exp);
129 ASSERT (mantissa == 0.5L);
131 for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
134 long double mantissa = frexpl (x, &exp);
136 ASSERT (mantissa == 0.5L);
139 for (i = 1, x = -1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
142 long double mantissa = frexpl (x, &exp);
144 ASSERT (mantissa == -0.5L);
146 for (i = 1, x = -1.0L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
149 long double mantissa = frexpl (x, &exp);
151 ASSERT (mantissa == -0.5L);
153 for (; i >= LDBL_MIN_EXP - 100 && x < 0.0L; i--, x *= 0.5L)
156 long double mantissa = frexpl (x, &exp);
158 ASSERT (mantissa == -0.5L);
161 for (i = 1, x = 1.01L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
164 long double mantissa = frexpl (x, &exp);
166 ASSERT (mantissa == 0.505L);
168 for (i = 1, x = 1.01L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
171 long double mantissa = frexpl (x, &exp);
173 ASSERT (mantissa == 0.505L);
175 for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
178 long double mantissa = frexpl (x, &exp);
180 ASSERT (mantissa >= 0.5L);
181 ASSERT (mantissa < 1.0L);
182 ASSERT (mantissa == my_ldexp (x, - exp));
185 for (i = 1, x = 1.73205L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
188 long double mantissa = frexpl (x, &exp);
190 ASSERT (mantissa == 0.866025L);
192 for (i = 1, x = 1.73205L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
195 long double mantissa = frexpl (x, &exp);
197 ASSERT (mantissa == 0.866025L);
199 for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
202 long double mantissa = frexpl (x, &exp);
203 ASSERT (exp == i || exp == i + 1);
204 ASSERT (mantissa >= 0.5L);
205 ASSERT (mantissa < 1.0L);
206 ASSERT (mantissa == my_ldexp (x, - exp));