From 2a2e729f59da29bfcc780a01423c42c8ed8d5e72 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Mon, 2 Sep 2024 16:15:17 -0700 Subject: [PATCH] wip --- deferred_division/.gitignore | 1 - deferred_division/Cargo.lock | 36 ------ deferred_division/Cargo.toml | 14 --- deferred_division/src/lib.rs | 8 -- deferred_division/src/ratio.rs | 157 --------------------------- deferred_division/src/tests/mod.rs | 4 - deferred_division/src/tests/tests.rs | 22 ---- deferred_division/src/tests/wide.rs | 15 --- deferred_division/src/wide.rs | 58 ---------- fixed_wide/Cargo.lock | 12 +- fixed_wide/Cargo.toml | 7 +- fixed_wide/src/fixed.rs | 97 ++++++++++++++++- fixed_wide/src/fixed_wide_traits.rs | 88 --------------- fixed_wide/src/lib.rs | 9 +- fixed_wide/src/ratio.rs | 10 ++ fixed_wide/src/tests.rs | 1 - fixed_wide/src/traits.rs | 8 ++ fixed_wide/src/zeroes.rs | 58 ++++++++++ fixed_wide_traits/.gitignore | 1 - fixed_wide_traits/Cargo.lock | 63 ----------- fixed_wide_traits/Cargo.toml | 10 -- fixed_wide_traits/src/lib.rs | 2 - fixed_wide_traits/src/narrow.rs | 57 ---------- fixed_wide_traits/src/wide.rs | 16 --- 24 files changed, 188 insertions(+), 566 deletions(-) delete mode 100644 deferred_division/.gitignore delete mode 100644 deferred_division/Cargo.lock delete mode 100644 deferred_division/Cargo.toml delete mode 100644 deferred_division/src/lib.rs delete mode 100644 deferred_division/src/ratio.rs delete mode 100644 deferred_division/src/tests/mod.rs delete mode 100644 deferred_division/src/tests/tests.rs delete mode 100644 deferred_division/src/tests/wide.rs delete mode 100644 deferred_division/src/wide.rs delete mode 100644 fixed_wide/src/fixed_wide_traits.rs create mode 100644 fixed_wide/src/ratio.rs create mode 100644 fixed_wide/src/traits.rs create mode 100644 fixed_wide/src/zeroes.rs delete mode 100644 fixed_wide_traits/.gitignore delete mode 100644 fixed_wide_traits/Cargo.lock delete mode 100644 fixed_wide_traits/Cargo.toml delete mode 100644 fixed_wide_traits/src/lib.rs delete mode 100644 fixed_wide_traits/src/narrow.rs delete mode 100644 fixed_wide_traits/src/wide.rs diff --git a/deferred_division/.gitignore b/deferred_division/.gitignore deleted file mode 100644 index ea8c4bf..0000000 --- a/deferred_division/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/target diff --git a/deferred_division/Cargo.lock b/deferred_division/Cargo.lock deleted file mode 100644 index 512511f..0000000 --- a/deferred_division/Cargo.lock +++ /dev/null @@ -1,36 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 3 - -[[package]] -name = "bnum" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3e31ea183f6ee62ac8b8a8cf7feddd766317adfb13ff469de57ce033efd6a790" - -[[package]] -name = "deferred_division" -version = "0.1.0" -dependencies = [ - "fixed_wide", - "fixed_wide_traits", -] - -[[package]] -name = "fixed_wide" -version = "0.1.0" -dependencies = [ - "bnum", - "fixed_wide_traits", - "typenum", -] - -[[package]] -name = "fixed_wide_traits" -version = "0.1.0" - -[[package]] -name = "typenum" -version = "1.17.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" diff --git a/deferred_division/Cargo.toml b/deferred_division/Cargo.toml deleted file mode 100644 index 0f2ab79..0000000 --- a/deferred_division/Cargo.toml +++ /dev/null @@ -1,14 +0,0 @@ -[package] -name = "deferred_division" -version = "0.1.0" -edition = "2021" - -[features] -default=["fixed_wide_traits"] -fixed_wide_traits=["dep:fixed_wide_traits"] - -[dependencies] -fixed_wide_traits = { version = "0.1.0", path = "../fixed_wide_traits", optional = true } - -[dev-dependencies] -fixed_wide = { version = "0.1.0", path = "../fixed_wide" } diff --git a/deferred_division/src/lib.rs b/deferred_division/src/lib.rs deleted file mode 100644 index 86c9a9d..0000000 --- a/deferred_division/src/lib.rs +++ /dev/null @@ -1,8 +0,0 @@ -pub mod ratio; - - -#[cfg(feature="fixed_wide_traits")] -mod wide; - -#[cfg(test)] -mod tests; diff --git a/deferred_division/src/ratio.rs b/deferred_division/src/ratio.rs deleted file mode 100644 index c5abad4..0000000 --- a/deferred_division/src/ratio.rs +++ /dev/null @@ -1,157 +0,0 @@ -use std::ops::Mul; - -#[derive(Clone,Copy,Debug,Hash)] -pub struct Ratio{ - pub(crate)num:Num, - pub(crate)den:Den, -} -impl Ratio{ - pub const fn new(num:Num,den:Den)->Self{ - Self{num,den} - } -} - -impl PartialEq for Ratio - where - Den:Copy, - Rhs:Mul+Copy, - Num:PartialEq<>::Output> -{ - fn eq(&self,rhs:&Rhs)->bool{ - self.num.eq(&rhs.mul(self.den)) - } -} -/* -//You can't do Ratio==Ratio I guess -impl Eq for Ratio -where - Num:Mul, - >::Output:PartialEq -{} -*/ - -// num/den == rhs -// num == rhs * den - -impl PartialOrd for Ratio - where - Den:Copy, - Rhs:Mul+Copy, - Num:PartialOrd<>::Output> -{ - fn partial_cmp(&self,rhs:&Rhs)->Option{ - self.num.partial_cmp(&rhs.mul(self.den)) - } -} -/* -impl Ord for Ratio<>::Output,Den> -where - Rhs:Mul, - Rhs:Ord, - >::Output:Ord, -{ - fn cmp(&self,other:&Rhs)->std::cmp::Ordering{ - self.num.cmp(&other.mul(self.den)) - } -} -*/ - -impl,Den> std::ops::Neg for Ratio{ - type Output=Ratio; - fn neg(self)->Self::Output{ - Ratio{ - num:self.num.neg(), - den:self.den, - } - } -} - -// num/den + rhs == new_num/den -// new_num = num + rhs * den - -macro_rules! impl_operator { - ($struct:ident,$trait:ident,$method:ident)=>{ - impl core::ops::$trait for $struct - where - Den:Copy, - Rhs:Mul, - Num:core::ops::$trait<>::Output>, - { - type Output=$struct<>::Output>>::Output,Den>; - - fn $method(self,rhs:Rhs)->Self::Output{ - $struct{ - num:self.num.$method(rhs.mul(self.den)), - den:self.den, - } - } - } - }; -} -macro_rules! impl_assign_operator{ - ($struct:ident,$trait:ident,$method:ident)=>{ - impl core::ops::$trait for $struct - where - Den:Copy, - Rhs:Mul, - Num:core::ops::$trait<>::Output>, - { - fn $method(&mut self,rhs:Rhs){ - self.num.$method(rhs.mul(self.den)); - } - } - }; -} - -// Impl arithmetic operators -impl_assign_operator!(Ratio,AddAssign,add_assign); -impl_operator!(Ratio,Add,add); -impl_assign_operator!(Ratio,SubAssign,sub_assign); -impl_operator!(Ratio,Sub,sub); -// num/den % rhs == new_num/den -// new_num = num % (rhs * den) -impl_assign_operator!(Ratio,RemAssign,rem_assign); -impl_operator!(Ratio,Rem,rem); - -//mul and div is special -impl Mul for Ratio - where - Num:Mul, -{ - type Output=Ratio<>::Output,Den>; - fn mul(self,rhs:Rhs)->Self::Output{ - Ratio{ - num:self.num.mul(rhs), - den:self.den, - } - } -} -impl core::ops::MulAssign for Ratio - where - Num:core::ops::MulAssign, -{ - fn mul_assign(&mut self,rhs:Rhs){ - self.num.mul_assign(rhs); - } -} - -impl core::ops::Div for Ratio - where - Den:Mul, -{ - type Output=Ratio>::Output>; - fn div(self,rhs:Rhs)->Self::Output{ - Ratio{ - num:self.num, - den:self.den.mul(rhs), - } - } -} -impl core::ops::DivAssign for Ratio - where - Den:core::ops::MulAssign, -{ - fn div_assign(&mut self,rhs:Rhs){ - self.den.mul_assign(rhs); - } -} diff --git a/deferred_division/src/tests/mod.rs b/deferred_division/src/tests/mod.rs deleted file mode 100644 index a74aed6..0000000 --- a/deferred_division/src/tests/mod.rs +++ /dev/null @@ -1,4 +0,0 @@ -mod tests; - -#[cfg(feature="fixed_wide_traits")] -mod wide; diff --git a/deferred_division/src/tests/tests.rs b/deferred_division/src/tests/tests.rs deleted file mode 100644 index d004719..0000000 --- a/deferred_division/src/tests/tests.rs +++ /dev/null @@ -1,22 +0,0 @@ -use crate::ratio::Ratio; - -#[test] -fn ratio(){ - let r=Ratio::new(5,3); - let a=r%1; - assert_eq!(a.num,2); - assert_eq!(a.den,3); - let b=r*2; - assert_eq!(b.num,10); - assert_eq!(b.den,3); - let c=r/2; - assert_eq!(c.num,5); - assert_eq!(c.den,6); -} - -#[test] -fn add_ratio_cmp(){ - let a=Ratio::new(5,3); - let b=Ratio::new(1,3); - assert_eq!(a+b,2); -} diff --git a/deferred_division/src/tests/wide.rs b/deferred_division/src/tests/wide.rs deleted file mode 100644 index 4246565..0000000 --- a/deferred_division/src/tests/wide.rs +++ /dev/null @@ -1,15 +0,0 @@ -use crate::ratio::Ratio; -use fixed_wide_traits::wide::{WideMul,WideDiv}; -use fixed_wide::types::I32F32; -use fixed_wide::types::I64F64; - -#[test] -fn ratio(){ - let r=Ratio::new(I32F32::from(5),I32F32::from(3)); - let a=r.wide_mul(I32F32::from(7)>>2); - assert_eq!(a.num,I64F64::from(7*5)>>2); - assert_eq!(a.den,I32F32::from(3)); - let a=r.wide_div(I32F32::from(7)>>2); - assert_eq!(a.num,I32F32::from(5)); - assert_eq!(a.den,I64F64::from(3*7)>>2); -} diff --git a/deferred_division/src/wide.rs b/deferred_division/src/wide.rs deleted file mode 100644 index c2e1205..0000000 --- a/deferred_division/src/wide.rs +++ /dev/null @@ -1,58 +0,0 @@ -use std::ops::{Add,Mul}; -use crate::ratio::Ratio; -use fixed_wide_traits::wide::{WideMul,WideDiv,WideDot,WideCross}; - -impl Ratio -{ - pub fn rational_add(self,rhs:T)->Ratio<>::Output>>::Output,Den> - where - Den:Mul, - Num:Add<>::Output>, - { - Ratio{ - num:self.num+self.den.mul(rhs), - den:self.den, - } - } - pub fn wide_rational_add(self,rhs:T)->Ratio<>::Output>>::Output,Den> - where - Den:WideMul, - Num:Add<>::Output>, - { - Ratio{ - num:self.num+self.den.wide_mul(rhs), - den:self.den, - } - } -} -macro_rules! impl_mul_operator { - ($struct:ident,$trait:ident,$method:ident)=>{ - impl $trait for $struct - where - Num:$trait, - { - type Output=$struct<>::Output,Den>; - fn $method(self,rhs:Rhs)->Self::Output{ - $struct{ - num:self.num.$method(rhs), - den:self.den, - } - } - } - } -} -impl_mul_operator!(Ratio,WideMul,wide_mul); -impl_mul_operator!(Ratio,WideDot,wide_dot); -impl_mul_operator!(Ratio,WideCross,wide_cross); -impl WideDiv for Ratio - where - Den:WideMul, -{ - type Output=Ratio>::Output>; - fn wide_div(self,rhs:T)->Ratio>::Output>{ - Ratio{ - num:self.num, - den:self.den.wide_mul(rhs), - } - } -} diff --git a/fixed_wide/Cargo.lock b/fixed_wide/Cargo.lock index c65cd17..e28b2bc 100644 --- a/fixed_wide/Cargo.lock +++ b/fixed_wide/Cargo.lock @@ -2,6 +2,12 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "arrayvec" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50" + [[package]] name = "bnum" version = "0.11.0" @@ -12,15 +18,11 @@ checksum = "3e31ea183f6ee62ac8b8a8cf7feddd766317adfb13ff469de57ce033efd6a790" name = "fixed_wide" version = "0.1.0" dependencies = [ + "arrayvec", "bnum", - "fixed_wide_traits", "typenum", ] -[[package]] -name = "fixed_wide_traits" -version = "0.1.0" - [[package]] name = "typenum" version = "1.17.0" diff --git a/fixed_wide/Cargo.toml b/fixed_wide/Cargo.toml index 84206fd..8b2b4e9 100644 --- a/fixed_wide/Cargo.toml +++ b/fixed_wide/Cargo.toml @@ -4,10 +4,11 @@ version = "0.1.0" edition = "2021" [features] -default=["fixed_wide_traits"] -fixed_wide_traits=["dep:fixed_wide_traits"] +default=["zeroes"] +ratio=[] +zeroes=["ratio","dep:arrayvec"] [dependencies] bnum = "0.11.0" typenum = "1.17.0" -fixed_wide_traits = { version = "0.1.0", path = "../fixed_wide_traits", optional = true } +arrayvec = { version = "0.7.6", optional = true } diff --git a/fixed_wide/src/fixed.rs b/fixed_wide/src/fixed.rs index 663be09..4045ff1 100644 --- a/fixed_wide/src/fixed.rs +++ b/fixed_wide/src/fixed.rs @@ -1,5 +1,6 @@ use bnum::{BInt,cast::As}; -use typenum::Unsigned; +use typenum::{Sum,Unsigned}; +use crate::traits::WideMul; #[derive(Clone,Copy,Debug,Hash)] pub struct Fixed{ @@ -277,3 +278,97 @@ impl_shift_assign_operator!( Fixed, ShlAssign, shl_assign ); impl_shift_operator!( Fixed, Shl, shl, Self ); impl_shift_assign_operator!( Fixed, ShrAssign, shr_assign ); impl_shift_operator!( Fixed, Shr, shr, Self ); + +// WIDE MUL: multiply into a wider type +// let a = I32F32::ONE; +// let b:I64F64 = a.wide_mul(a); +macro_rules! impl_wide_mul{ + ($lhs:expr,$rhs:expr)=>{ + impl WideMul> for Fixed<$lhs,A> + where + A:std::ops::Add, + B:Unsigned, + { + type Output=Fixed<{$lhs+$rhs},Sum>; + fn wide_mul(self,rhs:Fixed<$rhs,B>)->Self::Output{ + Fixed::from_bits(self.bits.as_::>()*rhs.bits.as_::>()) + } + } + }; +} + +macro_rules! impl_wide_mul_all{ + ($(($x:expr, $y:expr)),*)=>{ + $( + impl_wide_mul!($x, $y); + )* + }; +} + +//const generics sidestepped wahoo +impl_wide_mul_all!( + (1,1),(2,1),(3,1),(4,1),(5,1),(6,1),(7,1),(8,1), + (1,2),(2,2),(3,2),(4,2),(5,2),(6,2),(7,2),(8,2), + (1,3),(2,3),(3,3),(4,3),(5,3),(6,3),(7,3),(8,3), + (1,4),(2,4),(3,4),(4,4),(5,4),(6,4),(7,4),(8,4), + (1,5),(2,5),(3,5),(4,5),(5,5),(6,5),(7,5),(8,5), + (1,6),(2,6),(3,6),(4,6),(5,6),(6,6),(7,6),(8,6), + (1,7),(2,7),(3,7),(4,7),(5,7),(6,7),(7,7),(8,7), + (1,8),(2,8),(3,8),(4,8),(5,8),(6,8),(7,8),(8,8) +); +impl Fixed{ + pub fn resize_into(self)->Fixed{ + Fixed::from_bits(self.bits.as_::>()) + } +} + +macro_rules! impl_const{ + ($n:expr)=>{ + impl Fixed<$n,F>{ + pub fn sqrt_unchecked(self)->Self{ + //1<>1 (sqrt-ish) + //3. add on fractional offset + //Voila + let used_bits=self.bits.bits() as i32-1-F::I32; + let max_shift=((used_bits>>1)+F::I32) as u32; + let mut result=Self::ZERO; + + //multiply by one to make the types match (hack) + let wide_self=self.wide_mul(Self::ONE); + //descend down the bits and check if flipping each bit would push the square over the input value + for shift in (0..=max_shift).rev(){ + let new_result=result|Self::from_bits(BInt::from_bits(bnum::BUint::power_of_two(shift))); + if new_result.wide_mul(new_result)<=wide_self{ + result=new_result; + } + } + result + } + pub fn sqrt(self)->Self{ + if selfOption{ + if self { - impl WideMul> for Fixed<$lhs,A> - where - A:std::ops::Add, - B:Unsigned, - { - type Output=Fixed<{$lhs+$rhs},Sum>; - fn wide_mul(self,rhs:Fixed<$rhs,B>)->Self::Output{ - Fixed::from_bits(self.bits.as_::>()*rhs.bits.as_::>()) - } - } - }; -} - -macro_rules! impl_wide_mul_all { - ($(($x:expr, $y:expr)),*) => { - $( - impl_wide_mul!($x, $y); - )* - }; -} - -//const generics sidestepped wahoo -impl_wide_mul_all!( - (1,1),(2,1),(3,1),(4,1),(5,1),(6,1),(7,1),(8,1), - (1,2),(2,2),(3,2),(4,2),(5,2),(6,2),(7,2),(8,2), - (1,3),(2,3),(3,3),(4,3),(5,3),(6,3),(7,3),(8,3), - (1,4),(2,4),(3,4),(4,4),(5,4),(6,4),(7,4),(8,4), - (1,5),(2,5),(3,5),(4,5),(5,5),(6,5),(7,5),(8,5), - (1,6),(2,6),(3,6),(4,6),(5,6),(6,6),(7,6),(8,6), - (1,7),(2,7),(3,7),(4,7),(5,7),(6,7),(7,7),(8,7), - (1,8),(2,8),(3,8),(4,8),(5,8),(6,8),(7,8),(8,8) -); -impl Fixed{ - pub fn widen(self)->Fixed{ - Fixed::from_bits(self.bits.as_::>()) - } -} - -impl Fixed - where - Fixed:::WideMul, - as WideMul>::Output:Ord, -{ - pub fn sqrt_unchecked(self)->Self{ - //1<>1 (sqrt-ish) - //3. add on fractional offset - //Voila - let used_bits=self.bits.bits() as i32-1-Frac::I32; - let max_shift=((used_bits>>1)+Frac::I32) as u32; - let mut result=Self::ZERO; - - //multiply by one to make the types match (hack) - let wide_self=self.wide_mul(Fixed::::ONE); - //descend down the bits and check if flipping each bit would push the square over the input value - for shift in (0..=max_shift).rev(){ - let new_result=result|Fixed::::from_bits(BInt::from_bits(bnum::BUint::power_of_two(shift))); - if new_result.wide_mul(new_result)<=wide_self{ - result=new_result; - } - } - result - } - pub fn sqrt(self)->Self{ - if selfOption{ - if self{ + pub(crate)num:Num, + pub(crate)den:Den, +} +impl Ratio{ + pub const fn new(num:Num,den:Den)->Self{ + Self{num,den} + } +} diff --git a/fixed_wide/src/tests.rs b/fixed_wide/src/tests.rs index 61e6b96..1e92025 100644 --- a/fixed_wide/src/tests.rs +++ b/fixed_wide/src/tests.rs @@ -1,4 +1,3 @@ -use fixed_wide_traits::wide::WideMul; use crate::types::I32F32; #[test] diff --git a/fixed_wide/src/traits.rs b/fixed_wide/src/traits.rs new file mode 100644 index 0000000..486fc49 --- /dev/null +++ b/fixed_wide/src/traits.rs @@ -0,0 +1,8 @@ +pub trait WideMul{ + type Output; + fn wide_mul(self,rhs:Rhs)->Self::Output; +} +pub trait WideDiv{ + type Output; + fn wide_div(self,rhs:Rhs)->Self::Output; +} diff --git a/fixed_wide/src/zeroes.rs b/fixed_wide/src/zeroes.rs new file mode 100644 index 0000000..60f5a47 --- /dev/null +++ b/fixed_wide/src/zeroes.rs @@ -0,0 +1,58 @@ +use crate::fixed::Fixed; +use crate::ratio::Ratio; + +use typenum::{Sum,Unsigned}; +use arrayvec::ArrayVec; +use std::cmp::Ordering; +macro_rules! impl_zeroes{ + ($n:expr)=>{ + impl Fixed<$n,F> + where + F:Unsigned+std::ops::Add, + ::Output:Unsigned, + { + #[inline] + pub fn zeroes2(a0:Self,a1:Self,a2:Self)->ArrayVec,2>{ + let a2pos=match a2.cmp(&Self::ZERO){ + Ordering::Greater=>true, + Ordering::Equal=>return ArrayVec::from_iter($crate::zeroes::zeroes1(a0,a1).into_iter()), + Ordering::Less=>true, + }; + let radicand=$crate::traits::WideMul::wide_mul(a1,a1)-$crate::traits::WideMul::wide_mul(a2,a0)*4; + match radicand.cmp(&Fixed::<{$n*2},Sum>::ZERO){ + Ordering::Greater=>{ + //start with f64 sqrt + //failure case: 2^63 < sqrt(2^127) + let planar_radicand=radicand.sqrt(); + //TODO: one or two newtons + //sort roots ascending and avoid taking the difference of large numbers + match (a2pos,Self::ZERO[Ratio::new(-a1-planar_radicand,a2*2),Ratio::new(a0*2,-a1-planar_radicand)].into(), + (true, false)=>[Ratio::new(a0*2,-a1+planar_radicand),Ratio::new(-a1+planar_radicand,a2*2)].into(), + (false,true )=>[Ratio::new(a0*2,-a1-planar_radicand),Ratio::new(-a1-planar_radicand,a2*2)].into(), + (false,false)=>[Ratio::new(-a1+planar_radicand,a2*2),Ratio::new(a0*2,-a1+planar_radicand)].into(), + } + }, + Ordering::Equal=>ArrayVec::from_iter([Ratio::new(a1,a2*-2)]), + Ordering::Less=>ArrayVec::new_const(), + } + } + #[inline] + pub fn zeroes1(a0:Self,a1:Self)->ArrayVec,1>{ + if a1==Self::ZERO{ + ArrayVec::new_const() + }else{ + ArrayVec::from_iter([Ratio::new(-a0,a1)]) + } + } + } + }; +} +impl_zeroes!(1); +impl_zeroes!(2); +impl_zeroes!(3); +impl_zeroes!(4); +impl_zeroes!(5); +impl_zeroes!(6); +impl_zeroes!(7); +impl_zeroes!(8); diff --git a/fixed_wide_traits/.gitignore b/fixed_wide_traits/.gitignore deleted file mode 100644 index ea8c4bf..0000000 --- a/fixed_wide_traits/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/target diff --git a/fixed_wide_traits/Cargo.lock b/fixed_wide_traits/Cargo.lock deleted file mode 100644 index 8ece885..0000000 --- a/fixed_wide_traits/Cargo.lock +++ /dev/null @@ -1,63 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 3 - -[[package]] -name = "az" -version = "1.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b7e4c2464d97fe331d41de9d5db0def0a96f4d823b8b32a2efd503578988973" - -[[package]] -name = "bytemuck" -version = "1.17.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6fd4c6dcc3b0aea2f5c0b4b82c2b15fe39ddbc76041a310848f4706edf76bb31" - -[[package]] -name = "cfg-if" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" - -[[package]] -name = "crunchy" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" - -[[package]] -name = "fixed" -version = "1.28.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85c6e0b89bf864acd20590dbdbad56f69aeb898abfc9443008fd7bd48b2cc85a" -dependencies = [ - "az", - "bytemuck", - "half", - "typenum", -] - -[[package]] -name = "fixed_wide_traits" -version = "0.1.0" -dependencies = [ - "fixed", - "typenum", -] - -[[package]] -name = "half" -version = "2.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6dd08c532ae367adf81c312a4580bc67f1d0fe8bc9c460520283f4c0ff277888" -dependencies = [ - "cfg-if", - "crunchy", -] - -[[package]] -name = "typenum" -version = "1.17.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" diff --git a/fixed_wide_traits/Cargo.toml b/fixed_wide_traits/Cargo.toml deleted file mode 100644 index b761c99..0000000 --- a/fixed_wide_traits/Cargo.toml +++ /dev/null @@ -1,10 +0,0 @@ -[package] -name = "fixed_wide_traits" -version = "0.1.0" -edition = "2021" - -[dependencies] - -[dev-dependencies] -fixed = "1.28.0" -typenum = "1.17.0" diff --git a/fixed_wide_traits/src/lib.rs b/fixed_wide_traits/src/lib.rs deleted file mode 100644 index 21c9224..0000000 --- a/fixed_wide_traits/src/lib.rs +++ /dev/null @@ -1,2 +0,0 @@ -pub mod wide; -pub mod narrow; diff --git a/fixed_wide_traits/src/narrow.rs b/fixed_wide_traits/src/narrow.rs deleted file mode 100644 index 698eacc..0000000 --- a/fixed_wide_traits/src/narrow.rs +++ /dev/null @@ -1,57 +0,0 @@ -pub trait Narrow{ - type Output; - fn narrow(self)->Self::Output; -} -#[derive(Debug)] -pub enum Error{ - Overflow, - Underflow, -} -impl std::fmt::Display for Error{ - fn fmt(&self,f:&mut std::fmt::Formatter<'_>)->std::fmt::Result{ - write!(f,"{self:?}") - } -} -impl std::error::Error for Error{} -pub trait TryNarrow{ - type Output; - fn try_narrow(self)->Result; -} -#[cfg(test)] -mod tests { - use super::*; - - //TODO: use num_traits to do a blanket implementation (selfResult{ - if self{ - type Output=i8; - fn narrow(self)->Self::Output{ - (self.to_bits()>>8) as i8 - } - } - - #[test] - fn test_fixed_i16_i8(){ - let a=fixed::FixedI16::::from(5)/2; - assert_eq!(a.narrow(),2); - } -} diff --git a/fixed_wide_traits/src/wide.rs b/fixed_wide_traits/src/wide.rs deleted file mode 100644 index dfb7b15..0000000 --- a/fixed_wide_traits/src/wide.rs +++ /dev/null @@ -1,16 +0,0 @@ -pub trait WideMul{ - type Output; - fn wide_mul(self,rhs:Rhs)->Self::Output; -} -pub trait WideDiv{ - type Output; - fn wide_div(self,rhs:Rhs)->Self::Output; -} -pub trait WideDot{ - type Output; - fn wide_dot(self,rhs:Rhs)->Self::Output; -} -pub trait WideCross{ - type Output; - fn wide_cross(self,rhs:Rhs)->Self::Output; -}