Compare commits
18 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 33b335f987 | |||
| c9b999b8a6 | |||
| 23a6ffb243 | |||
| 9ef9b5ff3f | |||
| c277c31f29 | |||
| 617952c1e3 | |||
| 9f9e8c793b | |||
| 1f6594468d | |||
| cc3cb35309 | |||
| a923a6b5d1 | |||
| 68d1c23cfa | |||
| 8aa7da6be7 | |||
| 0be0dd5c6f | |||
| f4ab9403a4 | |||
| 67ac4cf7ff | |||
| 002d3d9eac | |||
| e1368962c1 | |||
| 4ae391e9fd |
@@ -3,8 +3,8 @@ 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;
|
||||
impl<Num,Den> Ratio<Num,Den>{
|
||||
pub const fn new(num:Num,den:Den)->Self{
|
||||
Self{num,den}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,15 +12,17 @@ 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};
|
||||
pub const MAX:Self=Self{bits:BInt::<CHUNKS>::MAX,frac:PhantomData};
|
||||
pub const MIN:Self=Self{bits:BInt::<CHUNKS>::MIN,frac:PhantomData};
|
||||
}
|
||||
|
||||
impl<const CHUNKS:usize,FracDst:Unsigned,T> From<T> for Fixed<CHUNKS,FracDst>
|
||||
impl<const CHUNKS:usize,Frac:Unsigned,T> From<T> for Fixed<CHUNKS,Frac>
|
||||
where
|
||||
BInt<CHUNKS>:From<T>
|
||||
{
|
||||
fn from(value:T)->Self{
|
||||
Self{
|
||||
bits:BInt::<{CHUNKS}>::from(value)<<FracDst::U32,
|
||||
bits:BInt::<{CHUNKS}>::from(value)<<Frac::U32,
|
||||
frac:PhantomData,
|
||||
}
|
||||
}
|
||||
@@ -66,6 +68,19 @@ macro_rules! impl_operator {
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<const CHUNKS:usize,Frac:Unsigned,U> core::ops::$trait<U> for $struct<CHUNKS,Frac>
|
||||
where
|
||||
BInt::<CHUNKS>:From<U>,
|
||||
{
|
||||
type Output = $output;
|
||||
|
||||
fn $method(self, other: U) -> Self::Output {
|
||||
Self {
|
||||
bits:self.bits.$method(BInt::<CHUNKS>::from(other)<<Frac::U32),
|
||||
frac:PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
macro_rules! impl_assign_operator {
|
||||
@@ -75,6 +90,14 @@ macro_rules! impl_assign_operator {
|
||||
self.bits.$method(other.bits);
|
||||
}
|
||||
}
|
||||
impl<const CHUNKS:usize,Frac:Unsigned,U> core::ops::$trait<U> for $struct<CHUNKS,Frac>
|
||||
where
|
||||
BInt::<CHUNKS>:From<U>,
|
||||
{
|
||||
fn $method(&mut self, other: U) {
|
||||
self.bits.$method(BInt::<CHUNKS>::from(other)<<Frac::U32);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -9,3 +9,6 @@ pub mod typenum{
|
||||
mod fixed_wide_traits;
|
||||
#[cfg(feature="fixed_wide_traits")]
|
||||
pub use ::fixed_wide_traits::wide;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
14
fixed_wide/src/tests.rs
Normal file
14
fixed_wide/src/tests.rs
Normal file
@@ -0,0 +1,14 @@
|
||||
use fixed_wide_traits::wide::WideMul;
|
||||
|
||||
#[test]
|
||||
fn test_wide_mul(){
|
||||
let a=crate::types::I32F32::ONE;
|
||||
let aa=a.wide_mul(a);
|
||||
assert_eq!(aa,crate::types::I64F64::ONE);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bint(){
|
||||
let a=crate::types::I32F32::ONE;
|
||||
assert_eq!(a*2,crate::types::I32F32::from(2));
|
||||
}
|
||||
@@ -1,9 +1,12 @@
|
||||
use std::ops::Add;
|
||||
use fixed_wide_traits::wide::WideDot;
|
||||
|
||||
//TODO: replace this with 4x3 matrix
|
||||
// mat4x3.wide_dot(vec3.extend(1))
|
||||
|
||||
pub struct Affine<M,T>{
|
||||
pub matrix:M,
|
||||
pub offset:T,
|
||||
pub matrix:M,
|
||||
pub offset:T,
|
||||
}
|
||||
|
||||
impl<M:Copy,T:Copy> Affine<M,T>{
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
mod macros;
|
||||
mod traits;
|
||||
mod vector;
|
||||
|
||||
#[cfg(feature="fixed_wide_traits")]
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
#[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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,283 +1,323 @@
|
||||
#[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)]
|
||||
#[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 ), +
|
||||
}
|
||||
}
|
||||
( $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 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 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) ), +
|
||||
}
|
||||
}
|
||||
}
|
||||
/// 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: 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();
|
||||
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() } ), +
|
||||
}
|
||||
}
|
||||
}
|
||||
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;
|
||||
impl<T> From<($($generic), +)> for $struct<T> {
|
||||
fn from(from: ($($generic), +)) -> Self {
|
||||
let ( $($field), + ) = from;
|
||||
|
||||
Self {
|
||||
$( $field ), +
|
||||
}
|
||||
}
|
||||
}
|
||||
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);
|
||||
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()
|
||||
}
|
||||
}
|
||||
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: PartialEq> PartialEq for $struct<T> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
$( self.$field == other.$field ) && +
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Eq> Eq for $struct<T> { }
|
||||
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: 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: Clone> Clone for $struct<T> {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
$( $field: self.$field.clone() ), +
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Copy> Copy for $struct<T> { }
|
||||
impl<T: Copy> Copy for $struct<T> { }
|
||||
|
||||
impl<T: Default> Default for $struct<T> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
$( $field: T::default() ), +
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<T: Default> Default for $struct<T> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
$( $field: T::default() ), +
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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: 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;
|
||||
impl $struct<bool>{
|
||||
pub fn all(&self)->bool{
|
||||
const ALL:[bool;$size]=[true;$size];
|
||||
core::matches!(self.to_array(),ALL)
|
||||
}
|
||||
pub fn any(&self)->bool{
|
||||
$( self.$field )|| +
|
||||
}
|
||||
}
|
||||
|
||||
fn neg(self) -> Self::Output {
|
||||
Self {
|
||||
$( $field: -self.$field ), +
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<T: core::ops::Neg<Output = T>> core::ops::Neg for $struct<T> {
|
||||
type Output = Self;
|
||||
|
||||
// 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 );
|
||||
fn neg(self) -> Self::Output {
|
||||
Self {
|
||||
$( $field: -self.$field ), +
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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 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 floating-point based methods
|
||||
$crate::impl_wide_operations!( $struct { $($field), + }, $size );
|
||||
};
|
||||
// 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
|
||||
#[cfg(feature="fixed_wide_traits")]
|
||||
$crate::impl_wide_operations!( $struct { $($field), + }, $size );
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
#[doc(hidden)]
|
||||
#[macro_export(local_inner_macros)]
|
||||
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) ); +
|
||||
}
|
||||
}
|
||||
};
|
||||
macro_rules! impl_matrix_inner {
|
||||
( $struct_outer: ident { $($field_outer: ident), + }, $size_outer: expr,
|
||||
$field_inner: ident) => {
|
||||
$struct_outer {
|
||||
$(
|
||||
$field_outer: self.$field_outer.$field_inner
|
||||
),+
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[macro_export(local_inner_macros)]
|
||||
macro_rules! impl_matrix {
|
||||
( $struct_outer: ident { $($field_outer: ident),+ }, $size_outer: expr) => {
|
||||
impl<T> Transpose for $struct_outer<$struct_inner<T>> {
|
||||
fn transpose(self) -> $struct_inner<$struct_outer<T>> {
|
||||
$struct_inner {
|
||||
$(
|
||||
$field_inner: impl_matrix_inner!(
|
||||
Vector2{x,y}, $size_outer,
|
||||
$field_inner
|
||||
)
|
||||
),+
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[macro_export(local_inner_macros)]
|
||||
macro_rules! impl_operator {
|
||||
( $struct: ident { $($field: ident), + }, $trait: ident, $method: ident, $output: ty ) => {
|
||||
impl<T:core::ops::$trait<Output=T>> core::ops::$trait 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 {
|
||||
$struct {
|
||||
$( $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,34 +1,34 @@
|
||||
#[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> {
|
||||
( $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) ), +
|
||||
}
|
||||
}
|
||||
#[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 ) +
|
||||
)
|
||||
}
|
||||
}
|
||||
};
|
||||
impl<V:core::ops::Add<Output=V>,U,T:fixed_wide_traits::wide::WideMul<U,Output=V>> fixed_wide_traits::wide::WideDot<$struct<U>> for $struct<T> {
|
||||
type Output=V;
|
||||
#[inline]
|
||||
fn wide_dot(self, rhs: $struct<U>) -> Self::Output {
|
||||
$crate::sum_repeating!(
|
||||
$( + (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_length_squared(&self) -> U {
|
||||
$crate::sum_repeating!(
|
||||
$( + self.$field.wide_mul(self.$field) ) +
|
||||
)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ macro_rules! impl_wide_operations {
|
||||
#[doc(hidden)]
|
||||
#[macro_export(local_inner_macros)]
|
||||
macro_rules! sum_repeating {
|
||||
( + $($item: tt) * ) => {
|
||||
$($item) *
|
||||
};
|
||||
( + $($item: tt) * ) => {
|
||||
$($item) *
|
||||
};
|
||||
}
|
||||
|
||||
51
fixed_wide_vectors/src/tests/fixed_wide_traits.rs
Normal file
51
fixed_wide_vectors/src/tests/fixed_wide_traits.rs
Normal file
@@ -0,0 +1,51 @@
|
||||
use fixed_wide_traits::wide::WideMul;
|
||||
use fixed_wide_traits::wide::WideDot;
|
||||
|
||||
use crate::Vector3;
|
||||
|
||||
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 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,62 +1,5 @@
|
||||
use fixed_wide_traits::wide::WideMul;
|
||||
mod tests;
|
||||
|
||||
use crate::Vector3;
|
||||
|
||||
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 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));
|
||||
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));
|
||||
}
|
||||
#[cfg(feature="fixed_wide_traits")]
|
||||
mod fixed_wide_traits;
|
||||
|
||||
16
fixed_wide_vectors/src/tests/tests.rs
Normal file
16
fixed_wide_vectors/src/tests/tests.rs
Normal file
@@ -0,0 +1,16 @@
|
||||
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 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 you_can_shr_numbers(){
|
||||
let a=Planar64::from(4);
|
||||
assert_eq!(a>>1,Planar64::from(2))
|
||||
}
|
||||
8
fixed_wide_vectors/src/traits.rs
Normal file
8
fixed_wide_vectors/src/traits.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
pub trait Dot<Rhs=Self>{
|
||||
type Output;
|
||||
fn dot(self,rhs:Rhs)->Self::Output;
|
||||
}
|
||||
pub trait Transpose{
|
||||
type Output;
|
||||
fn transpose(self)->Self::Output;
|
||||
}
|
||||
@@ -14,8 +14,8 @@
|
||||
/// assert_eq!(vec2.y, 4);
|
||||
/// ```
|
||||
pub struct Vector2<T> {
|
||||
pub x: T,
|
||||
pub y: T,
|
||||
pub x: T,
|
||||
pub y: T,
|
||||
}
|
||||
|
||||
|
||||
@@ -34,9 +34,9 @@ pub struct Vector2<T> {
|
||||
/// assert_eq!(vec3.z, 6);
|
||||
/// ```
|
||||
pub struct Vector3<T> {
|
||||
pub x: T,
|
||||
pub y: T,
|
||||
pub z: T,
|
||||
pub x: T,
|
||||
pub y: T,
|
||||
pub z: T,
|
||||
}
|
||||
|
||||
|
||||
@@ -56,13 +56,18 @@ pub struct Vector3<T> {
|
||||
/// assert_eq!(vec4.w, 8);
|
||||
/// ```
|
||||
pub struct Vector4<T> {
|
||||
pub x: T,
|
||||
pub y: T,
|
||||
pub z: T,
|
||||
pub w: 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);
|
||||
|
||||
//This internally implements non square matrices, idk how else to separate the repeated fields
|
||||
crate::impl_matrix!(Vector2 { x, y }, 2);
|
||||
crate::impl_matrix!(Vector3 { x, y, z }, 3);
|
||||
crate::impl_matrix!(Vector4 { x, y, z, w }, 4);
|
||||
|
||||
Reference in New Issue
Block a user