From 5f8104d53100fd815de3a2742a0d76fc2e2550bb Mon Sep 17 00:00:00 2001 From: Quaternions Date: Fri, 23 Aug 2024 14:17:48 -0700 Subject: [PATCH] wild --- fixed_wide/src/lib.rs | 1 + fixed_wide/src/types.rs | 2 ++ fixed_wide/src/wide.rs | 66 +++++++++++++++++++++++++++++++++++++---- src/lib.rs | 30 +++++++------------ 4 files changed, 75 insertions(+), 24 deletions(-) create mode 100644 fixed_wide/src/types.rs diff --git a/fixed_wide/src/lib.rs b/fixed_wide/src/lib.rs index 4472d7e..8bdda41 100644 --- a/fixed_wide/src/lib.rs +++ b/fixed_wide/src/lib.rs @@ -1 +1,2 @@ pub mod wide; +pub mod types; diff --git a/fixed_wide/src/types.rs b/fixed_wide/src/types.rs new file mode 100644 index 0000000..37ba47f --- /dev/null +++ b/fixed_wide/src/types.rs @@ -0,0 +1,2 @@ +pub type I256F128=crate::wide::FixedI256; +pub type I512F256=crate::wide::FixedI512; diff --git a/fixed_wide/src/wide.rs b/fixed_wide/src/wide.rs index a57d255..7376ac0 100644 --- a/fixed_wide/src/wide.rs +++ b/fixed_wide/src/wide.rs @@ -1,13 +1,10 @@ +use bnum::cast::As; use bnum::types::{I256,I512}; use fixed::{FixedI64,FixedI128}; use typenum::{Sum, Unsigned}; use typenum::consts::{U32,U64,U96,U128,U160,U192,U224,U256}; -pub trait WideMul{ - type Output; - fn wide_mul(self,rhs:Rhs)->Self::Output; -} - +#[derive(Clone,Copy,Debug)] pub struct Fixed{ bits:Int, phantom:std::marker::PhantomData, @@ -15,6 +12,28 @@ pub struct Fixed{ pub type FixedI256=Fixed; pub type FixedI512=Fixed; +impl+std::ops::Shl,FracDst:Unsigned> From for Fixed{ + fn from(value:i128)->Self{ + Self{ + bits:Int::from(value)< PartialEq for Fixed{ + fn eq(&self,other:&Self)->bool{ + self.bits==other.bits + } +} +impl Eq for Fixed{} + +pub trait WideMul{ + type Output; + fn wide_mul(self,rhs:Rhs)->Self::Output; +} + +//going wide from foreign fixed type impl WideMul> for FixedI128 where A:std::ops::Add, @@ -28,3 +47,40 @@ impl WideMul> for FixedI128 } } } +//going wider native +impl WideMul> for FixedI256 + where + A:std::ops::Add, + B:Unsigned, +{ + type Output=FixedI512>; + fn wide_mul(self,rhs:FixedI256)->Self::Output{ + FixedI512{ + bits:self.bits.as_::()*rhs.bits.as_::(), + phantom:std::marker::PhantomData, + } + } +} + +pub enum Narrower{ + Fits(T), + Max, + Min, +} + +fn tat(){ + let a:i8=match 257i64.try_into() { + Ok(it) => it, + Err(err) => panic!(), + }; +} + +//i16 -> i8 +// 257i16.try_narrow::() -> Narrower::Max +// 128i16.try_narrow::() -> Narrower::Fits(128i8) +// -257i16.try_narrow::() -> Narrower::Min + +pub trait TryNarrow{ + type Output; + fn wide_mul(self,rhs:Rhs)->Self::Output; +} diff --git a/src/lib.rs b/src/lib.rs index d1507fe..9e67f80 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,22 +1,9 @@ -use bnum::cast::As; +use fixed_wide::wide::WideMul; type Planar64=fixed::types::I32F32; type Planar64Wide1=fixed::types::I64F64; -type Planar64Wide2=bnum::types::I256; -type Planar64Wide3=bnum::types::I512; -//wouldn't that be nice -//type Planar64Wide2=fixed::FixedI256; -//type Planar64Wide3=fixed::FixedI512; - -pub fn wide_mul(left: Planar64, right: Planar64) -> Planar64Wide1 { - left.wide_mul(right) -} -pub fn wide_mul_2(left: Planar64Wide1, right: Planar64Wide1) -> Planar64Wide2 { - Planar64Wide2::from(left.to_bits())*Planar64Wide2::from(right.to_bits()) -} -pub fn wide_mul_3(left: Planar64Wide2, right: Planar64Wide2) -> Planar64Wide3 { - left.as_::()*right.as_::() -} +type Planar64Wide2=fixed_wide::types::I256F128; +type Planar64Wide3=fixed_wide::types::I512F256; #[cfg(test)] mod tests { @@ -24,13 +11,18 @@ mod tests { #[test] fn it_works() { - let result = add(2.into(), 2.into()); - assert_eq!(result, 4); + let a=Planar64::from(2); + let b=Planar64::from(3); + + let w1=a.wide_mul(b); + let w2=w1.wide_mul(w1); + let w3=w2.wide_mul(w2); + + assert_eq!(w3,Planar64Wide3::from(1296)); } #[test] pub fn main(){ - let a=6f128; let max=i32::MAX as i64; println!("{} {}",max*max,i64::MAX); }