Debian Codegraph @

src/idct.rs

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 * Copyright (c) 2023.
3 *
4 * This software is free software;
5 *
6 * You can redistribute it or modify it under terms of the MIT, Apache License or Zlib license
7 */
8
9 //! Routines for IDCT
10 //!
11 //! Essentially we provide 2 routines for IDCT, a scalar implementation and a not super optimized
12 //! AVX2 one, i'll talk about them here.
13 //!
14 //! There are 2 reasons why we have the avx one
15 //! 1. No one compiles with -C target-features=avx2 hence binaries won't probably take advantage(even
16 //! if it exists).
17 //! 2. AVX employs zero short circuit in a way the scalar code cannot employ it.
18 //! - AVX does this by checking for MCU's whose 63 AC coefficients are zero and if true, it writes
19 //! values directly, if false, it goes the long way of calculating.
20 //! - Although this can be trivially implemented in the scalar version, it generates code
21 //! I'm not happy width(scalar version that basically loops and that is too many branches for me)
22 //! The avx one does a better job of using bitwise or's with (`_mm256_or_si256`) which is magnitudes of faster
23 //! than anything I could come up with
24 //!
25 //! The AVX code also has some cool transpose_u16 instructions which look so complicated to be cool
26 //! (spoiler alert, i barely understand how it works, that's why I credited the owner).
27 //!
28 #![allow(
29 clippy::excessive_precision,
30 clippy::unreadable_literal,
31 clippy::module_name_repetitions,
32 unused_parens,
33 clippy::wildcard_imports
34 )]
35
36 use zune_corerust-analyzer cargo zune-core 0.5.1 crate/::logrust-analyzer cargo log 0.4.29 crate/::debugrust-analyzer cargo log 0.4.29 macros/debug!;
37 use zune_corerust-analyzer cargo zune-core 0.5.1 crate/::optionsrust-analyzer cargo zune-core 0.5.1 options/::DecoderOptionsrust-analyzer cargo zune-core 0.5.1 options/decoder/DecoderOptions#;
38
39 use craterust-analyzer cargo zune-jpeg 0.5.15 crate/::decoderrust-analyzer cargo zune-jpeg 0.5.15 decoder/::IDCTPtrrust-analyzer cargo zune-jpeg 0.5.15 decoder/IDCTPtr#;
40 use craterust-analyzer cargo zune-jpeg 0.5.15 crate/::idctrust-analyzer cargo zune-jpeg 0.5.15 idct/::scalarrust-analyzer cargo zune-jpeg 0.5.15 idct/scalar/::{idct_intrust-analyzer cargo zune-jpeg 0.5.15 idct/scalar/idct_int()., idct_int_1x1rust-analyzer cargo zune-jpeg 0.5.15 idct/scalar/idct_int_1x1().};
41
42 #[cfg(feature = "x86")]
43 pub mod avx2rust-analyzer cargo zune-jpeg 0.5.15 idct/avx2/;
44 #[cfg(feature = "neon")]
45 pub mod neon;
46
47 pub mod scalarrust-analyzer cargo zune-jpeg 0.5.15 idct/scalar/;
48
49 /// Choose an appropriate IDCT function
50 #[allow(unused_variables)]
51 pub fn choose_idct_funcrust-analyzer cargo zune-jpeg 0.5.15 idct/choose_idct_func().(options: &DecoderOptionsrust-analyzer cargo zune-core 0.5.1 options/decoder/DecoderOptions#) -> IDCTPtrrust-analyzer cargo zune-jpeg 0.5.15 decoder/IDCTPtr# {
52 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
53 #[cfg(feature = "x86")]
54 {
55 if options.use_avx2rust-analyzer cargo zune-core 0.5.1 options/decoder/impl#[DecoderOptions]use_avx2().() {
56 debugrust-analyzer cargo log 0.4.29 macros/debug!!("Using vector integer IDCT");
57 return |a: &mut [i32; 64], b: &mut [i16], c: usize| {
58 // SAFETY: `options.use_avx2()` only returns true if avx2 is supported.
59 unsafe { avx2rust-analyzer cargo zune-jpeg 0.5.15 idct/avx2/::idct_avx2rust-analyzer cargo zune-jpeg 0.5.15 idct/avx2/idct_avx2().(a,b,c) }
60 };
61 }
62 }
63 #[cfg(target_arch = "aarch64")]
64 #[cfg(feature = "neon")]
65 {
66 if options.use_neon() {
67 debugrust-analyzer cargo log 0.4.29 macros/debug!!("Using vector integer IDCT");
68 return |a: &mut [i32; 64], b: &mut [i16], c: usize| {
69 // SAFETY: `options.use_neon()` only returns true if neon is supported.
70 unsafe { neon::idct_neon(a,b,c) }
71 };
72 }
73 }
74 debugrust-analyzer cargo log 0.4.29 macros/debug!!("Using scalar integer IDCT");
75 // use generic one
76 return idct_intrust-analyzer cargo zune-jpeg 0.5.15 idct/scalar/idct_int().;
77 }
78
79 /// Choose a function to implement 4x4 IDCT.
80 ///
81 /// These functions get the same input but have an extra contract: Only the first 4x4 block of
82 /// coefficients are non-zero. All other entries are zeroed.
83 ///
84 /// **The callee must uphold that contract on return**
85 pub fn choose_idct_4x4_funcrust-analyzer cargo zune-jpeg 0.5.15 idct/choose_idct_4x4_func().(_options: &DecoderOptionsrust-analyzer cargo zune-core 0.5.1 options/decoder/DecoderOptions#) -> IDCTPtrrust-analyzer cargo zune-jpeg 0.5.15 decoder/IDCTPtr# {
86 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
87 #[cfg(feature = "x86")]
88 {
89 if _options.use_avx2rust-analyzer cargo zune-core 0.5.1 options/decoder/impl#[DecoderOptions]use_avx2().() {
90 debugrust-analyzer cargo log 0.4.29 macros/debug!!("Using vector integer IDCT");
91 return |a: &mut [i32; 64], b: &mut [i16], c: usize| {
92 // SAFETY: `options.use_avx2()` only returns true if avx2 is supported.
93 unsafe { avx2rust-analyzer cargo zune-jpeg 0.5.15 idct/avx2/::idct_avx2_4x4rust-analyzer cargo zune-jpeg 0.5.15 idct/avx2/idct_avx2_4x4().(a,b,c) }
94 };
95 }
96 }
97
98 scalarrust-analyzer cargo zune-jpeg 0.5.15 idct/scalar/::idct4x4rust-analyzer cargo zune-jpeg 0.5.15 idct/scalar/idct4x4().
99 }
100
101 pub fn choose_idct_1x1_funcrust-analyzer cargo zune-jpeg 0.5.15 idct/choose_idct_1x1_func().(_: &DecoderOptionsrust-analyzer cargo zune-core 0.5.1 options/decoder/DecoderOptions#) -> IDCTPtrrust-analyzer cargo zune-jpeg 0.5.15 decoder/IDCTPtr# {
102 // These are simple stores, no alternative implementation for now
103 idct_int_1x1rust-analyzer cargo zune-jpeg 0.5.15 idct/scalar/idct_int_1x1().
104 }
105
106 #[cfg(test)]
107 #[allow(unreachable_code)]
108 #[allow(dead_code)]
109 mod testsrust-analyzer cargo zune-jpeg 0.5.15 idct/tests/ {
110 use superrust-analyzer cargo zune-jpeg 0.5.15 idct/::*;
•111 use allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/::Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#;
112 use allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/;
113
114 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
115 fn idct_test0rust-analyzer cargo zune-jpeg 0.5.15 idct/tests/idct_test0().() {
116 let stride = 8;
117 let mut coeff = [10; 64];
118 let mut coeff2 = [10; 64];
119 let mut output_scalar = [0; 64];
120 let mut output_vector = [0; 64];
121 let idct_func = choose_idct_funcrust-analyzer cargo zune-jpeg 0.5.15 idct/choose_idct_func().(&DecoderOptionsrust-analyzer cargo zune-core 0.5.1 options/decoder/DecoderOptions#::new_fastrust-analyzer cargo zune-core 0.5.1 options/decoder/impl#[DecoderOptions]new_fast().());
122 idct_func(&mut coeff, &mut output_vector, stride);
123 idct_intrust-analyzer cargo zune-jpeg 0.5.15 idct/scalar/idct_int().(&mut coeff2, &mut output_scalar, stride);
124 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(output_scalar, output_vector, "IDCT and scalar do not match");
125 }
126
127 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
128 fn do_idct_test1rust-analyzer cargo zune-jpeg 0.5.15 idct/tests/do_idct_test1().() {
129 let stride = 8;
130 let mut coeff = [14; 64];
131 let mut coeff2 = [14; 64];
132 let mut output_scalar = [0; 64];
133 let mut output_vector = [0; 64];
134 let idct_func = choose_idct_funcrust-analyzer cargo zune-jpeg 0.5.15 idct/choose_idct_func().(&DecoderOptionsrust-analyzer cargo zune-core 0.5.1 options/decoder/DecoderOptions#::new_fastrust-analyzer cargo zune-core 0.5.1 options/decoder/impl#[DecoderOptions]new_fast().());
135 idct_func(&mut coeff, &mut output_vector, stride);
136 idct_intrust-analyzer cargo zune-jpeg 0.5.15 idct/scalar/idct_int().(&mut coeff2, &mut output_scalar, stride);
137 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(output_scalar, output_vector, "IDCT and scalar do not match");
138 }
139
140 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
141 fn do_idct_test2rust-analyzer cargo zune-jpeg 0.5.15 idct/tests/do_idct_test2().() {
142 let stride = 8;
143 let mut coeff = [0; 64];
144 coeff[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`IndexMut<I>`]index_mut().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`IndexMut<I>`]index_mut(). = 255;
145 coeff[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`IndexMut<I>`]index_mut().63]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`IndexMut<I>`]index_mut(). = -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().256;
146 let mut coeff2 = coeff;
147 let mut output_scalar = [0; 64];
148 let mut output_vector = [0; 64];
149 let idct_func = choose_idct_funcrust-analyzer cargo zune-jpeg 0.5.15 idct/choose_idct_func().(&DecoderOptionsrust-analyzer cargo zune-core 0.5.1 options/decoder/DecoderOptions#::new_fastrust-analyzer cargo zune-core 0.5.1 options/decoder/impl#[DecoderOptions]new_fast().());
150 idct_func(&mut coeff, &mut output_vector, stride);
151 idct_intrust-analyzer cargo zune-jpeg 0.5.15 idct/scalar/idct_int().(&mut coeff2, &mut output_scalar, stride);
152 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(output_scalar, output_vector, "IDCT and scalar do not match");
153 }
154
155 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
156 fn do_idct_zerosrust-analyzer cargo zune-jpeg 0.5.15 idct/tests/do_idct_zeros().() {
157 let stride = 8;
158 let mut coeff = [0; 64];
159 let mut coeff2 = [0; 64];
160 let mut output_scalar = [0; 64];
161 let mut output_vector = [0; 64];
162 let idct_func = choose_idct_funcrust-analyzer cargo zune-jpeg 0.5.15 idct/choose_idct_func().(&DecoderOptionsrust-analyzer cargo zune-core 0.5.1 options/decoder/DecoderOptions#::new_fastrust-analyzer cargo zune-core 0.5.1 options/decoder/impl#[DecoderOptions]new_fast().());
163 idct_func(&mut coeff, &mut output_vector, stride);
164 idct_intrust-analyzer cargo zune-jpeg 0.5.15 idct/scalar/idct_int().(&mut coeff2, &mut output_scalar, stride);
165 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(output_scalar, output_vector, "IDCT and scalar do not match");
166 }
167
168 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
169 fn idct_4x4rust-analyzer cargo zune-jpeg 0.5.15 idct/tests/idct_4x4().() {
170 #[rustfmt::skip]
171 const Arust-analyzer cargo zune-jpeg 0.5.15 idct/tests/A.: [i32; 32] = [
172 -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().254, -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().7, 0, 0, 0, 0, 0, 0,
173 7, 0, -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().30, 32, 0, 0, 0, 0,
174 7, 0, -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().30, 32, 0, 0, 0, 0,
175 7, 0, -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().30, 32, 0, 0, 0, 0,
176 ];
177
178 let v: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<IDCTPtrrust-analyzer cargo zune-jpeg 0.5.15 decoder/IDCTPtr#> = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![
179 choose_idct_funcrust-analyzer cargo zune-jpeg 0.5.15 idct/choose_idct_func().(&DecoderOptionsrust-analyzer cargo zune-core 0.5.1 options/decoder/DecoderOptions#::new_saferust-analyzer cargo zune-core 0.5.1 options/decoder/impl#[DecoderOptions]new_safe().()),
180 choose_idct_4x4_funcrust-analyzer cargo zune-jpeg 0.5.15 idct/choose_idct_4x4_func().(&DecoderOptionsrust-analyzer cargo zune-core 0.5.1 options/decoder/DecoderOptions#::new_saferust-analyzer cargo zune-core 0.5.1 options/decoder/impl#[DecoderOptions]new_safe().()),
181 choose_idct_funcrust-analyzer cargo zune-jpeg 0.5.15 idct/choose_idct_func().(&DecoderOptionsrust-analyzer cargo zune-core 0.5.1 options/decoder/DecoderOptions#::new_fastrust-analyzer cargo zune-core 0.5.1 options/decoder/impl#[DecoderOptions]new_fast().()),
182 choose_idct_4x4_funcrust-analyzer cargo zune-jpeg 0.5.15 idct/choose_idct_4x4_func().(&DecoderOptionsrust-analyzer cargo zune-core 0.5.1 options/decoder/DecoderOptions#::new_fastrust-analyzer cargo zune-core 0.5.1 options/decoder/impl#[DecoderOptions]new_fast().()),
183 ];
184 let dct_names = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![
185 "safe idct",
186 "safe idct 4x4",
187 "fast idct",
188 "fast idct 4x4",
189 ];
190
191 let mut color = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![];
192
193 for idct in v {
194 let mut a = [0i32; 64];
195 a[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`IndexMut<I>`]index_mut()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#32]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`IndexMut<I>`]index_mut()..copy_from_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]copy_from_slice().(&Arust-analyzer cargo zune-jpeg 0.5.15 idct/tests/A.);
196 let mut b = [0i16; 64];
197
198 idct(&mut a, &mut b, 8);
199
200 color.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(b);
201 }
202
203 for (wnd, name) in color.windowsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]windows().(2).ziprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#zip().(&dct_names) {
204 let [a, b] = wnd else { unreachablerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/unreachable!!() };
205 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(a, b, "{name}");
206 }
207 }
208 }