mech_math/arithmetic/
abs.rs

1use crate::*;
2use mech_core::*;
3use num_traits::*;
4#[cfg(feature = "matrix")]
5use mech_core::matrix::Matrix;
6
7// Abs ------------------------------------------------------------------------
8
9use libm::{fabs,fabsf};
10
11macro_rules! uabs_op {
12  ($arg:expr, $out:expr) => {
13    unsafe{(*$out) = (*$arg).clone();}
14  };}
15
16macro_rules! uabs_vec_op {
17  ($arg:expr, $out:expr) => {
18    unsafe {
19      for i in 0..(*$arg).len() {
20        (&mut (*$out))[i] =  (&(*$arg))[i].clone();
21      }}};}
22
23macro_rules! abs_op {
24  ($arg:expr, $out:expr) => {
25    unsafe{(*$out) = (*$arg).abs();}
26  };}
27
28macro_rules! abs_vec_op {
29  ($arg:expr, $out:expr) => {
30    unsafe {
31      for i in 0..(*$arg).len() {
32        (&mut (*$out))[i] =  (&(*$arg))[i].abs();
33      }}};}
34
35macro_rules! fabs_op {
36  ($arg:expr, $out:expr) => {
37    unsafe{(*$out) = fabs((*$arg));}
38  };}
39
40macro_rules! fabs_vec_op {
41  ($arg:expr, $out:expr) => {
42    unsafe {
43      for i in 0..(*$arg).len() {
44        ((&mut (*$out))[i]) = fabs(((&(*$arg))[i]));
45      }}};}
46
47macro_rules! fabsf_op {
48  ($arg:expr, $out:expr) => {
49    unsafe{(*$out) = fabsf((*$arg));}
50  };}  
51
52macro_rules! fabsf_vec_op {
53  ($arg:expr, $out:expr) => {
54    unsafe {
55      for i in 0..(*$arg).len() {
56        ((&mut (*$out))[i]) = fabsf(((&(*$arg))[i]));
57      }}};}
58
59#[cfg(feature = "u8")]
60impl_math_unop!(MathAbs, u8, uabs, FeatureFlag::Custom(hash_str("math/abs")));
61#[cfg(feature = "u16")]
62impl_math_unop!(MathAbs, u16, uabs, FeatureFlag::Custom(hash_str("math/abs")));
63#[cfg(feature = "u32")]
64impl_math_unop!(MathAbs, u32, uabs, FeatureFlag::Custom(hash_str("math/abs")));
65#[cfg(feature = "u64")]
66impl_math_unop!(MathAbs, u64, uabs, FeatureFlag::Custom(hash_str("math/abs")));
67#[cfg(feature = "u128")]
68impl_math_unop!(MathAbs, u128, uabs, FeatureFlag::Custom(hash_str("math/abs")));
69
70#[cfg(feature = "i8")]
71impl_math_unop!(MathAbs, i8, abs, FeatureFlag::Custom(hash_str("math/abs")));
72#[cfg(feature = "i16")]
73impl_math_unop!(MathAbs, i16, abs, FeatureFlag::Custom(hash_str("math/abs")));
74#[cfg(feature = "i32")]
75impl_math_unop!(MathAbs, i32, abs, FeatureFlag::Custom(hash_str("math/abs")));
76#[cfg(feature = "i64")]
77impl_math_unop!(MathAbs, i64, abs, FeatureFlag::Custom(hash_str("math/abs")));
78#[cfg(feature = "i128")]
79impl_math_unop!(MathAbs, i128, abs, FeatureFlag::Custom(hash_str("math/abs")));
80
81#[cfg(feature = "f32")]
82impl_math_unop!(MathAbs, f32, fabsf, FeatureFlag::Custom(hash_str("math/abs")));
83#[cfg(feature = "f64")]
84impl_math_unop!(MathAbs, f64, fabs, FeatureFlag::Custom(hash_str("math/abs")));
85
86#[cfg(feature = "c64")]
87impl_math_unop!(MathAbs, C64, abs, FeatureFlag::Custom(hash_str("math/abs")));
88
89#[cfg(feature = "r64")]
90impl_math_unop!(MathAbs, R64, abs, FeatureFlag::Custom(hash_str("math/abs")));
91
92fn impl_abs_fxn(lhs_value: Value) -> MResult<Box<dyn MechFunction>> {
93  impl_urnop_match_arms2!(
94    MathAbs,
95    (lhs_value),
96    U8 => MatrixU8, u8, u8::zero(), "u8";
97    U16 => MatrixU16, u16, u16::zero(), "u16";
98    U32 => MatrixU32, u32, u32::zero(), "u32";
99    U64 => MatrixU64, u64, u64::zero(), "u64";
100    U128 => MatrixU128, u128, u128::zero(), "u128";
101    I8 => MatrixI8, i8, i8::zero(), "i8";
102    I16 => MatrixI16, i16, i16::zero(), "i16";
103    I32 => MatrixI32, i32, i32::zero(), "i32";
104    I64 => MatrixI64, i64, i64::zero(), "i64";
105    I128 => MatrixI128, i128, i128::zero(), "i128";
106    F32 => MatrixF32, f32, f32::zero(), "f32";
107    F64 => MatrixF64, f64, f64::zero(), "f64";
108    C64 => MatrixC64, C64, C64::default(), "c64";
109    R64 => MatrixR64, R64, R64::zero(), "r64";
110  )
111}
112
113pub struct MathAbs {}
114
115impl NativeFunctionCompiler for MathAbs {
116  fn compile(&self, arguments: &Vec<Value>) -> MResult<Box<dyn MechFunction>> {
117    if arguments.len() != 1 {
118      return Err(MechError2::new(IncorrectNumberOfArguments { expected: 1, found: arguments.len() }, None).with_compiler_loc());
119    }
120    let input = arguments[0].clone();
121    match impl_abs_fxn(input.clone()) {
122      Ok(fxn) => Ok(fxn),
123      Err(_) => {
124        match (input) {
125          (Value::MutableReference(input)) => {impl_abs_fxn(input.borrow().clone())}
126          x => Err(MechError2::new(
127              UnhandledFunctionArgumentKind1 { arg: x.kind(), fxn_name: "math/abs".to_string() },
128              None
129            ).with_compiler_loc()
130          ),
131        }
132      }
133    }
134  }
135}
136
137register_descriptor! {
138  FunctionCompilerDescriptor {
139    name: "math/abs",
140    ptr: &MathAbs{},
141  }
142}