1use crate::conversions::LutBarycentricReduction;
30use crate::conversions::interpolator::BarycentricWeight;
31use crate::conversions::sse::interpolator_q0_15::*;
32use crate::transform::PointeeSizeExpressible;
33use crate::{CmsError, DataColorSpace, InterpolationMethod, Layout, TransformExecutor};
34use num_traits::AsPrimitive;
35#[cfg(target_arch = "x86")]
36use std::arch::x86::*;
37#[cfg(target_arch = "x86_64")]
38use std::arch::x86_64::*;
39use std::marker::PhantomData;
40
41pub(crate) struct TransformLut4To3SseQ0_15<
42 T,
43 U,
44 const LAYOUT: u8,
45 const GRID_SIZE: usize,
46 const BIT_DEPTH: usize,
47 const BINS: usize,
48 const BARYCENTRIC_BINS: usize,
49> {
50 pub(crate) lut: Vec<SseAlignedI16x4>,
51 pub(crate) _phantom: PhantomData<T>,
52 pub(crate) _phantom1: PhantomData<U>,
53 pub(crate) interpolation_method: InterpolationMethod,
54 pub(crate) weights: Box<[BarycentricWeight<i16>; BINS]>,
55 pub(crate) color_space: DataColorSpace,
56 pub(crate) is_linear: bool,
57}
58
59impl<
60 T: Copy + AsPrimitive<f32> + Default + PointeeSizeExpressible,
61 U: AsPrimitive<usize>,
62 const LAYOUT: u8,
63 const GRID_SIZE: usize,
64 const BIT_DEPTH: usize,
65 const BINS: usize,
66 const BARYCENTRIC_BINS: usize,
67> TransformLut4To3SseQ0_15<T, U, LAYOUT, GRID_SIZE, BIT_DEPTH, BINS, BARYCENTRIC_BINS>
68where
69 f32: AsPrimitive<T>,
70 u32: AsPrimitive<T>,
71 (): LutBarycentricReduction<T, U>,
72{
73 #[allow(unused_unsafe)]
74 #[target_feature(enable = "sse4.1")]
75 unsafe fn transform_chunk<'b, Interpolator: SseMdInterpolationQ0_15<'b, GRID_SIZE>>(
76 &'b self,
77 src: &[T],
78 dst: &mut [T],
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 = _mm_set1_ps(1. / ((1 << 14i32) - 1) as f32);
87 let max_value = ((1u32 << BIT_DEPTH) - 1).as_();
88 let v_max_scale = if T::FINITE {
89 _mm_set1_epi16(((1i32 << BIT_DEPTH) - 1) as i16)
90 } else {
91 _mm_set1_epi16(((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 const Q: i16 = ((1i32 << 15) - 1) as i16;
113 let t: i16 = k_weights.w;
114 let t_n: i16 = Q - t;
115
116 let table1 = &self.lut[(w * grid_size3) as usize..];
117 let table2 = &self.lut[(w_n * grid_size3) as usize..];
118
119 let tetrahedral1 = Interpolator::new(table1);
120 let tetrahedral2 = Interpolator::new(table2);
121 let a0 = tetrahedral1.inter3_sse(c, m, y, &self.weights).v;
122 let b0 = tetrahedral2.inter3_sse(c, m, y, &self.weights).v;
123
124 let hp = _mm_mulhrs_epi16(_mm_set1_epi16(t_n), a0);
125 let v = _mm_add_epi16(hp, _mm_mulhrs_epi16(b0, _mm_set1_epi16(t)));
126
127 if T::FINITE {
128 let mut o = _mm_max_epi16(v, _mm_setzero_si128());
129 o = _mm_min_epi16(o, v_max_scale);
130
131 let x = _mm_extract_epi16::<0>(o);
132 let y = _mm_extract_epi16::<1>(o);
133 let z = _mm_extract_epi16::<2>(o);
134
135 dst[cn.r_i()] = (x as u32).as_();
136 dst[cn.g_i()] = (y as u32).as_();
137 dst[cn.b_i()] = (z as u32).as_();
138 } else {
139 let mut r = _mm_cvtepi32_ps(_mm_cvtepi16_epi32(v));
140 r = _mm_mul_ps(r, f_value_scale);
141 dst[cn.r_i()] = f32::from_bits(_mm_extract_ps::<0>(r) as u32).as_();
142 dst[cn.g_i()] = f32::from_bits(_mm_extract_ps::<1>(r) as u32).as_();
143 dst[cn.b_i()] = f32::from_bits(_mm_extract_ps::<2>(r) as u32).as_();
144 }
145 if channels == 4 {
146 dst[cn.a_i()] = max_value;
147 }
148 }
149 }
150 }
151}
152
153impl<
154 T: Copy + AsPrimitive<f32> + Default + PointeeSizeExpressible,
155 U: AsPrimitive<usize>,
156 const LAYOUT: u8,
157 const GRID_SIZE: usize,
158 const BIT_DEPTH: usize,
159 const BINS: usize,
160 const BARYCENTRIC_BINS: usize,
161> TransformExecutor<T>
162 for TransformLut4To3SseQ0_15<T, U, LAYOUT, GRID_SIZE, BIT_DEPTH, BINS, BARYCENTRIC_BINS>
163where
164 f32: AsPrimitive<T>,
165 u32: AsPrimitive<T>,
166 (): LutBarycentricReduction<T, U>,
167{
168 fn transform(&self, src: &[T], dst: &mut [T]) -> Result<(), CmsError> {
169 let cn = Layout::from(LAYOUT);
170 let channels = cn.channels();
171 if src.len() % 4 != 0 {
172 return Err(CmsError::LaneMultipleOfChannels);
173 }
174 if dst.len() % channels != 0 {
175 return Err(CmsError::LaneMultipleOfChannels);
176 }
177 let src_chunks = src.len() / 4;
178 let dst_chunks = dst.len() / channels;
179 if src_chunks != dst_chunks {
180 return Err(CmsError::LaneSizeMismatch);
181 }
182
183 unsafe {
184 if self.color_space == DataColorSpace::Lab
185 || (self.is_linear && self.color_space == DataColorSpace::Rgb)
186 || self.color_space == DataColorSpace::Xyz
187 {
188 self.transform_chunk::<TrilinearSseQ0_15<GRID_SIZE>>(src, dst);
189 } else {
190 match self.interpolation_method {
191 #[cfg(feature = "options")]
192 InterpolationMethod::Tetrahedral => {
193 self.transform_chunk::<TetrahedralSseQ0_15<GRID_SIZE>>(src, dst);
194 }
195 #[cfg(feature = "options")]
196 InterpolationMethod::Pyramid => {
197 self.transform_chunk::<PyramidalSseQ0_15<GRID_SIZE>>(src, dst);
198 }
199 #[cfg(feature = "options")]
200 InterpolationMethod::Prism => {
201 self.transform_chunk::<PrismaticSseQ0_15<GRID_SIZE>>(src, dst);
202 }
203 InterpolationMethod::Linear => {
204 self.transform_chunk::<TrilinearSseQ0_15<GRID_SIZE>>(src, dst);
205 }
206 }
207 }
208 }
209
210 Ok(())
211 }
212}