| 1 |
/* Split a double into fraction and mantissa, for hexadecimal printf. |
| 2 |
Copyright (C) 2007, 2009-2026 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is free software: you can redistribute it and/or modify |
| 5 |
it under the terms of the GNU Lesser General Public License as |
| 6 |
published by the Free Software Foundation; either version 2.1 of the |
| 7 |
License, or (at your option) any later version. |
| 8 |
|
| 9 |
This file 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 Lesser General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU Lesser General Public License |
| 15 |
along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 16 |
|
| 17 |
#if ! defined USE_LONG_DOUBLE |
| 18 |
# include <config.h> |
| 19 |
#endif |
| 20 |
|
| 21 |
/* Specification. */ |
| 22 |
#ifdef USE_LONG_DOUBLE |
| 23 |
# include "printf-frexpl.h" |
| 24 |
#else |
| 25 |
# include "printf-frexp.h" |
| 26 |
#endif |
| 27 |
|
| 28 |
#include <float.h> |
| 29 |
#include <math.h> |
| 30 |
#ifdef USE_LONG_DOUBLE |
| 31 |
# include "fpucw.h" |
| 32 |
#endif |
| 33 |
|
| 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. */ |
| 37 |
|
| 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 |
| 44 |
# define FREXP frexpl |
| 45 |
# define LDEXP ldexpl |
| 46 |
# endif |
| 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 |
| 51 |
#else |
| 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 |
| 57 |
# define FREXP frexp |
| 58 |
# define LDEXP ldexp |
| 59 |
# endif |
| 60 |
# define DECL_ROUNDING |
| 61 |
# define BEGIN_ROUNDING() |
| 62 |
# define END_ROUNDING() |
| 63 |
# define L_(literal) literal |
| 64 |
#endif |
| 65 |
|
| 66 |
DOUBLE |
| 67 |
FUNC (DOUBLE x, int *expptr) |
| 68 |
{ |
| 69 |
DECL_ROUNDING |
| 70 |
|
| 71 |
BEGIN_ROUNDING (); |
| 72 |
|
| 73 |
int exponent; |
| 74 |
|
| 75 |
#ifdef USE_FREXP_LDEXP |
| 76 |
/* frexp and ldexp are usually faster than the loop below. */ |
| 77 |
x = FREXP (x, &exponent); |
| 78 |
|
| 79 |
x = x + x; |
| 80 |
exponent -= 1; |
| 81 |
|
| 82 |
if (exponent < MIN_EXP - 1) |
| 83 |
{ |
| 84 |
x = LDEXP (x, exponent - (MIN_EXP - 1)); |
| 85 |
exponent = MIN_EXP - 1; |
| 86 |
} |
| 87 |
#else |
| 88 |
{ |
| 89 |
/* Since the exponent is an 'int', it fits in 64 bits. Therefore the |
| 90 |
loops are executed no more than 64 times. */ |
| 91 |
DOUBLE pow2[64]; /* pow2[i] = 2^2^i */ |
| 92 |
DOUBLE powh[64]; /* powh[i] = 2^-2^i */ |
| 93 |
int i; |
| 94 |
|
| 95 |
exponent = 0; |
| 96 |
if (x >= L_(1.0)) |
| 97 |
{ |
| 98 |
/* A nonnegative exponent. */ |
| 99 |
{ |
| 100 |
DOUBLE pow2_i; /* = pow2[i] */ |
| 101 |
DOUBLE powh_i; /* = powh[i] */ |
| 102 |
|
| 103 |
/* Invariants: pow2_i = 2^2^i, powh_i = 2^-2^i, |
| 104 |
x * 2^exponent = argument, x >= 1.0. */ |
| 105 |
for (i = 0, pow2_i = L_(2.0), powh_i = L_(0.5); |
| 106 |
; |
| 107 |
i++, pow2_i = pow2_i * pow2_i, powh_i = powh_i * powh_i) |
| 108 |
{ |
| 109 |
if (x >= pow2_i) |
| 110 |
{ |
| 111 |
exponent += (1 << i); |
| 112 |
x *= powh_i; |
| 113 |
} |
| 114 |
else |
| 115 |
break; |
| 116 |
|
| 117 |
pow2[i] = pow2_i; |
| 118 |
powh[i] = powh_i; |
| 119 |
} |
| 120 |
} |
| 121 |
/* Here 1.0 <= x < 2^2^i. */ |
| 122 |
} |
| 123 |
else |
| 124 |
{ |
| 125 |
/* A negative exponent. */ |
| 126 |
{ |
| 127 |
DOUBLE pow2_i; /* = pow2[i] */ |
| 128 |
DOUBLE powh_i; /* = powh[i] */ |
| 129 |
|
| 130 |
/* Invariants: pow2_i = 2^2^i, powh_i = 2^-2^i, |
| 131 |
x * 2^exponent = argument, x < 1.0, exponent >= MIN_EXP - 1. */ |
| 132 |
for (i = 0, pow2_i = L_(2.0), powh_i = L_(0.5); |
| 133 |
; |
| 134 |
i++, pow2_i = pow2_i * pow2_i, powh_i = powh_i * powh_i) |
| 135 |
{ |
| 136 |
if (exponent - (1 << i) < MIN_EXP - 1) |
| 137 |
break; |
| 138 |
|
| 139 |
exponent -= (1 << i); |
| 140 |
x *= pow2_i; |
| 141 |
if (x >= L_(1.0)) |
| 142 |
break; |
| 143 |
|
| 144 |
pow2[i] = pow2_i; |
| 145 |
powh[i] = powh_i; |
| 146 |
} |
| 147 |
} |
| 148 |
/* Here either x < 1.0 and exponent - 2^i < MIN_EXP - 1 <= exponent, |
| 149 |
or 1.0 <= x < 2^2^i and exponent >= MIN_EXP - 1. */ |
| 150 |
|
| 151 |
if (x < L_(1.0)) |
| 152 |
/* Invariants: x * 2^exponent = argument, x < 1.0 and |
| 153 |
exponent - 2^i < MIN_EXP - 1 <= exponent. */ |
| 154 |
while (i > 0) |
| 155 |
{ |
| 156 |
i--; |
| 157 |
if (exponent - (1 << i) >= MIN_EXP - 1) |
| 158 |
{ |
| 159 |
exponent -= (1 << i); |
| 160 |
x *= pow2[i]; |
| 161 |
if (x >= L_(1.0)) |
| 162 |
break; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
/* Here either x < 1.0 and exponent = MIN_EXP - 1, |
| 167 |
or 1.0 <= x < 2^2^i and exponent >= MIN_EXP - 1. */ |
| 168 |
} |
| 169 |
|
| 170 |
/* Invariants: x * 2^exponent = argument, and |
| 171 |
either x < 1.0 and exponent = MIN_EXP - 1, |
| 172 |
or 1.0 <= x < 2^2^i and exponent >= MIN_EXP - 1. */ |
| 173 |
while (i > 0) |
| 174 |
{ |
| 175 |
i--; |
| 176 |
if (x >= pow2[i]) |
| 177 |
{ |
| 178 |
exponent += (1 << i); |
| 179 |
x *= powh[i]; |
| 180 |
} |
| 181 |
} |
| 182 |
/* Here either x < 1.0 and exponent = MIN_EXP - 1, |
| 183 |
or 1.0 <= x < 2.0 and exponent >= MIN_EXP - 1. */ |
| 184 |
} |
| 185 |
#endif |
| 186 |
|
| 187 |
END_ROUNDING (); |
| 188 |
|
| 189 |
*expptr = exponent; |
| 190 |
return x; |
| 191 |
} |