Debian Codegraph @

debian/missing-sources/moxcms/src/conversions/neon/lut4_to_3_q0_15.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::neon::interpolator_q0_15::*;
33 use crate::transform::PointeeSizeExpressible;
34 use crate::{CmsError, DataColorSpace, InterpolationMethod, Layout, TransformExecutor};
35 use num_traits::AsPrimitive;
36 use std::arch::aarch64::*;
37 use std::marker::PhantomData;
38
39 pub(crate) struct TransformLut4To3NeonQ0_15<
40 T,
41 U,
42 const LAYOUT: u8,
43 const GRID_SIZE: usize,
44 const BIT_DEPTH: usize,
45 const BINS: usize,
46 const BARYCENTRIC_BINS: usize,
47 > {
48 pub(crate) lut: Vec<NeonAlignedI16x4>,
49 pub(crate) _phantom: PhantomData<T>,
50 pub(crate) _phantom1: PhantomData<U>,
51 pub(crate) interpolation_method: InterpolationMethod,
52 pub(crate) weights: Box<[BarycentricWeight<i16>; BINS]>,
53 pub(crate) color_space: DataColorSpace,
54 pub(crate) is_linear: bool,
55 }
56
57 impl<
58 T: Copy + AsPrimitive<f32> + Default + PointeeSizeExpressible,
59 U: AsPrimitive<usize>,
60 const LAYOUT: u8,
61 const GRID_SIZE: usize,
62 const BIT_DEPTH: usize,
63 const BINS: usize,
64 const BARYCENTRIC_BINS: usize,
65 > TransformLut4To3NeonQ0_15<T, U, LAYOUT, GRID_SIZE, BIT_DEPTH, BINS, BARYCENTRIC_BINS>
66 where
67 f32: AsPrimitive<T>,
68 u32: AsPrimitive<T>,
69 (): LutBarycentricReduction<T, U>,
70 {
71 #[allow(unused_unsafe)]
72 #[target_feature(enable = "rdm")]
73 #[inline(never)]
74 unsafe fn transform_chunk(
75 &self,
76 src: &[T],
77 dst: &mut [T],
78 interpolator: Box<dyn NeonMdInterpolationQ0_15Double + Send + Sync>,
79 ) {
80 unsafe {
81 let cn = Layout::from(LAYOUT);
82 let channels = cn.channels();
83 let grid_size = GRID_SIZE as i32;
84 let grid_size3 = grid_size * grid_size * grid_size;
85
86 let f_value_scale = vdupq_n_f32(1. / ((1 << 14i32) - 1) as f32);
87 let max_value = ((1u32 << BIT_DEPTH) - 1).as_();
88 let v_max_scale = if T::FINITE {
89 vdup_n_s16(((1i32 << BIT_DEPTH) - 1) as i16)
90 } else {
91 vdup_n_s16(((1i32 << 14i32) - 1) as i16)
92 };
93
94 for (src, dst) in src.chunks_exact(4).zip(dst.chunks_exact_mut(channels)) {
95 let c = <() as LutBarycentricReduction<T, U>>::reduce::<BIT_DEPTH, BARYCENTRIC_BINS>(
96 src[0],
97 );
98 let m = <() as LutBarycentricReduction<T, U>>::reduce::<BIT_DEPTH, BARYCENTRIC_BINS>(
99 src[1],
100 );
101 let y = <() as LutBarycentricReduction<T, U>>::reduce::<BIT_DEPTH, BARYCENTRIC_BINS>(
102 src[2],
103 );
104 let k = <() as LutBarycentricReduction<T, U>>::reduce::<BIT_DEPTH, BARYCENTRIC_BINS>(
105 src[3],
106 );
107
108 let k_weights = self.weights[k.as_()];
109
110 let w: i32 = k_weights.x;
111 let w_n: i32 = k_weights.x_n;
112 let t: i16 = k_weights.w;
113
114 let table1 = &self.lut[(w * grid_size3) as usize..];
115 let table2 = &self.lut[(w_n * grid_size3) as usize..];
116
117 let (a0, b0) = interpolator.inter3_neon(
118 table1,
119 table2,
120 c.as_(),
121 m.as_(),
122 y.as_(),
123 self.weights.as_slice(),
124 );
125 let (a0, b0) = (a0.v, b0.v);
126
127 let t0 = vdup_n_s16(t);
128 let hp = vqrdmlsh_s16(a0, a0, t0);
129 let mut v = vqrdmlah_s16(hp, b0, t0);
130
131 if T::FINITE {
132 v = vmax_s16(v, vdup_n_s16(0));
133 v = vmin_s16(v, v_max_scale);
134 dst[cn.r_i()] = (vget_lane_s16::<0>(v) as u32).as_();
135 dst[cn.g_i()] = (vget_lane_s16::<1>(v) as u32).as_();
136 dst[cn.b_i()] = (vget_lane_s16::<2>(v) as u32).as_();
137 } else {
138 let o = vcvtq_f32_s32(vmovl_s16(v));
139 let r = vmulq_f32(o, f_value_scale);
140 dst[cn.r_i()] = vgetq_lane_f32::<0>(r).as_();
141 dst[cn.g_i()] = vgetq_lane_f32::<1>(r).as_();
142 dst[cn.b_i()] = vgetq_lane_f32::<2>(r).as_();
143 }
144 if channels == 4 {
145 dst[cn.a_i()] = max_value;
146 }
147 }
148 }
149 }
150 }
151
152 impl<
153 T: Copy + AsPrimitive<f32> + Default + PointeeSizeExpressible,
154 U: AsPrimitive<usize>,
155 const LAYOUT: u8,
156 const GRID_SIZE: usize,
157 const BIT_DEPTH: usize,
158 const BINS: usize,
159 const BARYCENTRIC_BINS: usize,
160 > TransformExecutor<T>
161 for TransformLut4To3NeonQ0_15<T, U, LAYOUT, GRID_SIZE, BIT_DEPTH, BINS, BARYCENTRIC_BINS>
162 where
163 f32: AsPrimitive<T>,
164 u32: AsPrimitive<T>,
165 (): LutBarycentricReduction<T, U>,
166 {
167 fn transform(&self, src: &[T], dst: &mut [T]) -> Result<(), CmsError> {
168 let cn = Layout::from(LAYOUT);
169 let channels = cn.channels();
170 if src.len() % 4 != 0 {
171 return Err(CmsError::LaneMultipleOfChannels);
172 }
173 if dst.len() % channels != 0 {
174 return Err(CmsError::LaneMultipleOfChannels);
175 }
176 let src_chunks = src.len() / 4;
177 let dst_chunks = dst.len() / channels;
178 if src_chunks != dst_chunks {
179 return Err(CmsError::LaneSizeMismatch);
180 }
181
182 unsafe {
183 if self.color_space == DataColorSpace::Lab
184 || (self.is_linear && self.color_space == DataColorSpace::Rgb)
185 || self.color_space == DataColorSpace::Xyz
186 {
187 self.transform_chunk(src, dst, Box::new(TrilinearNeonQ0_15Double::<GRID_SIZE> {}));
188 } else {
189 match self.interpolation_method {
190 #[cfg(feature = "options")]
191 InterpolationMethod::Tetrahedral => {
192 self.transform_chunk(
193 src,
194 dst,
195 Box::new(TetrahedralNeonQ0_15Double::<GRID_SIZE> {}),
196 );
197 }
198 #[cfg(feature = "options")]
199 InterpolationMethod::Pyramid => {
200 self.transform_chunk(
201 src,
202 dst,
203 Box::new(PyramidalNeonQ0_15Double::<GRID_SIZE> {}),
204 );
205 }
206 #[cfg(feature = "options")]
207 InterpolationMethod::Prism => {
208 self.transform_chunk(
209 src,
210 dst,
211 Box::new(PrismaticNeonQ0_15Double::<GRID_SIZE> {}),
212 );
213 }
214 InterpolationMethod::Linear => {
215 self.transform_chunk(
216 src,
217 dst,
218 Box::new(TrilinearNeonQ0_15Double::<GRID_SIZE> {}),
219 );
220 }
221 }
222 }
223 }
224
225 Ok(())
226 }
227 }