Skip to main content

moxcms/conversions/sse/
lut4_to_3_q0_15.rs

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