2016-12-05 05:44:42 +08:00
|
|
|
#![macro_use]
|
|
|
|
|
2020-11-15 23:57:49 +08:00
|
|
|
// TODO: merge with `md_impl`.
|
2016-12-05 05:44:42 +08:00
|
|
|
/// Macro for the implementation of multiplication and division.
|
|
|
|
macro_rules! md_impl(
|
|
|
|
(
|
2018-09-24 12:48:42 +08:00
|
|
|
// Operator, operator method, and scalar bounds.
|
2016-12-05 05:44:42 +08:00
|
|
|
$Op: ident, $op: ident $(where N: $($ScalarBounds: ident),*)*;
|
|
|
|
// Storage dimensions, and dimension bounds.
|
2021-04-08 17:53:01 +08:00
|
|
|
($R1: ty, $C1: ty),($R2: ty, $C2: ty)
|
|
|
|
// Const type declaration
|
|
|
|
const $($D: ident),*;
|
|
|
|
// Other generic type declarations.
|
|
|
|
for $($DimsDecl: ident),*;
|
|
|
|
// Where clause.
|
|
|
|
where $($ConstraintType: ty: $ConstraintBound: ident$(<$($ConstraintBoundParams: ty $( = $EqBound: ty )*),*>)*),*;
|
2016-12-05 05:44:42 +08:00
|
|
|
// Argument identifiers and types + output.
|
|
|
|
$lhs: ident: $Lhs: ty, $rhs: ident: $Rhs: ty, Output = $Result: ty;
|
2018-09-24 12:48:42 +08:00
|
|
|
// Operator actual implementation.
|
2016-12-05 05:44:42 +08:00
|
|
|
$action: expr;
|
|
|
|
// Lifetime.
|
|
|
|
$($lives: tt),*) => {
|
2021-04-08 17:53:01 +08:00
|
|
|
impl<$($lives ,)* N $(, $DimsDecl)* $(, const $D: usize)*> $Op<$Rhs> for $Lhs
|
2019-12-17 07:09:14 +08:00
|
|
|
where N: Scalar + Zero + One + ClosedAdd + ClosedMul $($(+ $ScalarBounds)*)*,
|
2017-08-03 01:37:44 +08:00
|
|
|
DefaultAllocator: Allocator<N, $R1, $C1> +
|
|
|
|
Allocator<N, $R2, $C2> +
|
|
|
|
Allocator<N, $R1, $C2>,
|
2021-04-08 17:53:01 +08:00
|
|
|
$( $ConstraintType: $ConstraintBound$(<$( $ConstraintBoundParams $( = $EqBound )*),*>)* ),*
|
2016-12-05 05:44:42 +08:00
|
|
|
{
|
|
|
|
type Output = $Result;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn $op($lhs, $rhs: $Rhs) -> Self::Output {
|
|
|
|
$action
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
/// Macro for the implementation of multiplication and division.
|
|
|
|
/// Implements all the argument reference combinations.
|
|
|
|
macro_rules! md_impl_all(
|
|
|
|
(
|
2018-09-24 12:48:42 +08:00
|
|
|
// Operator, operator method, and scalar bounds.
|
2016-12-05 05:44:42 +08:00
|
|
|
$Op: ident, $op: ident $(where N: $($ScalarBounds: ident),*)*;
|
|
|
|
// Storage dimensions, and dimension bounds.
|
2021-04-08 17:53:01 +08:00
|
|
|
($R1: ty, $C1: ty),($R2: ty, $C2: ty)
|
|
|
|
// Const type declaration
|
|
|
|
const $($D: ident),*;
|
|
|
|
// Other generic type declarations.
|
|
|
|
for $($DimsDecl: ident),*;
|
|
|
|
// Where clause.
|
|
|
|
where $($ConstraintType: ty: $ConstraintBound: ident$(<$($ConstraintBoundParams: ty $( = $EqBound: ty )*),*>)*),*;
|
2016-12-05 05:44:42 +08:00
|
|
|
// Argument identifiers and types + output.
|
|
|
|
$lhs: ident: $Lhs: ty, $rhs: ident: $Rhs: ty, Output = $Result: ty;
|
|
|
|
// Operators actual implementations.
|
|
|
|
[val val] => $action_val_val: expr;
|
|
|
|
[ref val] => $action_ref_val: expr;
|
|
|
|
[val ref] => $action_val_ref: expr;
|
|
|
|
[ref ref] => $action_ref_ref: expr;) => {
|
|
|
|
|
|
|
|
md_impl!(
|
|
|
|
$Op, $op $(where N: $($ScalarBounds),*)*;
|
2021-04-08 17:53:01 +08:00
|
|
|
($R1, $C1),($R2, $C2)
|
|
|
|
const $($D),*;
|
|
|
|
for $($DimsDecl),*;
|
|
|
|
where $($ConstraintType: $ConstraintBound$(<$($ConstraintBoundParams $( = $EqBound )*),*>)*),*;
|
2016-12-05 05:44:42 +08:00
|
|
|
$lhs: $Lhs, $rhs: $Rhs, Output = $Result;
|
|
|
|
$action_val_val; );
|
|
|
|
|
|
|
|
md_impl!(
|
|
|
|
$Op, $op $(where N: $($ScalarBounds),*)*;
|
2021-04-08 17:53:01 +08:00
|
|
|
($R1, $C1),($R2, $C2)
|
|
|
|
const $($D),*;
|
|
|
|
for $($DimsDecl),*;
|
|
|
|
where $($ConstraintType: $ConstraintBound$(<$($ConstraintBoundParams $( = $EqBound )*),*>)*),*;
|
2016-12-05 05:44:42 +08:00
|
|
|
$lhs: &'a $Lhs, $rhs: $Rhs, Output = $Result;
|
|
|
|
$action_ref_val; 'a);
|
|
|
|
|
|
|
|
md_impl!(
|
|
|
|
$Op, $op $(where N: $($ScalarBounds),*)*;
|
2021-04-08 17:53:01 +08:00
|
|
|
($R1, $C1),($R2, $C2)
|
|
|
|
const $($D),*;
|
|
|
|
for $($DimsDecl),*;
|
|
|
|
where $($ConstraintType: $ConstraintBound$(<$($ConstraintBoundParams $( = $EqBound )*),*>)*),*;
|
2016-12-05 05:44:42 +08:00
|
|
|
$lhs: $Lhs, $rhs: &'b $Rhs, Output = $Result;
|
|
|
|
$action_val_ref; 'b);
|
|
|
|
|
|
|
|
md_impl!(
|
|
|
|
$Op, $op $(where N: $($ScalarBounds),*)*;
|
2021-04-08 17:53:01 +08:00
|
|
|
($R1, $C1),($R2, $C2)
|
|
|
|
const $($D),*;
|
|
|
|
for $($DimsDecl),*;
|
|
|
|
where $($ConstraintType: $ConstraintBound$(<$($ConstraintBoundParams $( = $EqBound )*),*>)*),*;
|
2016-12-05 05:44:42 +08:00
|
|
|
$lhs: &'a $Lhs, $rhs: &'b $Rhs, Output = $Result;
|
|
|
|
$action_ref_ref; 'a, 'b);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2018-09-24 12:48:42 +08:00
|
|
|
/// Macro for the implementation of assignment-multiplication and assignment-division.
|
2016-12-05 05:44:42 +08:00
|
|
|
macro_rules! md_assign_impl(
|
|
|
|
(
|
|
|
|
// Operator, operator method, and scalar bounds.
|
2021-04-08 17:53:01 +08:00
|
|
|
$Op: ident, $op: ident $(where N: $($ScalarBounds: ident),*)* $(for N::Element: $($ElementBounds: ident),*)*;
|
2016-12-05 05:44:42 +08:00
|
|
|
// Storage dimensions, and dimension bounds.
|
2021-04-08 17:53:01 +08:00
|
|
|
($R1: ty, $C1: ty),($R2: ty, $C2: ty)
|
|
|
|
// Const type declaration
|
|
|
|
const $($D: ident),*;
|
|
|
|
// Other generic type declarations.
|
|
|
|
for $($DimsDecl: ident),*;
|
|
|
|
// Where clause.
|
|
|
|
where $($ConstraintType: ty: $ConstraintBound: ident$(<$($ConstraintBoundParams: ty $( = $EqBound: ty )*),*>)*),*;
|
2016-12-05 05:44:42 +08:00
|
|
|
// Argument identifiers and types.
|
|
|
|
$lhs: ident: $Lhs: ty, $rhs: ident: $Rhs: ty;
|
|
|
|
// Actual implementation and lifetimes.
|
|
|
|
$action: expr; $($lives: tt),*) => {
|
2021-04-08 17:53:01 +08:00
|
|
|
impl<$($lives ,)* N $(, $DimsDecl)* $(, const $D: usize)*> $Op<$Rhs> for $Lhs
|
2019-12-17 07:09:14 +08:00
|
|
|
where N: Scalar + Zero + One + ClosedAdd + ClosedMul $($(+ $ScalarBounds)*)*,
|
2021-04-08 17:53:01 +08:00
|
|
|
$($(N::Element: $ElementBounds,)*)*
|
2017-08-03 01:37:44 +08:00
|
|
|
DefaultAllocator: Allocator<N, $R1, $C1> +
|
|
|
|
Allocator<N, $R2, $C2>,
|
|
|
|
$( $ConstraintType: $ConstraintBound $(<$( $ConstraintBoundParams $( = $EqBound )*),*>)* ),*
|
2016-12-05 05:44:42 +08:00
|
|
|
{
|
|
|
|
#[inline]
|
|
|
|
fn $op(&mut $lhs, $rhs: $Rhs) {
|
|
|
|
$action
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2018-09-24 12:48:42 +08:00
|
|
|
/// Macro for the implementation of assignment-multiplication and assignment-division with and
|
2016-12-05 05:44:42 +08:00
|
|
|
/// without reference to the right-hand-side.
|
|
|
|
macro_rules! md_assign_impl_all(
|
|
|
|
(
|
|
|
|
// Operator, operator method, and scalar bounds.
|
2020-03-22 06:22:55 +08:00
|
|
|
$Op: ident, $op: ident $(where N: $($ScalarBounds: ident),*)* $(for N::Element: $($ElementBounds: ident),*)*;
|
2016-12-05 05:44:42 +08:00
|
|
|
// Storage dimensions, and dimension bounds.
|
2021-04-08 17:53:01 +08:00
|
|
|
($R1: ty, $C1: ty),($R2: ty, $C2: ty)
|
|
|
|
// Const type declaration
|
|
|
|
const $($D: ident),*;
|
|
|
|
// Other generic type declarations.
|
|
|
|
for $($DimsDecl: ident),*;
|
|
|
|
// Where clause.
|
|
|
|
where $($ConstraintType: ty: $ConstraintBound: ident$(<$($ConstraintBoundParams: ty $( = $EqBound: ty )*),*>)*),*;
|
2016-12-05 05:44:42 +08:00
|
|
|
// Argument identifiers and types.
|
|
|
|
$lhs: ident: $Lhs: ty, $rhs: ident: $Rhs: ty;
|
|
|
|
// Actual implementation and lifetimes.
|
|
|
|
[val] => $action_val: expr;
|
|
|
|
[ref] => $action_ref: expr;) => {
|
|
|
|
md_assign_impl!(
|
2020-03-22 06:22:55 +08:00
|
|
|
$Op, $op $(where N: $($ScalarBounds),*)* $(for N::Element: $($ElementBounds),*)*;
|
2021-04-08 17:53:01 +08:00
|
|
|
($R1, $C1),($R2, $C2)
|
|
|
|
const $($D),*;
|
|
|
|
for $($DimsDecl),*;
|
|
|
|
where $($ConstraintType: $ConstraintBound$(<$($ConstraintBoundParams $( = $EqBound )*),*>)*),*;
|
2016-12-05 05:44:42 +08:00
|
|
|
$lhs: $Lhs, $rhs: $Rhs;
|
|
|
|
$action_val; );
|
|
|
|
|
|
|
|
md_assign_impl!(
|
2020-03-22 06:22:55 +08:00
|
|
|
$Op, $op $(where N: $($ScalarBounds),*)* $(for N::Element: $($ElementBounds),*)*;
|
2021-04-08 17:53:01 +08:00
|
|
|
($R1, $C1),($R2, $C2)
|
|
|
|
const $($D),*;
|
|
|
|
for $($DimsDecl),*;
|
|
|
|
where $($ConstraintType: $ConstraintBound$(<$($ConstraintBoundParams $( = $EqBound )*),*>)*),*;
|
2016-12-05 05:44:42 +08:00
|
|
|
$lhs: $Lhs, $rhs: &'b $Rhs;
|
|
|
|
$action_ref; 'b);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-11-15 23:57:49 +08:00
|
|
|
// TODO: merge with `as_impl`.
|
2016-12-05 05:44:42 +08:00
|
|
|
/// Macro for the implementation of addition and subtraction.
|
|
|
|
macro_rules! add_sub_impl(
|
|
|
|
($Op: ident, $op: ident, $bound: ident;
|
2021-04-08 17:53:01 +08:00
|
|
|
($R1: ty, $C1: ty),($R2: ty, $C2: ty) $(-> ($RRes: ty))*
|
|
|
|
// Const type declaration
|
|
|
|
const $($D: ident),*;
|
|
|
|
// Other generic type declarations.
|
|
|
|
for $($DimsDecl: ident),*;
|
|
|
|
// Where clause.
|
|
|
|
where $($ConstraintType: ty: $ConstraintBound: ident$(<$($ConstraintBoundParams: ty $( = $EqBound: ty )*),*>)*),*;
|
2016-12-05 05:44:42 +08:00
|
|
|
$lhs: ident: $Lhs: ty, $rhs: ident: $Rhs: ty, Output = $Result: ty;
|
|
|
|
$action: expr; $($lives: tt),*) => {
|
2021-04-08 17:53:01 +08:00
|
|
|
impl<$($lives ,)* N $(, $DimsDecl)* $(, const $D: usize)*> $Op<$Rhs> for $Lhs
|
2019-12-17 07:09:14 +08:00
|
|
|
where N: Scalar + $bound,
|
2017-08-03 01:37:44 +08:00
|
|
|
DefaultAllocator: Allocator<N, $R1, $C1> +
|
|
|
|
Allocator<N, $R2, $C2> +
|
|
|
|
SameShapeAllocator<N, $R1, $C1, $R2, $C2>,
|
2016-12-05 05:44:42 +08:00
|
|
|
ShapeConstraint: SameNumberOfRows<$R1, $R2 $(, Representative = $RRes)*> +
|
2021-04-08 17:53:01 +08:00
|
|
|
SameNumberOfColumns<$C1, $C2>,
|
|
|
|
$( $ConstraintType: $ConstraintBound$(<$( $ConstraintBoundParams $( = $EqBound )*),*>)* ),* {
|
2016-12-05 05:44:42 +08:00
|
|
|
type Output = $Result;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn $op($lhs, $rhs: $Rhs) -> Self::Output {
|
|
|
|
$action
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-11-15 23:57:49 +08:00
|
|
|
// TODO: merge with `md_assign_impl`.
|
2018-09-24 12:48:42 +08:00
|
|
|
/// Macro for the implementation of assignment-addition and assignment-subtraction.
|
2016-12-05 05:44:42 +08:00
|
|
|
macro_rules! add_sub_assign_impl(
|
|
|
|
($Op: ident, $op: ident, $bound: ident;
|
2021-04-08 17:53:01 +08:00
|
|
|
$(const $D: ident),*;
|
2016-12-05 05:44:42 +08:00
|
|
|
$lhs: ident: $Lhs: ty, $rhs: ident: $Rhs: ty;
|
|
|
|
$action: expr; $($lives: tt),*) => {
|
2021-04-08 17:53:01 +08:00
|
|
|
impl<$($lives ,)* N $(, const $D: usize),*> $Op<$Rhs> for $Lhs
|
|
|
|
where N: Scalar + $bound {
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
|
|
|
fn $op(&mut $lhs, $rhs: $Rhs) {
|
|
|
|
$action
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|