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__); \
45 /* On MIPS IRIX machines, LDBL_MIN_EXP is -1021, but the smallest reliable
46 exponent for 'long double' is -964. Similarly, on PowerPC machines,
47 LDBL_MIN_EXP is -1021, but the smallest reliable exponent for 'long double'
48 is -968. For exponents below that, the precision may be truncated to the
49 precision used for 'double'. */
51 # define MIN_NORMAL_EXP (LDBL_MIN_EXP + 57)
52 #elif defined __ppc || defined __ppc__ || defined __powerpc || defined __powerpc__
53 # define MIN_NORMAL_EXP (LDBL_MIN_EXP + 53)
55 # define MIN_NORMAL_EXP LDBL_MIN_EXP
59 my_ldexp (long double x, int d)
73 DECL_LONG_DOUBLE_ROUNDING
75 BEGIN_LONG_DOUBLE_ROUNDING ();
81 mantissa = frexpl (x, &exp);
82 ASSERT (isnanl (mantissa));
85 { /* Positive infinity. */
89 mantissa = frexpl (x, &exp);
90 ASSERT (mantissa == x);
93 { /* Negative infinity. */
97 mantissa = frexpl (x, &exp);
98 ASSERT (mantissa == x);
101 { /* Positive zero. */
103 long double mantissa;
105 mantissa = frexpl (x, &exp);
107 ASSERT (mantissa == x);
108 ASSERT (!signbit (mantissa));
111 { /* Negative zero. */
113 long double mantissa;
115 mantissa = frexpl (x, &exp);
117 ASSERT (mantissa == x);
118 ASSERT (signbit (mantissa));
121 for (i = 1, x = 1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
124 long double mantissa = frexpl (x, &exp);
126 ASSERT (mantissa == 0.5L);
128 for (i = 1, x = 1.0L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
131 long double mantissa = frexpl (x, &exp);
133 ASSERT (mantissa == 0.5L);
135 for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
138 long double mantissa = frexpl (x, &exp);
140 ASSERT (mantissa == 0.5L);
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.01L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
168 long double mantissa = frexpl (x, &exp);
170 ASSERT (mantissa == 0.505L);
172 for (i = 1, x = 1.01L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
175 long double mantissa = frexpl (x, &exp);
177 ASSERT (mantissa == 0.505L);
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);
185 ASSERT (mantissa < 1.0L);
186 ASSERT (mantissa == my_ldexp (x, - exp));
189 for (i = 1, x = 1.73205L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
192 long double mantissa = frexpl (x, &exp);
194 ASSERT (mantissa == 0.866025L);
196 for (i = 1, x = 1.73205L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
199 long double mantissa = frexpl (x, &exp);
201 ASSERT (mantissa == 0.866025L);
203 for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
206 long double mantissa = frexpl (x, &exp);
207 ASSERT (exp == i || exp == i + 1);
208 ASSERT (mantissa >= 0.5L);
209 ASSERT (mantissa < 1.0L);
210 ASSERT (mantissa == my_ldexp (x, - exp));