Debian Codegraph @

debian/missing-sources/moxcms/src/conversions/sse/rgb_xyz_opt.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 = "sse_shaper_optimized_paths")]
30 use crate::conversions::rgbxyz::TransformMatrixShaperOptimizedV;
31 use crate::conversions::sse::SseAlignedU16;
32 use crate::transform::PointeeSizeExpressible;
33 use crate::{CmsError, Layout, TransformExecutor};
34 use num_traits::AsPrimitive;
35 #[cfg(target_arch = "x86")]
36 use std::arch::x86::*;
37 #[cfg(target_arch = "x86_64")]
38 use std::arch::x86_64::*;
39
40 pub(crate) struct TransformShaperRgbOptSse<
41 T: Clone + Copy + 'static + PointeeSizeExpressible + Default,
42 const SRC_LAYOUT: u8,
43 const DST_LAYOUT: u8,
44 > {
45 pub(crate) profile: TransformMatrixShaperOptimizedV<T>,
46 pub(crate) bit_depth: usize,
47 pub(crate) gamma_lut: usize,
48 }
49
50 impl<
51 T: Clone + Copy + 'static + PointeeSizeExpressible + Default,
52 const SRC_LAYOUT: u8,
53 const DST_LAYOUT: u8,
54 > TransformShaperRgbOptSse<T, SRC_LAYOUT, DST_LAYOUT>
55 where
56 u32: AsPrimitive<T>,
57 {
58 #[target_feature(enable = "sse4.1")]
59 unsafe fn transform_impl(&self, src: &[T], dst: &mut [T]) -> Result<(), CmsError> {
60 let src_cn = Layout::from(SRC_LAYOUT);
61 let dst_cn = Layout::from(DST_LAYOUT);
62 let src_channels = src_cn.channels();
63 let dst_channels = dst_cn.channels();
64
65 let mut temporary = SseAlignedU16([0; 8]);
66
67 if src.len() / src_channels != dst.len() / dst_channels {
68 return Err(CmsError::LaneSizeMismatch);
69 }
70 if src.len() % src_channels != 0 {
71 return Err(CmsError::LaneMultipleOfChannels);
72 }
73 if dst.len() % dst_channels != 0 {
74 return Err(CmsError::LaneMultipleOfChannels);
75 }
76
77 let t = self.profile.adaptation_matrix.transpose();
78
79 let scale = (self.gamma_lut - 1) as f32;
80 let max_colors: T = ((1 << self.bit_depth) - 1).as_();
81
82 // safety precondition for linearization table
83 if T::FINITE {
84 let cap = (1 << self.bit_depth) - 1;
85 assert!(self.profile.linear.len() >= cap);
86 } else {
87 assert!(self.profile.linear.len() >= T::NOT_FINITE_LINEAR_TABLE_SIZE);
88 }
89
90 let lut_lin = &self.profile.linear;
91
92 unsafe {
93 let m0 = _mm_setr_ps(t.v[0][0], t.v[0][1], t.v[0][2], 0f32);
94 let m1 = _mm_setr_ps(t.v[1][0], t.v[1][1], t.v[1][2], 0f32);
95 let m2 = _mm_setr_ps(t.v[2][0], t.v[2][1], t.v[2][2], 0f32);
96
97 let zeros = _mm_setzero_ps();
98
99 let v_scale = _mm_set1_ps(scale);
100
101 for (src, dst) in src
102 .chunks_exact(src_channels)
103 .zip(dst.chunks_exact_mut(dst_channels))
104 {
105 let rp = lut_lin.get_unchecked(src[src_cn.r_i()]._as_usize());
106 let gp = lut_lin.get_unchecked(src[src_cn.g_i()]._as_usize());
107 let bp = lut_lin.get_unchecked(src[src_cn.b_i()]._as_usize());
108
109 let mut r = _mm_load_ss(rp);
110 let mut g = _mm_load_ss(gp);
111 let mut b = _mm_load_ss(bp);
112 let a = if src_channels == 4 {
113 src[src_cn.a_i()]
114 } else {
115 max_colors
116 };
117
118 r = _mm_shuffle_ps::<0>(r, r);
119 g = _mm_shuffle_ps::<0>(g, g);
120 b = _mm_shuffle_ps::<0>(b, b);
121
122 let v0 = _mm_mul_ps(r, m0);
123 let v1 = _mm_mul_ps(g, m1);
124 let v2 = _mm_mul_ps(b, m2);
125
126 let mut v = _mm_add_ps(_mm_add_ps(v0, v1), v2);
127 v = _mm_max_ps(v, zeros);
128 v = _mm_mul_ps(v, v_scale);
129 v = _mm_min_ps(v, v_scale);
130
131 let zx = _mm_cvtps_epi32(v);
132 _mm_store_si128(temporary.0.as_mut_ptr() as *mut _, zx);
133
134 dst[dst_cn.r_i()] = self.profile.gamma[temporary.0[0] as usize];
135 dst[dst_cn.g_i()] = self.profile.gamma[temporary.0[2] as usize];
136 dst[dst_cn.b_i()] = self.profile.gamma[temporary.0[4] as usize];
137 if dst_channels == 4 {
138 dst[dst_cn.a_i()] = a;
139 }
140 }
141 }
142
143 Ok(())
144 }
145 }
146
147 impl<
148 T: Clone + Copy + 'static + PointeeSizeExpressible + Default,
149 const SRC_LAYOUT: u8,
150 const DST_LAYOUT: u8,
151 > TransformExecutor<T> for TransformShaperRgbOptSse<T, SRC_LAYOUT, DST_LAYOUT>
152 where
153 u32: AsPrimitive<T>,
154 {
155 fn transform(&self, src: &[T], dst: &mut [T]) -> Result<(), CmsError> {
156 unsafe { self.transform_impl(src, dst) }
157 }
158 }