Skip to main content

singe_npp/signal/
arithmetic_unary_ops.rs

1use super::*;
2
3macro_rules! impl_signal_unary_dispatch {
4    ($trait:ident, $method:ident, $function:ident, [$($ty:ty => $direct:ident),* $(,)?]) => {
5        pub trait $trait: DataTypeLike {
6            fn $method(
7                stream_context: &StreamContext,
8                source: &SignalView<'_, Self>,
9                destination: &mut SignalViewMut<'_, Self>,
10            ) -> Result<()>;
11        }
12
13        $(
14            impl $trait for $ty {
15                fn $method(
16                    stream_context: &StreamContext,
17                    source: &SignalView<'_, Self>,
18                    destination: &mut SignalViewMut<'_, Self>,
19                ) -> Result<()> {
20                    $direct(stream_context, source, destination)
21                }
22            }
23        )*
24
25        pub fn $function<T: $trait>(
26            stream_context: &StreamContext,
27            source: &SignalView<'_, T>,
28            destination: &mut SignalViewMut<'_, T>,
29        ) -> Result<()> {
30            T::$method(stream_context, source, destination)
31        }
32    };
33}
34
35macro_rules! impl_signal_unary_in_place_dispatch {
36    ($trait:ident, $method:ident, $function:ident, [$($ty:ty => $direct:ident),* $(,)?]) => {
37        pub trait $trait: DataTypeLike {
38            fn $method(
39                stream_context: &StreamContext,
40                signal: &mut SignalViewMut<'_, Self>,
41            ) -> Result<()>;
42        }
43
44        $(
45            impl $trait for $ty {
46                fn $method(
47                    stream_context: &StreamContext,
48                    signal: &mut SignalViewMut<'_, Self>,
49                ) -> Result<()> {
50                    $direct(stream_context, signal)
51                }
52            }
53        )*
54
55        pub fn $function<T: $trait>(
56            stream_context: &StreamContext,
57            signal: &mut SignalViewMut<'_, T>,
58        ) -> Result<()> {
59            T::$method(stream_context, signal)
60        }
61    };
62}
63
64macro_rules! impl_signal_scaled_unary_dispatch {
65    ($trait:ident, $method:ident, $function:ident, [$($ty:ty => $direct:ident),* $(,)?]) => {
66        pub trait $trait: DataTypeLike {
67            fn $method(
68                stream_context: &StreamContext,
69                source: &SignalView<'_, Self>,
70                destination: &mut SignalViewMut<'_, Self>,
71                scale_factor: i32,
72            ) -> Result<()>;
73        }
74
75        $(
76            impl $trait for $ty {
77                fn $method(
78                    stream_context: &StreamContext,
79                    source: &SignalView<'_, Self>,
80                    destination: &mut SignalViewMut<'_, Self>,
81                    scale_factor: i32,
82                ) -> Result<()> {
83                    $direct(stream_context, source, destination, scale_factor)
84                }
85            }
86        )*
87
88        pub fn $function<T: $trait>(
89            stream_context: &StreamContext,
90            source: &SignalView<'_, T>,
91            destination: &mut SignalViewMut<'_, T>,
92            scale_factor: i32,
93        ) -> Result<()> {
94            T::$method(stream_context, source, destination, scale_factor)
95        }
96    };
97}
98
99macro_rules! impl_signal_scaled_unary_in_place_dispatch {
100    ($trait:ident, $method:ident, $function:ident, [$($ty:ty => $direct:ident),* $(,)?]) => {
101        pub trait $trait: DataTypeLike {
102            fn $method(
103                stream_context: &StreamContext,
104                signal: &mut SignalViewMut<'_, Self>,
105                scale_factor: i32,
106            ) -> Result<()>;
107        }
108
109        $(
110            impl $trait for $ty {
111                fn $method(
112                    stream_context: &StreamContext,
113                    signal: &mut SignalViewMut<'_, Self>,
114                    scale_factor: i32,
115                ) -> Result<()> {
116                    $direct(stream_context, signal, scale_factor)
117                }
118            }
119        )*
120
121        pub fn $function<T: $trait>(
122            stream_context: &StreamContext,
123            signal: &mut SignalViewMut<'_, T>,
124            scale_factor: i32,
125        ) -> Result<()> {
126            T::$method(stream_context, signal, scale_factor)
127        }
128    };
129}
130
131impl_signal_unary_dispatch!(Absolute, absolute, absolute, [
132    i16 => absolute_i16,
133    i32 => absolute_i32,
134    f32 => absolute_f32,
135    f64 => absolute_f64
136]);
137impl_signal_unary_dispatch!(Exponent, exponent, exponent, [
138    f32 => exponent_f32,
139    f64 => exponent_f64
140]);
141impl_signal_unary_dispatch!(NaturalLogarithm, natural_logarithm, natural_logarithm, [
142    f32 => natural_logarithm_f32,
143    f64 => natural_logarithm_f64
144]);
145impl_signal_unary_dispatch!(Arctangent, arctangent, arctangent, [
146    f32 => arctangent_f32,
147    f64 => arctangent_f64
148]);
149impl_signal_unary_dispatch!(Not, not, not, [
150    u8 => not_u8,
151    u16 => not_u16,
152    u32 => not_u32
153]);
154impl_signal_unary_dispatch!(Square, square, square, [
155    f32 => square_f32,
156    f64 => square_f64,
157    Complex32 => square_f32_complex,
158    Complex64 => square_f64_complex
159]);
160impl_signal_unary_dispatch!(SquareRoot, square_root, square_root, [
161    f32 => square_root_f32,
162    f64 => square_root_f64,
163    Complex32 => square_root_f32_complex,
164    Complex64 => square_root_f64_complex
165]);
166
167impl_signal_unary_in_place_dispatch!(AbsoluteInPlace, absolute_in_place, absolute_in_place, [
168    i16 => absolute_i16_in_place,
169    i32 => absolute_i32_in_place,
170    f32 => absolute_f32_in_place,
171    f64 => absolute_f64_in_place
172]);
173impl_signal_unary_in_place_dispatch!(ExponentInPlace, exponent_in_place, exponent_in_place, [
174    f32 => exponent_f32_in_place,
175    f64 => exponent_f64_in_place
176]);
177impl_signal_unary_in_place_dispatch!(NaturalLogarithmInPlace, natural_logarithm_in_place, natural_logarithm_in_place, [
178    f32 => natural_logarithm_f32_in_place,
179    f64 => natural_logarithm_f64_in_place
180]);
181impl_signal_unary_in_place_dispatch!(ArctangentInPlace, arctangent_in_place, arctangent_in_place, [
182    f32 => arctangent_f32_in_place,
183    f64 => arctangent_f64_in_place
184]);
185impl_signal_unary_in_place_dispatch!(NotInPlace, not_in_place, not_in_place, [
186    u8 => not_u8_in_place,
187    u16 => not_u16_in_place,
188    u32 => not_u32_in_place
189]);
190impl_signal_unary_in_place_dispatch!(SquareInPlace, square_in_place, square_in_place, [
191    f32 => square_f32_in_place,
192    f64 => square_f64_in_place,
193    Complex32 => square_f32_complex_in_place,
194    Complex64 => square_f64_complex_in_place
195]);
196impl_signal_unary_in_place_dispatch!(SquareRootInPlace, square_root_in_place, square_root_in_place, [
197    f32 => square_root_f32_in_place,
198    f64 => square_root_f64_in_place,
199    Complex32 => square_root_f32_complex_in_place,
200    Complex64 => square_root_f64_complex_in_place
201]);
202
203impl_signal_scaled_unary_dispatch!(ExponentScaled, exponent_scaled, exponent_scaled, [
204    i16 => exponent_i16_scaled,
205    i32 => exponent_i32_scaled,
206    i64 => exponent_i64_scaled
207]);
208impl_signal_scaled_unary_dispatch!(NaturalLogarithmScaled, natural_logarithm_scaled, natural_logarithm_scaled, [
209    i16 => natural_logarithm_i16_scaled,
210    i32 => natural_logarithm_i32_scaled
211]);
212impl_signal_scaled_unary_dispatch!(SquareScaled, square_scaled, square_scaled, [
213    u8 => square_u8_scaled,
214    u16 => square_u16_scaled,
215    i16 => square_i16_scaled,
216    ComplexI16 => square_i16_complex_scaled
217]);
218impl_signal_scaled_unary_dispatch!(SquareRootScaled, square_root_scaled, square_root_scaled, [
219    u8 => square_root_u8_scaled,
220    u16 => square_root_u16_scaled,
221    i16 => square_root_i16_scaled,
222    i64 => square_root_i64_scaled,
223    ComplexI16 => square_root_i16_complex_scaled
224]);
225
226impl_signal_scaled_unary_in_place_dispatch!(ExponentScaledInPlace, exponent_scaled_in_place, exponent_scaled_in_place, [
227    i16 => exponent_i16_scaled_in_place,
228    i32 => exponent_i32_scaled_in_place,
229    i64 => exponent_i64_scaled_in_place
230]);
231impl_signal_scaled_unary_in_place_dispatch!(NaturalLogarithmScaledInPlace, natural_logarithm_scaled_in_place, natural_logarithm_scaled_in_place, [
232    i16 => natural_logarithm_i16_scaled_in_place,
233    i32 => natural_logarithm_i32_scaled_in_place
234]);
235impl_signal_scaled_unary_in_place_dispatch!(SquareScaledInPlace, square_scaled_in_place, square_scaled_in_place, [
236    u8 => square_u8_scaled_in_place,
237    u16 => square_u16_scaled_in_place,
238    i16 => square_i16_scaled_in_place,
239    ComplexI16 => square_i16_complex_scaled_in_place
240]);
241impl_signal_scaled_unary_in_place_dispatch!(SquareRootScaledInPlace, square_root_scaled_in_place, square_root_scaled_in_place, [
242    u8 => square_root_u8_scaled_in_place,
243    u16 => square_root_u16_scaled_in_place,
244    i16 => square_root_i16_scaled_in_place,
245    i64 => square_root_i64_scaled_in_place,
246    ComplexI16 => square_root_i16_complex_scaled_in_place
247]);