1 /* Split a double into fraction and mantissa, for hexadecimal printf.
2 Copyright (C) 2007, 2009-2017 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 3 of the License, or
7 (at your option) any later version.
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
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 #if ! defined USE_LONG_DOUBLE
22 #ifdef USE_LONG_DOUBLE
23 # include "printf-frexpl.h"
25 # include "printf-frexp.h"
30 #ifdef USE_LONG_DOUBLE
34 /* This file assumes FLT_RADIX = 2. If FLT_RADIX is a power of 2 greater
35 than 2, or not even a power of 2, some rounding errors can occur, so that
36 then the returned mantissa is only guaranteed to be <= 2.0, not < 2.0. */
38 #ifdef USE_LONG_DOUBLE
39 # define FUNC printf_frexpl
40 # define DOUBLE long double
41 # define MIN_EXP LDBL_MIN_EXP
42 # if HAVE_FREXPL_IN_LIBC && HAVE_LDEXPL_IN_LIBC
43 # define USE_FREXP_LDEXP
47 # define DECL_ROUNDING DECL_LONG_DOUBLE_ROUNDING
48 # define BEGIN_ROUNDING() BEGIN_LONG_DOUBLE_ROUNDING ()
49 # define END_ROUNDING() END_LONG_DOUBLE_ROUNDING ()
50 # define L_(literal) literal##L
52 # define FUNC printf_frexp
53 # define DOUBLE double
54 # define MIN_EXP DBL_MIN_EXP
55 # if HAVE_FREXP_IN_LIBC && HAVE_LDEXP_IN_LIBC
56 # define USE_FREXP_LDEXP
60 # define DECL_ROUNDING
61 # define BEGIN_ROUNDING()
62 # define END_ROUNDING()
63 # define L_(literal) literal
67 FUNC (DOUBLE x
, int *expptr
)
74 #ifdef USE_FREXP_LDEXP
75 /* frexp and ldexp are usually faster than the loop below. */
76 x
= FREXP (x
, &exponent
);
81 if (exponent
< MIN_EXP
- 1)
83 x
= LDEXP (x
, exponent
- (MIN_EXP
- 1));
84 exponent
= MIN_EXP
- 1;
88 /* Since the exponent is an 'int', it fits in 64 bits. Therefore the
89 loops are executed no more than 64 times. */
90 DOUBLE pow2
[64]; /* pow2[i] = 2^2^i */
91 DOUBLE powh
[64]; /* powh[i] = 2^-2^i */
97 /* A nonnegative exponent. */
99 DOUBLE pow2_i
; /* = pow2[i] */
100 DOUBLE powh_i
; /* = powh[i] */
102 /* Invariants: pow2_i = 2^2^i, powh_i = 2^-2^i,
103 x * 2^exponent = argument, x >= 1.0. */
104 for (i
= 0, pow2_i
= L_(2.0), powh_i
= L_(0.5);
106 i
++, pow2_i
= pow2_i
* pow2_i
, powh_i
= powh_i
* powh_i
)
110 exponent
+= (1 << i
);
120 /* Here 1.0 <= x < 2^2^i. */
124 /* A negative exponent. */
126 DOUBLE pow2_i
; /* = pow2[i] */
127 DOUBLE powh_i
; /* = powh[i] */
129 /* Invariants: pow2_i = 2^2^i, powh_i = 2^-2^i,
130 x * 2^exponent = argument, x < 1.0, exponent >= MIN_EXP - 1. */
131 for (i
= 0, pow2_i
= L_(2.0), powh_i
= L_(0.5);
133 i
++, pow2_i
= pow2_i
* pow2_i
, powh_i
= powh_i
* powh_i
)
135 if (exponent
- (1 << i
) < MIN_EXP
- 1)
138 exponent
-= (1 << i
);
147 /* Here either x < 1.0 and exponent - 2^i < MIN_EXP - 1 <= exponent,
148 or 1.0 <= x < 2^2^i and exponent >= MIN_EXP - 1. */
151 /* Invariants: x * 2^exponent = argument, x < 1.0 and
152 exponent - 2^i < MIN_EXP - 1 <= exponent. */
156 if (exponent
- (1 << i
) >= MIN_EXP
- 1)
158 exponent
-= (1 << i
);
165 /* Here either x < 1.0 and exponent = MIN_EXP - 1,
166 or 1.0 <= x < 2^2^i and exponent >= MIN_EXP - 1. */
169 /* Invariants: x * 2^exponent = argument, and
170 either x < 1.0 and exponent = MIN_EXP - 1,
171 or 1.0 <= x < 2^2^i and exponent >= MIN_EXP - 1. */
177 exponent
+= (1 << i
);
181 /* Here either x < 1.0 and exponent = MIN_EXP - 1,
182 or 1.0 <= x < 2.0 and exponent >= MIN_EXP - 1. */