moxcms/conversions/avx/
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 */
29use 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<'b, Interpolator: AvxMdInterpolationQ0_15Double<'b, GRID_SIZE>>(
73        &'b self,
74        src: &[T],
75        dst: &mut [T],
76    ) {
77        unsafe {
78            let cn = Layout::from(LAYOUT);
79            let channels = cn.channels();
80            let grid_size = GRID_SIZE as i32;
81            let grid_size3 = grid_size * grid_size * grid_size;
82
83            let f_value_scale = _mm_set1_ps(1. / ((1 << 14i32) - 1) as f32);
84            let max_value = ((1u32 << BIT_DEPTH) - 1).as_();
85            let v_max_scale = if T::FINITE {
86                _mm_set1_epi16(((1i32 << BIT_DEPTH) - 1) as i16)
87            } else {
88                _mm_set1_epi16(((1i32 << 14i32) - 1) as i16)
89            };
90
91            for (src, dst) in src.chunks_exact(4).zip(dst.chunks_exact_mut(channels)) {
92                let c = <() as LutBarycentricReduction<T, U>>::reduce::<BIT_DEPTH, BARYCENTRIC_BINS>(
93                    src[0],
94                );
95                let m = <() as LutBarycentricReduction<T, U>>::reduce::<BIT_DEPTH, BARYCENTRIC_BINS>(
96                    src[1],
97                );
98                let y = <() as LutBarycentricReduction<T, U>>::reduce::<BIT_DEPTH, BARYCENTRIC_BINS>(
99                    src[2],
100                );
101                let k = <() as LutBarycentricReduction<T, U>>::reduce::<BIT_DEPTH, BARYCENTRIC_BINS>(
102                    src[3],
103                );
104
105                let k_weights = self.weights[k.as_()];
106
107                let w: i32 = k_weights.x;
108                let w_n: i32 = k_weights.x_n;
109                const Q: i16 = ((1i32 << 15) - 1) as i16;
110                let t: i16 = k_weights.w;
111                let t_n: i16 = Q - t;
112
113                let table1 = &self.lut[(w * grid_size3) as usize..];
114                let table2 = &self.lut[(w_n * grid_size3) as usize..];
115
116                let interpolator = Interpolator::new(table1, table2);
117                let v = interpolator.inter3_sse(c, m, y, &self.weights);
118                let (a0, b0) = (v.0.v, v.1.v);
119
120                let hp = _mm_mulhrs_epi16(_mm_set1_epi16(t_n), a0);
121                let v = _mm_add_epi16(hp, _mm_mulhrs_epi16(b0, _mm_set1_epi16(t)));
122
123                if T::FINITE {
124                    let mut o = _mm_max_epi16(v, _mm_setzero_si128());
125                    o = _mm_min_epi16(o, v_max_scale);
126                    let x = _mm_extract_epi16::<0>(o);
127                    let y = _mm_extract_epi16::<1>(o);
128                    let z = _mm_extract_epi16::<2>(o);
129
130                    dst[cn.r_i()] = (x as u32).as_();
131                    dst[cn.g_i()] = (y as u32).as_();
132                    dst[cn.b_i()] = (z as u32).as_();
133                } else {
134                    let mut r = _mm_cvtepi32_ps(_mm_cvtepi16_epi32(v));
135                    r = _mm_mul_ps(r, f_value_scale);
136                    dst[cn.r_i()] = f32::from_bits(_mm_extract_ps::<0>(r) as u32).as_();
137                    dst[cn.g_i()] = f32::from_bits(_mm_extract_ps::<1>(r) as u32).as_();
138                    dst[cn.b_i()] = f32::from_bits(_mm_extract_ps::<2>(r) as u32).as_();
139                }
140                if channels == 4 {
141                    dst[cn.a_i()] = max_value;
142                }
143            }
144        }
145    }
146}
147
148impl<
149    T: Copy + AsPrimitive<f32> + Default + PointeeSizeExpressible,
150    U: AsPrimitive<usize>,
151    const LAYOUT: u8,
152    const GRID_SIZE: usize,
153    const BIT_DEPTH: usize,
154    const BINS: usize,
155    const BARYCENTRIC_BINS: usize,
156> TransformExecutor<T>
157    for TransformLut4To3AvxQ0_15<T, U, LAYOUT, GRID_SIZE, BIT_DEPTH, BINS, BARYCENTRIC_BINS>
158where
159    f32: AsPrimitive<T>,
160    u32: AsPrimitive<T>,
161    (): LutBarycentricReduction<T, U>,
162{
163    fn transform(&self, src: &[T], dst: &mut [T]) -> Result<(), CmsError> {
164        let cn = Layout::from(LAYOUT);
165        let channels = cn.channels();
166        if src.len() % 4 != 0 {
167            return Err(CmsError::LaneMultipleOfChannels);
168        }
169        if dst.len() % channels != 0 {
170            return Err(CmsError::LaneMultipleOfChannels);
171        }
172        let src_chunks = src.len() / 4;
173        let dst_chunks = dst.len() / channels;
174        if src_chunks != dst_chunks {
175            return Err(CmsError::LaneSizeMismatch);
176        }
177
178        unsafe {
179            if self.color_space == DataColorSpace::Lab
180                || (self.is_linear && self.color_space == DataColorSpace::Rgb)
181                || self.color_space == DataColorSpace::Xyz
182            {
183                self.transform_chunk::<TrilinearAvxQ0_15Double<GRID_SIZE>>(src, dst);
184            } else {
185                match self.interpolation_method {
186                    #[cfg(feature = "options")]
187                    InterpolationMethod::Tetrahedral => {
188                        self.transform_chunk::<TetrahedralAvxQ0_15Double<GRID_SIZE>>(src, dst);
189                    }
190                    #[cfg(feature = "options")]
191                    InterpolationMethod::Pyramid => {
192                        self.transform_chunk::<PyramidAvxFmaQ0_15Double<GRID_SIZE>>(src, dst);
193                    }
194                    #[cfg(feature = "options")]
195                    InterpolationMethod::Prism => {
196                        self.transform_chunk::<PrismaticAvxQ0_15Double<GRID_SIZE>>(src, dst);
197                    }
198                    InterpolationMethod::Linear => {
199                        self.transform_chunk::<TrilinearAvxQ0_15Double<GRID_SIZE>>(src, dst);
200                    }
201                }
202            }
203        }
204
205        Ok(())
206    }
207}