|
|
|
@ -197,34 +197,6 @@ macro_rules! impl_into_float {
|
|
|
|
|
impl_into_float!(f32,u32,8,24);
|
|
|
|
|
impl_into_float!(f64,u64,11,53);
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn integer_decode_f32(f: f32) -> (u64, i16, bool) {
|
|
|
|
|
let bits: u32 = f.to_bits();
|
|
|
|
|
let sign: bool = bits & (1<<31) != 0;
|
|
|
|
|
let mut exponent: i16 = ((bits >> 23) & 0xff) as i16;
|
|
|
|
|
let mantissa = if exponent == 0 {
|
|
|
|
|
(bits & 0x7fffff) << 1
|
|
|
|
|
} else {
|
|
|
|
|
(bits & 0x7fffff) | 0x800000
|
|
|
|
|
};
|
|
|
|
|
// Exponent bias + mantissa shift
|
|
|
|
|
exponent -= 127 + 23;
|
|
|
|
|
(mantissa as u64, exponent, sign)
|
|
|
|
|
}
|
|
|
|
|
#[inline]
|
|
|
|
|
fn integer_decode_f64(f: f64) -> (u64, i16, bool) {
|
|
|
|
|
let bits: u64 = f.to_bits();
|
|
|
|
|
let sign: bool = bits & (1u64<<63) != 0;
|
|
|
|
|
let mut exponent: i16 = ((bits >> 52) & 0x7ff) as i16;
|
|
|
|
|
let mantissa = if exponent == 0 {
|
|
|
|
|
(bits & 0xfffffffffffff) << 1
|
|
|
|
|
} else {
|
|
|
|
|
(bits & 0xfffffffffffff) | 0x10000000000000
|
|
|
|
|
};
|
|
|
|
|
// Exponent bias + mantissa shift
|
|
|
|
|
exponent -= 1023 + 52;
|
|
|
|
|
(mantissa, exponent, sign)
|
|
|
|
|
}
|
|
|
|
|
#[derive(Debug,Eq,PartialEq)]
|
|
|
|
|
pub enum FixedFromFloatError{
|
|
|
|
|
Nan,
|
|
|
|
@ -240,13 +212,19 @@ impl FixedFromFloatError{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct FloatInfo{
|
|
|
|
|
sign:bool,
|
|
|
|
|
digit_index:usize,
|
|
|
|
|
bits:[u64;2],
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
macro_rules! impl_from_float {
|
|
|
|
|
( $decode:ident, $input: ty, $mantissa_bits:expr ) => {
|
|
|
|
|
( $input: ty, $unsigned: ty, $exponent_bits:expr, $mantissa_bits:expr, $exp_bias:expr ) => {
|
|
|
|
|
impl<const N:usize,const F:usize> TryFrom<$input> for Fixed<N,F>{
|
|
|
|
|
type Error=FixedFromFloatError;
|
|
|
|
|
#[inline]
|
|
|
|
|
fn try_from(value:$input)->Result<Self,Self::Error>{
|
|
|
|
|
const DIGIT_SHIFT:u32=6;
|
|
|
|
|
match value.classify(){
|
|
|
|
|
std::num::FpCategory::Nan=>Err(FixedFromFloatError::Nan),
|
|
|
|
|
std::num::FpCategory::Infinite=>Err(FixedFromFloatError::Infinite),
|
|
|
|
@ -254,27 +232,54 @@ macro_rules! impl_from_float {
|
|
|
|
|
std::num::FpCategory::Subnormal
|
|
|
|
|
|std::num::FpCategory::Normal
|
|
|
|
|
=>{
|
|
|
|
|
let (m,e,s)=$decode(value);
|
|
|
|
|
fn to_float_info<const F:usize>(f:$input)->Option<FloatInfo>{
|
|
|
|
|
const DIGIT_SHIFT:u32=6;
|
|
|
|
|
let bits=f.to_bits();
|
|
|
|
|
//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;
|
|
|
|
|
//if it's less than zero, that's a conversion underflow.
|
|
|
|
|
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_bias.rem_euclid(64).overflowing_add($exp_bias+64).0;
|
|
|
|
|
let out_bits=
|
|
|
|
|
bits
|
|
|
|
|
//remove (mask) sign bit and exponent
|
|
|
|
|
&((1 as $unsigned<<($mantissa_bits-1))-1)
|
|
|
|
|
//write exponent
|
|
|
|
|
|((exp_cycle as $unsigned)<<($mantissa_bits-1));
|
|
|
|
|
//ready to convert
|
|
|
|
|
let _128=<$input>::from_bits(out_bits) as u128;
|
|
|
|
|
Some(FloatInfo{
|
|
|
|
|
sign:f.is_sign_negative(),
|
|
|
|
|
//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],
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let FloatInfo{
|
|
|
|
|
sign,
|
|
|
|
|
digit_index,
|
|
|
|
|
bits:[lo,hi],
|
|
|
|
|
}=to_float_info::<F>(value)
|
|
|
|
|
.ok_or(FixedFromFloatError::Underflow)?;
|
|
|
|
|
|
|
|
|
|
let mut digits=[0u64;N];
|
|
|
|
|
let most_significant_bit=e as i32+$mantissa_bits as i32+F as i32;
|
|
|
|
|
if most_significant_bit<0{
|
|
|
|
|
return Err(FixedFromFloatError::Underflow);
|
|
|
|
|
}
|
|
|
|
|
let digit_index=most_significant_bit>>DIGIT_SHIFT;
|
|
|
|
|
let digit=digits.get_mut(digit_index as usize).ok_or(FixedFromFloatError::Overflow)?;
|
|
|
|
|
let take_bits=most_significant_bit-(digit_index<<DIGIT_SHIFT);
|
|
|
|
|
let rest_of_mantissa=-($mantissa_bits as i32-(take_bits as i32));
|
|
|
|
|
*digit=signed_shift(m,rest_of_mantissa);
|
|
|
|
|
if rest_of_mantissa<0&&digit_index!=0{
|
|
|
|
|
//we don't care if some float bits are partially truncated
|
|
|
|
|
if let Some(digit)=digits.get_mut((digit_index-1) as usize){
|
|
|
|
|
let take_bits=most_significant_bit-((digit_index-1)<<DIGIT_SHIFT);
|
|
|
|
|
let rest_of_mantissa=-($mantissa_bits as i32-(take_bits as i32));
|
|
|
|
|
*digit=signed_shift(m,rest_of_mantissa);
|
|
|
|
|
}
|
|
|
|
|
let digit=digits.get_mut(digit_index)
|
|
|
|
|
.ok_or(FixedFromFloatError::Overflow)?;
|
|
|
|
|
*digit=hi;
|
|
|
|
|
|
|
|
|
|
if digit_index!=0{
|
|
|
|
|
//if digit_index exists, so does digit_index-1
|
|
|
|
|
digits[digit_index-1]=lo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let bits=BInt::from_bits(bnum::BUint::from_digits(digits));
|
|
|
|
|
Ok(if s{
|
|
|
|
|
if bits.is_negative()&&!(sign&&bits==BInt::MIN){
|
|
|
|
|
return Err(FixedFromFloatError::Overflow);
|
|
|
|
|
}
|
|
|
|
|
Ok(if sign{
|
|
|
|
|
Self::from_bits(bits.overflowing_neg().0)
|
|
|
|
|
}else{
|
|
|
|
|
Self::from_bits(bits)
|
|
|
|
@ -285,8 +290,8 @@ macro_rules! impl_from_float {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
impl_from_float!(integer_decode_f32,f32,24);
|
|
|
|
|
impl_from_float!(integer_decode_f64,f64,53);
|
|
|
|
|
impl_from_float!(f32,u32,8,24,127);
|
|
|
|
|
impl_from_float!(f64,u64,11,53,1023);
|
|
|
|
|
|
|
|
|
|
impl<const N:usize,const F:usize> core::fmt::Display for Fixed<N,F>{
|
|
|
|
|
#[inline]
|
|
|
|
@ -412,7 +417,7 @@ macro_rules! impl_multiply_operator_not_const_generic {
|
|
|
|
|
type Output=Self;
|
|
|
|
|
#[inline]
|
|
|
|
|
fn divide(self, other: i64)->Self::Output{
|
|
|
|
|
Self::from_bits(self.bits.div_euclid(BInt::from(other)))
|
|
|
|
|
Self::from_bits(self.bits/BInt::from(other))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -422,11 +427,11 @@ macro_rules! impl_divide_operator_not_const_generic {
|
|
|
|
|
impl<const F:usize> $struct<$width,F>{
|
|
|
|
|
paste::item!{
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn [<fixed_ $method>](self,other:Self)->Self{
|
|
|
|
|
pub fn [<fixed_ $method>](self, other: Self) -> Self {
|
|
|
|
|
//this only needs to be $width+F as u32/64+1 but MUH CONST GENERICS!!!!!
|
|
|
|
|
let lhs=self.bits.as_::<BInt::<{$width*2}>>().shl(F as u32);
|
|
|
|
|
let rhs=other.bits.as_::<BInt::<{$width*2}>>();
|
|
|
|
|
Self::from_bits(lhs.div_euclid(rhs).as_())
|
|
|
|
|
Self::from_bits(lhs.div(rhs).as_())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -446,28 +451,28 @@ macro_rules! impl_divide_operator_not_const_generic {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
macro_rules! impl_multiplicative_operator {
|
|
|
|
|
( $struct: ident, $trait: ident, $method: ident, $inner_method: ident, $output: ty ) => {
|
|
|
|
|
( $struct: ident, $trait: ident, $method: ident, $output: ty ) => {
|
|
|
|
|
impl<const N:usize,const F:usize,U> core::ops::$trait<U> for $struct<N,F>
|
|
|
|
|
where
|
|
|
|
|
BInt::<N>:From<U>+core::ops::$trait,
|
|
|
|
|
{
|
|
|
|
|
type Output = $output;
|
|
|
|
|
#[inline]
|
|
|
|
|
fn $method(self,other:U)->Self::Output{
|
|
|
|
|
Self::from_bits(self.bits.$inner_method(BInt::<N>::from(other)))
|
|
|
|
|
fn $method(self, other: U) -> Self::Output {
|
|
|
|
|
Self::from_bits(self.bits.$method(BInt::<N>::from(other)))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
macro_rules! impl_multiplicative_assign_operator {
|
|
|
|
|
( $struct: ident, $trait: ident, $method: ident, $not_assign_method: ident ) => {
|
|
|
|
|
( $struct: ident, $trait: ident, $method: ident ) => {
|
|
|
|
|
impl<const N:usize,const F:usize,U> core::ops::$trait<U> for $struct<N,F>
|
|
|
|
|
where
|
|
|
|
|
BInt::<N>:From<U>+core::ops::$trait,
|
|
|
|
|
{
|
|
|
|
|
#[inline]
|
|
|
|
|
fn $method(&mut self,other:U){
|
|
|
|
|
self.bits=self.bits.$not_assign_method(BInt::<N>::from(other));
|
|
|
|
|
fn $method(&mut self, other: U) {
|
|
|
|
|
self.bits.$method(BInt::<N>::from(other));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
@ -495,10 +500,10 @@ macro_16!( impl_multiplicative_assign_operator_not_const_generic, (Fixed, MulAss
|
|
|
|
|
macro_16!( impl_multiply_operator_not_const_generic, (Fixed, Mul, mul, Self) );
|
|
|
|
|
macro_16!( impl_multiplicative_assign_operator_not_const_generic, (Fixed, DivAssign, div_assign, div) );
|
|
|
|
|
macro_16!( impl_divide_operator_not_const_generic, (Fixed, Div, div, Self) );
|
|
|
|
|
impl_multiplicative_assign_operator!( Fixed, MulAssign, mul_assign, mul );
|
|
|
|
|
impl_multiplicative_operator!( Fixed, Mul, mul, mul, Self );
|
|
|
|
|
impl_multiplicative_assign_operator!( Fixed, DivAssign, div_assign, div_euclid );
|
|
|
|
|
impl_multiplicative_operator!( Fixed, Div, div, div_euclid, Self );
|
|
|
|
|
impl_multiplicative_assign_operator!( Fixed, MulAssign, mul_assign );
|
|
|
|
|
impl_multiplicative_operator!( Fixed, Mul, mul, Self );
|
|
|
|
|
impl_multiplicative_assign_operator!( Fixed, DivAssign, div_assign );
|
|
|
|
|
impl_multiplicative_operator!( Fixed, Div, div, Self );
|
|
|
|
|
#[cfg(feature="deferred-division")]
|
|
|
|
|
impl<const LHS_N:usize,const LHS_F:usize,const RHS_N:usize,const RHS_F:usize> core::ops::Div<Fixed<RHS_N,RHS_F>> for Fixed<LHS_N,LHS_F>{
|
|
|
|
|
type Output=ratio_ops::ratio::Ratio<Fixed<LHS_N,LHS_F>,Fixed<RHS_N,RHS_F>>;
|
|
|
|
|