mech_compare/
lib.rs

1#![no_main]
2#![allow(warnings)]
3#[macro_use]
4extern crate mech_core;
5extern crate paste;
6
7use mech_core::*;
8
9#[cfg(feature = "vector3")]
10use nalgebra::Vector3;
11#[cfg(feature = "vectord")]
12use nalgebra::DVector;
13#[cfg(feature = "vector2")]
14use nalgebra::Vector2;
15#[cfg(feature = "vector4")]
16use nalgebra::Vector4;
17#[cfg(feature = "rowdvector")]
18use nalgebra::RowDVector;
19#[cfg(feature = "row_vectord")]
20use nalgebra::RowDVector;
21#[cfg(feature = "matrix1")]
22use nalgebra::Matrix1;
23#[cfg(feature = "matrix3")]
24use nalgebra::Matrix3;
25#[cfg(feature = "matrix4")]
26use nalgebra::Matrix4;
27#[cfg(feature = "row_vector3")]
28use nalgebra::RowVector3;
29#[cfg(feature = "row_vector4")]
30use nalgebra::RowVector4;
31#[cfg(feature = "row_vector2")]
32use nalgebra::RowVector2;
33#[cfg(feature = "matrixd")]
34use nalgebra::DMatrix;
35#[cfg(feature = "matrix2x3")]
36use nalgebra::Matrix2x3;
37#[cfg(feature = "matrix3x2")]
38use nalgebra::Matrix3x2;
39#[cfg(feature = "matrix2")]
40use nalgebra::Matrix2;
41
42use paste::paste;
43
44#[cfg(feature = "gt")]
45pub mod gt;
46#[cfg(feature = "lt")]
47pub mod lt;
48#[cfg(feature = "lte")]
49pub mod lte;
50#[cfg(feature = "gte")]
51pub mod gte;
52#[cfg(feature = "eq")]
53pub mod eq;
54#[cfg(feature = "neq")]
55pub mod neq;
56#[cfg(feature = "min")]
57pub mod min;
58#[cfg(feature = "max")]
59pub mod max;
60
61#[cfg(feature = "gt")]
62pub use self::gt::*;
63#[cfg(feature = "lt")]
64pub use self::lt::*;
65#[cfg(feature = "lte")]
66pub use self::lte::*;
67#[cfg(feature = "gte")]
68pub use self::gte::*;
69#[cfg(feature = "eq")]
70pub use self::eq::*;
71#[cfg(feature = "neq")]
72pub use self::neq::*;
73#[cfg(feature = "min")]
74pub use self::min::*;
75#[cfg(feature = "max")]
76pub use self::max::*;
77
78// ----------------------------------------------------------------------------
79// Compare Library
80// ----------------------------------------------------------------------------
81
82#[macro_export]
83macro_rules! impl_compare_binop {
84  ($struct_name:ident, $arg1_type:ty, $arg2_type:ty, $out_type:ty, $op:ident, $feature_flag:expr) => {
85    #[derive(Debug)]
86    struct $struct_name<T> {
87      lhs: Ref<$arg1_type>,
88      rhs: Ref<$arg2_type>,
89      out: Ref<$out_type>,
90    }
91    impl<T> MechFunctionFactory for $struct_name<T>
92    where
93      T: std::fmt::Debug + Clone + Sync + Send + 'static + 
94      ConstElem + CompileConst + AsValueKind +
95      PartialEq + PartialOrd,
96      Ref<$out_type>: ToValue
97    {
98      fn new(args: FunctionArgs) -> MResult<Box<dyn MechFunction>> {
99        match args {
100          FunctionArgs::Binary(out, arg1, arg2) => {
101            let lhs: Ref<$arg1_type> = unsafe { arg1.as_unchecked() }.clone();
102            let rhs: Ref<$arg2_type> = unsafe { arg2.as_unchecked() }.clone();
103            let out: Ref<$out_type> = unsafe { out.as_unchecked() }.clone();
104            Ok(Box::new(Self {lhs, rhs, out }))
105          },
106          _ => Err(MechError2::new(
107              IncorrectNumberOfArguments { expected: 2, found: args.len() }, 
108              None
109            ).with_compiler_loc()
110          ),
111        }
112      }
113    }
114    impl<T> MechFunctionImpl for $struct_name<T>
115    where
116      T: std::fmt::Debug + Clone + Sync + Send + 'static + 
117      PartialEq + PartialOrd,
118      Ref<$out_type>: ToValue
119    {
120    fn solve(&self) {
121      let lhs_ptr = self.lhs.as_ptr();
122      let rhs_ptr = self.rhs.as_ptr();
123      let out_ptr = self.out.as_mut_ptr();
124      $op!(lhs_ptr,rhs_ptr,out_ptr);
125    }
126    fn out(&self) -> Value { self.out.to_value() }
127    fn to_string(&self) -> String { format!("{:#?}", self) }
128  }
129  #[cfg(feature = "compiler")]
130  impl<T> MechFunctionCompiler for $struct_name<T> 
131  where
132    T: ConstElem + CompileConst + AsValueKind
133  {
134    fn compile(&self, ctx: &mut CompileCtx) -> MResult<Register> {
135      let name = format!("{}<{}>", stringify!($struct_name), T::as_value_kind());
136      compile_binop!(name, self.out, self.lhs, self.rhs, ctx, $feature_flag);
137    }
138  }
139  register_fxn_descriptor!($struct_name, bool, "bool", String, "string", u8, "u8", i8, "i8", u16, "u16", i16, "i16", u32, "u32", i32, "i32", u64, "u64", i64, "i64", u128, "u128", i128, "i128", f32, "f32", f64, "f64", R64, "r64", C64, "c64");
140};}
141
142#[macro_export]
143macro_rules! impl_compare_fxns {
144  ($lib:ident) => {
145    impl_fxns!($lib,T,bool,impl_compare_binop);
146  }
147}
148
149#[macro_export]
150macro_rules! impl_compare_fxns2 {
151  ($lib:ident) => {
152    impl_fxns!($lib,T,T,impl_compare_binop);
153  }
154}