Compare commits

...

2 Commits
master ... sqrt

Author SHA1 Message Date
bf55d52c1b wip cordic 2024-08-27 17:17:23 -07:00
e878644a48 how to do bnum sqrt 2024-08-27 16:50:55 -07:00
5 changed files with 110 additions and 0 deletions

56
fixed_wide/Cargo.lock generated
View File

@ -2,17 +2,63 @@
# 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 = "bnum"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3e31ea183f6ee62ac8b8a8cf7feddd766317adfb13ff469de57ce033efd6a790"
[[package]]
name = "bytemuck"
version = "1.17.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "773d90827bc3feecfb67fab12e24de0749aad83c74b9504ecde46237b5cd24e2"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "cordic"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ed0a176c0b8c5c95fa0523177530364c5b68a8895d9745730dbfa692a7412d0"
dependencies = [
"fixed",
]
[[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"
version = "0.1.0"
dependencies = [
"bnum",
"cordic",
"fixed_wide_traits",
"typenum",
]
@ -21,6 +67,16 @@ dependencies = [
name = "fixed_wide_traits"
version = "0.1.0"
[[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"

View File

@ -6,8 +6,10 @@ edition = "2021"
[features]
default=["fixed_wide_traits"]
fixed_wide_traits=["dep:fixed_wide_traits"]
cordic=["dep:cordic"]
[dependencies]
bnum = "0.11.0"
typenum = "1.17.0"
fixed_wide_traits = { version = "0.1.0", path = "../fixed_wide_traits", optional = true }
cordic = { version = "0.1.5", optional = true }

33
fixed_wide/src/cordic.rs Normal file
View File

@ -0,0 +1,33 @@
use bnum::BInt;
use typenum::Unsigned;
use crate::fixed::Fixed;
use std::marker::PhantomData;
impl<const CHUNKS:usize,Frac:Unsigned> cordic::CordicNumber for Fixed<CHUNKS,Frac>{
fn floor(self)->Self{
Self{
bits:self.bits.bitand(BInt::<CHUNKS>::ONE.shl(Frac::U32).sub(BInt::<CHUNKS>::ONE).not()),
frac:PhantomData,
}
}
fn zero()->Self{
Self::ZERO
}
fn one()->Self{
Self::ONE
}
fn from_u0f64(val:U0F64)->Self{
val
}
fn num_fract_bits()->u8{
Frac::U8
}
fn num_bits()->u8{
CHUNKS as u8*64
}
}

View File

@ -125,3 +125,19 @@ 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 );
macro_rules! impl_sqrt {
( $struct: ident, $chunks: expr) => {
impl<Frac:Unsigned> $struct<$chunks,Frac>{
pub fn sqrt(self)->$struct<{$chunks/2+1},typenum::PartialDiv<Frac,typenum::U2>>{
$struct{
bits:self.bits.sqrt(),
frac:PhantomData,
}
}
}
};
}
impl_sqrt!(Fixed,1);impl_sqrt!(Fixed,2);impl_sqrt!(Fixed,3);impl_sqrt!(Fixed,4);impl_sqrt!(Fixed,5);impl_sqrt!(Fixed,6);impl_sqrt!(Fixed,7);impl_sqrt!(Fixed,8);
impl_sqrt!(Fixed,9);impl_sqrt!(Fixed,10);impl_sqrt!(Fixed,11);impl_sqrt!(Fixed,12);impl_sqrt!(Fixed,13);impl_sqrt!(Fixed,14);impl_sqrt!(Fixed,15);impl_sqrt!(Fixed,16);

View File

@ -9,3 +9,6 @@ pub mod typenum{
mod fixed_wide_traits;
#[cfg(feature="fixed_wide_traits")]
pub use ::fixed_wide_traits::wide;
#[cfg(feature="cordic")]
mod cordic;