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 # ifdef USE_LONG_DOUBLE
33 # define FUNC printf_frexpl
34 # define DOUBLE long double
35 # define MIN_EXP LDBL_MIN_EXP
36 # if HAVE_FREXPL_IN_LIBC && HAVE_LDEXPL_IN_LIBC
37 # define USE_FREXP_LDEXP
41 # define L_(literal) literal##L
43 # define FUNC printf_frexp
44 # define DOUBLE double
45 # define MIN_EXP DBL_MIN_EXP
46 # if HAVE_FREXP_IN_LIBC && HAVE_LDEXP_IN_LIBC
47 # define USE_FREXP_LDEXP
51 # define L_(literal) literal
55 FUNC (DOUBLE x, int *exp)
59 # ifdef USE_FREXP_LDEXP
60 /* frexp and ldexp are usually faster than the loop below. */
61 x = FREXP (x, &exponent);
66 if (exponent < MIN_EXP - 1)
68 x = LDEXP (x, exponent - (MIN_EXP - 1));
69 exponent = MIN_EXP - 1;
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 nonnegative exponent. */
83 DOUBLE pow2_i; /* = pow2[i] */
84 DOUBLE powh_i; /* = powh[i] */
86 /* Invariants: pow2_i = 2^2^i, powh_i = 2^-2^i,
87 x * 2^exponent = argument, x >= 1.0. */
88 for (i = 0, pow2_i = L_(2.0), powh_i = L_(0.5);
90 i++, pow2_i = pow2_i * pow2_i, powh_i = powh_i * powh_i)
104 /* Here 1.0 <= x < 2^2^i. */
108 /* A negative exponent. */
110 DOUBLE pow2_i; /* = pow2[i] */
111 DOUBLE powh_i; /* = powh[i] */
113 /* Invariants: pow2_i = 2^2^i, powh_i = 2^-2^i,
114 x * 2^exponent = argument, x < 1.0, exponent >= MIN_EXP - 1. */
115 for (i = 0, pow2_i = L_(2.0), powh_i = L_(0.5);
117 i++, pow2_i = pow2_i * pow2_i, powh_i = powh_i * powh_i)
119 if (exponent - (1 << i) < MIN_EXP - 1)
122 exponent -= (1 << i);
131 /* Here either x < 1.0 and exponent - 2^i < MIN_EXP - 1 <= exponent,
132 or 1.0 <= x < 2^2^i and exponent >= MIN_EXP - 1. */
135 /* Invariants: x * 2^exponent = argument, x < 1.0 and
136 exponent - 2^i < MIN_EXP - 1 <= exponent. */
140 if (exponent - (1 << i) >= MIN_EXP - 1)
142 exponent -= (1 << i);
149 /* Here either x < 1.0 and exponent = MIN_EXP - 1,
150 or 1.0 <= x < 2^2^i and exponent >= MIN_EXP - 1. */
153 /* Invariants: x * 2^exponent = argument, and
154 either x < 1.0 and exponent = MIN_EXP - 1,
155 or 1.0 <= x < 2^2^i and exponent >= MIN_EXP - 1. */
161 exponent += (1 << i);
165 /* Here either x < 1.0 and exponent = MIN_EXP - 1,
166 or 1.0 <= x < 2.0 and exponent >= MIN_EXP - 1. */
175 /* This declaration is solely to ensure that after preprocessing
176 this file is never empty. */