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
61 my_ldexp (long double x, int d)
75 DECL_LONG_DOUBLE_ROUNDING
77 BEGIN_LONG_DOUBLE_ROUNDING ();
83 mantissa = frexpl (x, &exp);
84 ASSERT (isnanl (mantissa));
87 { /* Positive infinity. */
91 mantissa = frexpl (x, &exp);
92 ASSERT (mantissa == x);
95 { /* Negative infinity. */
99 mantissa = frexpl (x, &exp);
100 ASSERT (mantissa == x);
103 { /* Positive zero. */
105 long double mantissa;
107 mantissa = frexpl (x, &exp);
109 ASSERT (mantissa == x);
110 ASSERT (!signbit (mantissa));
113 { /* Negative zero. */
115 long double mantissa;
117 mantissa = frexpl (x, &exp);
119 ASSERT (mantissa == x);
120 ASSERT (signbit (mantissa));
123 for (i = 1, x = 1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
126 long double mantissa = frexpl (x, &exp);
128 ASSERT (mantissa == 0.5L);
130 for (i = 1, x = 1.0L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
133 long double mantissa = frexpl (x, &exp);
135 ASSERT (mantissa == 0.5L);
137 for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
140 long double mantissa = frexpl (x, &exp);
142 ASSERT (mantissa == 0.5L);
145 for (i = 1, x = -1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
148 long double mantissa = frexpl (x, &exp);
150 ASSERT (mantissa == -0.5L);
152 for (i = 1, x = -1.0L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
155 long double mantissa = frexpl (x, &exp);
157 ASSERT (mantissa == -0.5L);
159 for (; i >= LDBL_MIN_EXP - 100 && x < 0.0L; i--, x *= 0.5L)
162 long double mantissa = frexpl (x, &exp);
164 ASSERT (mantissa == -0.5L);
167 for (i = 1, x = 1.01L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
170 long double mantissa = frexpl (x, &exp);
172 ASSERT (mantissa == 0.505L);
174 for (i = 1, x = 1.01L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
177 long double mantissa = frexpl (x, &exp);
179 ASSERT (mantissa == 0.505L);
181 for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
184 long double mantissa = frexpl (x, &exp);
186 ASSERT (mantissa >= 0.5L);
187 ASSERT (mantissa < 1.0L);
188 ASSERT (mantissa == my_ldexp (x, - exp));
191 for (i = 1, x = 1.73205L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
194 long double mantissa = frexpl (x, &exp);
196 ASSERT (mantissa == 0.866025L);
198 for (i = 1, x = 1.73205L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
201 long double mantissa = frexpl (x, &exp);
203 ASSERT (mantissa == 0.866025L);
205 for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
208 long double mantissa = frexpl (x, &exp);
209 ASSERT (exp == i || exp == i + 1);
210 ASSERT (mantissa >= 0.5L);
211 ASSERT (mantissa < 1.0L);
212 ASSERT (mantissa == my_ldexp (x, - exp));