Compare commits
4 Commits
array-back
...
partialord
Author | SHA1 | Date | |
---|---|---|---|
17d43852d3 | |||
12b825bb15 | |||
cd70967c08 | |||
626eb7e9e2 |
deferred_division
fixed_wide
fixed_wide_traits
fixed_wide_vectors
1
deferred_division/.gitignore
vendored
Normal file
1
deferred_division/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/target
|
14
deferred_division/Cargo.lock
generated
Normal file
14
deferred_division/Cargo.lock
generated
Normal file
@ -0,0 +1,14 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "deferred_division"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"fixed_wide_traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fixed_wide_traits"
|
||||
version = "0.1.0"
|
11
deferred_division/Cargo.toml
Normal file
11
deferred_division/Cargo.toml
Normal file
@ -0,0 +1,11 @@
|
||||
[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 }
|
8
deferred_division/src/lib.rs
Normal file
8
deferred_division/src/lib.rs
Normal file
@ -0,0 +1,8 @@
|
||||
pub mod ratio;
|
||||
|
||||
|
||||
#[cfg(feature="fixed_wide_traits")]
|
||||
mod wide;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
10
deferred_division/src/ratio.rs
Normal file
10
deferred_division/src/ratio.rs
Normal file
@ -0,0 +1,10 @@
|
||||
#[derive(Clone,Copy,Debug,Hash)]
|
||||
pub struct Ratio<Num,Den>{
|
||||
pub(crate)num:Num,
|
||||
pub(crate)den:Den,
|
||||
}
|
||||
//this trait is like a constructor for Ratio
|
||||
pub trait DeferredDiv<Rhs=Self>{
|
||||
type Output;
|
||||
fn deferred_div(self,rhs:Rhs)->Self::Output;
|
||||
}
|
63
deferred_division/src/wide.rs
Normal file
63
deferred_division/src/wide.rs
Normal file
@ -0,0 +1,63 @@
|
||||
use std::ops::{Add,Mul};
|
||||
use std::cmp::Ordering;
|
||||
use crate::ratio::Ratio;
|
||||
use fixed_wide_traits::wide::{WideMul,WideDiv};
|
||||
|
||||
impl<Num,Den,T> PartialOrd<T> for Ratio<Num,Den>
|
||||
{
|
||||
fn partial_cmp(&self,other:&T)->Option<Ordering>{
|
||||
//a < c*b
|
||||
match self.den.cmp(0){
|
||||
Ordering::Less=>Some(self.num.partial_cmp(self.den.mul(other))),
|
||||
Ordering::Equal=>None,//divide by zero
|
||||
Ordering::Greater=>Some(self.num.partial_cmp(self.den.mul(other)).reverse()),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<Num,Den:Copy> Ratio<Num,Den>
|
||||
{
|
||||
pub fn rational_add<T>(self,rhs:T)->Ratio<<Num as Add<<Den as Mul<T>>::Output>>::Output,Den>
|
||||
where
|
||||
Den:Mul<T>,
|
||||
Num:Add<<Den as Mul<T>>::Output>,
|
||||
{
|
||||
Ratio{
|
||||
num:self.num+self.den.mul(rhs),
|
||||
den:self.den,
|
||||
}
|
||||
}
|
||||
pub fn wide_rational_add<T>(self,rhs:T)->Ratio<<Num as Add<<Den as WideMul<T>>::Output>>::Output,Den>
|
||||
where
|
||||
Den:WideMul<T>,
|
||||
Num:Add<<Den as WideMul<T>>::Output>,
|
||||
{
|
||||
Ratio{
|
||||
num:self.num+self.den.wide_mul(rhs),
|
||||
den:self.den,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<Num,Den,T> WideMul<T> for Ratio<Num,Den>
|
||||
where
|
||||
Num:WideMul<T>,
|
||||
{
|
||||
type Output=Ratio<<Num as WideMul<T>>::Output,Den>;
|
||||
fn wide_mul(self,rhs:T)->Ratio<<Num as WideMul<T>>::Output,Den>{
|
||||
Ratio{
|
||||
num:self.num.wide_mul(rhs),
|
||||
den:self.den,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<Num,Den,T> WideDiv<T> for Ratio<Num,Den>
|
||||
where
|
||||
Den:WideMul<T>,
|
||||
{
|
||||
type Output=Ratio<Num,<Den as WideMul<T>>::Output>;
|
||||
fn wide_div(self,rhs:T)->Ratio<Num,<Den as WideMul<T>>::Output>{
|
||||
Ratio{
|
||||
num:self.num,
|
||||
den:self.den.wide_mul(rhs),
|
||||
}
|
||||
}
|
||||
}
|
20
fixed_wide/Cargo.lock
generated
20
fixed_wide/Cargo.lock
generated
@ -2,12 +2,6 @@
|
||||
# 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"
|
||||
@ -18,13 +12,17 @@ checksum = "3e31ea183f6ee62ac8b8a8cf7feddd766317adfb13ff469de57ce033efd6a790"
|
||||
name = "fixed_wide"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"arrayvec",
|
||||
"bnum",
|
||||
"paste",
|
||||
"fixed_wide_traits",
|
||||
"typenum",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "paste"
|
||||
version = "1.0.15"
|
||||
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 = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
|
||||
checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825"
|
||||
|
@ -4,11 +4,10 @@ version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[features]
|
||||
default=["zeroes"]
|
||||
ratio=[]
|
||||
zeroes=["ratio","dep:arrayvec"]
|
||||
default=["fixed_wide_traits"]
|
||||
fixed_wide_traits=["dep:fixed_wide_traits"]
|
||||
|
||||
[dependencies]
|
||||
bnum = "0.11.0"
|
||||
arrayvec = { version = "0.7.6", optional = true }
|
||||
paste = "1.0.15"
|
||||
typenum = "1.17.0"
|
||||
fixed_wide_traits = { version = "0.1.0", path = "../fixed_wide_traits", optional = true }
|
||||
|
@ -1,386 +1,82 @@
|
||||
use bnum::{BInt,cast::As};
|
||||
use bnum::BInt;
|
||||
use typenum::Unsigned;
|
||||
use std::marker::PhantomData;
|
||||
|
||||
#[derive(Clone,Copy,Debug,Hash)]
|
||||
/// N is the number of u64s to use
|
||||
/// F is the number of fractional bits (always N*32 lol)
|
||||
pub struct Fixed<const N:usize,const F:usize>{
|
||||
pub(crate)bits:BInt<{N}>,
|
||||
pub struct Fixed<const CHUNKS:usize,Frac>{
|
||||
pub(crate)bits:BInt<{CHUNKS}>,
|
||||
pub(crate)frac:PhantomData<Frac>,
|
||||
}
|
||||
|
||||
impl<const N:usize,const F:usize> Fixed<N,F>{
|
||||
pub const MAX:Self=Self::from_bits(BInt::<N>::MAX);
|
||||
pub const MIN:Self=Self::from_bits(BInt::<N>::MIN);
|
||||
pub const ZERO:Self=Self::from_bits(BInt::<N>::ZERO);
|
||||
pub const EPSILON:Self=Self::from_bits(BInt::<N>::ONE);
|
||||
pub const NEG_EPSILON:Self=Self::from_bits(BInt::<N>::NEG_ONE);
|
||||
pub const ONE:Self=Self::from_bits(BInt::<N>::ONE.shl(F as u32));
|
||||
pub const TWO:Self=Self::from_bits(BInt::<N>::TWO.shl(F as u32));
|
||||
pub const HALF:Self=Self::from_bits(BInt::<N>::ONE.shl(F as u32-1));
|
||||
pub const NEG_ONE:Self=Self::from_bits(BInt::<N>::NEG_ONE.shl(F as u32));
|
||||
pub const NEG_TWO:Self=Self::from_bits(BInt::<N>::NEG_TWO.shl(F as u32));
|
||||
pub const NEG_HALF:Self=Self::from_bits(BInt::<N>::NEG_ONE.shl(F as u32-1));
|
||||
}
|
||||
impl<const N:usize,const F:usize> Fixed<N,F>{
|
||||
#[inline]
|
||||
pub const fn from_bits(bits:BInt::<N>)->Self{
|
||||
Self{
|
||||
bits,
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
pub const fn to_bits(self)->BInt<N>{
|
||||
self.bits
|
||||
}
|
||||
#[inline]
|
||||
pub const fn raw(value:i64)->Self{
|
||||
Self::from_bits(BInt::from_bits(bnum::BUint::from_digit(value as u64)))
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N:usize,const F:usize,T> From<T> for Fixed<N,F>
|
||||
impl<const CHUNKS:usize,FracDst:Unsigned,T> From<T> for Fixed<CHUNKS,FracDst>
|
||||
where
|
||||
BInt<N>:From<T>
|
||||
BInt<CHUNKS>:From<T>
|
||||
{
|
||||
fn from(value:T)->Self{
|
||||
Self::from_bits(BInt::<{N}>::from(value)<<F as u32)
|
||||
Self{
|
||||
bits:BInt::<{CHUNKS}>::from(value)<<FracDst::U32,
|
||||
frac:PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N:usize,const F:usize> PartialEq for Fixed<N,F>{
|
||||
impl<const CHUNKS:usize,Frac> PartialEq for Fixed<CHUNKS,Frac>{
|
||||
fn eq(&self,other:&Self)->bool{
|
||||
self.bits.eq(&other.bits)
|
||||
}
|
||||
}
|
||||
impl<const N:usize,const F:usize> Eq for Fixed<N,F>{}
|
||||
impl<const CHUNKS:usize,Frac> Eq for Fixed<CHUNKS,Frac>{}
|
||||
|
||||
impl<const N:usize,const F:usize> PartialOrd for Fixed<N,F>{
|
||||
fn partial_cmp(&self,other:&Self)->Option<std::cmp::Ordering>{
|
||||
self.bits.partial_cmp(&other.bits)
|
||||
}
|
||||
}
|
||||
impl<const N:usize,const F:usize> Ord for Fixed<N,F>{
|
||||
fn cmp(&self,other:&Self)->std::cmp::Ordering{
|
||||
self.bits.cmp(&other.bits)
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N:usize,const F:usize> std::ops::Neg for Fixed<N,F>{
|
||||
impl<const CHUNKS:usize,Frac> std::ops::Neg for Fixed<CHUNKS,Frac>{
|
||||
type Output=Self;
|
||||
fn neg(self)->Self{
|
||||
Self::from_bits(self.bits.neg())
|
||||
Self{
|
||||
bits:self.bits.neg(),
|
||||
frac:PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_additive_operator {
|
||||
macro_rules! impl_operator {
|
||||
( $struct: ident, $trait: ident, $method: ident, $output: ty ) => {
|
||||
impl<const N:usize,const F:usize> core::ops::$trait for $struct<N,F>{
|
||||
type Output = $output;
|
||||
impl<const CHUNKS:usize,Frac> core::ops::$trait<$struct<CHUNKS,Frac>> for $struct<CHUNKS,Frac>{
|
||||
type Output = $output;
|
||||
|
||||
fn $method(self, other: Self) -> Self::Output {
|
||||
Self::from_bits(self.bits.$method(other.bits))
|
||||
}
|
||||
}
|
||||
impl<const N:usize,const F:usize,U> core::ops::$trait<U> for $struct<N,F>
|
||||
where
|
||||
BInt::<N>:From<U>,
|
||||
{
|
||||
type Output = $output;
|
||||
|
||||
fn $method(self, other: U) -> Self::Output {
|
||||
Self::from_bits(self.bits.$method(BInt::<N>::from(other).shl(F as u32)))
|
||||
}
|
||||
}
|
||||
};
|
||||
fn $method(self, other: Self) -> Self::Output {
|
||||
Self {
|
||||
bits:self.bits.$method(other.bits),
|
||||
frac:PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
macro_rules! impl_additive_assign_operator {
|
||||
( $struct: ident, $trait: ident, $method: ident ) => {
|
||||
impl<const N:usize,const F:usize> core::ops::$trait for $struct<N,F>{
|
||||
fn $method(&mut self, other: Self) {
|
||||
self.bits.$method(other.bits);
|
||||
}
|
||||
}
|
||||
impl<const N:usize,const F:usize,U> core::ops::$trait<U> for $struct<N,F>
|
||||
where
|
||||
BInt::<N>:From<U>,
|
||||
{
|
||||
fn $method(&mut self, other: U) {
|
||||
self.bits.$method(BInt::<N>::from(other)<<F as u32);
|
||||
}
|
||||
}
|
||||
};
|
||||
macro_rules! impl_assign_operator {
|
||||
( $struct: ident, $trait: ident, $method: ident ) => {
|
||||
impl<const CHUNKS:usize,Frac> core::ops::$trait<$struct<CHUNKS,Frac>> for $struct<CHUNKS,Frac>{
|
||||
fn $method(&mut self, other: Self) {
|
||||
self.bits.$method(other.bits);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// Impl arithmetic pperators
|
||||
impl_additive_assign_operator!( Fixed, AddAssign, add_assign );
|
||||
impl_additive_operator!( Fixed, Add, add, Self );
|
||||
impl_additive_assign_operator!( Fixed, SubAssign, sub_assign );
|
||||
impl_additive_operator!( Fixed, Sub, sub, Self );
|
||||
impl_additive_assign_operator!( Fixed, RemAssign, rem_assign );
|
||||
impl_additive_operator!( Fixed, Rem, rem, Self );
|
||||
impl_assign_operator!( Fixed, AddAssign, add_assign );
|
||||
impl_operator!( Fixed, Add, add, Self );
|
||||
impl_assign_operator!( Fixed, SubAssign, sub_assign );
|
||||
impl_operator!( Fixed, Sub, sub, Self );
|
||||
impl_assign_operator!( Fixed, MulAssign, mul_assign );
|
||||
impl_operator!( Fixed, Mul, mul, Self );
|
||||
impl_assign_operator!( Fixed, DivAssign, div_assign );
|
||||
impl_operator!( Fixed, Div, div, Self );
|
||||
impl_assign_operator!( Fixed, RemAssign, rem_assign );
|
||||
impl_operator!( Fixed, Rem, rem, Self );
|
||||
|
||||
// Impl bitwise operators
|
||||
impl_additive_assign_operator!( Fixed, BitAndAssign, bitand_assign );
|
||||
impl_additive_operator!( Fixed, BitAnd, bitand, Self );
|
||||
impl_additive_assign_operator!( Fixed, BitOrAssign, bitor_assign );
|
||||
impl_additive_operator!( Fixed, BitOr, bitor, Self );
|
||||
impl_additive_assign_operator!( Fixed, BitXorAssign, bitxor_assign );
|
||||
impl_additive_operator!( Fixed, BitXor, bitxor, Self );
|
||||
|
||||
macro_rules! impl_multiply_operator_const {
|
||||
( $width:expr, $struct: ident, $trait: ident, $method: ident, $output: ty ) => {
|
||||
impl<const F:usize> core::ops::$trait for $struct<$width,F>{
|
||||
type Output = $output;
|
||||
|
||||
fn $method(self, other: Self) -> Self::Output {
|
||||
//this can be done better but that is a job for later
|
||||
let lhs=self.bits.as_::<BInt::<{$width*2}>>();
|
||||
let rhs=other.bits.as_::<BInt::<{$width*2}>>();
|
||||
Self::from_bits(lhs.mul(rhs).shr(F as u32).as_())
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
macro_rules! impl_multiply_assign_operator_const {
|
||||
( $width:expr, $struct: ident, $trait: ident, $method: ident ) => {
|
||||
impl<const F:usize> core::ops::$trait for $struct<$width,F>{
|
||||
fn $method(&mut self, other: Self) {
|
||||
self.bits.$method(other.bits);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! impl_divide_operator_const {
|
||||
( $width:expr, $struct: ident, $trait: ident, $method: ident, $output: ty ) => {
|
||||
impl<const F:usize> core::ops::$trait for $struct<$width,F>{
|
||||
type Output = $output;
|
||||
|
||||
fn $method(self, other: Self) -> Self::Output {
|
||||
//this can be done better but that is a job for later
|
||||
//this only needs to be $width+F as u32/64+1 but MUH CONST GENERICS!!!!!
|
||||
let lhs=self.bits.as_::<BInt::<{$width*2}>>().shl(F as u32);
|
||||
let rhs=other.bits.as_::<BInt::<{$width*2}>>();
|
||||
Self::from_bits(lhs.div(rhs).as_())
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
macro_rules! impl_divide_assign_operator_const {
|
||||
( $width:expr, $struct: ident, $trait: ident, $method: ident ) => {
|
||||
impl<const F:usize> core::ops::$trait for $struct<$width,F>{
|
||||
fn $method(&mut self, other: Self) {
|
||||
self.bits.$method(other.bits);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! impl_multiplicatave_operator {
|
||||
( $struct: ident, $trait: ident, $method: ident, $output: ty ) => {
|
||||
impl<const N:usize,const F:usize,U> core::ops::$trait<U> for $struct<N,F>
|
||||
where
|
||||
BInt::<N>:From<U>+core::ops::$trait,
|
||||
{
|
||||
type Output = $output;
|
||||
|
||||
fn $method(self, other: U) -> Self::Output {
|
||||
Self::from_bits(self.bits.$method(BInt::<N>::from(other)))
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
macro_rules! impl_multiplicatave_assign_operator {
|
||||
( $struct: ident, $trait: ident, $method: ident ) => {
|
||||
impl<const N:usize,const F:usize,U> core::ops::$trait<U> for $struct<N,F>
|
||||
where
|
||||
BInt::<N>:From<U>+core::ops::$trait,
|
||||
{
|
||||
fn $method(&mut self, other: U) {
|
||||
self.bits.$method(BInt::<N>::from(other));
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
impl<const N:usize,const F:usize> std::iter::Sum for Fixed<N,F>{
|
||||
fn sum<I:Iterator<Item=Self>>(iter:I)->Self{
|
||||
let mut sum=Self::ZERO;
|
||||
for elem in iter{
|
||||
sum+=elem;
|
||||
}
|
||||
sum
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_operator_16 {
|
||||
( $macro: ident, $struct: ident, $trait: ident, $method: ident, $output: ty ) => {
|
||||
$macro!(1,$struct,$trait,$method,$output);
|
||||
$macro!(2,$struct,$trait,$method,$output);
|
||||
$macro!(3,$struct,$trait,$method,$output);
|
||||
$macro!(4,$struct,$trait,$method,$output);
|
||||
$macro!(5,$struct,$trait,$method,$output);
|
||||
$macro!(6,$struct,$trait,$method,$output);
|
||||
$macro!(7,$struct,$trait,$method,$output);
|
||||
$macro!(8,$struct,$trait,$method,$output);
|
||||
$macro!(9,$struct,$trait,$method,$output);
|
||||
$macro!(10,$struct,$trait,$method,$output);
|
||||
$macro!(11,$struct,$trait,$method,$output);
|
||||
$macro!(12,$struct,$trait,$method,$output);
|
||||
$macro!(13,$struct,$trait,$method,$output);
|
||||
$macro!(14,$struct,$trait,$method,$output);
|
||||
$macro!(15,$struct,$trait,$method,$output);
|
||||
$macro!(16,$struct,$trait,$method,$output);
|
||||
}
|
||||
}
|
||||
macro_rules! impl_assign_operator_16 {
|
||||
( $macro: ident, $struct: ident, $trait: ident, $method: ident ) => {
|
||||
$macro!(1,$struct,$trait,$method);
|
||||
$macro!(2,$struct,$trait,$method);
|
||||
$macro!(3,$struct,$trait,$method);
|
||||
$macro!(4,$struct,$trait,$method);
|
||||
$macro!(5,$struct,$trait,$method);
|
||||
$macro!(6,$struct,$trait,$method);
|
||||
$macro!(7,$struct,$trait,$method);
|
||||
$macro!(8,$struct,$trait,$method);
|
||||
$macro!(9,$struct,$trait,$method);
|
||||
$macro!(10,$struct,$trait,$method);
|
||||
$macro!(11,$struct,$trait,$method);
|
||||
$macro!(12,$struct,$trait,$method);
|
||||
$macro!(13,$struct,$trait,$method);
|
||||
$macro!(14,$struct,$trait,$method);
|
||||
$macro!(15,$struct,$trait,$method);
|
||||
$macro!(16,$struct,$trait,$method);
|
||||
}
|
||||
}
|
||||
|
||||
impl_assign_operator_16!( impl_multiply_assign_operator_const, Fixed, MulAssign, mul_assign );
|
||||
impl_operator_16!( impl_multiply_operator_const, Fixed, Mul, mul, Self );
|
||||
impl_assign_operator_16!( impl_divide_assign_operator_const, Fixed, DivAssign, div_assign );
|
||||
impl_operator_16!( impl_divide_operator_const, Fixed, Div, div, Self );
|
||||
impl_multiplicatave_assign_operator!( Fixed, MulAssign, mul_assign );
|
||||
impl_multiplicatave_operator!( Fixed, Mul, mul, Self );
|
||||
impl_multiplicatave_assign_operator!( Fixed, DivAssign, div_assign );
|
||||
impl_multiplicatave_operator!( Fixed, Div, div, Self );
|
||||
|
||||
macro_rules! impl_shift_operator {
|
||||
( $struct: ident, $trait: ident, $method: ident, $output: ty ) => {
|
||||
impl<const N:usize,const F:usize> core::ops::$trait<u32> for $struct<N,F>{
|
||||
type Output = $output;
|
||||
|
||||
fn $method(self, other: u32) -> Self::Output {
|
||||
Self::from_bits(self.bits.$method(other))
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
macro_rules! impl_shift_assign_operator {
|
||||
( $struct: ident, $trait: ident, $method: ident ) => {
|
||||
impl<const N:usize,const F:usize> core::ops::$trait<u32> for $struct<N,F>{
|
||||
fn $method(&mut self, other: u32) {
|
||||
self.bits.$method(other);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
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 Fixed<$lhs,{$lhs*32}>
|
||||
{
|
||||
paste::item!{
|
||||
pub fn [<wide_mul_ $lhs _ $rhs>](self,rhs:Fixed<$rhs,{$rhs*32}>)->Fixed<{$lhs+$rhs},{($lhs+$rhs)*32}>{
|
||||
Fixed::from_bits(self.bits.as_::<BInt<{$lhs+$rhs}>>()*rhs.bits.as_::<BInt<{$lhs+$rhs}>>())
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
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<const SRC:usize,const F:usize> Fixed<SRC,F>{
|
||||
pub fn resize_into<const DST:usize>(self)->Fixed<DST,F>{
|
||||
Fixed::from_bits(self.bits.as_::<BInt<DST>>())
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_const{
|
||||
($n:expr)=>{
|
||||
impl Fixed<{$n*2},{$n*2*32}>{
|
||||
pub fn halve_precision(self)->Fixed<$n,{$n*32}>{
|
||||
Fixed::from_bits(bnum::cast::As::as_(self.bits.shr($n*32)))
|
||||
}
|
||||
}
|
||||
impl Fixed<$n,{$n*32}>{
|
||||
paste::item!{
|
||||
pub fn sqrt_unchecked(self)->Self{
|
||||
//1<<max_shift must be the minimum power of two which when squared is greater than self
|
||||
//calculating max_shift:
|
||||
//1. count "used" bits to the left of the decimal, not including the sign bit (so -1)
|
||||
//2. divide by 2 via >>1 (sqrt-ish)
|
||||
//3. add on fractional offset
|
||||
//Voila
|
||||
let used_bits=self.bits.bits() as i32-1-($n*32) as i32;
|
||||
let max_shift=((used_bits>>1)+($n*32) as i32) as u32;
|
||||
let mut result=Self::ZERO;
|
||||
|
||||
//multiply by one to make the types match (hack)
|
||||
let wide_self=self.[<wide_mul_ $n _ $n>](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_ $n _ $n>](new_result)<=wide_self{
|
||||
result=new_result;
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
}
|
||||
pub fn sqrt(self)->Self{
|
||||
if self<Self::ZERO{
|
||||
panic!("Square root less than zero")
|
||||
}else{
|
||||
self.sqrt_unchecked()
|
||||
}
|
||||
}
|
||||
pub fn sqrt_checked(self)->Option<Self>{
|
||||
if self<Self::ZERO{
|
||||
None
|
||||
}else{
|
||||
Some(self.sqrt_unchecked())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
impl_const!(1);
|
||||
impl_const!(2);
|
||||
impl_const!(3);
|
||||
impl_const!(4);
|
||||
impl_const!(5);
|
||||
impl_const!(6);
|
||||
impl_const!(7);
|
||||
impl_const!(8);
|
||||
impl_assign_operator!( Fixed, BitAndAssign, bitand_assign );
|
||||
impl_operator!( Fixed, BitAnd, bitand, Self );
|
||||
impl_assign_operator!( Fixed, BitOrAssign, bitor_assign );
|
||||
impl_operator!( Fixed, BitOr, bitor, Self );
|
||||
impl_assign_operator!( Fixed, BitXorAssign, bitxor_assign );
|
||||
impl_operator!( Fixed, BitXor, bitxor, Self );
|
||||
|
44
fixed_wide/src/fixed_wide_traits.rs
Normal file
44
fixed_wide/src/fixed_wide_traits.rs
Normal file
@ -0,0 +1,44 @@
|
||||
use bnum::BInt;
|
||||
use bnum::cast::As;
|
||||
use typenum::{Sum,Unsigned};
|
||||
use crate::fixed::Fixed;
|
||||
use fixed_wide_traits::wide::WideMul;
|
||||
use std::marker::PhantomData;
|
||||
|
||||
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,
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
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)
|
||||
);
|
@ -1,10 +1,7 @@
|
||||
pub mod fixed;
|
||||
pub mod types;
|
||||
|
||||
#[cfg(feature="zeroes")]
|
||||
pub mod zeroes;
|
||||
#[cfg(feature="ratio")]
|
||||
pub mod ratio;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
#[cfg(feature="fixed_wide_traits")]
|
||||
mod fixed_wide_traits;
|
||||
#[cfg(feature="fixed_wide_traits")]
|
||||
pub use ::fixed_wide_traits::wide;
|
||||
|
@ -1,10 +0,0 @@
|
||||
#[derive(Clone,Copy,Debug,Hash)]
|
||||
pub struct Ratio<Num,Den>{
|
||||
pub(crate)num:Num,
|
||||
pub(crate)den:Den,
|
||||
}
|
||||
impl<Num,Den> Ratio<Num,Den>{
|
||||
pub const fn new(num:Num,den:Den)->Self{
|
||||
Self{num,den}
|
||||
}
|
||||
}
|
@ -1,90 +0,0 @@
|
||||
use crate::types::I32F32;
|
||||
use crate::types::I256F256;
|
||||
|
||||
#[test]
|
||||
fn you_can_add_numbers(){
|
||||
let a=I256F256::from((3i128*2).pow(4));
|
||||
assert_eq!(a+a,I256F256::from((3i128*2).pow(4)*2))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn you_can_shr_numbers(){
|
||||
let a=I32F32::from(4);
|
||||
assert_eq!(a>>1,I32F32::from(2))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_wide_mul(){
|
||||
let a=I32F32::ONE;
|
||||
let aa=a.wide_mul_1_1(a);
|
||||
assert_eq!(aa,crate::types::I64F64::ONE);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_wide_mul_repeated() {
|
||||
let a=I32F32::from(2);
|
||||
let b=I32F32::from(3);
|
||||
|
||||
let w1=a.wide_mul_1_1(b);
|
||||
let w2=w1.wide_mul_2_2(w1);
|
||||
let w3=w2.wide_mul_4_4(w2);
|
||||
|
||||
assert_eq!(w3,I256F256::from((3i128*2).pow(4)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bint(){
|
||||
let a=I32F32::ONE;
|
||||
assert_eq!(a*2,I32F32::from(2));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sqrt(){
|
||||
let a=I32F32::ONE*4;
|
||||
assert_eq!(a.sqrt(),I32F32::from(2));
|
||||
}
|
||||
#[test]
|
||||
fn test_sqrt_zero(){
|
||||
let a=I32F32::ZERO;
|
||||
assert_eq!(a.sqrt(),I32F32::ZERO);
|
||||
}
|
||||
#[test]
|
||||
fn test_sqrt_low(){
|
||||
let a=I32F32::HALF;
|
||||
let b=a*a;
|
||||
assert_eq!(b.sqrt(),a);
|
||||
}
|
||||
fn find_equiv_sqrt_via_f64(n:I32F32)->I32F32{
|
||||
//GIMME THEM BITS BOY
|
||||
let &[bits]=n.to_bits().to_bits().digits();
|
||||
let ibits=bits as i64;
|
||||
let f=(ibits as f64)/((1u64<<32) as f64);
|
||||
let f_ans=f.sqrt();
|
||||
let i=(f_ans*((1u64<<32) as f64)) as i64;
|
||||
let r=I32F32::from_bits(bnum::BInt::<1>::from(i));
|
||||
//mimic the behaviour of the algorithm,
|
||||
//return the result if it truncates to the exact answer
|
||||
if (r+I32F32::EPSILON).wide_mul_1_1(r+I32F32::EPSILON)==n.wide_mul_1_1(I32F32::ONE){
|
||||
return r+I32F32::EPSILON;
|
||||
}
|
||||
if (r-I32F32::EPSILON).wide_mul_1_1(r-I32F32::EPSILON)==n.wide_mul_1_1(I32F32::ONE){
|
||||
return r-I32F32::EPSILON;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
fn test_exact(n:I32F32){
|
||||
assert_eq!(n.sqrt(),find_equiv_sqrt_via_f64(n));
|
||||
}
|
||||
#[test]
|
||||
fn test_sqrt_exact(){
|
||||
//43
|
||||
for i in 0..((i64::MAX as f32).ln() as u32){
|
||||
let n=I32F32::from_bits(bnum::BInt::<1>::from((i as f32).exp() as i64));
|
||||
test_exact(n);
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn test_sqrt_max(){
|
||||
let a=I32F32::MAX;
|
||||
test_exact(a);
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
pub type I32F32=crate::fixed::Fixed<1,32>;
|
||||
pub type I64F64=crate::fixed::Fixed<2,64>;
|
||||
pub type I128F128=crate::fixed::Fixed<4,128>;
|
||||
pub type I256F256=crate::fixed::Fixed<8,256>;
|
||||
pub type I32F32=crate::fixed::Fixed<1,typenum::consts::U32>;
|
||||
pub type I64F64=crate::fixed::Fixed<2,typenum::consts::U64>;
|
||||
pub type I128F128=crate::fixed::Fixed<4,typenum::consts::U128>;
|
||||
pub type I256F256=crate::fixed::Fixed<8,typenum::consts::U256>;
|
||||
|
@ -1,53 +0,0 @@
|
||||
use crate::fixed::Fixed;
|
||||
use crate::ratio::Ratio;
|
||||
|
||||
use arrayvec::ArrayVec;
|
||||
use std::cmp::Ordering;
|
||||
macro_rules! impl_zeroes{
|
||||
($n:expr)=>{
|
||||
impl Fixed<$n,{$n*32}>{
|
||||
#[inline]
|
||||
pub fn zeroes2(a0:Self,a1:Self,a2:Self)->ArrayVec<Ratio<Self,Self>,2>{
|
||||
let a2pos=match a2.cmp(&Self::ZERO){
|
||||
Ordering::Greater=>true,
|
||||
Ordering::Equal=>return ArrayVec::from_iter(Self::zeroes1(a0,a1).into_iter()),
|
||||
Ordering::Less=>true,
|
||||
};
|
||||
paste::item!{
|
||||
let radicand=a1.[<wide_mul_ $n _ $n>](a1)-a2.[<wide_mul_ $n _ $n>](a0)*4;
|
||||
}
|
||||
match radicand.cmp(&Fixed::<{$n*2},{$n*2*32}>::ZERO){
|
||||
Ordering::Greater=>{
|
||||
let planar_radicand=radicand.sqrt().halve_precision();
|
||||
//sort roots ascending and avoid taking the difference of large numbers
|
||||
match (a2pos,Self::ZERO<a1){
|
||||
(true, true )=>[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<Ratio<Self,Self>,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);
|
||||
//sqrt doubles twice!
|
||||
//impl_zeroes!(5);
|
||||
//impl_zeroes!(6);
|
||||
//impl_zeroes!(7);
|
||||
//impl_zeroes!(8);
|
1
fixed_wide_traits/.gitignore
vendored
Normal file
1
fixed_wide_traits/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/target
|
63
fixed_wide_traits/Cargo.lock
generated
Normal file
63
fixed_wide_traits/Cargo.lock
generated
Normal 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 = "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"
|
10
fixed_wide_traits/Cargo.toml
Normal file
10
fixed_wide_traits/Cargo.toml
Normal file
@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "fixed_wide_traits"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
|
||||
[dev-dependencies]
|
||||
fixed = "1.28.0"
|
||||
typenum = "1.17.0"
|
2
fixed_wide_traits/src/lib.rs
Normal file
2
fixed_wide_traits/src/lib.rs
Normal file
@ -0,0 +1,2 @@
|
||||
pub mod wide;
|
||||
pub mod narrow;
|
57
fixed_wide_traits/src/narrow.rs
Normal file
57
fixed_wide_traits/src/narrow.rs
Normal file
@ -0,0 +1,57 @@
|
||||
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<Self::Output,Error>;
|
||||
}
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
//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,Error>{
|
||||
if self<i8::MIN as i16{
|
||||
return Err(Error::Underflow);
|
||||
}
|
||||
if (i8::MAX as i16)<self{
|
||||
return Err(Error::Overflow);
|
||||
}
|
||||
Ok(self as i8)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_i16_i8(){
|
||||
assert!(matches!(257i16.try_narrow(),Err(Error::Overflow)));
|
||||
assert!(matches!(64i16.try_narrow(),Ok(64i8)));
|
||||
assert!(matches!((-257i16).try_narrow(),Err(Error::Underflow)));
|
||||
}
|
||||
|
||||
impl Narrow for fixed::FixedI16<typenum::consts::U8>{
|
||||
type Output=i8;
|
||||
fn narrow(self)->Self::Output{
|
||||
(self.to_bits()>>8) as i8
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fixed_i16_i8(){
|
||||
let a=fixed::FixedI16::<typenum::consts::U8>::from(5)/2;
|
||||
assert_eq!(a.narrow(),2);
|
||||
}
|
||||
}
|
16
fixed_wide_traits/src/wide.rs
Normal file
16
fixed_wide_traits/src/wide.rs
Normal file
@ -0,0 +1,16 @@
|
||||
pub trait WideMul<Rhs=Self>{
|
||||
type Output;
|
||||
fn wide_mul(self,rhs:Rhs)->Self::Output;
|
||||
}
|
||||
pub trait WideDiv<Rhs=Self>{
|
||||
type Output;
|
||||
fn wide_div(self,rhs:Rhs)->Self::Output;
|
||||
}
|
||||
pub trait WideDot<Rhs=Self>{
|
||||
type Output;
|
||||
fn wide_dot(self,rhs:Rhs)->Self::Output;
|
||||
}
|
||||
pub trait WideCross<Rhs=Self>{
|
||||
type Output;
|
||||
fn wide_cross(self,rhs:Rhs)->Self::Output;
|
||||
}
|
22
fixed_wide_vectors/Cargo.lock
generated
22
fixed_wide_vectors/Cargo.lock
generated
@ -2,12 +2,6 @@
|
||||
# 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"
|
||||
@ -18,21 +12,25 @@ checksum = "3e31ea183f6ee62ac8b8a8cf7feddd766317adfb13ff469de57ce033efd6a790"
|
||||
name = "fixed_wide"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"arrayvec",
|
||||
"bnum",
|
||||
"paste",
|
||||
"fixed_wide_traits",
|
||||
"typenum",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fixed_wide_traits"
|
||||
version = "0.1.0"
|
||||
|
||||
[[package]]
|
||||
name = "fixed_wide_vectors"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"fixed_wide",
|
||||
"paste",
|
||||
"fixed_wide_traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "paste"
|
||||
version = "1.0.15"
|
||||
name = "typenum"
|
||||
version = "1.17.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
|
||||
checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825"
|
||||
|
@ -4,10 +4,11 @@ version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[features]
|
||||
default=["fixed_wide","named-fields"]
|
||||
named-fields=[]
|
||||
fixed_wide=["dep:fixed_wide","dep:paste"]
|
||||
default=["fixed_wide_traits"]
|
||||
fixed_wide_traits=["dep:fixed_wide_traits"]
|
||||
|
||||
[dependencies]
|
||||
fixed_wide = { version = "0.1.0", path = "../fixed_wide", optional = true }
|
||||
paste = { version = "1.0.15", optional = true }
|
||||
fixed_wide_traits = { version = "0.1.0", path = "../fixed_wide_traits", optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
fixed_wide = { version = "0.1.0", path = "../fixed_wide" }
|
||||
|
@ -1,10 +1,9 @@
|
||||
mod macros;
|
||||
pub mod types;
|
||||
pub mod vector;
|
||||
pub mod matrix;
|
||||
mod vector;
|
||||
|
||||
#[cfg(feature="named-fields")]
|
||||
mod named;
|
||||
pub use vector::Vector2;
|
||||
pub use vector::Vector3;
|
||||
pub use vector::Vector4;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
@ -1 +0,0 @@
|
||||
|
@ -1,212 +0,0 @@
|
||||
#[doc(hidden)]
|
||||
#[macro_export(local_inner_macros)]
|
||||
macro_rules! impl_wide_vector_operations_2arg_not_const_generic {
|
||||
(
|
||||
(),
|
||||
($lhs:expr, $rhs:expr)
|
||||
) => {
|
||||
impl<const N:usize> Vector<N,fixed_wide::fixed::Fixed<{$lhs},{$lhs*32}>>{
|
||||
paste::item!{
|
||||
#[inline]
|
||||
pub fn [<wide_mul_ $lhs _ $rhs>](self,rhs:Vector<N,fixed_wide::fixed::Fixed<{$rhs},{$rhs*32}>>)->Vector<N,fixed_wide::fixed::Fixed<{$lhs+$rhs},{($lhs+$rhs)*32}>>{
|
||||
self.map_zip(rhs,|(a,b)|a.[<wide_mul_ $lhs _ $rhs>](b))
|
||||
}
|
||||
#[inline]
|
||||
pub fn [<wide_dot_ $lhs _ $rhs>](self,rhs:Vector<N,fixed_wide::fixed::Fixed<{$rhs},{$rhs*32}>>)->fixed_wide::fixed::Fixed<{$lhs+$rhs},{($lhs+$rhs)*32}>{
|
||||
self.array.into_iter().zip(rhs.array).map(|(a,b)|a.[<wide_mul_ $lhs _ $rhs>](b)).sum()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#[doc(hidden)]
|
||||
#[macro_export(local_inner_macros)]
|
||||
macro_rules! impl_wide_vector_operations_1arg_not_const_generic {
|
||||
(
|
||||
(),
|
||||
$n:expr
|
||||
) => {
|
||||
impl<const N:usize> Vector<N,fixed_wide::fixed::Fixed<{$n},{$n*32}>>{
|
||||
paste::item!{
|
||||
#[inline]
|
||||
pub fn wide_length_squared(&self)->fixed_wide::fixed::Fixed<{$n*2},{$n*2*32}>{
|
||||
self.array.into_iter().map(|t|t.[<wide_mul_ $n _ $n>](t)).sum()
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[macro_export(local_inner_macros)]
|
||||
macro_rules! do_macro_8x8{
|
||||
(
|
||||
$macro:ident,
|
||||
$any:tt
|
||||
)=>{
|
||||
$crate::macro_repeated!($macro, $any,
|
||||
(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)
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[macro_export(local_inner_macros)]
|
||||
macro_rules! do_macro_8{
|
||||
(
|
||||
$macro:ident,
|
||||
$any:tt
|
||||
)=>{
|
||||
$crate::macro_repeated!($macro, $any, 1,2,3,4,5,6,7,8);
|
||||
};
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[macro_export(local_inner_macros)]
|
||||
macro_rules! impl_wide_vector_operations {
|
||||
() => {
|
||||
$crate::do_macro_8!(impl_wide_vector_operations_1arg_not_const_generic,());
|
||||
$crate::do_macro_8x8!(impl_wide_vector_operations_2arg_not_const_generic,());
|
||||
};
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[macro_export(local_inner_macros)]
|
||||
macro_rules! impl_vector_3_wide_cross {
|
||||
(
|
||||
(),
|
||||
($lhs:expr, $rhs:expr)
|
||||
)=>{
|
||||
impl Vector<3,fixed_wide::fixed::Fixed<{$lhs},{$lhs*32}>>{
|
||||
paste::item!{
|
||||
#[inline]
|
||||
pub fn [<wide_cross_ $lhs _ $rhs>](self,rhs:Vector<3,fixed_wide::fixed::Fixed<{$rhs},{$rhs*32}>>)->Vector<3,fixed_wide::fixed::Fixed<{$lhs+$rhs},{($lhs+$rhs)*32}>>{
|
||||
Vector::new([
|
||||
self.y.[<wide_mul_ $lhs _ $rhs>](rhs.z)-self.z.[<wide_mul_ $lhs _ $rhs>](rhs.y),
|
||||
self.z.[<wide_mul_ $lhs _ $rhs>](rhs.x)-self.x.[<wide_mul_ $lhs _ $rhs>](rhs.z),
|
||||
self.x.[<wide_mul_ $lhs _ $rhs>](rhs.y)-self.y.[<wide_mul_ $lhs _ $rhs>](rhs.x),
|
||||
])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[macro_export(local_inner_macros)]
|
||||
macro_rules! impl_vector_wide_3 {
|
||||
()=>{
|
||||
$crate::do_macro_8x8!(impl_vector_3_wide_cross,());
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[macro_export(local_inner_macros)]
|
||||
macro_rules! do_macro_4_dumb{
|
||||
(
|
||||
$macro:ident,
|
||||
$any:tt
|
||||
)=>{
|
||||
$crate::macro_repeated!($macro, $any, (1,2),(2,4),(3,6),(4,8));
|
||||
};
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[macro_export(local_inner_macros)]
|
||||
macro_rules! impl_matrix_wide_dot {
|
||||
(
|
||||
(),
|
||||
($lhs: expr, $rhs: expr)
|
||||
) => {
|
||||
impl<const X:usize,const Y:usize> Matrix<X,Y,fixed_wide::fixed::Fixed<{$lhs},{$lhs*32}>>{
|
||||
paste::item!{
|
||||
#[inline]
|
||||
pub fn [<wide_dot_ $lhs _ $rhs>]<const Z:usize>(self,rhs:Matrix<Z,X,fixed_wide::fixed::Fixed<{$rhs},{$rhs*32}>>)->Matrix<Z,Y,fixed_wide::fixed::Fixed<{$lhs+$rhs},{($lhs+$rhs)*32}>>{
|
||||
let mut array_of_iterators=rhs.array.map(|axis|axis.into_iter().cycle());
|
||||
Matrix::new(
|
||||
self.array.map(|axis|
|
||||
core::array::from_fn(|_|
|
||||
// axis dot product with transposed rhs array
|
||||
axis.iter().zip(
|
||||
array_of_iterators.iter_mut()
|
||||
).map(|(&lhs_value,rhs_iter)|
|
||||
lhs_value.[<wide_mul_ $lhs _ $rhs>](rhs_iter.next().unwrap())
|
||||
).sum()
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[macro_export(local_inner_macros)]
|
||||
macro_rules! impl_matrix_wide_dot_8x8 {
|
||||
() => {
|
||||
$crate::do_macro_8x8!(impl_matrix_wide_dot,());
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[macro_export(local_inner_macros)]
|
||||
macro_rules! impl_matrix_wide_3x3_det_not_const_generic {
|
||||
(
|
||||
$n: expr,
|
||||
$_2n: expr
|
||||
)=>{
|
||||
impl Matrix<3,3,fixed_wide::fixed::Fixed<$n,{$n*32}>>{
|
||||
paste::item!{
|
||||
pub fn [<wide_det_3x3_ $n>](self)->fixed_wide::fixed::Fixed<{$n*3},{$n*3*32}>{
|
||||
//[<wide_dot_ $n _ $n*2>] will not compile, so the doubles are hardcoded above
|
||||
self.x_axis.[<wide_dot_ $n _ $_2n>](self.y_axis.[<wide_cross_ $n _ $n>](self.z_axis))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#[doc(hidden)]
|
||||
#[macro_export(local_inner_macros)]
|
||||
macro_rules! impl_matrix_wide_3x3_det_not_const_generic_shim {
|
||||
(
|
||||
(),($n: expr,$_2n: expr)
|
||||
)=>{
|
||||
$crate::impl_matrix_wide_3x3_det_not_const_generic!($n,$_2n);
|
||||
}
|
||||
}
|
||||
#[doc(hidden)]
|
||||
#[macro_export(local_inner_macros)]
|
||||
macro_rules! impl_matrix_wide_3x3_adjugate_not_const_generic {
|
||||
(
|
||||
(),
|
||||
$n: expr
|
||||
)=>{
|
||||
impl Matrix<3,3,fixed_wide::fixed::Fixed<$n,{$n*32}>>{
|
||||
paste::item!{
|
||||
pub fn [<wide_adjugate_3x3_ $n>](self)->Matrix<3,3,fixed_wide::fixed::Fixed<{$n*2},{$n*2*32}>>{
|
||||
Matrix::new([
|
||||
[self.y_axis.y.[<wide_mul_ $n _ $n>](self.z_axis.z)-self.y_axis.z.[<wide_mul_ $n _ $n>](self.z_axis.y),self.x_axis.z.[<wide_mul_ $n _ $n>](self.z_axis.y)-self.x_axis.y.[<wide_mul_ $n _ $n>](self.z_axis.z),self.x_axis.y.[<wide_mul_ $n _ $n>](self.y_axis.z)-self.x_axis.z.[<wide_mul_ $n _ $n>](self.y_axis.y)],
|
||||
[self.y_axis.z.[<wide_mul_ $n _ $n>](self.z_axis.x)-self.y_axis.x.[<wide_mul_ $n _ $n>](self.z_axis.z),self.x_axis.x.[<wide_mul_ $n _ $n>](self.z_axis.z)-self.x_axis.z.[<wide_mul_ $n _ $n>](self.z_axis.x),self.x_axis.z.[<wide_mul_ $n _ $n>](self.y_axis.x)-self.x_axis.x.[<wide_mul_ $n _ $n>](self.y_axis.z)],
|
||||
[self.y_axis.x.[<wide_mul_ $n _ $n>](self.z_axis.y)-self.y_axis.y.[<wide_mul_ $n _ $n>](self.z_axis.x),self.x_axis.y.[<wide_mul_ $n _ $n>](self.z_axis.x)-self.x_axis.x.[<wide_mul_ $n _ $n>](self.z_axis.y),self.x_axis.x.[<wide_mul_ $n _ $n>](self.y_axis.y)-self.x_axis.y.[<wide_mul_ $n _ $n>](self.y_axis.x)],
|
||||
])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#[doc(hidden)]
|
||||
#[macro_export(local_inner_macros)]
|
||||
macro_rules! impl_matrix_wide_3x3 {
|
||||
()=>{
|
||||
$crate::do_macro_4_dumb!(impl_matrix_wide_3x3_det_not_const_generic_shim,());
|
||||
$crate::do_macro_8!(impl_matrix_wide_3x3_adjugate_not_const_generic,());
|
||||
}
|
||||
}
|
@ -1,129 +0,0 @@
|
||||
#[doc(hidden)]
|
||||
#[macro_export(local_inner_macros)]
|
||||
macro_rules! impl_matrix {
|
||||
() => {
|
||||
impl<const X:usize,const Y:usize,T> Matrix<X,Y,T>{
|
||||
#[inline(always)]
|
||||
pub const fn new(array:[[T;X];Y])->Self{
|
||||
Self{array}
|
||||
}
|
||||
#[inline(always)]
|
||||
pub fn to_array(self)->[[T;X];Y]{
|
||||
self.array
|
||||
}
|
||||
#[inline]
|
||||
pub fn map<F,U>(self,f:F)->Matrix<X,Y,U>
|
||||
where
|
||||
F:Fn(T)->U
|
||||
{
|
||||
Matrix::new(
|
||||
self.array.map(|inner|inner.map(&f)),
|
||||
)
|
||||
}
|
||||
#[inline]
|
||||
pub fn transpose(self)->Matrix<Y,X,T>{
|
||||
//how did I think of this
|
||||
let mut array_of_iterators=self.array.map(|axis|axis.into_iter());
|
||||
Matrix::new(
|
||||
core::array::from_fn(|_|
|
||||
array_of_iterators.each_mut().map(|iter|
|
||||
iter.next().unwrap()
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
#[inline]
|
||||
// MatY<VecX>.MatX<VecZ> = MatY<VecZ>
|
||||
pub fn dot<const Z:usize,U,V>(self,rhs:Matrix<Z,X,U>)->Matrix<Z,Y,V>
|
||||
where
|
||||
T:core::ops::Mul<U,Output=V>+Copy,
|
||||
V:core::iter::Sum,
|
||||
U:Copy,
|
||||
{
|
||||
let mut array_of_iterators=rhs.array.map(|axis|axis.into_iter().cycle());
|
||||
Matrix::new(
|
||||
self.array.map(|axis|
|
||||
core::array::from_fn(|_|
|
||||
// axis dot product with transposed rhs array
|
||||
axis.iter().zip(
|
||||
array_of_iterators.iter_mut()
|
||||
).map(|(&lhs_value,rhs_iter)|
|
||||
lhs_value*rhs_iter.next().unwrap()
|
||||
).sum()
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
impl<const X:usize,const Y:usize,T> Matrix<X,Y,T>
|
||||
where
|
||||
T:Copy
|
||||
{
|
||||
pub const fn from_value(value:T)->Self{
|
||||
Self::new([[value;X];Y])
|
||||
}
|
||||
}
|
||||
|
||||
impl<const X:usize,const Y:usize,T:Default> Default for Matrix<X,Y,T>{
|
||||
fn default()->Self{
|
||||
Self::new(
|
||||
core::array::from_fn(|_|core::array::from_fn(|_|Default::default()))
|
||||
)
|
||||
}
|
||||
}
|
||||
#[cfg(feature="fixed_wide")]
|
||||
$crate::impl_matrix_wide_dot_8x8!();
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[macro_export(local_inner_macros)]
|
||||
macro_rules! impl_matrix_named_fields_shape {
|
||||
(
|
||||
($struct_outer:ident, $size_outer: expr),
|
||||
($size_inner: expr)
|
||||
) => {
|
||||
impl<T> core::ops::Deref for Matrix<$size_outer,$size_inner,T>{
|
||||
type Target=$struct_outer<Vector<$size_inner,T>>;
|
||||
fn deref(&self)->&Self::Target{
|
||||
unsafe{core::mem::transmute(&self.array)}
|
||||
}
|
||||
}
|
||||
impl<T> core::ops::DerefMut for Matrix<$size_outer,$size_inner,T>{
|
||||
fn deref_mut(&mut self)->&mut Self::Target{
|
||||
unsafe{core::mem::transmute(&mut self.array)}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[macro_export(local_inner_macros)]
|
||||
macro_rules! impl_matrix_named_fields_shape_shim {
|
||||
(
|
||||
($($vector_info:tt),+),
|
||||
$matrix_info:tt
|
||||
) => {
|
||||
$crate::macro_repeated!(impl_matrix_named_fields_shape,$matrix_info,$($vector_info),+);
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[macro_export(local_inner_macros)]
|
||||
macro_rules! impl_matrix_named_fields {
|
||||
(
|
||||
($($matrix_info:tt),+),
|
||||
$vector_infos:tt
|
||||
) => {
|
||||
$crate::macro_repeated!(impl_matrix_named_fields_shape_shim,$vector_infos,$($matrix_info),+);
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[macro_export(local_inner_macros)]
|
||||
macro_rules! impl_matrix_3x3 {
|
||||
()=>{
|
||||
#[cfg(feature="fixed_wide")]
|
||||
$crate::impl_matrix_wide_3x3!();
|
||||
}
|
||||
}
|
@ -1,20 +1,243 @@
|
||||
#[cfg(feature="fixed_wide")]
|
||||
pub mod fixed_wide;
|
||||
#[cfg(feature="fixed_wide_traits")]
|
||||
pub mod wide;
|
||||
|
||||
// Stolen from https://github.com/c1m50c/fixed-vectors (MIT license)
|
||||
#[doc(hidden)]
|
||||
#[macro_export(local_inner_macros)]
|
||||
macro_rules! impl_vector {
|
||||
( $struct: ident { $($field: ident), + }, ( $($generic: ident), + ), $size: expr ) => {
|
||||
impl<T> $struct<T> {
|
||||
/// Constructs a new vector with the specified values for each field.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use fixed_wide_vectors::Vector2;
|
||||
///
|
||||
/// let vec2 = Vector2::new(0, 0);
|
||||
///
|
||||
/// assert_eq!(vec2.x, 0);
|
||||
/// assert_eq!(vec2.y, 0);
|
||||
/// ```
|
||||
#[inline(always)]
|
||||
pub const fn new( $($field: T), + ) -> Self {
|
||||
Self {
|
||||
$( $field ), +
|
||||
}
|
||||
}
|
||||
|
||||
/// Consumes the vector and returns its values as an array.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use fixed_wide_vectors::Vector2;
|
||||
///
|
||||
/// let vec2 = Vector2::new(0, 0);
|
||||
/// let array = vec2.to_array();
|
||||
///
|
||||
/// assert_eq!(array, [0, 0]);
|
||||
/// ```
|
||||
#[inline(always)]
|
||||
pub fn to_array(self) -> [T; $size] {
|
||||
[ $(self.$field), + ]
|
||||
}
|
||||
|
||||
/// Consumes the vector and returns its values as a tuple.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use fixed_wide_vectors::Vector2;
|
||||
///
|
||||
/// let vec2 = Vector2::new(0, 0);
|
||||
/// let tuple = vec2.to_tuple();
|
||||
///
|
||||
/// assert_eq!(tuple, (0, 0));
|
||||
/// ```
|
||||
#[inline(always)]
|
||||
pub fn to_tuple(self) -> ( $($generic), + ) {
|
||||
( $(self.$field), + )
|
||||
}
|
||||
|
||||
/// Consumes the vector and returns a new vector with the given function applied on each field.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use fixed_wide_vectors::Vector2;
|
||||
///
|
||||
/// let vec2 = Vector2::new(1, 2)
|
||||
/// .map(|i| i * 2);
|
||||
///
|
||||
/// assert_eq!(vec2, Vector2::new(2, 4));
|
||||
/// ```
|
||||
#[inline]
|
||||
pub fn map<F, U>(self, f: F) -> $struct<U>
|
||||
where
|
||||
F: Fn(T) -> U
|
||||
{
|
||||
$struct {
|
||||
$( $field: f(self.$field) ), +
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Copy> $struct<T> {
|
||||
/// Constructs a vector using the given `value` as the value for all of its fields.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use fixed_wide_vectors::Vector2;
|
||||
///
|
||||
/// let vec2 = Vector2::from_value(0);
|
||||
///
|
||||
/// assert_eq!(vec2, Vector2::new(0, 0));
|
||||
/// ```
|
||||
#[inline(always)]
|
||||
pub const fn from_value(value: T) -> Self {
|
||||
Self {
|
||||
$( $field: value ), +
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<[T; $size]> for $struct<T> {
|
||||
fn from(from: [T; $size]) -> Self {
|
||||
let mut iterator = from.into_iter();
|
||||
|
||||
Self {
|
||||
// SAFETY: We know the size of `from` so `iterator.next()` is always `Some(..)`
|
||||
$( $field: unsafe { iterator.next().unwrap_unchecked() } ), +
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<($($generic), +)> for $struct<T> {
|
||||
fn from(from: ($($generic), +)) -> Self {
|
||||
let ( $($field), + ) = from;
|
||||
|
||||
Self {
|
||||
$( $field ), +
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: core::fmt::Debug> core::fmt::Debug for $struct<T> {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
let identifier = core::stringify!($struct);
|
||||
|
||||
f.debug_struct(identifier)
|
||||
$( .field( core::stringify!($field), &self.$field ) ) +
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: PartialEq> PartialEq for $struct<T> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
$( self.$field == other.$field ) && +
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Eq> Eq for $struct<T> { }
|
||||
|
||||
impl<T: core::hash::Hash> core::hash::Hash for $struct<T> {
|
||||
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
|
||||
$( self.$field.hash(state); ) +
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Clone> Clone for $struct<T> {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
$( $field: self.$field.clone() ), +
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Copy> Copy for $struct<T> { }
|
||||
|
||||
impl<T: Default> Default for $struct<T> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
$( $field: T::default() ), +
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: core::ops::Neg<Output = T>> core::ops::Neg for $struct<T> {
|
||||
type Output = Self;
|
||||
|
||||
fn neg(self) -> Self::Output {
|
||||
Self {
|
||||
$( $field: -self.$field ), +
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Impl arithmetic pperators
|
||||
$crate::impl_operator!( $struct { $($field), + }, AddAssign, add_assign );
|
||||
$crate::impl_operator!( $struct { $($field), + }, Add, add, Self );
|
||||
$crate::impl_operator!( $struct { $($field), + }, SubAssign, sub_assign );
|
||||
$crate::impl_operator!( $struct { $($field), + }, Sub, sub, Self );
|
||||
$crate::impl_operator!( $struct { $($field), + }, MulAssign, mul_assign );
|
||||
$crate::impl_operator!( $struct { $($field), + }, Mul, mul, Self );
|
||||
$crate::impl_operator!( $struct { $($field), + }, DivAssign, div_assign );
|
||||
$crate::impl_operator!( $struct { $($field), + }, Div, div, Self );
|
||||
$crate::impl_operator!( $struct { $($field), + }, RemAssign, rem_assign );
|
||||
$crate::impl_operator!( $struct { $($field), + }, Rem, rem, Self );
|
||||
|
||||
// Impl bitwise operators
|
||||
$crate::impl_operator!( $struct { $($field), + }, BitAndAssign, bitand_assign );
|
||||
$crate::impl_operator!( $struct { $($field), + }, BitAnd, bitand, Self );
|
||||
$crate::impl_operator!( $struct { $($field), + }, BitOrAssign, bitor_assign );
|
||||
$crate::impl_operator!( $struct { $($field), + }, BitOr, bitor, Self );
|
||||
$crate::impl_operator!( $struct { $($field), + }, BitXorAssign, bitxor_assign );
|
||||
$crate::impl_operator!( $struct { $($field), + }, BitXor, bitxor, Self );
|
||||
|
||||
// Impl floating-point based methods
|
||||
$crate::impl_wide_operations!( $struct { $($field), + }, $size );
|
||||
};
|
||||
}
|
||||
|
||||
pub mod common;
|
||||
pub mod vector;
|
||||
pub mod matrix;
|
||||
|
||||
#[doc(hidden)]
|
||||
#[macro_export(local_inner_macros)]
|
||||
macro_rules! macro_repeated{
|
||||
(
|
||||
$macro:ident,
|
||||
$any:tt,
|
||||
$($repeated:tt),*
|
||||
)=>{
|
||||
$(
|
||||
$crate::$macro!($any, $repeated);
|
||||
)*
|
||||
};
|
||||
macro_rules! impl_operator {
|
||||
( $struct: ident { $($field: ident), + }, $trait: ident, $method: ident, $output: ty ) => {
|
||||
impl<T: core::ops::$trait<Output = T>> core::ops::$trait<Self> for $struct<T> {
|
||||
type Output = $output;
|
||||
|
||||
fn $method(self, other: Self) -> Self::Output {
|
||||
Self {
|
||||
$( $field: self.$field.$method(other.$field) ), +
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: core::ops::$trait<Output = T> + Copy> core::ops::$trait<T> for $struct<T> {
|
||||
type Output = $output;
|
||||
|
||||
fn $method(self, other: T) -> Self::Output {
|
||||
Self {
|
||||
$( $field: self.$field.$method(other) ), +
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
( $struct: ident { $($field: ident), + }, $trait: ident, $method: ident ) => {
|
||||
impl<T: core::ops::$trait> core::ops::$trait for $struct<T> {
|
||||
fn $method(&mut self, other: Self) {
|
||||
$( self.$field.$method(other.$field) ); +
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: core::ops::$trait + Copy> core::ops::$trait<T> for $struct<T> {
|
||||
fn $method(&mut self, other: T) {
|
||||
$( self.$field.$method(other) ); +
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -1,187 +0,0 @@
|
||||
#[doc(hidden)]
|
||||
#[macro_export(local_inner_macros)]
|
||||
macro_rules! impl_vector {
|
||||
() => {
|
||||
impl<const N:usize,T> Vector<N,T>{
|
||||
#[inline(always)]
|
||||
pub const fn new(array:[T;N])->Self{
|
||||
Self{array}
|
||||
}
|
||||
#[inline(always)]
|
||||
pub fn to_array(self)->[T;N]{
|
||||
self.array
|
||||
}
|
||||
#[inline]
|
||||
pub fn map<F,U>(self,f:F)->Vector<N,U>
|
||||
where
|
||||
F:Fn(T)->U
|
||||
{
|
||||
Vector::new(
|
||||
self.array.map(f)
|
||||
)
|
||||
}
|
||||
#[inline]
|
||||
pub fn map_zip<F,U,V>(self,other:Vector<N,U>,f:F)->Vector<N,V>
|
||||
where
|
||||
F:Fn((T,U))->V,
|
||||
{
|
||||
let mut iter=self.array.into_iter().zip(other.array);
|
||||
Vector::new(
|
||||
core::array::from_fn(|_|f(iter.next().unwrap())),
|
||||
)
|
||||
}
|
||||
}
|
||||
impl<const N:usize,T:Copy> Vector<N,T>{
|
||||
#[inline(always)]
|
||||
pub const fn from_value(value:T)->Self{
|
||||
Self::new([value;N])
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N:usize,T:Default> Default for Vector<N,T>{
|
||||
fn default()->Self{
|
||||
Self::new(
|
||||
core::array::from_fn(|_|Default::default())
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N:usize,T:Ord> Vector<N,T>{
|
||||
#[inline]
|
||||
pub fn min(self,rhs:Self)->Self{
|
||||
self.map_zip(rhs,|(a,b)|a.min(b))
|
||||
}
|
||||
#[inline]
|
||||
pub fn max(self,rhs:Self)->Self{
|
||||
self.map_zip(rhs,|(a,b)|a.max(b))
|
||||
}
|
||||
#[inline]
|
||||
pub fn cmp(self,rhs:Self)->Vector<N,core::cmp::Ordering>{
|
||||
self.map_zip(rhs,|(a,b)|a.cmp(&b))
|
||||
}
|
||||
#[inline]
|
||||
pub fn lt(self,rhs:Self)->Vector<N,bool>{
|
||||
self.map_zip(rhs,|(a,b)|a.lt(&b))
|
||||
}
|
||||
#[inline]
|
||||
pub fn gt(self,rhs:Self)->Vector<N,bool>{
|
||||
self.map_zip(rhs,|(a,b)|a.gt(&b))
|
||||
}
|
||||
#[inline]
|
||||
pub fn ge(self,rhs:Self)->Vector<N,bool>{
|
||||
self.map_zip(rhs,|(a,b)|a.ge(&b))
|
||||
}
|
||||
#[inline]
|
||||
pub fn le(self,rhs:Self)->Vector<N,bool>{
|
||||
self.map_zip(rhs,|(a,b)|a.le(&b))
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N:usize> Vector<N,bool>{
|
||||
#[inline]
|
||||
pub fn all(&self)->bool{
|
||||
self.array==[true;N]
|
||||
}
|
||||
#[inline]
|
||||
pub fn any(&self)->bool{
|
||||
self.array!=[false;N]
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N:usize,T:core::ops::Neg<Output=V>,V> core::ops::Neg for Vector<N,T>{
|
||||
type Output=Vector<N,V>;
|
||||
fn neg(self)->Self::Output{
|
||||
Vector::new(
|
||||
self.array.map(|t|-t)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// Impl arithmetic operators
|
||||
$crate::impl_vector_assign_operator!(AddAssign, add_assign );
|
||||
$crate::impl_vector_operator!(Add, add );
|
||||
$crate::impl_vector_assign_operator!(SubAssign, sub_assign );
|
||||
$crate::impl_vector_operator!(Sub, sub );
|
||||
$crate::impl_vector_assign_operator!(MulAssign, mul_assign );
|
||||
$crate::impl_vector_operator!(Mul, mul );
|
||||
$crate::impl_vector_assign_operator!(DivAssign, div_assign );
|
||||
$crate::impl_vector_operator!(Div, div );
|
||||
$crate::impl_vector_assign_operator!(RemAssign, rem_assign );
|
||||
$crate::impl_vector_operator!(Rem, rem );
|
||||
|
||||
// Impl bitwise operators
|
||||
$crate::impl_vector_assign_operator!(BitAndAssign, bitand_assign );
|
||||
$crate::impl_vector_operator!(BitAnd, bitand );
|
||||
$crate::impl_vector_assign_operator!(BitOrAssign, bitor_assign );
|
||||
$crate::impl_vector_operator!(BitOr, bitor );
|
||||
$crate::impl_vector_assign_operator!(BitXorAssign, bitxor_assign );
|
||||
$crate::impl_vector_operator!(BitXor, bitxor );
|
||||
|
||||
// Impl floating-point based methods
|
||||
#[cfg(feature="fixed_wide")]
|
||||
$crate::impl_wide_vector_operations!();
|
||||
}
|
||||
}
|
||||
#[doc(hidden)]
|
||||
#[macro_export(local_inner_macros)]
|
||||
macro_rules! impl_vector_operator {
|
||||
($trait: ident, $method: ident ) => {
|
||||
impl<const N:usize,T:core::ops::$trait<U,Output=V>,U,V> core::ops::$trait<Vector<N,U>> for Vector<N,T>{
|
||||
type Output=Vector<N,V>;
|
||||
fn $method(self,rhs:Vector<N,U>)->Self::Output{
|
||||
self.map_zip(rhs,|(a,b)|a.$method(b))
|
||||
}
|
||||
}
|
||||
impl<const N:usize,T:core::ops::$trait<i64,Output=T>> core::ops::$trait<i64> for Vector<N,T>{
|
||||
type Output=Self;
|
||||
fn $method(self,rhs:i64)->Self::Output{
|
||||
self.map(|t|t.$method(rhs))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#[doc(hidden)]
|
||||
#[macro_export(local_inner_macros)]
|
||||
macro_rules! impl_vector_assign_operator {
|
||||
($trait: ident, $method: ident ) => {
|
||||
impl<const N:usize,T:core::ops::$trait<U>,U> core::ops::$trait<Vector<N,U>> for Vector<N,T>{
|
||||
fn $method(&mut self,rhs:Vector<N,U>){
|
||||
self.array.iter_mut().zip(rhs.array)
|
||||
.for_each(|(a,b)|a.$method(b))
|
||||
}
|
||||
}
|
||||
impl<const N:usize,T:core::ops::$trait<i64>> core::ops::$trait<i64> for Vector<N,T>{
|
||||
fn $method(&mut self,rhs:i64){
|
||||
self.array.iter_mut()
|
||||
.for_each(|t|t.$method(rhs))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[macro_export(local_inner_macros)]
|
||||
macro_rules! impl_vector_named_fields {
|
||||
( $struct:ident, $size: expr ) => {
|
||||
impl<T> core::ops::Deref for Vector<$size,T>{
|
||||
type Target=$struct<T>;
|
||||
fn deref(&self)->&Self::Target{
|
||||
unsafe{core::mem::transmute(&self.array)}
|
||||
}
|
||||
}
|
||||
impl<T> core::ops::DerefMut for Vector<$size,T>{
|
||||
fn deref_mut(&mut self)->&mut Self::Target{
|
||||
unsafe{core::mem::transmute(&mut self.array)}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[macro_export(local_inner_macros)]
|
||||
macro_rules! impl_vector_3 {
|
||||
()=>{
|
||||
#[cfg(feature="fixed_wide")]
|
||||
$crate::impl_vector_wide_3!();
|
||||
}
|
||||
}
|
43
fixed_wide_vectors/src/macros/wide.rs
Normal file
43
fixed_wide_vectors/src/macros/wide.rs
Normal file
@ -0,0 +1,43 @@
|
||||
#[doc(hidden)]
|
||||
#[macro_export(local_inner_macros)]
|
||||
macro_rules! impl_wide_operations {
|
||||
( $struct: ident { $($field: ident), + }, $size: expr ) => {
|
||||
impl<U,T:Copy+fixed_wide_traits::wide::WideMul<Output=U>> fixed_wide_traits::wide::WideMul for $struct<T> {
|
||||
type Output=$struct<U>;
|
||||
#[inline]
|
||||
fn wide_mul(self, rhs: Self) -> Self::Output {
|
||||
$struct{
|
||||
$( $field: self.$field.wide_mul(rhs.$field) ), +
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<U:std::ops::Add<Output=U>,T:Copy+fixed_wide_traits::wide::WideMul<Output=U>> $struct<T> {
|
||||
#[inline]
|
||||
pub fn wide_dot(self, other: Self) -> U {
|
||||
$crate::sum_repeating!(
|
||||
$( + (self.$field.wide_mul(other.$field)) ) +
|
||||
)
|
||||
}
|
||||
pub fn wide_length_squared(&self) -> U {
|
||||
let squared = $struct {
|
||||
$( $field: self.$field.wide_mul(self.$field) ), +
|
||||
};
|
||||
|
||||
$crate::sum_repeating!(
|
||||
$( + squared.$field ) +
|
||||
)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
// HACK: Allows us to sum repeating tokens in macros.
|
||||
// See: https://stackoverflow.com/a/60187870/17452730
|
||||
#[doc(hidden)]
|
||||
#[macro_export(local_inner_macros)]
|
||||
macro_rules! sum_repeating {
|
||||
( + $($item: tt) * ) => {
|
||||
$($item) *
|
||||
};
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
#[derive(Clone,Copy,Hash,Eq,PartialEq)]
|
||||
pub struct Matrix<const X:usize,const Y:usize,T>{
|
||||
pub(crate) array:[[T;X];Y],
|
||||
}
|
||||
|
||||
crate::impl_matrix!();
|
||||
|
||||
//Special case 3x3 matrix operations because I cba to write macros for the arbitrary cases
|
||||
crate::impl_matrix_3x3!();
|
@ -1,59 +0,0 @@
|
||||
use crate::vector::Vector;
|
||||
use crate::matrix::Matrix;
|
||||
|
||||
#[repr(C)]
|
||||
pub struct Vector2<T> {
|
||||
pub x: T,
|
||||
pub y: T,
|
||||
}
|
||||
#[repr(C)]
|
||||
pub struct Vector3<T> {
|
||||
pub x: T,
|
||||
pub y: T,
|
||||
pub z: T,
|
||||
}
|
||||
#[repr(C)]
|
||||
pub struct Vector4<T> {
|
||||
pub x: T,
|
||||
pub y: T,
|
||||
pub z: T,
|
||||
pub w: T,
|
||||
}
|
||||
|
||||
crate::impl_vector_named_fields!(Vector2, 2);
|
||||
crate::impl_vector_named_fields!(Vector3, 3);
|
||||
crate::impl_vector_named_fields!(Vector4, 4);
|
||||
|
||||
#[repr(C)]
|
||||
pub struct Matrix2<T> {
|
||||
pub x_axis: T,
|
||||
pub y_axis: T,
|
||||
}
|
||||
#[repr(C)]
|
||||
pub struct Matrix3<T> {
|
||||
pub x_axis: T,
|
||||
pub y_axis: T,
|
||||
pub z_axis: T,
|
||||
}
|
||||
#[repr(C)]
|
||||
pub struct Matrix4<T> {
|
||||
pub x_axis: T,
|
||||
pub y_axis: T,
|
||||
pub z_axis: T,
|
||||
pub w_axis: T,
|
||||
}
|
||||
|
||||
crate::impl_matrix_named_fields!(
|
||||
//outer struct
|
||||
(
|
||||
(Matrix2, 2),
|
||||
(Matrix3, 3),
|
||||
(Matrix4, 4)
|
||||
),
|
||||
//inner struct
|
||||
(
|
||||
(2),
|
||||
(3),
|
||||
(4)
|
||||
)
|
||||
);
|
@ -1,94 +0,0 @@
|
||||
use crate::types::{Matrix3,Matrix2x3,Matrix4x3,Matrix2x4,Vector3};
|
||||
|
||||
type Planar64=fixed_wide::types::I32F32;
|
||||
type Planar64Wide1=fixed_wide::types::I64F64;
|
||||
//type Planar64Wide2=fixed_wide::types::I128F128;
|
||||
type Planar64Wide3=fixed_wide::types::I256F256;
|
||||
|
||||
#[test]
|
||||
fn wide_vec3(){
|
||||
let v=Vector3::from_value(Planar64::from(3));
|
||||
let v1=v.wide_mul_1_1(v);
|
||||
let v2=v1.wide_mul_2_2(v1);
|
||||
let v3=v2.wide_mul_4_4(v2);
|
||||
|
||||
assert_eq!(v3.array,Vector3::from_value(Planar64Wide3::from(3i128.pow(8))).array);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wide_vec3_dot(){
|
||||
let v=Vector3::from_value(Planar64::from(3));
|
||||
let v1=v.wide_mul_1_1(v);
|
||||
let v2=v1.wide_mul_2_2(v1);
|
||||
let v3=v2.wide_dot_4_4(v2);
|
||||
|
||||
assert_eq!(v3,Planar64Wide3::from(3i128.pow(8)*3));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wide_vec3_length_squared(){
|
||||
let v=Vector3::from_value(Planar64::from(3));
|
||||
let v1=v.wide_mul_1_1(v);
|
||||
let v2=v1.wide_mul_2_2(v1);
|
||||
let v3=v2.wide_length_squared();
|
||||
|
||||
assert_eq!(v3,Planar64Wide3::from(3i128.pow(8)*3));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wide_matrix_dot(){
|
||||
let lhs=Matrix4x3::new([
|
||||
[Planar64::from(1),Planar64::from(2),Planar64::from(3),Planar64::from(4)],
|
||||
[Planar64::from(5),Planar64::from(6),Planar64::from(7),Planar64::from(8)],
|
||||
[Planar64::from(9),Planar64::from(10),Planar64::from(11),Planar64::from(12)],
|
||||
]);
|
||||
let rhs=Matrix2x4::new([
|
||||
[Planar64::from(1),Planar64::from(2)],
|
||||
[Planar64::from(3),Planar64::from(4)],
|
||||
[Planar64::from(5),Planar64::from(6)],
|
||||
[Planar64::from(7),Planar64::from(8)],
|
||||
]);
|
||||
// Mat3<Vec4>.dot(Mat4<Vec2>) -> Mat3<Vec2>
|
||||
let m_dot=lhs.wide_dot_1_1(rhs);
|
||||
//In[1]:= {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}} . {{1, 2}, {3, 4}, {5, 6}, {7, 8}}
|
||||
//Out[1]= {{50, 60}, {114, 140}, {178, 220}}
|
||||
assert_eq!(
|
||||
m_dot.array,
|
||||
Matrix2x3::new([
|
||||
[Planar64Wide1::from(50),Planar64Wide1::from(60)],
|
||||
[Planar64Wide1::from(114),Planar64Wide1::from(140)],
|
||||
[Planar64Wide1::from(178),Planar64Wide1::from(220)],
|
||||
]).array
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wide_matrix_det(){
|
||||
let m=Matrix3::new([
|
||||
[Planar64::from(1),Planar64::from(2),Planar64::from(3)],
|
||||
[Planar64::from(4),Planar64::from(5),Planar64::from(7)],
|
||||
[Planar64::from(6),Planar64::from(8),Planar64::from(9)],
|
||||
]);
|
||||
// In[2]:= Det[{{1, 2, 3}, {4, 5, 7}, {6, 8, 9}}]
|
||||
// Out[2]= 7
|
||||
assert_eq!(m.wide_det_3x3_1(),fixed_wide::fixed::Fixed::<3,96>::from(7));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wide_matrix_adjugate(){
|
||||
let m=Matrix3::new([
|
||||
[Planar64::from(1),Planar64::from(2),Planar64::from(3)],
|
||||
[Planar64::from(4),Planar64::from(5),Planar64::from(7)],
|
||||
[Planar64::from(6),Planar64::from(8),Planar64::from(9)],
|
||||
]);
|
||||
// In[6]:= Adjugate[{{1, 2, 3}, {4, 5, 7}, {6, 8, 9}}]
|
||||
// Out[6]= {{-11, 6, -1}, {6, -9, 5}, {2, 4, -3}}
|
||||
assert_eq!(
|
||||
m.wide_adjugate_3x3_1().array,
|
||||
Matrix3::new([
|
||||
[Planar64Wide1::from(-11),Planar64Wide1::from(6),Planar64Wide1::from(-1)],
|
||||
[Planar64Wide1::from(6),Planar64Wide1::from(-9),Planar64Wide1::from(5)],
|
||||
[Planar64Wide1::from(2),Planar64Wide1::from(4),Planar64Wide1::from(-3)],
|
||||
]).array
|
||||
);
|
||||
}
|
@ -1,7 +1,56 @@
|
||||
mod tests;
|
||||
use fixed_wide_traits::wide::WideMul;
|
||||
|
||||
#[cfg(feature="named-fields")]
|
||||
mod named;
|
||||
use crate::Vector3;
|
||||
|
||||
#[cfg(feature="fixed_wide")]
|
||||
mod fixed_wide;
|
||||
type Planar64=fixed_wide::types::I32F32;
|
||||
//type Planar64Wide1=fixed::types::I64F64;
|
||||
//type Planar64Wide2=fixed_wide::types::I128F128;
|
||||
type Planar64Wide3=fixed_wide::types::I256F256;
|
||||
|
||||
#[test]
|
||||
fn wide_int64() {
|
||||
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((3i128*2).pow(4)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn you_can_add_numbers(){
|
||||
let a=Planar64Wide3::from((3i128*2).pow(4));
|
||||
assert_eq!(a+a,Planar64Wide3::from((3i128*2).pow(4)*2))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wide_vec3(){
|
||||
let v=Vector3::from_value(Planar64::from(3));
|
||||
let v1=v.wide_mul(v);
|
||||
let v2=v1.wide_mul(v1);
|
||||
let v3=v2.wide_mul(v2);
|
||||
|
||||
assert_eq!(v3,Vector3::from_value(Planar64Wide3::from(3i128.pow(8))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wide_vec3_dot(){
|
||||
let v=Vector3::from_value(Planar64::from(3));
|
||||
let v1=v.wide_mul(v);
|
||||
let v2=v1.wide_mul(v1);
|
||||
let v3=v2.wide_dot(v2);
|
||||
|
||||
assert_eq!(v3,Planar64Wide3::from(3i128.pow(8)*3));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wide_vec3_length_squared(){
|
||||
let v=Vector3::from_value(Planar64::from(3));
|
||||
let v1=v.wide_mul(v);
|
||||
let v2=v1.wide_mul(v1);
|
||||
let v3=v2.wide_length_squared();
|
||||
|
||||
assert_eq!(v3,Planar64Wide3::from(3i128.pow(8)*3));
|
||||
}
|
||||
|
@ -1,30 +0,0 @@
|
||||
use crate::types::{Vector3,Matrix3};
|
||||
|
||||
#[test]
|
||||
fn test_vector(){
|
||||
let mut v=Vector3::new([1,2,3]);
|
||||
assert_eq!(v.x,1);
|
||||
assert_eq!(v.y,2);
|
||||
assert_eq!(v.z,3);
|
||||
|
||||
v.x=5;
|
||||
assert_eq!(v.x,5);
|
||||
|
||||
v.y*=v.x;
|
||||
assert_eq!(v.y,10);
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn test_matrix(){
|
||||
let mut v=Matrix3::from_value(2);
|
||||
assert_eq!(v.x_axis.x,2);
|
||||
assert_eq!(v.y_axis.y,2);
|
||||
assert_eq!(v.z_axis.z,2);
|
||||
|
||||
v.x_axis.x=5;
|
||||
assert_eq!(v.x_axis.x,5);
|
||||
|
||||
v.y_axis.z*=v.x_axis.x;
|
||||
assert_eq!(v.y_axis.z,10);
|
||||
}
|
@ -1,43 +0,0 @@
|
||||
use crate::types::{Vector3,Matrix4x3,Matrix2x4,Matrix2x3};
|
||||
|
||||
#[test]
|
||||
fn test_bool(){
|
||||
assert_eq!(Vector3::new([false,false,false]).any(),false);
|
||||
assert_eq!(Vector3::new([false,false,true]).any(),true);
|
||||
assert_eq!(Vector3::new([false,false,true]).all(),false);
|
||||
assert_eq!(Vector3::new([true,true,true]).all(),true);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_arithmetic(){
|
||||
let a=Vector3::new([1,2,3]);
|
||||
assert_eq!((a+a*2).array,Vector3::new([1*3,2*3,3*3]).array);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn matrix_dot(){
|
||||
|
||||
let rhs=Matrix2x4::new([
|
||||
[ 1.0, 2.0],
|
||||
[ 3.0, 4.0],
|
||||
[ 5.0, 6.0],
|
||||
[ 7.0, 8.0],
|
||||
]); // | | |
|
||||
let lhs=Matrix4x3::new([ // | | |
|
||||
[1.0, 2.0, 3.0, 4.0],// [ 50.0, 60.0],
|
||||
[5.0, 6.0, 7.0, 8.0],// [114.0,140.0],
|
||||
[9.0,10.0,11.0,12.0],// [178.0,220.0],
|
||||
]);
|
||||
// Mat3<Vec4>.dot(Mat4<Vec2>) -> Mat3<Vec2>
|
||||
let m_dot=lhs.dot(rhs);
|
||||
//In[1]:= {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}} . {{1, 2}, {3, 4}, {5, 6}, {7, 8}}
|
||||
//Out[1]= {{50, 60}, {114, 140}, {178, 220}}
|
||||
assert_eq!(
|
||||
m_dot.array,
|
||||
Matrix2x3::new([
|
||||
[50.0,60.0],
|
||||
[114.0,140.0],
|
||||
[178.0,220.0],
|
||||
]).array
|
||||
);
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
use crate::vector::Vector;
|
||||
use crate::matrix::Matrix;
|
||||
|
||||
pub type Vector2<T>=Vector<2,T>;
|
||||
pub type Vector3<T>=Vector<3,T>;
|
||||
pub type Vector4<T>=Vector<4,T>;
|
||||
|
||||
pub type Matrix2<T>=Matrix<2,2,T>;
|
||||
pub type Matrix2x3<T>=Matrix<2,3,T>;
|
||||
pub type Matrix2x4<T>=Matrix<2,4,T>;
|
||||
|
||||
pub type Matrix3x2<T>=Matrix<3,2,T>;
|
||||
pub type Matrix3<T>=Matrix<3,3,T>;
|
||||
pub type Matrix3x4<T>=Matrix<3,4,T>;
|
||||
|
||||
pub type Matrix4x2<T>=Matrix<4,2,T>;
|
||||
pub type Matrix4x3<T>=Matrix<4,3,T>;
|
||||
pub type Matrix4<T>=Matrix<4,4,T>;
|
@ -1,9 +1,68 @@
|
||||
#[derive(Clone,Copy,Hash,Eq,PartialEq)]
|
||||
pub struct Vector<const N:usize,T>{
|
||||
pub(crate) array:[T;N],
|
||||
// Stolen from https://github.com/c1m50c/fixed-vectors (MIT license)
|
||||
|
||||
/// Vector for holding two-dimensional values.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use fixed_wide_vectors::Vector2;
|
||||
///
|
||||
/// let mut vec2 = Vector2::new(1, 2);
|
||||
/// vec2 += Vector2::new(1, 2);
|
||||
///
|
||||
/// assert_eq!(vec2.x, 2);
|
||||
/// assert_eq!(vec2.y, 4);
|
||||
/// ```
|
||||
pub struct Vector2<T> {
|
||||
pub x: T,
|
||||
pub y: T,
|
||||
}
|
||||
|
||||
crate::impl_vector!();
|
||||
|
||||
//cross product
|
||||
crate::impl_vector_3!();
|
||||
/// Vector for holding three-dimensional values.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use fixed_wide_vectors::Vector3;
|
||||
///
|
||||
/// let mut vec3 = Vector3::new(1, 2, 3);
|
||||
/// vec3 += Vector3::new(1, 2, 3);
|
||||
///
|
||||
/// assert_eq!(vec3.x, 2);
|
||||
/// assert_eq!(vec3.y, 4);
|
||||
/// assert_eq!(vec3.z, 6);
|
||||
/// ```
|
||||
pub struct Vector3<T> {
|
||||
pub x: T,
|
||||
pub y: T,
|
||||
pub z: T,
|
||||
}
|
||||
|
||||
|
||||
/// Vector for holding four-dimensional values.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use fixed_wide_vectors::Vector4;
|
||||
///
|
||||
/// let mut vec4 = Vector4::new(1, 2, 3, 4);
|
||||
/// vec4 += Vector4::new(1, 2, 3, 4);
|
||||
///
|
||||
/// assert_eq!(vec4.x, 2);
|
||||
/// assert_eq!(vec4.y, 4);
|
||||
/// assert_eq!(vec4.z, 6);
|
||||
/// assert_eq!(vec4.w, 8);
|
||||
/// ```
|
||||
pub struct Vector4<T> {
|
||||
pub x: T,
|
||||
pub y: T,
|
||||
pub z: T,
|
||||
pub w: T,
|
||||
}
|
||||
|
||||
|
||||
crate::impl_vector!(Vector2 { x, y }, (T, T), 2);
|
||||
crate::impl_vector!(Vector3 { x, y, z }, (T, T, T), 3);
|
||||
crate::impl_vector!(Vector4 { x, y, z, w }, (T, T, T, T), 4);
|
||||
|
Reference in New Issue
Block a user