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