create wide_traits crate

This commit is contained in:
Quaternions 2024-08-26 17:04:41 -07:00
parent f12ab4a3c3
commit dda8ebefc4
14 changed files with 110 additions and 16 deletions

5
fixed_wide/Cargo.lock generated
View File

@ -14,6 +14,7 @@ version = "0.1.0"
dependencies = [
"bnum",
"typenum",
"wide_traits",
]
[[package]]
@ -21,3 +22,7 @@ name = "typenum"
version = "1.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825"
[[package]]
name = "wide_traits"
version = "0.1.0"

View File

@ -6,3 +6,4 @@ edition = "2021"
[dependencies]
bnum = "0.11.0"
typenum = "1.17.0"
wide_traits = { version = "0.1.0", path = "../wide_traits" }

View File

@ -1,3 +1,2 @@
pub mod wide;
pub mod types;
pub mod narrow;

View File

@ -1,5 +1,6 @@
use bnum::cast::As;
use bnum::BInt;
use wide_traits::wide::WideMul;
use typenum::{Sum,Unsigned};
use std::marker::PhantomData;
@ -82,11 +83,6 @@ impl_operator!( Fixed, BitOr, bitor, Self );
impl_assign_operator!( Fixed, BitXorAssign, bitxor_assign );
impl_operator!( Fixed, BitXor, bitxor, Self );
pub trait WideMul<Rhs=Self>{
type Output;
fn wide_mul(self,rhs:Rhs)->Self::Output;
}
//going wider native
macro_rules! impl_wide_mul {
($lhs: expr,$rhs: expr) => {

View File

@ -14,6 +14,7 @@ version = "0.1.0"
dependencies = [
"bnum",
"typenum",
"wide_traits",
]
[[package]]
@ -21,6 +22,7 @@ name = "fixed_wide_vectors"
version = "0.1.0"
dependencies = [
"fixed_wide",
"wide_traits",
]
[[package]]
@ -28,3 +30,7 @@ name = "typenum"
version = "1.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825"
[[package]]
name = "wide_traits"
version = "0.1.0"

View File

@ -5,3 +5,4 @@ edition = "2021"
[dependencies]
fixed_wide = { version = "0.1.0", path = "../fixed_wide" }
wide_traits = { version = "0.1.0", path = "../wide_traits" }

View File

@ -2,7 +2,7 @@
#[macro_export(local_inner_macros)]
macro_rules! impl_wide_operations {
( $struct: ident { $($field: ident), + }, $size: expr ) => {
impl<U,T:Copy+fixed_wide::wide::WideMul<Output=U>> fixed_wide::wide::WideMul for $struct<T> {
impl<U,T:Copy+wide_traits::wide::WideMul<Output=U>> wide_traits::wide::WideMul for $struct<T> {
type Output=$struct<U>;
#[inline]
fn wide_mul(self, rhs: Self) -> Self::Output {
@ -11,7 +11,7 @@ macro_rules! impl_wide_operations {
}
}
}
impl<U:std::ops::Add<Output=U>,T:Copy+fixed_wide::wide::WideMul<Output=U>> $struct<T> {
impl<U:std::ops::Add<Output=U>,T:Copy+wide_traits::wide::WideMul<Output=U>> $struct<T> {
#[inline]
pub fn wide_dot(self, other: Self) -> U {
$crate::sum_repeating!(

View File

@ -1,4 +1,4 @@
use fixed_wide::wide::WideMul;
use wide_traits::wide::WideMul;
use crate::Vector3;

1
wide_traits/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

63
wide_traits/Cargo.lock generated Normal file
View File

@ -0,0 +1,63 @@
# 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 = "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"
[[package]]
name = "wide_traits"
version = "0.1.0"
dependencies = [
"fixed",
"typenum",
]

10
wide_traits/Cargo.toml Normal file
View File

@ -0,0 +1,10 @@
[package]
name = "wide_traits"
version = "0.1.0"
edition = "2021"
[dependencies]
[dev-dependencies]
fixed = "1.28.0"
typenum = "1.17.0"

2
wide_traits/src/lib.rs Normal file
View File

@ -0,0 +1,2 @@
pub mod wide;
pub mod narrow;

View File

@ -3,13 +3,19 @@ pub trait Narrow{
fn narrow(self)->Self::Output;
}
#[derive(Debug)]
pub enum NarrowError{
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<Self::Output,NarrowError>;
fn try_narrow(self)->Result<Self::Output,Error>;
}
#[cfg(test)]
mod tests {
@ -18,12 +24,12 @@ mod tests {
//TODO: use num_traits to do a blanket implementation (self<T::MIN as U)
impl TryNarrow for i16{
type Output=i8;
fn try_narrow(self)->Result<Self::Output,NarrowError>{
fn try_narrow(self)->Result<Self::Output,Error>{
if self<i8::MIN as i16{
return Err(NarrowError::Underflow);
return Err(Error::Underflow);
}
if (i8::MAX as i16)<self{
return Err(NarrowError::Overflow);
return Err(Error::Overflow);
}
Ok(self as i8)
}
@ -31,9 +37,9 @@ mod tests {
#[test]
fn test_i16_i8(){
assert!(matches!(257i16.try_narrow(),Err(NarrowError::Overflow)));
assert!(matches!(257i16.try_narrow(),Err(Error::Overflow)));
assert!(matches!(64i16.try_narrow(),Ok(64i8)));
assert!(matches!((-257i16).try_narrow(),Err(NarrowError::Underflow)));
assert!(matches!((-257i16).try_narrow(),Err(Error::Underflow)));
}
impl Narrow for fixed::FixedI16<typenum::consts::U8>{

4
wide_traits/src/wide.rs Normal file
View File

@ -0,0 +1,4 @@
pub trait WideMul<Rhs=Self>{
type Output;
fn wide_mul(self,rhs:Rhs)->Self::Output;
}