10 Commits

Author SHA1 Message Date
42c174df0c idk 2024-08-27 18:57:11 -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
13 changed files with 227 additions and 2 deletions

1
deferred_division/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

14
deferred_division/Cargo.lock generated Normal file
View 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"

View 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 }

View File

@@ -0,0 +1,8 @@
pub mod ratio;
#[cfg(feature="fixed_wide_traits")]
mod wide;
#[cfg(test)]
mod tests;

View 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;
}

View File

@@ -0,0 +1,51 @@
use std::ops::{Add,Mul};
use crate::ratio::Ratio;
use fixed_wide_traits::wide::{WideMul,WideDiv};
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),
}
}
}

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,31 @@ 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 );

View File

@@ -1,6 +1,10 @@
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")]

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

@@ -0,0 +1,15 @@
#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! impl_length_operations {
( $struct: ident { $($field: ident), + }, $size: expr ) => {
impl<U,T:Copy+fixed_wide_traits::wide::WideMul<Output=U>> $struct<T> {
type Output=$struct<U>;
#[inline]
fn wide_with_length(self, rhs: Self) -> Self::Output {
let len=self.wide_length();
let ratio=rhs.ratio(len);
self.wide_mul(ratio)
}
}
}
}

View File

@@ -1,5 +1,7 @@
#[cfg(feature="fixed_wide_traits")]
pub mod wide;
#[cfg(all(feature="fixed_wide_traits",feature="deferred_division"))]
pub mod length;
// Stolen from https://github.com/c1m50c/fixed-vectors (MIT license)
#[doc(hidden)]
@@ -166,6 +168,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));