11 Commits

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
c43fab2f18 reexport typenum for convenience 2024-08-27 16:50:48 -07:00
20a317612e fixed: constants 2024-08-27 16:50:48 -07:00
60753490c6 vectors: implement Ord stuff as vectors of boolean 2024-08-27 16:50:48 -07:00
79ab26f118 fixed: implement shift operators 2024-08-27 16:50:48 -07:00
7a5406e769 fixed: fixup operator impls - this is implicitly Self 2024-08-27 14:59:52 -07:00
009aa0c296 ratio crate 2024-08-27 14:25:39 -07:00
2d87e4b5cc vectors: min, max 2024-08-27 14:24:21 -07:00
4ce5c045a8 fixed: PartialOrd, Ord 2024-08-27 14:24:21 -07:00
047451d247 affine 2024-08-27 14:24:21 -07:00
10 changed files with 225 additions and 14 deletions

View File

@@ -1,19 +1,7 @@
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>

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

@@ -8,6 +8,12 @@ pub struct Fixed<const CHUNKS:usize,Frac>{
pub(crate)frac:PhantomData<Frac>,
}
impl<const CHUNKS:usize,Frac:Unsigned> Fixed<CHUNKS,Frac>{
pub const ZERO:Self=Self{bits:BInt::<CHUNKS>::ZERO,frac:PhantomData};
pub const ONE:Self=Self{bits:BInt::<CHUNKS>::ONE.shl(Frac::U32),frac:PhantomData};
pub const NEG_ONE:Self=Self{bits:BInt::<CHUNKS>::NEG_ONE.shl(Frac::U32),frac:PhantomData};
}
impl<const CHUNKS:usize,FracDst:Unsigned,T> From<T> for Fixed<CHUNKS,FracDst>
where
BInt<CHUNKS>:From<T>
@@ -27,6 +33,17 @@ impl<const CHUNKS:usize,Frac> PartialEq for Fixed<CHUNKS,Frac>{
}
impl<const CHUNKS:usize,Frac> Eq for Fixed<CHUNKS,Frac>{}
impl<const CHUNKS:usize,Frac> PartialOrd for Fixed<CHUNKS,Frac>{
fn partial_cmp(&self,other:&Self)->Option<std::cmp::Ordering>{
self.bits.partial_cmp(&other.bits)
}
}
impl<const CHUNKS:usize,Frac> Ord for Fixed<CHUNKS,Frac>{
fn cmp(&self,other:&Self)->std::cmp::Ordering{
self.bits.cmp(&other.bits)
}
}
impl<const CHUNKS:usize,Frac> std::ops::Neg for Fixed<CHUNKS,Frac>{
type Output=Self;
fn neg(self)->Self{
@@ -39,7 +56,7 @@ impl<const CHUNKS:usize,Frac> std::ops::Neg for Fixed<CHUNKS,Frac>{
macro_rules! impl_operator {
( $struct: ident, $trait: ident, $method: ident, $output: ty ) => {
impl<const CHUNKS:usize,Frac> core::ops::$trait<$struct<CHUNKS,Frac>> for $struct<CHUNKS,Frac>{
impl<const CHUNKS:usize,Frac> core::ops::$trait for $struct<CHUNKS,Frac>{
type Output = $output;
fn $method(self, other: Self) -> Self::Output {
@@ -53,7 +70,7 @@ macro_rules! impl_operator {
}
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>{
impl<const CHUNKS:usize,Frac> core::ops::$trait for $struct<CHUNKS,Frac>{
fn $method(&mut self, other: Self) {
self.bits.$method(other.bits);
}
@@ -80,3 +97,47 @@ 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 );
macro_rules! impl_shift_operator {
( $struct: ident, $trait: ident, $method: ident, $output: ty ) => {
impl<const CHUNKS:usize,Frac> core::ops::$trait<u32> for $struct<CHUNKS,Frac>{
type Output = $output;
fn $method(self, other: u32) -> Self::Output {
Self {
bits:self.bits.$method(other),
frac:PhantomData,
}
}
}
};
}
macro_rules! impl_shift_assign_operator {
( $struct: ident, $trait: ident, $method: ident ) => {
impl<const CHUNKS:usize,Frac> core::ops::$trait<u32> for $struct<CHUNKS,Frac>{
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 );
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

@@ -1,7 +1,14 @@
pub mod fixed;
pub mod types;
pub mod typenum{
pub use typenum::Unsigned;
}
#[cfg(feature="fixed_wide_traits")]
mod fixed_wide_traits;
#[cfg(feature="fixed_wide_traits")]
pub use ::fixed_wide_traits::wide;
#[cfg(feature="cordic")]
mod cordic;

View File

@@ -0,0 +1,17 @@
use std::ops::Add;
use fixed_wide_traits::wide::WideDot;
pub struct Affine<M,T>{
pub matrix:M,
pub offset:T,
}
impl<M:Copy,T:Copy> Affine<M,T>{
pub fn wide_transform<X>(&self,input:X)-><<M as WideDot<X>>::Output as Add<T>>::Output
where
M:WideDot<X>,
<M as WideDot<X>>::Output:Add<T>,
{
self.matrix.wide_dot(input)+self.offset
}
}

View File

@@ -1,6 +1,9 @@
mod macros;
mod vector;
#[cfg(feature="fixed_wide_traits")]
pub mod affine;
pub use vector::Vector2;
pub use vector::Vector3;
pub use vector::Vector4;

View File

@@ -166,6 +166,44 @@ macro_rules! impl_vector {
}
}
impl<T: Ord> $struct<T> {
pub fn min(self, rhs: Self) -> $struct<T> {
$struct{
$( $field: self.$field.min(rhs.$field) ), +
}
}
pub fn max(self, rhs: Self) -> $struct<T> {
$struct{
$( $field: self.$field.max(rhs.$field) ), +
}
}
pub fn cmp(self, rhs: Self) -> $struct<core::cmp::Ordering> {
$struct{
$( $field: self.$field.cmp(&rhs.$field) ), +
}
}
pub fn lt(self, rhs: Self) -> $struct<bool> {
$struct{
$( $field: self.$field.lt(&rhs.$field) ), +
}
}
pub fn gt(self, rhs: Self) -> $struct<bool> {
$struct{
$( $field: self.$field.gt(&rhs.$field) ), +
}
}
pub fn ge(self, rhs: Self) -> $struct<bool> {
$struct{
$( $field: self.$field.ge(&rhs.$field) ), +
}
}
pub fn le(self, rhs: Self) -> $struct<bool> {
$struct{
$( $field: self.$field.le(&rhs.$field) ), +
}
}
}
impl<T: core::ops::Neg<Output = T>> core::ops::Neg for $struct<T> {
type Output = Self;

View File

@@ -25,6 +25,12 @@ fn you_can_add_numbers(){
assert_eq!(a+a,Planar64Wide3::from((3i128*2).pow(4)*2))
}
#[test]
fn you_can_shr_numbers(){
let a=Planar64::from(4);
assert_eq!(a>>1,Planar64::from(2))
}
#[test]
fn wide_vec3(){
let v=Vector3::from_value(Planar64::from(3));