Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

condensed duplicate code for trait specialization using macros #424

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
261 changes: 97 additions & 164 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,102 +17,148 @@

#![macro_use]

/// generates an impl fn, allowing specialization if simd is enabled
macro_rules! impl_fn {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think I'd prefer this called impl_operator_method!



Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra space here!

// Mutable Binary
(fn $op:ident(&mut $lhs:ident, $rhs:ident:$Rhs:ty) $body:block ) => {
#[cfg(feature = "simd")]
#[inline]
default fn $op(&mut $lhs, $rhs: $Rhs) $body

#[cfg(not(feature = "simd"))]
#[inline]
fn $op(&mut $lhs, $rhs: $Rhs) $body
};

// Unary
(fn $op:ident($x:ident) -> $Output:ty { $body:expr }) => {
#[cfg(feature = "simd")]
#[inline]
default fn $op(self) -> $Output {
let $x = self; $body
}

#[cfg(not(feature = "simd"))]
#[inline]
fn $op(self) -> $Output {
let $x = self; $body
}
};

// Binary
(fn $op:ident($lhs:ident, $rhs:ident:$Rhs:ty) -> $Output:ty { $body:expr }) => {
#[cfg(feature = "simd")]
#[inline]
default fn $op(self, other: $Rhs) -> $Output {
let ($lhs, $rhs) = (self, other); $body
}

#[cfg(not(feature = "simd"))]
#[inline]
fn $op(self, other: $Rhs) -> $Output {
let ($lhs, $rhs) = (self, other); $body
}
};
}


/// Generates a binary operator implementation for the permutations of by-ref and by-val
macro_rules! impl_operator {
// When it is an unary operator
(<$S:ident: $Constraint:ident> $Op:ident for $Lhs:ty {
fn $op:ident($x:ident) -> $Output:ty { $body:expr }
}) => {
impl<$S: $Constraint> $Op for $Lhs {
type Output = $Output;
#[inline]
fn $op(self) -> $Output {
let $x = self; $body
}
type Output = $Output;
impl_fn!(
fn $op($x) -> $Output {
$body
}
);
}

impl<'a, $S: $Constraint> $Op for &'a $Lhs {
type Output = $Output;
#[inline]
fn $op(self) -> $Output {
let $x = self; $body
}
type Output = $Output;
impl_fn!(
fn $op($x) -> $Output {
$body
}
);
}
};
// When the right operand is a scalar
(<$S:ident: $Constraint:ident> $Op:ident<$Rhs:ident> for $Lhs:ty {
fn $op:ident($lhs:ident, $rhs:ident) -> $Output:ty { $body:expr }
}) => {
impl<$S: $Constraint> $Op<$Rhs> for $Lhs {
type Output = $Output;
#[inline]
fn $op(self, other: $Rhs) -> $Output {
let ($lhs, $rhs) = (self, other); $body
}
type Output = $Output;
impl_fn!(fn $op($lhs, $rhs : $Rhs) -> $Output {
$body
});
}

impl<'a, $S: $Constraint> $Op<$Rhs> for &'a $Lhs {
type Output = $Output;
#[inline]
fn $op(self, other: $Rhs) -> $Output {
let ($lhs, $rhs) = (self, other); $body
}
type Output = $Output;
impl_fn!(fn $op($lhs, $rhs : $Rhs) -> $Output {
$body
});
}
};
// When the right operand is a compound type
(<$S:ident: $Constraint:ident> $Op:ident<$Rhs:ty> for $Lhs:ty {
fn $op:ident($lhs:ident, $rhs:ident) -> $Output:ty { $body:expr }
}) => {
impl<$S: $Constraint> $Op<$Rhs> for $Lhs {
type Output = $Output;
#[inline]
fn $op(self, other: $Rhs) -> $Output {
let ($lhs, $rhs) = (self, other); $body
}
type Output = $Output;

impl_fn!(fn $op($lhs, $rhs : $Rhs) -> $Output {
$body
});
}

impl<'a, $S: $Constraint> $Op<&'a $Rhs> for $Lhs {
type Output = $Output;
#[inline]
fn $op(self, other: &'a $Rhs) -> $Output {
let ($lhs, $rhs) = (self, other); $body
}
type Output = $Output;

impl_fn!(fn $op($lhs, $rhs : &'a $Rhs) -> $Output {
$body
});
}

impl<'a, $S: $Constraint> $Op<$Rhs> for &'a $Lhs {
type Output = $Output;
#[inline]
fn $op(self, other: $Rhs) -> $Output {
let ($lhs, $rhs) = (self, other); $body
}
type Output = $Output;
impl_fn!(fn $op($lhs, $rhs : $Rhs) -> $Output {
$body
});
}

impl<'a, 'b, $S: $Constraint> $Op<&'a $Rhs> for &'b $Lhs {
type Output = $Output;
#[inline]
fn $op(self, other: &'a $Rhs) -> $Output {
let ($lhs, $rhs) = (self, other); $body
}
type Output = $Output;

impl_fn!(fn $op($lhs, $rhs : &'a $Rhs) -> $Output {
$body
});
}
};
// When the left operand is a scalar
($Op:ident<$Rhs:ident<$S:ident>> for $Lhs:ty {
fn $op:ident($lhs:ident, $rhs:ident) -> $Output:ty { $body:expr }
}) => {
impl $Op<$Rhs<$S>> for $Lhs {
type Output = $Output;
#[inline]
fn $op(self, other: $Rhs<$S>) -> $Output {
let ($lhs, $rhs) = (self, other); $body
}
type Output = $Output;

impl_fn!(fn $op($lhs, $rhs : $Rhs<$S>) -> $Output {
$body
});
}

impl<'a> $Op<&'a $Rhs<$S>> for $Lhs {
type Output = $Output;
#[inline]
fn $op(self, other: &'a $Rhs<$S>) -> $Output {
let ($lhs, $rhs) = (self, other); $body
}
type Output = $Output;

impl_fn!(fn $op($lhs, $rhs : &'a $Rhs<$S>) -> $Output {
$body
});
}
};
}
Expand All @@ -122,8 +168,7 @@ macro_rules! impl_assignment_operator {
fn $op:ident(&mut $lhs:ident, $rhs:ident) $body:block
}) => {
impl<$S: $Constraint + $Op<$S>> $Op<$Rhs> for $Lhs {
#[inline]
fn $op(&mut $lhs, $rhs: $Rhs) $body
impl_fn!(fn $op(&mut $lhs, $rhs : $Rhs) { $body });
}
};
}
Expand Down Expand Up @@ -255,118 +300,6 @@ macro_rules! impl_index_operators {
}
}

#[cfg(feature = "simd")]
macro_rules! impl_operator_default {
// When it is an unary operator
(<$S:ident: $Constraint:ident> $Op:ident for $Lhs:ty {
fn $op:ident($x:ident) -> $Output:ty { $body:expr }
}) => {
impl<$S: $Constraint> $Op for $Lhs {
type Output = $Output;
#[inline]
default fn $op(self) -> $Output {
let $x = self; $body
}
}

impl<'a, $S: $Constraint> $Op for &'a $Lhs {
type Output = $Output;
#[inline]
default fn $op(self) -> $Output {
let $x = self; $body
}
}
};
// When the right operand is a scalar
(<$S:ident: $Constraint:ident> $Op:ident<$Rhs:ident> for $Lhs:ty {
fn $op:ident($lhs:ident, $rhs:ident) -> $Output:ty { $body:expr }
}) => {
impl<$S: $Constraint> $Op<$Rhs> for $Lhs {
type Output = $Output;
#[inline]
default fn $op(self, other: $Rhs) -> $Output {
let ($lhs, $rhs) = (self, other); $body
}
}

impl<'a, $S: $Constraint> $Op<$Rhs> for &'a $Lhs {
type Output = $Output;
#[inline]
default fn $op(self, other: $Rhs) -> $Output {
let ($lhs, $rhs) = (self, other); $body
}
}
};
// When the right operand is a compound type
(<$S:ident: $Constraint:ident> $Op:ident<$Rhs:ty> for $Lhs:ty {
fn $op:ident($lhs:ident, $rhs:ident) -> $Output:ty { $body:expr }
}) => {
impl<$S: $Constraint> $Op<$Rhs> for $Lhs {
type Output = $Output;
#[inline]
default fn $op(self, other: $Rhs) -> $Output {
let ($lhs, $rhs) = (self, other); $body
}
}

impl<'a, $S: $Constraint> $Op<&'a $Rhs> for $Lhs {
type Output = $Output;
#[inline]
default fn $op(self, other: &'a $Rhs) -> $Output {
let ($lhs, $rhs) = (self, other); $body
}
}

impl<'a, $S: $Constraint> $Op<$Rhs> for &'a $Lhs {
type Output = $Output;
#[inline]
default fn $op(self, other: $Rhs) -> $Output {
let ($lhs, $rhs) = (self, other); $body
}
}

impl<'a, 'b, $S: $Constraint> $Op<&'a $Rhs> for &'b $Lhs {
type Output = $Output;
#[inline]
default fn $op(self, other: &'a $Rhs) -> $Output {
let ($lhs, $rhs) = (self, other); $body
}
}
};
// When the left operand is a scalar
($Op:ident<$Rhs:ident<$S:ident>> for $Lhs:ty {
fn $op:ident($lhs:ident, $rhs:ident) -> $Output:ty { $body:expr }
}) => {
impl $Op<$Rhs<$S>> for $Lhs {
type Output = $Output;
#[inline]
default fn $op(self, other: $Rhs<$S>) -> $Output {
let ($lhs, $rhs) = (self, other); $body
}
}

impl<'a> $Op<&'a $Rhs<$S>> for $Lhs {
type Output = $Output;
#[inline]
default fn $op(self, other: &'a $Rhs<$S>) -> $Output {
let ($lhs, $rhs) = (self, other); $body
}
}
};
}

#[cfg(feature = "simd")]
macro_rules! impl_assignment_operator_default {
(<$S:ident: $Constraint:ident> $Op:ident<$Rhs:ty> for $Lhs:ty {
fn $op:ident(&mut $lhs:ident, $rhs:ident) $body:block
}) => {
impl<$S: $Constraint + $Op<$S>> $Op<$Rhs> for $Lhs {
#[inline]
default fn $op(&mut $lhs, $rhs: $Rhs) $body
}
};
}

/// Generates a binary operator implementation for the permutations of by-ref and by-val, for simd
#[cfg(feature = "simd")]
macro_rules! impl_operator_simd {
Expand Down
10 changes: 7 additions & 3 deletions src/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@ use approx::ApproxEq;
use std::fmt;
use std::ops::*;

use num_traits::{Float, Num, NumCast};
use num_traits::{Float, Num, NumCast, Signed};

/// Base numeric types with partial ordering
pub trait BaseNum: Copy + Clone + fmt::Debug + Num + NumCast + PartialOrd + AddAssign + SubAssign + MulAssign + DivAssign + RemAssign {}

impl<T> BaseNum for T where T: Copy + Clone + fmt::Debug + Num + NumCast + PartialOrd + AddAssign + SubAssign + MulAssign + DivAssign + RemAssign {}

/// Base signed types
pub trait BaseSigned: BaseNum + Signed {}

impl<T> BaseSigned for T where T: BaseNum + Signed {}
/// Base floating point types
pub trait BaseFloat: BaseNum + Float + ApproxEq<Epsilon = Self> {}
pub trait BaseFloat: BaseSigned + Float + ApproxEq<Epsilon = Self> {}

impl<T> BaseFloat for T where T: BaseNum + Float + ApproxEq<Epsilon = Self> {}
impl<T> BaseFloat for T where T: BaseSigned + Float + ApproxEq<Epsilon = Self> {}
Loading