45 #define FLOAT_MANTISSA_BITS 23
46 #define FLOAT_EXPONENT_BITS 8
47 #define FLOAT_BIAS 127
53 #define FLOAT_POW5_INV_BITCOUNT 59
64 #define FLOAT_POW5_BITCOUNT 61
112 return (
value & ((1u << p) - 1)) == 0;
133 #ifdef RYU_32_BIT_PLATFORM
144 bits1Hi += (bits1Lo < bits0Hi);
146 const int32 s = shift - 32;
148 return (bits1Hi << (32 - s)) | (bits1Lo >> s);
152 const uint64 sum = (bits0 >> 32) + bits1;
153 const uint64 shiftedSum = sum >> (shift - 32);
156 return (
uint32) shiftedSum;
227 if (ieeeExponent == 0)
239 #if STRICTLY_SHORTEST
240 const bool even = (m2 & 1) == 0;
241 const bool acceptBounds = even;
243 const bool acceptBounds =
false;
248 const uint32 mp = 4 * m2 + 2;
251 const uint32 mmShift = ieeeMantissa != 0 || ieeeExponent <= 1;
252 const uint32 mm = 4 * m2 - 1 - mmShift;
259 bool vmIsTrailingZeros =
false;
260 bool vrIsTrailingZeros =
false;
261 uint8 lastRemovedDigit = 0;
270 const int32 i = -e2 + q + k;
276 if (q != 0 && (vp - 1) / 10 <= vm / 10)
300 else if (acceptBounds)
324 if (q != 0 && (vp - 1) / 10 <= vm / 10)
336 vrIsTrailingZeros =
true;
343 vmIsTrailingZeros = mmShift == 1;
367 if (vmIsTrailingZeros || vrIsTrailingZeros)
370 while (vp / 10 > vm / 10)
372 vmIsTrailingZeros &= vm - (vm / 10) * 10 == 0;
373 vrIsTrailingZeros &= lastRemovedDigit == 0;
374 lastRemovedDigit = (
uint8) (vr % 10);
380 if (vmIsTrailingZeros)
384 vrIsTrailingZeros &= lastRemovedDigit == 0;
385 lastRemovedDigit = (
uint8) (vr % 10);
393 if (vrIsTrailingZeros && lastRemovedDigit == 5 && vr % 2 == 0)
396 lastRemovedDigit = 4;
403 output = vr + ((vr == vm && (!acceptBounds || !vmIsTrailingZeros)) || lastRemovedDigit >= 5);
414 while (vp / 10 > vm / 10)
416 lastRemovedDigit = (
uint8) (vr % 10);
427 output = vr + (vr == vm || lastRemovedDigit >= 5);
430 const int32 exp = e10 + removed;
462 int32 nexp = exp + olength;
471 memcpy(result,
"0.000000", 8);
488 Assert(exp < 6 && exp + olength <= 6);
489 memset(result,
'0', 8);
495 const uint32 c0 = (
c % 100) << 1;
496 const uint32 c1 = (
c / 100) << 1;
534 memmove(result +
index - 1, result +
index, 4);
539 memmove(result +
index - 1, result +
index, 2);
552 index = olength + exp;
556 index = olength + (2 - nexp);
573 result[
index++] =
'-';
580 if (exp >= -4 && exp < 6)
628 const uint32 c0 = (
c % 100) << 1;
629 const uint32 c1 = (
c / 100) << 1;
664 result[
index + 1] =
'.';
665 index += olength + 1;
673 result[
index++] =
'e';
676 result[
index++] =
'-';
680 result[
index++] =
'+';
690 const uint32 ieeeExponent,
712 const uint32 mask = (1U << -e2) - 1;
713 const uint32 fraction = ieeeMantissa & mask;
756 if (ieeeExponent == ((1u <<
FLOAT_EXPONENT_BITS) - 1u) || (ieeeExponent == 0 && ieeeMantissa == 0))
758 return copy_special_str(result, ieeeSign, (ieeeExponent != 0), (ieeeMantissa != 0));
762 const bool isSmallInt =
f2d_small_int(ieeeMantissa, ieeeExponent, &v);
766 v =
f2d(ieeeMantissa, ieeeExponent);
769 return to_chars(v, ieeeSign, result);
786 result[
index] =
'\0';
#define Assert(condition)
static int to_chars(const floating_decimal_32 v, const bool sign, char *const result)
#define FLOAT_POW5_INV_BITCOUNT
static const uint64 FLOAT_POW5_SPLIT[47]
static bool multipleOfPowerOf2(const uint32 value, const uint32 p)
int float_to_shortest_decimal_buf(float f, char *result)
int float_to_shortest_decimal_bufn(float f, char *result)
static uint32 pow5Factor(uint32 value)
static uint32 decimalLength(const uint32 v)
#define FLOAT_MANTISSA_BITS
#define FLOAT_POW5_BITCOUNT
char * float_to_shortest_decimal(float f)
static bool f2d_small_int(const uint32 ieeeMantissa, const uint32 ieeeExponent, floating_decimal_32 *v)
static uint32 mulPow5InvDivPow2(const uint32 m, const uint32 q, const int32 j)
static uint32 mulPow5divPow2(const uint32 m, const uint32 i, const int32 j)
static bool multipleOfPowerOf5(const uint32 value, const uint32 p)
static floating_decimal_32 f2d(const uint32 ieeeMantissa, const uint32 ieeeExponent)
static const uint64 FLOAT_POW5_INV_SPLIT[31]
static int to_chars_f(const floating_decimal_32 v, const uint32 olength, char *const result)
#define FLOAT_EXPONENT_BITS
struct floating_decimal_32 floating_decimal_32
static uint32 mulShift(const uint32 m, const uint64 factor, const int32 shift)
static const char DIGIT_TABLE[200]
static int fd(const char *x, int i)
static uint32 float_to_bits(const float f)
static uint32 pow5bits(const int32 e)
static int32 log10Pow5(const int32 e)
static int copy_special_str(char *const result, const bool sign, const bool exponent, const bool mantissa)
static int32 log10Pow2(const int32 e)
#define FLOAT_SHORTEST_DECIMAL_LEN