Skip to content

Commit

Permalink
Cleanup fixed64_t interface
Browse files Browse the repository at this point in the history
  • Loading branch information
tgtakaoka committed Oct 21, 2024
1 parent 601bcda commit 66861f1
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 26 deletions.
12 changes: 3 additions & 9 deletions src/dec_float.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,10 @@ Error __dec_float16::set(const float80_t &f80) {
auto ieee_exp = f80.decompose(sig);
sig.round_off(MANT_DIG);
ieee_exp += sig.normalize();
const auto ieee_sig = sig.value(); // has explicit MSB
auto dec_sig = sig.value(MANT_DIG); // has explicit MSB
// DEC float fraction is [0.5 .. 1.0)
// IEEE float fraction is [1.0 .. 2.0)
auto dec_exp = ieee_exp + 1;
auto dec_sig = ieee_sig << 1; // discard hidden MSB
if (dec_exp >= EXP_BASE) {
error = OVERFLOW_RANGE;
dec_exp = -EXP_BASE; // dec_sig = 0
Expand All @@ -64,7 +63,6 @@ Error __dec_float16::set(const float80_t &f80) {
dec_exp = -EXP_BASE;
dec_sig = 0;
}
dec_sig >>= 64 - TAG_POS;
_u16 = bits(f80.isNegative(), dec_exp + EXP_BASE, dec_sig);
return error;
}
Expand Down Expand Up @@ -117,11 +115,10 @@ Error __dec_float32::set(const float80_t &f80) {
auto ieee_exp = f80.decompose(sig);
sig.round_off(MANT_DIG);
ieee_exp += sig.normalize();
const auto ieee_sig = sig.value(); // has explicit MSB
auto dec_sig = sig.value(MANT_DIG); // has explicit MSB
// DEC float fraction is [0.5 .. 1.0)
// IEEE float fraction is [1.0 .. 2.0)
auto dec_exp = ieee_exp + 1;
auto dec_sig = ieee_sig << 1; // discard hidden MSB
if (dec_exp >= EXP_BASE) {
error = OVERFLOW_RANGE;
dec_exp = -EXP_BASE; // dec_sig = 0
Expand All @@ -130,7 +127,6 @@ Error __dec_float32::set(const float80_t &f80) {
dec_exp = -EXP_BASE;
dec_sig = 0;
}
dec_sig >>= 64 - TAG_POS;
_u32 = bits(f80.isNegative(), dec_exp + EXP_BASE, dec_sig);
return error;
}
Expand Down Expand Up @@ -187,11 +183,10 @@ Error __dec_float64::set(const float80_t &f80) {
auto ieee_exp = f80.decompose(sig);
sig.round_off(MANT_DIG);
ieee_exp += sig.normalize();
const auto ieee_sig = sig.value(); // has explicit MSB
auto dec_sig = sig.value(MANT_DIG); // has explicit MSB
// DEC float fraction is [0.5 .. 1.0)
// IEEE float fraction is [1.0 .. 2.0)
auto dec_exp = ieee_exp + 1;
auto dec_sig = ieee_sig << 1; // discard hidden MSB
if (dec_exp >= EXP_BASE) {
error = OVERFLOW_RANGE;
dec_exp = -EXP_BASE; // dec_sig = 0
Expand All @@ -200,7 +195,6 @@ Error __dec_float64::set(const float80_t &f80) {
dec_exp = -EXP_BASE;
dec_sig = 0;
}
dec_sig >>= 64 - TAG_POS;
_u64 = bits(f80.isNegative(), dec_exp + EXP_BASE, dec_sig);
return error;
}
Expand Down
7 changes: 4 additions & 3 deletions src/fixed64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,15 @@ void __fixed64::clear_high() {
_v[_HIGH] = 0;
}

uint64_t __fixed64::value() const {
uint64_t __fixed64::value(uint_fast8_t bits) const {
uint64_t u64 = _v[_MSW];
u64 <<= _BITS;
u64 |= _v[_MSW - 1];
u64 <<= _BITS;
u64 |= _v[_LSW + 1];
u64 <<= _BITS;
return u64 | _v[_LSW];
u64 |= _v[_LSW];
return u64 >> (64 - bits);
}

uint16_t __fixed64::get_low() const {
Expand Down Expand Up @@ -102,7 +103,7 @@ __fixed64 __fixed64::operator/(const __fixed64 &rhs) const {
}

bool __fixed64::zero() const {
return value() == 0 && get_low() == 0;
return value(64) == 0 && get_low() == 0;
}

bool __fixed64::negative() const {
Expand Down
4 changes: 3 additions & 1 deletion src/fixed64.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ struct __fixed64 {
__fixed64 &operator=(const __fixed64 &u64) = default;

__fixed64 &set(uint64_t sig, uint16_t low = 0);
uint64_t value() const;
/** Extract most significant |bits| */
uint64_t value(uint_fast8_t bits = _DIG) const;
uint16_t get_high() const;
void clear_high();

Expand Down Expand Up @@ -129,6 +130,7 @@ struct __fixed64 {
static constexpr auto _MSW = 4; // most significant
static constexpr auto _HIGH = 5; // high guard
static constexpr auto _SIZE = _HIGH + 1;
static constexpr auto _DIG = _BITS * (_MSW - _LSW + 1);
uint16_t _v[_SIZE];

uint16_t get_low() const;
Expand Down
6 changes: 3 additions & 3 deletions src/float80_hard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ __float80_hard __float80_hard::compose(bool negative, int_fast16_t exp, fixed64_
exp = normalize(exp, sig);
if (exp > EXP_BASE)
return infinity(negative);
const auto significand = sig.value();
if (exp < -EXP_BASE || significand == 0)
const auto sigval = sig.value(MANT_DIG);
if (exp < -EXP_BASE || sigval == 0)
return zero(negative);
return bits(negative, exp + EXP_BASE, significand);
return bits(negative, exp + EXP_BASE, sigval);
}

__float80_hard __float80_hard::bits(bool sign, uint16_t exp, uint64_t sig) {
Expand Down
4 changes: 2 additions & 2 deletions src/float80_soft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ __float80_soft &__float80_soft::set(uint64_t u64) {
fixed64_t sig;
const auto exp = sig.set(u64).normalize() + 63;
_tag = exp + EXP_BASE;
_sig = sig.value();
_sig = sig.value(MANT_DIG);
}
return *this;
}
Expand Down Expand Up @@ -417,7 +417,7 @@ __float80_soft __float80_soft::compose(bool negative, int_fast16_t exp, fixed64_
exp = normalize(exp, sig);
if (exp > EXP_BASE)
return infinity(negative);
const auto sigval = sig.value();
const auto sigval = sig.value(MANT_DIG);
if (sigval == 0)
return zero(negative);
if (exp < -EXP_BASE)
Expand Down
4 changes: 2 additions & 2 deletions src/ibm_float.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ Error __ibm_float32::set(const float80_t &f80) {
sig.shift_right(1);
}
sig.round_off(MANT_DIG);
auto ibm_sig = sig.value(MANT_DIG); // has explicit MSB
auto ibm_exp = ieee_exp / 4;
auto ibm_sig = sig.value() >> (64 - TAG_POS); // has explicit MSB
if (ibm_exp >= EXP_BASE) {
error = OVERFLOW_RANGE;
ibm_exp = -EXP_BASE; // ibm_sig = 0
Expand Down Expand Up @@ -132,8 +132,8 @@ Error __ibm_float64::set(const float80_t &f80) {
sig.shift_right(1);
}
sig.round_off(MANT_DIG);
auto ibm_sig = sig.value(MANT_DIG); // has explicit MSB
auto ibm_exp = ieee_exp / 4;
auto ibm_sig = sig.value() >> (64 - TAG_POS); // has explicit MSB
if (ibm_exp >= EXP_BASE) {
error = OVERFLOW_RANGE;
ibm_exp = -EXP_BASE; // ibm_sig = 0
Expand Down
8 changes: 2 additions & 6 deletions src/ieee_float.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,11 @@ __float32 __float32::compose(bool negative, int_fast16_t exp, fixed64_t &sig) {
exp = normalize(exp, sig);
if (exp > EXP_BASE)
return infinity(negative);
auto sigval = sig.value();
const auto sigval = sig.value(MANT_DIG);
if (sigval == 0)
return zero(negative);
if (exp < -EXP_BASE)
return __float32(negative ? SGN_MASK : 0, UINT32_C(1));
sigval <<= 1; // discard hidden MSB
sigval >>= 64 - TAG_POS;
return bits(negative, exp + EXP_BASE, sigval);
}

Expand Down Expand Up @@ -208,13 +206,11 @@ __float64 __float64::compose(bool negative, int_fast16_t exp, fixed64_t &sig) {
exp = normalize(exp, sig);
if (exp > EXP_BASE)
return infinity(negative);
auto sigval = sig.value();
const auto sigval = sig.value(MANT_DIG);
if (sigval == 0)
return zero(negative);
if (exp < -EXP_BASE)
return __float64(negative ? SGN_MASK : 0, UINT64_C(1));
sigval <<= 1; // discard hidden MSB
sigval >>= 64 - TAG_POS;
return bits(negative, exp + EXP_BASE, sigval);
}

Expand Down

0 comments on commit 66861f1

Please sign in to comment.