From 2045d8047af3d8a8d2cb1dd112533c4e14e21ba7 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Wed, 2 Oct 2024 13:49:51 -0700 Subject: [PATCH] deduplicate calculation --- fixed_wide/src/fixed.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/fixed_wide/src/fixed.rs b/fixed_wide/src/fixed.rs index cffdd1a..0e45545 100644 --- a/fixed_wide/src/fixed.rs +++ b/fixed_wide/src/fixed.rs @@ -238,11 +238,10 @@ macro_rules! impl_from_float { //extract exponent, add fractional offset //usize is used to calculate digit_index. exp_cycle must be at least 8 bits so 32 bits is fine let exp=((bits>>($mantissa_bits-1)) as usize&((1<<$exponent_bits)-1))+F; - //digit_index is where the hi digit should end up in a fixed point number //if it's less than zero, that's a conversion underflow. - let digit_index=exp.checked_sub($exp_bias)?>>DIGIT_SHIFT; + let exp_bias=exp.checked_sub($exp_bias)?; //cycle the exponent to keep the top bit of the mantissa within the hi digit - let exp_cycle=exp.overflowing_sub($exp_bias+64).0.rem_euclid(64).overflowing_add($exp_bias+64).0; + let exp_cycle=exp_bias.rem_euclid(64).overflowing_add($exp_bias+64).0; let out_bits= bits //remove (mask) sign bit and exponent @@ -253,7 +252,8 @@ macro_rules! impl_from_float { let _128=<$input>::from_bits(out_bits) as u128; Some(FloatInfo{ sign:f.is_sign_negative(), - digit_index, + //digit_index is where the hi digit should end up in a fixed point number + digit_index:exp_bias>>DIGIT_SHIFT, bits:[_128 as u64,(_128>>64) as u64], }) }