fixed_wide_vectors/fixed_wide/src/wide.rs

123 lines
3.2 KiB
Rust
Raw Normal View History

2024-08-23 21:17:48 +00:00
use bnum::cast::As;
2024-08-23 20:42:44 +00:00
use bnum::types::{I256,I512};
2024-08-26 21:58:19 +00:00
use bnum::BInt;
2024-08-23 23:26:19 +00:00
use fixed::{FixedI64,FixedI128};
2024-08-23 22:45:01 +00:00
use typenum::{Sum,Unsigned};
2024-08-26 21:58:19 +00:00
use std::marker::PhantomData;
2024-08-23 20:42:44 +00:00
2024-08-23 21:17:48 +00:00
#[derive(Clone,Copy,Debug)]
2024-08-26 21:58:19 +00:00
pub struct Fixed<const CHUNKS:usize,Frac>{
bits:BInt<{CHUNKS}>,
frac:PhantomData<Frac>,
2024-08-23 20:00:22 +00:00
}
2024-08-26 21:58:19 +00:00
pub type FixedI192<Frac>=Fixed<3,Frac>;
pub type FixedI256<Frac>=Fixed<4,Frac>;
pub type FixedI512<Frac>=Fixed<8,Frac>;
2024-08-23 20:42:44 +00:00
2024-08-26 21:58:19 +00:00
impl<const CHUNKS:usize,FracDst:Unsigned> From<i128> for Fixed<CHUNKS,FracDst>{
2024-08-23 21:17:48 +00:00
fn from(value:i128)->Self{
Self{
2024-08-26 21:58:19 +00:00
bits:BInt::<{CHUNKS}>::from(value)<<FracDst::U32,
frac:PhantomData,
2024-08-23 21:17:48 +00:00
}
}
}
2024-08-26 21:58:19 +00:00
impl<const CHUNKS:usize,Frac> PartialEq for Fixed<CHUNKS,Frac>{
2024-08-23 21:17:48 +00:00
fn eq(&self,other:&Self)->bool{
2024-08-23 23:53:54 +00:00
self.bits.eq(&other.bits)
2024-08-23 21:17:48 +00:00
}
}
2024-08-26 21:58:19 +00:00
impl<const CHUNKS:usize,Frac> Eq for Fixed<CHUNKS,Frac>{}
2024-08-23 21:17:48 +00:00
2024-08-26 21:58:19 +00:00
impl<const CHUNKS:usize,Frac> std::ops::Add for Fixed<CHUNKS,Frac>{
2024-08-23 23:48:24 +00:00
type Output=Self;
fn add(self,rhs:Self)->Self::Output{
Self{
bits:self.bits+rhs.bits,
2024-08-26 21:58:19 +00:00
frac:PhantomData,
2024-08-23 23:48:24 +00:00
}
}
}
2024-08-23 21:17:48 +00:00
pub trait WideMul<Rhs=Self>{
type Output;
fn wide_mul(self,rhs:Rhs)->Self::Output;
}
//going wide from foreign fixed type
2024-08-23 23:26:19 +00:00
impl<A,B> WideMul<FixedI64<B>> for FixedI64<A>
where
A:std::ops::Add<B>,
B:Unsigned,
{
type Output=FixedI128<Sum<A,B>>;
fn wide_mul(self,rhs:FixedI64<B>)->Self::Output{
self.wide_mul(rhs)
}
}
2024-08-26 21:58:19 +00:00
impl<A,B> WideMul<FixedI64<B>> for FixedI128<A>
2024-08-23 20:42:44 +00:00
where
A:std::ops::Add<B>,
B:Unsigned,
{
2024-08-26 21:58:19 +00:00
type Output=FixedI192<Sum<A,B>>;
fn wide_mul(self,rhs:FixedI64<B>)->Self::Output{
FixedI192{
bits:BInt::<3>::from(self.to_bits())*BInt::<3>::from(rhs.to_bits()),
frac:PhantomData,
}
}
}
impl<A,B> WideMul<FixedI128<B>> for FixedI64<A>
where
A:std::ops::Add<B>,
B:Unsigned,
{
type Output=FixedI192<Sum<A,B>>;
2024-08-23 20:42:44 +00:00
fn wide_mul(self,rhs:FixedI128<B>)->Self::Output{
2024-08-26 21:58:19 +00:00
FixedI192{
bits:BInt::<3>::from(self.to_bits())*BInt::<3>::from(rhs.to_bits()),
frac:PhantomData,
2024-08-23 20:42:44 +00:00
}
}
}
2024-08-26 21:58:19 +00:00
impl<A,B> WideMul<FixedI128<B>> for FixedI128<A>
2024-08-23 21:17:48 +00:00
where
A:std::ops::Add<B>,
B:Unsigned,
{
2024-08-26 21:58:19 +00:00
type Output=FixedI256<Sum<A,B>>;
fn wide_mul(self,rhs:FixedI128<B>)->Self::Output{
FixedI256{
bits:I256::from(self.to_bits())*I256::from(rhs.to_bits()),
frac:PhantomData,
2024-08-23 21:17:48 +00:00
}
}
}
2024-08-26 21:58:19 +00:00
//going wider native
macro_rules! impl_wide_mul {
($lhs: expr,$rhs: expr) => {
impl<A,B> WideMul<Fixed<$rhs,B>> for Fixed<$lhs,A>
where
A:std::ops::Add<B>,
B:Unsigned,
{
type Output=Fixed<{$lhs+$rhs},Sum<A,B>>;
fn wide_mul(self,rhs:Fixed<$rhs,B>)->Self::Output{
Fixed{
bits:self.bits.as_::<BInt<{$lhs+$rhs}>>()*rhs.bits.as_::<BInt<{$lhs+$rhs}>>(),
frac:PhantomData,
}
}
}
};
}
impl_wide_mul!(3,3);impl_wide_mul!(4,3);impl_wide_mul!(5,3);impl_wide_mul!(6,3);impl_wide_mul!(7,3);impl_wide_mul!(8,3);
impl_wide_mul!(3,4);impl_wide_mul!(4,4);impl_wide_mul!(5,4);impl_wide_mul!(6,4);impl_wide_mul!(7,4);impl_wide_mul!(8,4);
impl_wide_mul!(3,5);impl_wide_mul!(4,5);impl_wide_mul!(5,5);impl_wide_mul!(6,5);impl_wide_mul!(7,5);impl_wide_mul!(8,5);
impl_wide_mul!(3,6);impl_wide_mul!(4,6);impl_wide_mul!(5,6);impl_wide_mul!(6,6);impl_wide_mul!(7,6);impl_wide_mul!(8,6);
impl_wide_mul!(3,7);impl_wide_mul!(4,7);impl_wide_mul!(5,7);impl_wide_mul!(6,7);impl_wide_mul!(7,7);impl_wide_mul!(8,7);
impl_wide_mul!(3,8);impl_wide_mul!(4,8);impl_wide_mul!(5,8);impl_wide_mul!(6,8);impl_wide_mul!(7,8);impl_wide_mul!(8,8);