1 /* Split a double into fraction and mantissa, for hexadecimal printf.
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. */
20 #if !(defined USE_LONG_DOUBLE && !HAVE_LONG_DOUBLE)
23 # ifdef USE_LONG_DOUBLE
24 # include "printf-frexpl.h"
26 # include "printf-frexp.h"
32 /* This file assumes FLT_RADIX = 2. If FLT_RADIX is a power of 2 greater
33 than 2, or not even a power of 2, some rounding errors can occur, so that
34 then the returned mantissa is only guaranteed to be <= 2.0, not < 2.0. */
36 # ifdef USE_LONG_DOUBLE
37 # define FUNC printf_frexpl
38 # define DOUBLE long double
39 # define MIN_EXP LDBL_MIN_EXP
40 # if HAVE_FREXPL_IN_LIBC && HAVE_LDEXPL_IN_LIBC
41 # define USE_FREXP_LDEXP
45 # define L_(literal) literal##L
47 # define FUNC printf_frexp
48 # define DOUBLE double
49 # define MIN_EXP DBL_MIN_EXP
50 # if HAVE_FREXP_IN_LIBC && HAVE_LDEXP_IN_LIBC
51 # define USE_FREXP_LDEXP
55 # define L_(literal) literal
59 FUNC (DOUBLE x, int *exp)
63 # ifdef USE_FREXP_LDEXP
64 /* frexp and ldexp are usually faster than the loop below. */
65 x = FREXP (x, &exponent);
70 if (exponent < MIN_EXP - 1)
72 x = LDEXP (x, exponent - (MIN_EXP - 1));
73 exponent = MIN_EXP - 1;
76 /* Since the exponent is an 'int', it fits in 64 bits. Therefore the
77 loops are executed no more than 64 times. */
78 DOUBLE pow2[64]; /* pow2[i] = 2^2^i */
79 DOUBLE powh[64]; /* powh[i] = 2^-2^i */
85 /* A nonnegative exponent. */
87 DOUBLE pow2_i; /* = pow2[i] */
88 DOUBLE powh_i; /* = powh[i] */
90 /* Invariants: pow2_i = 2^2^i, powh_i = 2^-2^i,
91 x * 2^exponent = argument, x >= 1.0. */
92 for (i = 0, pow2_i = L_(2.0), powh_i = L_(0.5);
94 i++, pow2_i = pow2_i * pow2_i, powh_i = powh_i * powh_i)
108 /* Here 1.0 <= x < 2^2^i. */
112 /* A negative exponent. */
114 DOUBLE pow2_i; /* = pow2[i] */
115 DOUBLE powh_i; /* = powh[i] */
117 /* Invariants: pow2_i = 2^2^i, powh_i = 2^-2^i,
118 x * 2^exponent = argument, x < 1.0, exponent >= MIN_EXP - 1. */
119 for (i = 0, pow2_i = L_(2.0), powh_i = L_(0.5);
121 i++, pow2_i = pow2_i * pow2_i, powh_i = powh_i * powh_i)
123 if (exponent - (1 << i) < MIN_EXP - 1)
126 exponent -= (1 << i);
135 /* Here either x < 1.0 and exponent - 2^i < MIN_EXP - 1 <= exponent,
136 or 1.0 <= x < 2^2^i and exponent >= MIN_EXP - 1. */
139 /* Invariants: x * 2^exponent = argument, x < 1.0 and
140 exponent - 2^i < MIN_EXP - 1 <= exponent. */
144 if (exponent - (1 << i) >= MIN_EXP - 1)
146 exponent -= (1 << i);
153 /* Here either x < 1.0 and exponent = MIN_EXP - 1,
154 or 1.0 <= x < 2^2^i and exponent >= MIN_EXP - 1. */
157 /* Invariants: x * 2^exponent = argument, and
158 either x < 1.0 and exponent = MIN_EXP - 1,
159 or 1.0 <= x < 2^2^i and exponent >= MIN_EXP - 1. */
165 exponent += (1 << i);
169 /* Here either x < 1.0 and exponent = MIN_EXP - 1,
170 or 1.0 <= x < 2.0 and exponent >= MIN_EXP - 1. */
179 /* This declaration is solely to ensure that after preprocessing
180 this file is never empty. */