Skip to content

Commit

Permalink
Extract FLOAT_BIAS and DOUBLE_BIAS.
Browse files Browse the repository at this point in the history
This slightly increases consistency (they're properties of the
IEEE representation, like the mantissa/exponent bits).
Also, they'll be needed for the "large fixed integer" codepaths
outside of f2d() and d2d().
  • Loading branch information
StephanTLavavej authored and ulfjack committed Nov 5, 2018
1 parent 2f5701e commit 5c36146
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 8 deletions.
6 changes: 2 additions & 4 deletions ryu/d2s.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,16 +236,14 @@ typedef struct floating_decimal_64 {
} floating_decimal_64;

static inline floating_decimal_64 d2d(const uint64_t ieeeMantissa, const uint32_t ieeeExponent) {
const int32_t bias = (1u << (DOUBLE_EXPONENT_BITS - 1)) - 1;

int32_t e2;
uint64_t m2;
if (ieeeExponent == 0) {
// We subtract 2 so that the bounds computation has 2 additional bits.
e2 = 1 - bias - DOUBLE_MANTISSA_BITS - 2;
e2 = 1 - DOUBLE_BIAS - DOUBLE_MANTISSA_BITS - 2;
m2 = ieeeMantissa;
} else {
e2 = (int32_t) ieeeExponent - bias - DOUBLE_MANTISSA_BITS - 2;
e2 = (int32_t) ieeeExponent - DOUBLE_BIAS - DOUBLE_MANTISSA_BITS - 2;
m2 = (1ull << DOUBLE_MANTISSA_BITS) | ieeeMantissa;
}
const bool even = (m2 & 1) == 0;
Expand Down
1 change: 1 addition & 0 deletions ryu/d2s.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ typedef __uint128_t uint128_t;

#define DOUBLE_MANTISSA_BITS 52
#define DOUBLE_EXPONENT_BITS 11
#define DOUBLE_BIAS 1023

#define DOUBLE_POW5_INV_BITCOUNT 122
#define DOUBLE_POW5_BITCOUNT 121
Expand Down
7 changes: 3 additions & 4 deletions ryu/f2s.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

#define FLOAT_MANTISSA_BITS 23
#define FLOAT_EXPONENT_BITS 8
#define FLOAT_BIAS 127

// This table is generated by PrintFloatLookupTable.
#define FLOAT_POW5_INV_BITCOUNT 59
Expand Down Expand Up @@ -151,16 +152,14 @@ typedef struct floating_decimal_32 {
} floating_decimal_32;

static inline floating_decimal_32 f2d(const uint32_t ieeeMantissa, const uint32_t ieeeExponent) {
const int32_t bias = (1u << (FLOAT_EXPONENT_BITS - 1)) - 1;

int32_t e2;
uint32_t m2;
if (ieeeExponent == 0) {
// We subtract 2 so that the bounds computation has 2 additional bits.
e2 = 1 - bias - FLOAT_MANTISSA_BITS - 2;
e2 = 1 - FLOAT_BIAS - FLOAT_MANTISSA_BITS - 2;
m2 = ieeeMantissa;
} else {
e2 = (int32_t) ieeeExponent - bias - FLOAT_MANTISSA_BITS - 2;
e2 = (int32_t) ieeeExponent - FLOAT_BIAS - FLOAT_MANTISSA_BITS - 2;
m2 = (1u << FLOAT_MANTISSA_BITS) | ieeeMantissa;
}
const bool even = (m2 & 1) == 0;
Expand Down

0 comments on commit 5c36146

Please sign in to comment.