1#[macro_use]
2use crate::*;
3use simba::scalar::ClosedNeg;
4use num_traits::*;
5#[cfg(feature = "matrix")]
6use mech_core::matrix::Matrix;
7
8#[derive(Debug)]
11struct NegateV<O> {
12 arg: Ref<O>,
13 out: Ref<O>,
14 _marker: PhantomData<O>,
15}
16impl<O> MechFunctionFactory for NegateV<O>
17where
18 O: Debug + Clone + Sync + Send + Neg<Output = O> + ClosedNeg + PartialEq + 'static +
19 CompileConst + ConstElem + AsValueKind,
20 Ref<O>: ToValue,
21{
22 fn new(args: FunctionArgs) -> MResult<Box<dyn MechFunction>> {
23 match args {
24 FunctionArgs::Unary(out, arg) => {
25 let arg: Ref<O> = unsafe { arg.as_unchecked() }.clone();
26 let out: Ref<O> = unsafe { out.as_unchecked() }.clone();
27 Ok(Box::new(Self {arg, out, _marker: PhantomData }))
28 },
29 _ => Err(MechError2::new(
30 IncorrectNumberOfArguments { expected: 1, found: args.len() },
31 None
32 ).with_compiler_loc()
33 ),
34 }
35 }
36}
37impl<O> MechFunctionImpl for NegateV<O>
38where
39 O: Debug + Clone + Sync + Send + Neg<Output = O> + ClosedNeg + PartialEq + 'static,
40 Ref<O>: ToValue,
41{
42 fn solve(&self) {
43 let arg_ptr = self.arg.as_ptr();
44 let out_ptr = self.out.as_mut_ptr();
45 unsafe { *out_ptr = (*arg_ptr).clone().neg(); }
46 }
47 fn out(&self) -> Value { self.out.to_value() }
48 fn to_string(&self) -> String { format!("{:#?}", self) }
49}
50#[cfg(feature = "compiler")]
51impl<O> MechFunctionCompiler for NegateV<O>
52where
53 O: CompileConst + ConstElem + AsValueKind,
54{
55 fn compile(&self, ctx: &mut CompileCtx) -> MResult<Register> {
56 let name = format!("NegateV<{}>", O::as_value_kind());
57 compile_unop!(name, self.out, self.arg, ctx, FeatureFlag::Builtin(FeatureKind::Neg) );
58 }
59}
60
61register_fxn_descriptor!(NegateV,
62 i8, "i8",
63 i16, "i16",
64 i32, "i32",
65 i64, "i64",
66 i128, "i128",
67 f32, "f32",
68 f64, "f64",
69 R64, "r64",
70 C64, "c64"
71);
72
73#[derive(Debug)]
74struct NegateS<O> {
75 arg: Ref<O>,
76 out: Ref<O>,
77 _marker: PhantomData<O>,
78}
79impl<O> MechFunctionFactory for NegateS<O>
80where
81 O: Copy + Debug + Clone + Sync + Send + Neg<Output = O> + ClosedNeg + PartialEq + 'static +
82 CompileConst + ConstElem + AsValueKind,
83 Ref<O>: ToValue,
84{
85 fn new(args: FunctionArgs) -> MResult<Box<dyn MechFunction>> {
86 match args {
87 FunctionArgs::Unary(out, arg) => {
88 let arg: Ref<O> = unsafe { arg.as_unchecked() }.clone();
89 let out: Ref<O> = unsafe { out.as_unchecked() }.clone();
90 Ok(Box::new(Self {arg, out, _marker: PhantomData }))
91 },
92 _ => Err(MechError2::new(
93 IncorrectNumberOfArguments { expected: 1, found: args.len() },
94 None
95 ).with_compiler_loc()
96 ),
97 }
98 }
99}
100impl<O> MechFunctionImpl for NegateS<O>
101where
102 O: Copy + Debug + Clone + Sync + Send + Neg<Output = O> + ClosedNeg + PartialEq + 'static,
103 Ref<O>: ToValue,
104{
105 fn solve(&self) {
106 let arg_ptr = self.arg.as_ptr();
107 let out_ptr = self.out.as_mut_ptr();
108 unsafe { *out_ptr = -*arg_ptr; }
109 }
110 fn out(&self) -> Value { self.out.to_value() }
111 fn to_string(&self) -> String { format!("{:#?}", self) }
112}
113#[cfg(feature = "compiler")]
114impl<O> MechFunctionCompiler for NegateS<O>
115where
116 O: CompileConst + ConstElem + AsValueKind,
117{
118 fn compile(&self, ctx: &mut CompileCtx) -> MResult<Register> {
119 let name = format!("NegateS<{}>", O::as_value_kind());
120 compile_unop!(name, self.out, self.arg, ctx, FeatureFlag::Builtin(FeatureKind::Neg) );
121 }
122}
123
124register_fxn_descriptor!(NegateS,
125 i8, "i8",
126 i16, "i16",
127 i32, "i32",
128 i64, "i64",
129 i128, "i128",
130 f32, "f32",
131 f64, "f64",
132 R64, "r64",
133 C64, "c64"
134);
135
136fn impl_neg_fxn(lhs_value: Value) -> MResult<Box<dyn MechFunction>> {
137 impl_urnop_match_arms!(
138 Negate,
139 (lhs_value),
140 I8, i8, "i8";
141 I16, i16, "i16";
142 I32, i32, "i32";
143 I64, i64, "i64";
144 I128, i128, "i128";
145 F32, f32, "f32";
146 F64, f64, "f64";
147 R64, R64, "rational";
148 C64, C64, "complex";
149 )
150}
151
152impl_mech_urnop_fxn!(MathNegate,impl_neg_fxn,"math/neg");