Debian Codegraph @

debian/missing-sources/moxcms/src/conversions/neon/lut4_to_3.rs

raw
1 /*
2 * // Copyright (c) Radzivon Bartoshyk 3/2025. All rights reserved.
3 * //
4 * // Redistribution and use in source and binary forms, with or without modification,
5 * // are permitted provided that the following conditions are met:
6 * //
7 * // 1. Redistributions of source code must retain the above copyright notice, this
8 * // list of conditions and the following disclaimer.
9 * //
10 * // 2. Redistributions in binary form must reproduce the above copyright notice,
11 * // this list of conditions and the following disclaimer in the documentation
12 * // and/or other materials provided with the distribution.
13 * //
14 * // 3. Neither the name of the copyright holder nor the names of its
15 * // contributors may be used to endorse or promote products derived from
16 * // this software without specific prior written permission.
17 * //
18 * // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 * // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29 #![cfg(feature = "neon_luts")]
30 use crate::conversions::LutBarycentricReduction;
31 use crate::conversions::interpolator::BarycentricWeight;
32 use crate::conversions::lut_transforms::Lut4x3Factory;
33 use crate::conversions::neon::interpolator::*;
34 use crate::conversions::neon::interpolator_q0_15::NeonAlignedI16x4;
35 use crate::conversions::neon::lut4_to_3_q0_15::TransformLut4To3NeonQ0_15;
36 use crate::conversions::neon::{NeonAlignedF32, assert_barycentric_lut_size_precondition};
37 use crate::transform::PointeeSizeExpressible;
38 use crate::{
39 BarycentricWeightScale, CmsError, DataColorSpace, InterpolationMethod, Layout,
40 TransformExecutor, TransformOptions,
41 };
42 use num_traits::AsPrimitive;
43 use std::arch::aarch64::*;
44 use std::marker::PhantomData;
45 use std::sync::Arc;
46
47 struct TransformLut4To3Neon<
48 T,
49 U,
50 const LAYOUT: u8,
51 const GRID_SIZE: usize,
52 const BIT_DEPTH: usize,
53 const BINS: usize,
54 const BARYCENTRIC_BINS: usize,
55 > {
56 lut: Vec<NeonAlignedF32>,
57 _phantom: PhantomData<T>,
58 _phantom1: PhantomData<U>,
59 interpolation_method: InterpolationMethod,
60 weights: Box<[BarycentricWeight<f32>; BINS]>,
61 color_space: DataColorSpace,
62 is_linear: bool,
63 }
64
65 impl<
66 T: Copy + AsPrimitive<f32> + Default + PointeeSizeExpressible,
67 U: AsPrimitive<usize>,
68 const LAYOUT: u8,
69 const GRID_SIZE: usize,
70 const BIT_DEPTH: usize,
71 const BINS: usize,
72 const BARYCENTRIC_BINS: usize,
73 > TransformLut4To3Neon<T, U, LAYOUT, GRID_SIZE, BIT_DEPTH, BINS, BARYCENTRIC_BINS>
74 where
75 f32: AsPrimitive<T>,
76 u32: AsPrimitive<T>,
77 (): LutBarycentricReduction<T, U>,
78 {
79 #[allow(unused_unsafe)]
80 #[inline(never)]
81 fn transform_chunk(
82 &self,
83 src: &[T],
84 dst: &mut [T],
85 interpolator: Box<dyn NeonMdInterpolationDouble + Send + Sync>,
86 ) {
87 let cn = Layout::from(LAYOUT);
88 let channels = cn.channels();
89 let grid_size = GRID_SIZE as i32;
90 let grid_size3 = grid_size * grid_size * grid_size;
91
92 let value_scale = unsafe { vdupq_n_f32(((1 << BIT_DEPTH) - 1) as f32) };
93 let max_value = ((1 << BIT_DEPTH) - 1u32).as_();
94
95 for (src, dst) in src.chunks_exact(4).zip(dst.chunks_exact_mut(channels)) {
96 let c = <() as LutBarycentricReduction<T, U>>::reduce::<BIT_DEPTH, BARYCENTRIC_BINS>(
97 src[0],
98 );
99 let m = <() as LutBarycentricReduction<T, U>>::reduce::<BIT_DEPTH, BARYCENTRIC_BINS>(
100 src[1],
101 );
102 let y = <() as LutBarycentricReduction<T, U>>::reduce::<BIT_DEPTH, BARYCENTRIC_BINS>(
103 src[2],
104 );
105 let k = <() as LutBarycentricReduction<T, U>>::reduce::<BIT_DEPTH, BARYCENTRIC_BINS>(
106 src[3],
107 );
108
109 let k_weights = self.weights[k.as_()];
110
111 let w: i32 = k_weights.x;
112 let w_n: i32 = k_weights.x_n;
113 let t: f32 = k_weights.w;
114
115 let table1 = &self.lut[(w * grid_size3) as usize..];
116 let table2 = &self.lut[(w_n * grid_size3) as usize..];
117
118 let (a0, b0) = interpolator.inter3_neon(
119 table1,
120 table2,
121 c.as_(),
122 m.as_(),
123 y.as_(),
124 self.weights.as_slice(),
125 );
126 let (a0, b0) = (a0.v, b0.v);
127
128 if T::FINITE {
129 unsafe {
130 let t0 = vdupq_n_f32(t);
131 let hp = vfmsq_f32(a0, a0, t0);
132 let mut v = vfmaq_f32(hp, b0, t0);
133 v = vmulq_f32(v, value_scale);
134 v = vminq_f32(v, value_scale);
135
136 let jvx = vcvtaq_u32_f32(v);
137
138 dst[cn.r_i()] = vgetq_lane_u32::<0>(jvx).as_();
139 dst[cn.g_i()] = vgetq_lane_u32::<1>(jvx).as_();
140 dst[cn.b_i()] = vgetq_lane_u32::<2>(jvx).as_();
141 }
142 } else {
143 unsafe {
144 let t0 = vdupq_n_f32(t);
145 let hp = vfmsq_f32(a0, a0, t0);
146 let v = vfmaq_f32(hp, b0, t0);
147
148 dst[cn.r_i()] = vgetq_lane_f32::<0>(v).as_();
149 dst[cn.g_i()] = vgetq_lane_f32::<1>(v).as_();
150 dst[cn.b_i()] = vgetq_lane_f32::<2>(v).as_();
151 }
152 }
153 if channels == 4 {
154 dst[cn.a_i()] = max_value;
155 }
156 }
157 }
158 }
159
160 impl<
161 T: Copy + AsPrimitive<f32> + Default + PointeeSizeExpressible,
162 U: AsPrimitive<usize>,
163 const LAYOUT: u8,
164 const GRID_SIZE: usize,
165 const BIT_DEPTH: usize,
166 const BINS: usize,
167 const BARYCENTRIC_BINS: usize,
168 > TransformExecutor<T>
169 for TransformLut4To3Neon<T, U, LAYOUT, GRID_SIZE, BIT_DEPTH, BINS, BARYCENTRIC_BINS>
170 where
171 f32: AsPrimitive<T>,
172 u32: AsPrimitive<T>,
173 (): LutBarycentricReduction<T, U>,
174 {
175 fn transform(&self, src: &[T], dst: &mut [T]) -> Result<(), CmsError> {
176 let cn = Layout::from(LAYOUT);
177 let channels = cn.channels();
178 if src.len() % 4 != 0 {
179 return Err(CmsError::LaneMultipleOfChannels);
180 }
181 if dst.len() % channels != 0 {
182 return Err(CmsError::LaneMultipleOfChannels);
183 }
184 let src_chunks = src.len() / 4;
185 let dst_chunks = dst.len() / channels;
186 if src_chunks != dst_chunks {
187 return Err(CmsError::LaneSizeMismatch);
188 }
189
190 if self.color_space == DataColorSpace::Lab
191 || (self.is_linear && self.color_space == DataColorSpace::Rgb)
192 || self.color_space == DataColorSpace::Xyz
193 {
194 self.transform_chunk(src, dst, Box::new(TrilinearNeonDouble::<GRID_SIZE> {}));
195 } else {
196 match self.interpolation_method {
197 #[cfg(feature = "options")]
198 InterpolationMethod::Tetrahedral => {
199 self.transform_chunk(src, dst, Box::new(TetrahedralNeonDouble::<GRID_SIZE> {}));
200 }
201 #[cfg(feature = "options")]
202 InterpolationMethod::Pyramid => {
203 self.transform_chunk(src, dst, Box::new(PyramidalNeonDouble::<GRID_SIZE> {}));
204 }
205 #[cfg(feature = "options")]
206 InterpolationMethod::Prism => {
207 self.transform_chunk(src, dst, Box::new(PrismaticNeonDouble::<GRID_SIZE> {}));
208 }
209 InterpolationMethod::Linear => {
210 self.transform_chunk(src, dst, Box::new(TrilinearNeonDouble::<GRID_SIZE> {}));
211 }
212 }
213 }
214
215 Ok(())
216 }
217 }
218
219 pub(crate) struct NeonLut4x3Factory {}
220
221 impl Lut4x3Factory for NeonLut4x3Factory {
222 fn make_transform_4x3<
223 T: Copy + AsPrimitive<f32> + Default + PointeeSizeExpressible + 'static + Send + Sync,
224 const LAYOUT: u8,
225 const GRID_SIZE: usize,
226 const BIT_DEPTH: usize,
227 >(
228 lut: Vec<f32>,
229 options: TransformOptions,
230 color_space: DataColorSpace,
231 is_linear: bool,
232 ) -> Arc<dyn TransformExecutor<T> + Sync + Send>
233 where
234 f32: AsPrimitive<T>,
235 u32: AsPrimitive<T>,
236 (): LutBarycentricReduction<T, u8>,
237 (): LutBarycentricReduction<T, u16>,
238 {
239 if options.prefer_fixed_point
240 && BIT_DEPTH < 16
241 && std::arch::is_aarch64_feature_detected!("rdm")
242 {
243 let q: f32 = if T::FINITE {
244 ((1i32 << BIT_DEPTH as i32) - 1) as f32
245 } else {
246 ((1i32 << 14i32) - 1) as f32
247 };
248 let lut = lut
249 .chunks_exact(3)
250 .map(|x| {
251 NeonAlignedI16x4([
252 (x[0] * q).round() as i16,
253 (x[1] * q).round() as i16,
254 (x[2] * q).round() as i16,
255 0,
256 ])
257 })
258 .collect::<Vec<_>>();
259 return match options.barycentric_weight_scale {
260 BarycentricWeightScale::Low => {
261 let bins = BarycentricWeight::<i16>::create_ranged_256::<GRID_SIZE>();
262 assert_barycentric_lut_size_precondition::<i16, GRID_SIZE>(bins.as_slice());
263 Arc::new(TransformLut4To3NeonQ0_15::<
264 T,
265 u8,
266 LAYOUT,
267 GRID_SIZE,
268 BIT_DEPTH,
269 256,
270 256,
271 > {
272 lut,
273 _phantom: PhantomData,
274 _phantom1: PhantomData,
275 interpolation_method: options.interpolation_method,
276 weights: bins,
277 color_space,
278 is_linear,
279 })
280 }
281 #[cfg(feature = "options")]
282 BarycentricWeightScale::High => {
283 let bins = BarycentricWeight::<i16>::create_binned::<GRID_SIZE, 65536>();
284 assert_barycentric_lut_size_precondition::<i16, GRID_SIZE>(bins.as_slice());
285 Arc::new(TransformLut4To3NeonQ0_15::<
286 T,
287 u16,
288 LAYOUT,
289 GRID_SIZE,
290 BIT_DEPTH,
291 65536,
292 65536,
293 > {
294 lut,
295 _phantom: PhantomData,
296 _phantom1: PhantomData,
297 interpolation_method: options.interpolation_method,
298 weights: bins,
299 color_space,
300 is_linear,
301 })
302 }
303 };
304 }
305 let lut = lut
306 .chunks_exact(3)
307 .map(|x| NeonAlignedF32([x[0], x[1], x[2], 0f32]))
308 .collect::<Vec<_>>();
309 match options.barycentric_weight_scale {
310 BarycentricWeightScale::Low => {
311 let bins = BarycentricWeight::<f32>::create_ranged_256::<GRID_SIZE>();
312 assert_barycentric_lut_size_precondition::<f32, GRID_SIZE>(bins.as_slice());
313 Arc::new(
314 TransformLut4To3Neon::<T, u8, LAYOUT, GRID_SIZE, BIT_DEPTH, 256, 256> {
315 lut,
316 _phantom: PhantomData,
317 _phantom1: PhantomData,
318 interpolation_method: options.interpolation_method,
319 weights: bins,
320 color_space,
321 is_linear,
322 },
323 )
324 }
325 #[cfg(feature = "options")]
326 BarycentricWeightScale::High => {
327 let bins = BarycentricWeight::<f32>::create_binned::<GRID_SIZE, 65536>();
328 assert_barycentric_lut_size_precondition::<f32, GRID_SIZE>(bins.as_slice());
329 Arc::new(
330 TransformLut4To3Neon::<T, u16, LAYOUT, GRID_SIZE, BIT_DEPTH, 65536, 65536> {
331 lut,
332 _phantom: PhantomData,
333 _phantom1: PhantomData,
334 interpolation_method: options.interpolation_method,
335 weights: bins,
336 color_space,
337 is_linear,
338 },
339 )
340 }
341 }
342 }
343 }