mech_interpreter/stdlib/math/
add.rs

1#[macro_use]
2use crate::stdlib::*;
3
4// Add ------------------------------------------------------------------------
5
6macro_rules! add_op {
7($lhs:expr, $rhs:expr, $out:expr) => {
8  unsafe { *$out = *$lhs + *$rhs; }
9  };}
10
11macro_rules! add_vec_op {
12  ($lhs:expr, $rhs:expr, $out:expr) => {
13    unsafe { (*$lhs).add_to(&*$rhs,&mut *$out) }
14  };}
15
16macro_rules! add_mat_vec_op {
17  ($lhs:expr, $rhs:expr, $out:expr) => {
18    unsafe {
19      let mut out_deref = &mut (*$out);
20      let lhs_deref = &(*$lhs);
21      let rhs_deref = &(*$rhs);
22      for (mut col, lhs_col) in out_deref.column_iter_mut().zip(lhs_deref.column_iter()) {
23        lhs_col.add_to(&rhs_deref,&mut col);
24      }
25    }
26  };}
27
28macro_rules! add_vec_mat_op {
29  ($lhs:expr, $rhs:expr, $out:expr) => {
30    unsafe {
31      let mut out_deref = &mut (*$out);
32      let lhs_deref = &(*$lhs);
33      let rhs_deref = &(*$rhs);
34      for (mut col, rhs_col) in out_deref.column_iter_mut().zip(rhs_deref.column_iter()) {
35        lhs_deref.add_to(&rhs_col,&mut col);
36      }
37    }
38  };}
39
40macro_rules! add_mat_row_op {
41  ($lhs:expr, $rhs:expr, $out:expr) => {
42    unsafe {
43      let mut out_deref = &mut (*$out);
44      let lhs_deref = &(*$lhs);
45      let rhs_deref = &(*$rhs);
46      for (mut row, lhs_row) in out_deref.row_iter_mut().zip(lhs_deref.row_iter()) {
47        lhs_row.add_to(&rhs_deref,&mut row);
48      }
49    }
50  };}
51
52macro_rules! add_row_mat_op {
53  ($lhs:expr, $rhs:expr, $out:expr) => {
54    unsafe {
55      let mut out_deref = &mut (*$out);
56      let lhs_deref = &(*$lhs);
57      let rhs_deref = &(*$rhs);
58      for (mut row, rhs_row) in out_deref.row_iter_mut().zip(rhs_deref.row_iter()) {
59        lhs_deref.add_to(&rhs_row,&mut row);
60      }
61    }
62  };}  
63
64macro_rules! add_scalar_lhs_op {
65  ($lhs:expr, $rhs:expr, $out:expr) => {
66    unsafe { *$out = (*$lhs).add_scalar(*$rhs); }
67  };}
68
69macro_rules! add_scalar_rhs_op {
70  ($lhs:expr, $rhs:expr, $out:expr) => {
71    unsafe { *$out = (*$rhs).add_scalar(*$lhs); }
72  };}
73
74impl_math_fxns!(Add);
75
76fn impl_add_fxn(lhs_value: Value, rhs_value: Value) -> Result<Box<dyn MechFunction>, MechError> {
77  impl_binop_match_arms!(
78    Add,
79    (lhs_value, rhs_value),
80    I8,   I8   => MatrixI8,   i8,   i8::zero(), "I8";
81    I16,  I16  => MatrixI16,  i16,  i16::zero(), "I16";
82    I32,  I32  => MatrixI32,  i32,  i32::zero(), "I32";
83    I64,  I64  => MatrixI64,  i64,  i64::zero(), "I64";
84    I128, I128 => MatrixI128, i128, i128::zero(), "I128";
85    U8,   U8   => MatrixU8,   u8,   u8::zero(), "U8";
86    U16,  U16  => MatrixU16,  u16,  u16::zero(), "U16";
87    U32,  U32  => MatrixU32,  u32,  u32::zero(), "U32";
88    U64,  U64  => MatrixU64,  u64,  u64::zero(), "U64";
89    U128, U128 => MatrixU128, u128, u128::zero(), "U128";
90    F32,  F32  => MatrixF32,  F32,  F32::zero(), "F32";
91    F64,  F64  => MatrixF64,  F64,  F64::zero(), "F64";
92  )
93}
94
95impl_mech_binop_fxn!(MathAdd,impl_add_fxn);