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
44 /* glibc (2.3..2.5 at least) and MacOS X 10.3 have frexpl and ldexpl in
45 libc, but don't declare them. */
46 # if !HAVE_DECL_FREXPL
47 extern long double frexpl (long double, int *);
49 # if !HAVE_DECL_LDEXPL
50 extern long double ldexpl (long double, int);
53 # define L_(literal) literal##L
55 # define FUNC printf_frexp
56 # define DOUBLE double
57 # define MIN_EXP DBL_MIN_EXP
58 # if HAVE_FREXP_IN_LIBC && HAVE_LDEXP_IN_LIBC
59 # define USE_FREXP_LDEXP
63 # define L_(literal) literal
67 FUNC (DOUBLE x, int *exp)
71 # ifdef USE_FREXP_LDEXP
72 /* frexp and ldexp are usually faster than the loop below. */
73 x = FREXP (x, &exponent);
78 if (exponent < MIN_EXP - 1)
80 x = LDEXP (x, exponent - (MIN_EXP - 1));
81 exponent = MIN_EXP - 1;
84 /* Since the exponent is an 'int', it fits in 64 bits. Therefore the
85 loops are executed no more than 64 times. */
86 DOUBLE pow2[64]; /* pow2[i] = 2^2^i */
87 DOUBLE powh[64]; /* powh[i] = 2^-2^i */
93 /* A nonnegative exponent. */
95 DOUBLE pow2_i; /* = pow2[i] */
96 DOUBLE powh_i; /* = powh[i] */
98 /* Invariants: pow2_i = 2^2^i, powh_i = 2^-2^i,
99 x * 2^exponent = argument, x >= 1.0. */
100 for (i = 0, pow2_i = L_(2.0), powh_i = L_(0.5);
102 i++, pow2_i = pow2_i * pow2_i, powh_i = powh_i * powh_i)
106 exponent += (1 << i);
116 /* Here 1.0 <= x < 2^2^i. */
120 /* A negative exponent. */
122 DOUBLE pow2_i; /* = pow2[i] */
123 DOUBLE powh_i; /* = powh[i] */
125 /* Invariants: pow2_i = 2^2^i, powh_i = 2^-2^i,
126 x * 2^exponent = argument, x < 1.0, exponent >= MIN_EXP - 1. */
127 for (i = 0, pow2_i = L_(2.0), powh_i = L_(0.5);
129 i++, pow2_i = pow2_i * pow2_i, powh_i = powh_i * powh_i)
131 if (exponent - (1 << i) < MIN_EXP - 1)
134 exponent -= (1 << i);
143 /* Here either x < 1.0 and exponent - 2^i < MIN_EXP - 1 <= exponent,
144 or 1.0 <= x < 2^2^i and exponent >= MIN_EXP - 1. */
147 /* Invariants: x * 2^exponent = argument, x < 1.0 and
148 exponent - 2^i < MIN_EXP - 1 <= exponent. */
152 if (exponent - (1 << i) >= MIN_EXP - 1)
154 exponent -= (1 << i);
161 /* Here either x < 1.0 and exponent = MIN_EXP - 1,
162 or 1.0 <= x < 2^2^i and exponent >= MIN_EXP - 1. */
165 /* Invariants: x * 2^exponent = argument, and
166 either x < 1.0 and exponent = MIN_EXP - 1,
167 or 1.0 <= x < 2^2^i and exponent >= MIN_EXP - 1. */
173 exponent += (1 << i);
177 /* Here either x < 1.0 and exponent = MIN_EXP - 1,
178 or 1.0 <= x < 2.0 and exponent >= MIN_EXP - 1. */
187 /* This declaration is solely to ensure that after preprocessing
188 this file is never empty. */