1 /* Split a 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 2, or (at your option)
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 along
15 with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* Written by Paolo Bonzini <bonzini@gnu.org>, 2003, and
19 Bruno Haible <bruno@clisp.org>, 2007. */
23 #if !(defined USE_LONG_DOUBLE && !HAVE_LONG_DOUBLE)
29 # ifdef USE_LONG_DOUBLE
30 # include "isnanl-nolibm.h"
35 /* This file assumes FLT_RADIX = 2. If FLT_RADIX is a power of 2 greater
36 than 2, or not even a power of 2, some rounding errors can occur, so that
37 then the returned mantissa is only guaranteed to be <= 1.0, not < 1.0. */
39 # ifdef USE_LONG_DOUBLE
41 # define DOUBLE long double
43 # define L_(literal) literal##L
46 # define DOUBLE double
48 # define L_(literal) literal
52 FUNC (DOUBLE x, int *exp)
57 /* Test for NaN, infinity, and zero. */
58 if (ISNAN (x) || x + x == x)
72 /* Since the exponent is an 'int', it fits in 64 bits. Therefore the
73 loops are executed no more than 64 times. */
74 DOUBLE pow2[64]; /* pow2[i] = 2^2^i */
75 DOUBLE powh[64]; /* powh[i] = 2^-2^i */
81 /* A positive exponent. */
82 DOUBLE pow2_i; /* = pow2[i] */
83 DOUBLE powh_i; /* = powh[i] */
85 /* Invariants: pow2_i = 2^2^i, powh_i = 2^-2^i,
86 x * 2^exponent = argument, x >= 1.0. */
87 for (i = 0, pow2_i = L_(2.0), powh_i = L_(0.5);
89 i++, pow2_i = pow2_i * pow2_i, powh_i = powh_i * powh_i)
102 /* Avoid making x too small, as it could become a denormalized
103 number and thus lose precision. */
104 while (i > 0 && x < pow2[i - 1])
109 exponent += (1 << i);
111 /* Here 2^-2^i <= x < 1.0. */
115 /* A negative or zero exponent. */
116 DOUBLE pow2_i; /* = pow2[i] */
117 DOUBLE powh_i; /* = powh[i] */
119 /* Invariants: pow2_i = 2^2^i, powh_i = 2^-2^i,
120 x * 2^exponent = argument, x < 1.0. */
121 for (i = 0, pow2_i = L_(2.0), powh_i = L_(0.5);
123 i++, pow2_i = pow2_i * pow2_i, powh_i = powh_i * powh_i)
127 exponent -= (1 << i);
136 /* Here 2^-2^i <= x < 1.0. */
139 /* Invariants: x * 2^exponent = argument, and 2^-2^i <= x < 1.0. */
145 exponent -= (1 << i);
149 /* Here 0.5 <= x < 1.0. */
153 return (sign < 0 ? - x : x);
158 /* This declaration is solely to ensure that after preprocessing
159 this file is never empty. */