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. */
21 #ifdef USE_LONG_DOUBLE
22 # include "printf-frexpl.h"
24 # include "printf-frexp.h"
29 #ifdef USE_LONG_DOUBLE
33 /* This file assumes FLT_RADIX = 2. If FLT_RADIX is a power of 2 greater
34 than 2, or not even a power of 2, some rounding errors can occur, so that
35 then the returned mantissa is only guaranteed to be <= 2.0, not < 2.0. */
37 #ifdef USE_LONG_DOUBLE
38 # define FUNC printf_frexpl
39 # define DOUBLE long double
40 # define MIN_EXP LDBL_MIN_EXP
41 # if HAVE_FREXPL_IN_LIBC && HAVE_LDEXPL_IN_LIBC
42 # define USE_FREXP_LDEXP
46 # define DECL_ROUNDING DECL_LONG_DOUBLE_ROUNDING
47 # define BEGIN_ROUNDING() BEGIN_LONG_DOUBLE_ROUNDING ()
48 # define END_ROUNDING() END_LONG_DOUBLE_ROUNDING ()
49 # define L_(literal) literal##L
51 # define FUNC printf_frexp
52 # define DOUBLE double
53 # define MIN_EXP DBL_MIN_EXP
54 # if HAVE_FREXP_IN_LIBC && HAVE_LDEXP_IN_LIBC
55 # define USE_FREXP_LDEXP
59 # define DECL_ROUNDING
60 # define BEGIN_ROUNDING()
61 # define END_ROUNDING()
62 # define L_(literal) literal
66 FUNC (DOUBLE x, int *exp)
73 #ifdef USE_FREXP_LDEXP
74 /* frexp and ldexp are usually faster than the loop below. */
75 x = FREXP (x, &exponent);
80 if (exponent < MIN_EXP - 1)
82 x = LDEXP (x, exponent - (MIN_EXP - 1));
83 exponent = MIN_EXP - 1;
87 /* Since the exponent is an 'int', it fits in 64 bits. Therefore the
88 loops are executed no more than 64 times. */
89 DOUBLE pow2[64]; /* pow2[i] = 2^2^i */
90 DOUBLE powh[64]; /* powh[i] = 2^-2^i */
96 /* A nonnegative exponent. */
98 DOUBLE pow2_i; /* = pow2[i] */
99 DOUBLE powh_i; /* = powh[i] */
101 /* Invariants: pow2_i = 2^2^i, powh_i = 2^-2^i,
102 x * 2^exponent = argument, x >= 1.0. */
103 for (i = 0, pow2_i = L_(2.0), powh_i = L_(0.5);
105 i++, pow2_i = pow2_i * pow2_i, powh_i = powh_i * powh_i)
109 exponent += (1 << i);
119 /* Here 1.0 <= x < 2^2^i. */
123 /* A negative exponent. */
125 DOUBLE pow2_i; /* = pow2[i] */
126 DOUBLE powh_i; /* = powh[i] */
128 /* Invariants: pow2_i = 2^2^i, powh_i = 2^-2^i,
129 x * 2^exponent = argument, x < 1.0, exponent >= MIN_EXP - 1. */
130 for (i = 0, pow2_i = L_(2.0), powh_i = L_(0.5);
132 i++, pow2_i = pow2_i * pow2_i, powh_i = powh_i * powh_i)
134 if (exponent - (1 << i) < MIN_EXP - 1)
137 exponent -= (1 << i);
146 /* Here either x < 1.0 and exponent - 2^i < MIN_EXP - 1 <= exponent,
147 or 1.0 <= x < 2^2^i and exponent >= MIN_EXP - 1. */
150 /* Invariants: x * 2^exponent = argument, x < 1.0 and
151 exponent - 2^i < MIN_EXP - 1 <= exponent. */
155 if (exponent - (1 << i) >= MIN_EXP - 1)
157 exponent -= (1 << i);
164 /* Here either x < 1.0 and exponent = MIN_EXP - 1,
165 or 1.0 <= x < 2^2^i and exponent >= MIN_EXP - 1. */
168 /* Invariants: x * 2^exponent = argument, and
169 either x < 1.0 and exponent = MIN_EXP - 1,
170 or 1.0 <= x < 2^2^i and exponent >= MIN_EXP - 1. */
176 exponent += (1 << i);
180 /* Here either x < 1.0 and exponent = MIN_EXP - 1,
181 or 1.0 <= x < 2.0 and exponent >= MIN_EXP - 1. */