1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
|
//===-- lib/Decimal/binary-to-decimal.cpp ---------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "big-radix-floating-point.h"
#include "flang/Decimal/decimal.h"
#include <cassert>
#include <cfloat>
#include <string>
namespace Fortran::decimal {
template <int PREC, int LOG10RADIX>
BigRadixFloatingPointNumber<PREC, LOG10RADIX>::BigRadixFloatingPointNumber(
BinaryFloatingPointNumber<PREC> x, enum FortranRounding rounding)
: rounding_{rounding} {
bool negative{x.IsNegative()};
if (x.IsZero()) {
isNegative_ = negative;
return;
}
if (negative) {
x.Negate();
}
int twoPow{x.UnbiasedExponent()};
twoPow -= x.bits - 1;
if (!x.isImplicitMSB) {
++twoPow;
}
int lshift{x.exponentBits};
if (twoPow <= -lshift) {
twoPow += lshift;
lshift = 0;
} else if (twoPow < 0) {
lshift += twoPow;
twoPow = 0;
}
auto word{x.Fraction()};
word <<= lshift;
SetTo(word);
isNegative_ = negative;
// The significand is now encoded in *this as an integer (D) and
// decimal exponent (E): x = D * 10.**E * 2.**twoPow
// twoPow can be positive or negative.
// The goal now is to get twoPow up or down to zero, leaving us with
// only decimal digits and decimal exponent. This is done by
// fast multiplications and divisions of D by 2 and 5.
// (5*D) * 10.**E * 2.**twoPow -> D * 10.**(E+1) * 2.**(twoPow-1)
for (; twoPow > 0 && IsDivisibleBy<5>(); --twoPow) {
DivideBy<5>();
++exponent_;
}
int overflow{0};
for (; twoPow >= 9; twoPow -= 9) {
// D * 10.**E * 2.**twoPow -> (D*(2**9)) * 10.**E * 2.**(twoPow-9)
overflow |= MultiplyBy<512>();
}
for (; twoPow >= 3; twoPow -= 3) {
// D * 10.**E * 2.**twoPow -> (D*(2**3)) * 10.**E * 2.**(twoPow-3)
overflow |= MultiplyBy<8>();
}
for (; twoPow > 0; --twoPow) {
// D * 10.**E * 2.**twoPow -> (2*D) * 10.**E * 2.**(twoPow-1)
overflow |= MultiplyBy<2>();
}
overflow |= DivideByPowerOfTwoInPlace(-twoPow);
assert(overflow == 0);
Normalize();
}
template <int PREC, int LOG10RADIX>
ConversionToDecimalResult
BigRadixFloatingPointNumber<PREC, LOG10RADIX>::ConvertToDecimal(char *buffer,
std::size_t n, enum DecimalConversionFlags flags, int maxDigits) const {
if (n < static_cast<std::size_t>(3 + digits_ * LOG10RADIX)) {
return {nullptr, 0, 0, Overflow};
}
char *start{buffer};
if (isNegative_) {
*start++ = '-';
} else if (flags & AlwaysSign) {
*start++ = '+';
}
if (IsZero()) {
*start++ = '0';
*start = '\0';
return {buffer, static_cast<std::size_t>(start - buffer), 0, Exact};
}
char *p{start};
static_assert((LOG10RADIX % 2) == 0, "radix not a power of 100");
static const char lut[] = "0001020304050607080910111213141516171819"
"2021222324252627282930313233343536373839"
"4041424344454647484950515253545556575859"
"6061626364656667686970717273747576777879"
"8081828384858687888990919293949596979899";
// Treat the MSD specially: don't emit leading zeroes.
Digit dig{digit_[digits_ - 1]};
char stack[LOG10RADIX], *sp{stack};
for (int k{0}; k < log10Radix; k += 2) {
Digit newDig{dig / 100};
auto d{static_cast<std::uint32_t>(dig) -
std::uint32_t{100} * static_cast<std::uint32_t>(newDig)};
dig = newDig;
const char *q{lut + d + d};
*sp++ = q[1];
*sp++ = q[0];
}
while (sp > stack && sp[-1] == '0') {
--sp;
}
while (sp > stack) {
*p++ = *--sp;
}
for (int j{digits_ - 1}; j-- > 0;) {
Digit dig{digit_[j]};
char *reverse{p += log10Radix};
for (int k{0}; k < log10Radix; k += 2) {
Digit newDig{dig / 100};
auto d{static_cast<std::uint32_t>(dig) -
std::uint32_t{100} * static_cast<std::uint32_t>(newDig)};
dig = newDig;
const char *q{lut + d + d};
*--reverse = q[1];
*--reverse = q[0];
}
}
// Adjust exponent so the effective decimal point is to
// the left of the first digit.
int expo = exponent_ + p - start;
// Trim trailing zeroes.
while (p[-1] == '0') {
--p;
}
char *end{start + maxDigits};
if (maxDigits == 0) {
p = end;
}
if (p <= end) {
*p = '\0';
return {buffer, static_cast<std::size_t>(p - buffer), expo, Exact};
} else {
// Apply a digit limit, possibly with rounding.
bool incr{false};
switch (rounding_) {
case RoundNearest:
incr = *end > '5' ||
(*end == '5' && (p > end + 1 || ((end[-1] - '0') & 1) != 0));
break;
case RoundUp:
incr = !isNegative_;
break;
case RoundDown:
incr = isNegative_;
break;
case RoundToZero:
break;
case RoundCompatible:
incr = *end >= '5';
break;
}
p = end;
if (incr) {
while (p > start && p[-1] == '9') {
--p;
}
if (p == start) {
*p++ = '1';
++expo;
} else {
++p[-1];
}
}
*p = '\0';
return {buffer, static_cast<std::size_t>(p - buffer), expo, Inexact};
}
}
template <int PREC, int LOG10RADIX>
bool BigRadixFloatingPointNumber<PREC, LOG10RADIX>::Mean(
const BigRadixFloatingPointNumber &that) {
while (digits_ < that.digits_) {
digit_[digits_++] = 0;
}
int carry{0};
for (int j{0}; j < that.digits_; ++j) {
Digit v{digit_[j] + that.digit_[j] + carry};
if (v >= radix) {
digit_[j] = v - radix;
carry = 1;
} else {
digit_[j] = v;
carry = 0;
}
}
if (carry != 0) {
AddCarry(that.digits_, carry);
}
return DivideBy<2>() != 0;
}
template <int PREC, int LOG10RADIX>
void BigRadixFloatingPointNumber<PREC, LOG10RADIX>::Minimize(
BigRadixFloatingPointNumber &&less, BigRadixFloatingPointNumber &&more) {
int leastExponent{exponent_};
if (less.exponent_ < leastExponent) {
leastExponent = less.exponent_;
}
if (more.exponent_ < leastExponent) {
leastExponent = more.exponent_;
}
while (exponent_ > leastExponent) {
--exponent_;
MultiplyBy<10>();
}
while (less.exponent_ > leastExponent) {
--less.exponent_;
less.MultiplyBy<10>();
}
while (more.exponent_ > leastExponent) {
--more.exponent_;
more.MultiplyBy<10>();
}
if (less.Mean(*this)) {
less.AddCarry(); // round up
}
if (!more.Mean(*this)) {
more.Decrement(); // round down
}
while (less.digits_ < more.digits_) {
less.digit_[less.digits_++] = 0;
}
while (more.digits_ < less.digits_) {
more.digit_[more.digits_++] = 0;
}
int digits{more.digits_};
int same{0};
while (same < digits &&
less.digit_[digits - 1 - same] == more.digit_[digits - 1 - same]) {
++same;
}
if (same == digits) {
return;
}
digits_ = same + 1;
int offset{digits - digits_};
exponent_ += offset * log10Radix;
for (int j{0}; j < digits_; ++j) {
digit_[j] = more.digit_[j + offset];
}
Digit least{less.digit_[offset]};
Digit my{digit_[0]};
while (true) {
Digit q{my / 10u};
Digit r{my - 10 * q};
Digit lq{least / 10u};
Digit lr{least - 10 * lq};
if (r != 0 && lq == q) {
Digit sub{(r - lr) >> 1};
digit_[0] -= sub;
break;
} else {
least = lq;
my = q;
DivideBy<10>();
++exponent_;
}
}
Normalize();
}
template <int PREC>
ConversionToDecimalResult ConvertToDecimal(char *buffer, std::size_t size,
enum DecimalConversionFlags flags, int digits,
enum FortranRounding rounding, BinaryFloatingPointNumber<PREC> x) {
if (x.IsNaN()) {
return {"NaN", 3, 0, Invalid};
} else if (x.IsInfinite()) {
if (x.IsNegative()) {
return {"-Inf", 4, 0, Exact};
} else if (flags & AlwaysSign) {
return {"+Inf", 4, 0, Exact};
} else {
return {"Inf", 3, 0, Exact};
}
} else {
using Big = BigRadixFloatingPointNumber<PREC>;
Big number{x, rounding};
if ((flags & Minimize) && !x.IsZero()) {
// To emit the fewest decimal digits necessary to represent the value
// in such a way that decimal-to-binary conversion to the same format
// with a fixed assumption about rounding will return the same binary
// value, we also perform binary-to-decimal conversion on the two
// binary values immediately adjacent to this one, use them to identify
// the bounds of the range of decimal values that will map back to the
// original binary value, and find a (not necessary unique) shortest
// decimal sequence in that range.
using Binary = typename Big::Real;
Binary less{x};
less.Previous();
Binary more{x};
if (!x.IsMaximalFiniteMagnitude()) {
more.Next();
}
number.Minimize(Big{less, rounding}, Big{more, rounding});
}
return number.ConvertToDecimal(buffer, size, flags, digits);
}
}
template ConversionToDecimalResult ConvertToDecimal<8>(char *, std::size_t,
enum DecimalConversionFlags, int, enum FortranRounding,
BinaryFloatingPointNumber<8>);
template ConversionToDecimalResult ConvertToDecimal<11>(char *, std::size_t,
enum DecimalConversionFlags, int, enum FortranRounding,
BinaryFloatingPointNumber<11>);
template ConversionToDecimalResult ConvertToDecimal<24>(char *, std::size_t,
enum DecimalConversionFlags, int, enum FortranRounding,
BinaryFloatingPointNumber<24>);
template ConversionToDecimalResult ConvertToDecimal<53>(char *, std::size_t,
enum DecimalConversionFlags, int, enum FortranRounding,
BinaryFloatingPointNumber<53>);
template ConversionToDecimalResult ConvertToDecimal<64>(char *, std::size_t,
enum DecimalConversionFlags, int, enum FortranRounding,
BinaryFloatingPointNumber<64>);
template ConversionToDecimalResult ConvertToDecimal<113>(char *, std::size_t,
enum DecimalConversionFlags, int, enum FortranRounding,
BinaryFloatingPointNumber<113>);
extern "C" {
ConversionToDecimalResult ConvertFloatToDecimal(char *buffer, std::size_t size,
enum DecimalConversionFlags flags, int digits,
enum FortranRounding rounding, float x) {
return Fortran::decimal::ConvertToDecimal(buffer, size, flags, digits,
rounding, Fortran::decimal::BinaryFloatingPointNumber<24>(x));
}
ConversionToDecimalResult ConvertDoubleToDecimal(char *buffer, std::size_t size,
enum DecimalConversionFlags flags, int digits,
enum FortranRounding rounding, double x) {
return Fortran::decimal::ConvertToDecimal(buffer, size, flags, digits,
rounding, Fortran::decimal::BinaryFloatingPointNumber<53>(x));
}
#if LDBL_MANT_DIG == 64
ConversionToDecimalResult ConvertLongDoubleToDecimal(char *buffer,
std::size_t size, enum DecimalConversionFlags flags, int digits,
enum FortranRounding rounding, long double x) {
return Fortran::decimal::ConvertToDecimal(buffer, size, flags, digits,
rounding, Fortran::decimal::BinaryFloatingPointNumber<64>(x));
}
#elif LDBL_MANT_DIG == 113
ConversionToDecimalResult ConvertLongDoubleToDecimal(char *buffer,
std::size_t size, enum DecimalConversionFlags flags, int digits,
enum FortranRounding rounding, long double x) {
return Fortran::decimal::ConvertToDecimal(buffer, size, flags, digits,
rounding, Fortran::decimal::BinaryFloatingPointNumber<113>(x));
}
#endif
}
template <int PREC, int LOG10RADIX>
template <typename STREAM>
STREAM &BigRadixFloatingPointNumber<PREC, LOG10RADIX>::Dump(STREAM &o) const {
if (isNegative_) {
o << '-';
}
o << "10**(" << exponent_ << ") * ...\n";
for (int j{digits_}; --j >= 0;) {
std::string str{std::to_string(digit_[j])};
o << std::string(20 - str.size(), ' ') << str << " [" << j << ']';
if (j + 1 == digitLimit_) {
o << " (limit)";
}
o << '\n';
}
return o;
}
} // namespace Fortran::decimal
|