tract_linalg/generic/
unicast.rs

1pub use tract_data::internal::f16;
2unicast_impl_wrap!(
3    f32,
4    SUnicastMul4,
5    4,
6    4,
7    fn run(a: &mut [f32], b: &[f32]) {
8        debug_assert!(a.len() == b.len());
9        debug_assert!(a.len() % Self::nr() == 0);
10        debug_assert!(a.as_ptr() as usize % Self::alignment_bytes() == 0);
11        debug_assert!(b.as_ptr() as usize % Self::alignment_bytes() == 0);
12        a.iter_mut().zip(b.iter()).for_each(|(a, b)| *a *= b)
13    }
14);
15
16unicast_impl_wrap!(
17    f16,
18    HUnicastMul8,
19    8,
20    8,
21    fn run(a: &mut [f16], b: &[f16]) {
22        debug_assert!(a.len() == b.len());
23        debug_assert!(a.len() % Self::nr() == 0);
24        debug_assert!(a.as_ptr() as usize % Self::alignment_bytes() == 0);
25        debug_assert!(b.as_ptr() as usize % Self::alignment_bytes() == 0);
26        a.iter_mut().zip(b.iter()).for_each(|(a, b)| *a *= b)
27    }
28);
29
30unicast_impl_wrap!(
31    f32,
32    SUnicastAdd4,
33    4,
34    4,
35    fn run(a: &mut [f32], b: &[f32]) {
36        debug_assert!(a.len() == b.len());
37        debug_assert!(a.len() % Self::nr() == 0);
38        debug_assert!(a.as_ptr() as usize % Self::alignment_bytes() == 0);
39        debug_assert!(b.as_ptr() as usize % Self::alignment_bytes() == 0);
40        a.iter_mut().zip(b.iter()).for_each(|(a, b)| *a += b)
41    }
42);
43
44unicast_impl_wrap!(
45    f16,
46    HUnicastAdd8,
47    8,
48    8,
49    fn run(a: &mut [f16], b: &[f16]) {
50        debug_assert!(a.len() == b.len());
51        debug_assert!(a.len() % Self::nr() == 0);
52        debug_assert!(a.as_ptr() as usize % Self::alignment_bytes() == 0);
53        debug_assert!(b.as_ptr() as usize % Self::alignment_bytes() == 0);
54        a.iter_mut().zip(b.iter()).for_each(|(a, b)| *a += b)
55    }
56);
57
58unicast_impl_wrap!(
59    f32,
60    SUnicastSub4,
61    4,
62    4,
63    fn run(a: &mut [f32], b: &[f32]) {
64        debug_assert!(a.len() == b.len());
65        debug_assert!(a.len() % Self::nr() == 0);
66        debug_assert!(a.as_ptr() as usize % Self::alignment_bytes() == 0);
67        debug_assert!(b.as_ptr() as usize % Self::alignment_bytes() == 0);
68        a.iter_mut().zip(b.iter()).for_each(|(a, b)| *a -= b)
69    }
70);
71
72unicast_impl_wrap!(
73    f16,
74    HUnicastSub8,
75    8,
76    8,
77    fn run(a: &mut [f16], b: &[f16]) {
78        debug_assert!(a.len() == b.len());
79        debug_assert!(a.len() % Self::nr() == 0);
80        debug_assert!(a.as_ptr() as usize % Self::alignment_bytes() == 0);
81        debug_assert!(b.as_ptr() as usize % Self::alignment_bytes() == 0);
82        a.iter_mut().zip(b.iter()).for_each(|(a, b)| *a -= b)
83    }
84);
85
86unicast_impl_wrap!(
87    f32,
88    SUnicastSubF4,
89    4,
90    4,
91    fn run(a: &mut [f32], b: &[f32]) {
92        debug_assert!(a.len() == b.len());
93        debug_assert!(a.len() % Self::nr() == 0);
94        debug_assert!(a.as_ptr() as usize % Self::alignment_bytes() == 0);
95        debug_assert!(b.as_ptr() as usize % Self::alignment_bytes() == 0);
96        a.iter_mut().zip(b.iter()).for_each(|(a, b)| *a = *b - *a)
97    }
98);
99
100unicast_impl_wrap!(
101    f16,
102    HUnicastSubF8,
103    8,
104    8,
105    fn run(a: &mut [f16], b: &[f16]) {
106        debug_assert!(a.len() == b.len());
107        debug_assert!(a.len() % Self::nr() == 0);
108        debug_assert!(a.as_ptr() as usize % Self::alignment_bytes() == 0);
109        debug_assert!(b.as_ptr() as usize % Self::alignment_bytes() == 0);
110        a.iter_mut().zip(b.iter()).for_each(|(a, b)| *a = *b - *a)
111    }
112);
113
114unicast_impl_wrap!(
115    f32,
116    SUnicastMin4,
117    4,
118    4,
119    fn run(a: &mut [f32], b: &[f32]) {
120        debug_assert!(a.len() == b.len());
121        debug_assert!(a.len() % Self::nr() == 0);
122        debug_assert!(a.as_ptr() as usize % Self::alignment_bytes() == 0);
123        debug_assert!(b.as_ptr() as usize % Self::alignment_bytes() == 0);
124        a.iter_mut().zip(b.iter()).for_each(|(a, b)| *a = a.min(*b))
125    }
126);
127
128unicast_impl_wrap!(
129    f16,
130    HUnicastMin8,
131    8,
132    8,
133    fn run(a: &mut [f16], b: &[f16]) {
134        debug_assert!(a.len() == b.len());
135        debug_assert!(a.len() % Self::nr() == 0);
136        debug_assert!(a.as_ptr() as usize % Self::alignment_bytes() == 0);
137        debug_assert!(b.as_ptr() as usize % Self::alignment_bytes() == 0);
138        a.iter_mut().zip(b.iter()).for_each(|(a, b)| *a = a.min(*b))
139    }
140);
141
142unicast_impl_wrap!(
143    f32,
144    SUnicastMax4,
145    4,
146    4,
147    fn run(a: &mut [f32], b: &[f32]) {
148        debug_assert!(a.len() == b.len());
149        debug_assert!(a.len() % Self::nr() == 0);
150        debug_assert!(a.as_ptr() as usize % Self::alignment_bytes() == 0);
151        debug_assert!(b.as_ptr() as usize % Self::alignment_bytes() == 0);
152        a.iter_mut().zip(b.iter()).for_each(|(a, b)| *a = a.max(*b))
153    }
154);
155
156unicast_impl_wrap!(
157    f16,
158    HUnicastMax8,
159    8,
160    8,
161    fn run(a: &mut [f16], b: &[f16]) {
162        debug_assert!(a.len() == b.len());
163        debug_assert!(a.len() % Self::nr() == 0);
164        debug_assert!(a.as_ptr() as usize % Self::alignment_bytes() == 0);
165        debug_assert!(b.as_ptr() as usize % Self::alignment_bytes() == 0);
166        a.iter_mut().zip(b.iter()).for_each(|(a, b)| *a = a.max(*b))
167    }
168);
169
170#[cfg(test)]
171#[macro_use]
172pub mod s {
173    use super::*;
174    use proptest::strategy::Strategy;
175    crate::unicast_frame_tests!(true, f32, SUnicastMul4, |a, b| a * b);
176    crate::unicast_frame_tests!(true, f32, SUnicastAdd4, |a, b| a + b);
177    crate::unicast_frame_tests!(true, f32, SUnicastSub4, |a, b| a - b);
178    crate::unicast_frame_tests!(true, f32, SUnicastSubF4, |a, b| b - a);
179    crate::unicast_frame_tests!(true, f32, SUnicastMin4, |a, b| a.min(b));
180    crate::unicast_frame_tests!(true, f32, SUnicastMax4, |a, b| a.max(b));
181}
182
183#[cfg(test)]
184#[macro_use]
185pub mod h {
186    use super::*;
187    use proptest::strategy::Strategy;
188    crate::unicast_frame_tests!(true, f16, HUnicastMul8, |a, b| a * b);
189    crate::unicast_frame_tests!(true, f16, HUnicastAdd8, |a, b| a + b);
190    crate::unicast_frame_tests!(true, f16, HUnicastSub8, |a, b| a - b);
191    crate::unicast_frame_tests!(true, f16, HUnicastSubF8, |a, b| b - a);
192    crate::unicast_frame_tests!(true, f16, HUnicastMin8, |a, b| a.min(b));
193    crate::unicast_frame_tests!(true, f16, HUnicastMax8, |a, b| a.max(b));
194}