mech_interpreter/stdlib/math/
negate.rs1#[macro_use]
2use crate::stdlib::*;
3
4macro_rules! neg_op {
7 ($arg:expr, $out:expr) => {
8 unsafe { *$out = -*$arg; }
9 };}
10
11macro_rules! neg_vec_op {
12($arg:expr, $out:expr) => {
13 unsafe { *$out = (*$arg).clone().neg(); }
14 };}
15
16macro_rules! impl_neg_op {
17($struct_name:ident, $out_type:ty, $op:ident) => {
18 #[derive(Debug)]
19 struct $struct_name<T> {
20 arg: Ref<$out_type>,
21 out: Ref<$out_type>,
22 }
23 impl<T> MechFunction for $struct_name<T>
24 where
25 T: Copy + Debug + Clone + Sync + Send + Neg + ClosedNeg + PartialEq + 'static,
26 Ref<$out_type>: ToValue
27 {
28 fn solve(&self) {
29 let arg_ptr = self.arg.as_ptr();
30 let out_ptr = self.out.as_ptr();
31 $op!(arg_ptr,out_ptr);
32 }
33 fn out(&self) -> Value { self.out.to_value() }
34 fn to_string(&self) -> String { format!("{:#?}", self) }
35 }};}
36
37impl_neg_op!(NegateS, T, neg_op);
38#[cfg(feature = "Matrix1")]
39impl_neg_op!(NegateM1, Matrix1<T>,neg_op);
40#[cfg(feature = "Matrix2")]
41impl_neg_op!(NegateM2, Matrix2<T>,neg_op);
42#[cfg(feature = "Matrix3")]
43impl_neg_op!(NegateM3, Matrix3<T>,neg_op);
44#[cfg(feature = "Matrix4")]
45impl_neg_op!(NegateM4, Matrix4<T>,neg_op);
46#[cfg(feature = "Matrix2x3")]
47impl_neg_op!(NegateM2x3, Matrix2x3<T>,neg_op);
48#[cfg(feature = "Matrix3x2")]
49impl_neg_op!(NegateM3x2, Matrix3x2<T>,neg_op);
50#[cfg(feature = "MatrixD")]
51impl_neg_op!(NegateMD, DMatrix<T>,neg_vec_op);
52#[cfg(feature = "RowVector2")]
53impl_neg_op!(NegateR2, RowVector2<T>,neg_op);
54#[cfg(feature = "RowVector3")]
55impl_neg_op!(NegateR3, RowVector3<T>,neg_op);
56#[cfg(feature = "RowVector4")]
57impl_neg_op!(NegateR4, RowVector4<T>,neg_op);
58#[cfg(feature = "RowVectorD")]
59impl_neg_op!(NegateRD, RowDVector<T>,neg_vec_op);
60#[cfg(feature = "Vector2")]
61impl_neg_op!(NegateV2, Vector2<T>,neg_op);
62#[cfg(feature = "Vector3")]
63impl_neg_op!(NegateV3, Vector3<T>,neg_op);
64#[cfg(feature = "Vector4")]
65impl_neg_op!(NegateV4, Vector4<T>,neg_op);
66#[cfg(feature = "VectorD")]
67impl_neg_op!(NegateVD, DVector<T>,neg_vec_op);
68
69fn impl_neg_fxn(lhs_value: Value) -> Result<Box<dyn MechFunction>, MechError> {
70impl_urnop_match_arms!(
71 Negate,
72 (lhs_value),
73 I8 => MatrixI8, i8, i8::zero(), "I8";
74 I16 => MatrixI16, i16, i16::zero(), "I16";
75 I32 => MatrixI32, i32, i32::zero(), "I32";
76 I64 => MatrixI64, i64, i64::zero(), "I64";
77 I128 => MatrixI128, i128, i128::zero(), "I128";
78 F32 => MatrixF32, F32, F32::zero(), "F32";
79 F64 => MatrixF64, F64, F64::zero(), "F64";
80)
81}
82
83impl_mech_urnop_fxn!(MathNegate,impl_neg_fxn);