Skip to main content

singe_npp/signal/
initialization.rs

1use singe_cuda::types::{Complex32, Complex64};
2use singe_npp_sys as sys;
3
4use crate::{
5    context::StreamContext,
6    error::Result,
7    signal::view::{SignalView, SignalViewMut},
8    try_ffi,
9    types::{ComplexI16, ComplexI32, ComplexI64, DataTypeLike, IntoNpp},
10    utility::{to_u64, validate_same_len},
11};
12
13macro_rules! impl_set {
14    ($name:ident, $ty:ty, $ffi:ident) => {
15        pub fn $name(
16            stream_context: &StreamContext,
17            value: $ty,
18            destination: &mut SignalViewMut<'_, $ty>,
19        ) -> Result<()> {
20            unsafe {
21                try_ffi!(sys::$ffi(
22                    value.into_npp(),
23                    destination.as_mut_ptr().cast(),
24                    to_u64(destination.len(), "len")?,
25                    stream_context.as_raw(),
26                ))?;
27            }
28            Ok(())
29        }
30    };
31}
32
33macro_rules! impl_zero {
34    ($name:ident, $ty:ty, $ffi:ident) => {
35        pub fn $name(
36            stream_context: &StreamContext,
37            destination: &mut SignalViewMut<'_, $ty>,
38        ) -> Result<()> {
39            unsafe {
40                try_ffi!(sys::$ffi(
41                    destination.as_mut_ptr().cast(),
42                    to_u64(destination.len(), "len")?,
43                    stream_context.as_raw(),
44                ))?;
45            }
46            Ok(())
47        }
48    };
49}
50
51macro_rules! impl_copy {
52    ($name:ident, $ty:ty, $ffi:ident) => {
53        pub fn $name(
54            stream_context: &StreamContext,
55            source: &SignalView<'_, $ty>,
56            destination: &mut SignalViewMut<'_, $ty>,
57        ) -> Result<()> {
58            validate_same_len(source.len(), destination.len(), "destination")?;
59
60            unsafe {
61                try_ffi!(sys::$ffi(
62                    source.as_ptr().cast(),
63                    destination.as_mut_ptr().cast(),
64                    to_u64(source.len(), "len")?,
65                    stream_context.as_raw(),
66                ))?;
67            }
68            Ok(())
69        }
70    };
71}
72
73impl_set!(set_u8, u8, nppsSet_8u_Ctx);
74impl_set!(set_i8, i8, nppsSet_8s_Ctx);
75impl_set!(set_u16, u16, nppsSet_16u_Ctx);
76impl_set!(set_i16, i16, nppsSet_16s_Ctx);
77impl_set!(set_u32, u32, nppsSet_32u_Ctx);
78impl_set!(set_i32, i32, nppsSet_32s_Ctx);
79impl_set!(set_i64, i64, nppsSet_64s_Ctx);
80impl_set!(set_f32, f32, nppsSet_32f_Ctx);
81impl_set!(set_f64, f64, nppsSet_64f_Ctx);
82
83impl_set!(set_i16_complex, ComplexI16, nppsSet_16sc_Ctx);
84impl_set!(set_i32_complex, ComplexI32, nppsSet_32sc_Ctx);
85impl_set!(set_i64_complex, ComplexI64, nppsSet_64sc_Ctx);
86impl_set!(set_f32_complex, Complex32, nppsSet_32fc_Ctx);
87impl_set!(set_f64_complex, Complex64, nppsSet_64fc_Ctx);
88
89impl_zero!(zero_u8, u8, nppsZero_8u_Ctx);
90impl_zero!(zero_i16, i16, nppsZero_16s_Ctx);
91impl_zero!(zero_i32, i32, nppsZero_32s_Ctx);
92impl_zero!(zero_i64, i64, nppsZero_64s_Ctx);
93impl_zero!(zero_f32, f32, nppsZero_32f_Ctx);
94impl_zero!(zero_f64, f64, nppsZero_64f_Ctx);
95
96impl_zero!(zero_i16_complex, ComplexI16, nppsZero_16sc_Ctx);
97impl_zero!(zero_i32_complex, ComplexI32, nppsZero_32sc_Ctx);
98impl_zero!(zero_i64_complex, ComplexI64, nppsZero_64sc_Ctx);
99impl_zero!(zero_f32_complex, Complex32, nppsZero_32fc_Ctx);
100impl_zero!(zero_f64_complex, Complex64, nppsZero_64fc_Ctx);
101
102impl_copy!(copy_u8, u8, nppsCopy_8u_Ctx);
103impl_copy!(copy_i16, i16, nppsCopy_16s_Ctx);
104impl_copy!(copy_i32, i32, nppsCopy_32s_Ctx);
105impl_copy!(copy_i64, i64, nppsCopy_64s_Ctx);
106impl_copy!(copy_f32, f32, nppsCopy_32f_Ctx);
107
108impl_copy!(copy_i16_complex, ComplexI16, nppsCopy_16sc_Ctx);
109impl_copy!(copy_i32_complex, ComplexI32, nppsCopy_32sc_Ctx);
110impl_copy!(copy_i64_complex, ComplexI64, nppsCopy_64sc_Ctx);
111impl_copy!(copy_f32_complex, Complex32, nppsCopy_32fc_Ctx);
112impl_copy!(copy_f64_complex, Complex64, nppsCopy_64fc_Ctx);
113
114macro_rules! impl_signal_set_dispatch {
115    ($trait:ident, $method:ident, $function:ident, [$($ty:ty => $direct:ident),* $(,)?]) => {
116        pub trait $trait: DataTypeLike {
117            fn $method(
118                stream_context: &StreamContext,
119                value: Self,
120                destination: &mut SignalViewMut<'_, Self>,
121            ) -> Result<()>;
122        }
123
124        $(
125            impl $trait for $ty {
126                fn $method(
127                    stream_context: &StreamContext,
128                    value: Self,
129                    destination: &mut SignalViewMut<'_, Self>,
130                ) -> Result<()> {
131                    $direct(stream_context, value, destination)
132                }
133            }
134        )*
135
136        pub fn $function<T: $trait>(
137            stream_context: &StreamContext,
138            value: T,
139            destination: &mut SignalViewMut<'_, T>,
140        ) -> Result<()> {
141            T::$method(stream_context, value, destination)
142        }
143    };
144}
145
146macro_rules! impl_signal_zero_dispatch {
147    ($trait:ident, $method:ident, $function:ident, [$($ty:ty => $direct:ident),* $(,)?]) => {
148        pub trait $trait: DataTypeLike {
149            fn $method(
150                stream_context: &StreamContext,
151                destination: &mut SignalViewMut<'_, Self>,
152            ) -> Result<()>;
153        }
154
155        $(
156            impl $trait for $ty {
157                fn $method(
158                    stream_context: &StreamContext,
159                    destination: &mut SignalViewMut<'_, Self>,
160                ) -> Result<()> {
161                    $direct(stream_context, destination)
162                }
163            }
164        )*
165
166        pub fn $function<T: $trait>(
167            stream_context: &StreamContext,
168            destination: &mut SignalViewMut<'_, T>,
169        ) -> Result<()> {
170            T::$method(stream_context, destination)
171        }
172    };
173}
174
175macro_rules! impl_signal_copy_dispatch {
176    ($trait:ident, $method:ident, $function:ident, [$($ty:ty => $direct:ident),* $(,)?]) => {
177        pub trait $trait: DataTypeLike {
178            fn $method(
179                stream_context: &StreamContext,
180                source: &SignalView<'_, Self>,
181                destination: &mut SignalViewMut<'_, Self>,
182            ) -> Result<()>;
183        }
184
185        $(
186            impl $trait for $ty {
187                fn $method(
188                    stream_context: &StreamContext,
189                    source: &SignalView<'_, Self>,
190                    destination: &mut SignalViewMut<'_, Self>,
191                ) -> Result<()> {
192                    $direct(stream_context, source, destination)
193                }
194            }
195        )*
196
197        pub fn $function<T: $trait>(
198            stream_context: &StreamContext,
199            source: &SignalView<'_, T>,
200            destination: &mut SignalViewMut<'_, T>,
201        ) -> Result<()> {
202            T::$method(stream_context, source, destination)
203        }
204    };
205}
206
207impl_signal_set_dispatch!(Set, set, set, [
208    u8 => set_u8,
209    i8 => set_i8,
210    u16 => set_u16,
211    i16 => set_i16,
212    u32 => set_u32,
213    i32 => set_i32,
214    i64 => set_i64,
215    f32 => set_f32,
216    f64 => set_f64,
217    ComplexI16 => set_i16_complex,
218    ComplexI32 => set_i32_complex,
219    ComplexI64 => set_i64_complex,
220    Complex32 => set_f32_complex,
221    Complex64 => set_f64_complex
222]);
223
224impl_signal_zero_dispatch!(Zero, zero, zero, [
225    u8 => zero_u8,
226    i16 => zero_i16,
227    i32 => zero_i32,
228    i64 => zero_i64,
229    f32 => zero_f32,
230    f64 => zero_f64,
231    ComplexI16 => zero_i16_complex,
232    ComplexI32 => zero_i32_complex,
233    ComplexI64 => zero_i64_complex,
234    Complex32 => zero_f32_complex,
235    Complex64 => zero_f64_complex
236]);
237
238impl_signal_copy_dispatch!(Copy, copy, copy, [
239    u8 => copy_u8,
240    i16 => copy_i16,
241    i32 => copy_i32,
242    i64 => copy_i64,
243    f32 => copy_f32,
244    ComplexI16 => copy_i16_complex,
245    ComplexI32 => copy_i32_complex,
246    ComplexI64 => copy_i64_complex,
247    Complex32 => copy_f32_complex,
248    Complex64 => copy_f64_complex
249]);