diff --git a/fixed_wide/src/fixed.rs b/fixed_wide/src/fixed.rs index 84667bc..bcee486 100644 --- a/fixed_wide/src/fixed.rs +++ b/fixed_wide/src/fixed.rs @@ -138,31 +138,72 @@ impl std::iter::Sum for Fixed{ } } +const fn signed_shift(lhs:u64,rhs:i32)->u64{ + if rhs.is_negative(){ + lhs>>-rhs + }else{ + lhs< { + ( $output: ty, $unsigned:ty, $exponent_bits:expr, $mantissa_bits:expr ) => { impl Into<$output> for Fixed{ #[inline] fn into(self)->$output{ - let mut total=0.0; - let bits=self.bits.to_bits(); - let digits=bits.digits(); - for (i,digit) in digits[0..N-1].iter().enumerate(){ - // (i*64-F) as i32 will interpret the highest order bit as a sign bit but whatever - total+=(*digit as $output)*(2.0 as $output).powi((i*64-F) as i32); + const DIGIT_SHIFT:u32=6;//Log2[64] + // SBBB BBBB + // 1001 1110 0000 0000 + let sign=if self.bits.is_negative(){(1 as $unsigned)<<(<$unsigned>::BITS-1)}else{0}; + println!("sign={sign}"); + let unsigned=self.bits.unsigned_abs(); + println!("unsigned={unsigned}"); + let most_significant_bit=unsigned.bits(); + println!("most_significant_bit={most_significant_bit}"); + let exp=if unsigned.is_zero(){ + 0 + }else{ + let msb=most_significant_bit as $unsigned; + println!("msb={msb}"); + let _127=((1 as $unsigned)<<($exponent_bits-1))-1; + println!("_127={_127}"); + let msb_offset=msb+_127-1-F as $unsigned; + println!("msb_offset={msb_offset}"); + msb_offset<<($mantissa_bits-1) + }; + println!("exp={exp:#034b}"); + let digits=unsigned.digits(); + println!("digits={digits:?}"); + let digit_index=most_significant_bit>>DIGIT_SHIFT; + println!("digit_index={digit_index}"); + let digit=digits[digit_index as usize]; + println!("digit={digit:#034b}"); + //How many bits does the mantissa take from this digit + let take_bits=most_significant_bit-(digit_index<::from_bits(bits) } } } } -impl_into_float!(f32); -impl_into_float!(f64); +impl_into_float!(f32,u32,8,24); +impl_into_float!(f64,u64,11,53); impl core::fmt::Display for Fixed{ #[inline]