Debian Codegraph @

.cargo/vendor/rav1e/src/asm/shared/dist/sse.rs

raw
1 // Copyright (c) 2020-2023, The rav1e contributors. All rights reserved
2 //
3 // This source code is subject to the terms of the BSD 2 Clause License and
4 // the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
5 // was not distributed with this source code in the LICENSE file, you can
6 // obtain it at www.aomedia.org/license/software. If the Alliance for Open
7 // Media Patent License 1.0 was not distributed with this source code in the
8 // PATENTS file, you can obtain it at www.aomedia.org/license/patent.
9
10 #[cfg(test)]
11 pub mod test {
12 use crate::config::CpuFeatureLevel;
13 use crate::dist::*;
14 use crate::frame::*;
15 use crate::partition::BlockSize;
16 use crate::rdo::DistortionScale;
17 use crate::tiling::Area;
18 use crate::util::*;
19 use rand::{rng, Rng};
20
21 fn random_planes<T: Pixel>(bd: usize) -> (Plane<T>, Plane<T>) {
22 let mut rng = rng();
23
24 // Two planes with different strides
25 let mut input_plane = Plane::new(640, 480, 0, 0, 128 + 8, 128 + 8);
26 let mut rec_plane = Plane::new(640, 480, 0, 0, 2 * 128 + 8, 2 * 128 + 8);
27
28 for rows in input_plane.as_region_mut().rows_iter_mut() {
29 for c in rows {
30 *c = T::cast_from(rng.random_range(0u16..(1 << bd)));
31 }
32 }
33
34 for rows in rec_plane.as_region_mut().rows_iter_mut() {
35 for c in rows {
36 *c = T::cast_from(rng.random_range(0u16..(1 << bd)));
37 }
38 }
39
40 (input_plane, rec_plane)
41 }
42
43 // Create planes with the max difference between the two values.
44 fn max_diff_planes<T: Pixel>(bd: usize) -> (Plane<T>, Plane<T>) {
45 // Two planes with different strides
46 let mut input_plane = Plane::new(640, 480, 0, 0, 128 + 8, 128 + 8);
47 let mut rec_plane = Plane::new(640, 480, 0, 0, 2 * 128 + 8, 2 * 128 + 8);
48
49 for rows in input_plane.as_region_mut().rows_iter_mut() {
50 for c in rows {
51 *c = T::cast_from(0);
52 }
53 }
54
55 for rows in rec_plane.as_region_mut().rows_iter_mut() {
56 for c in rows {
57 *c = T::cast_from((1 << bd) - 1);
58 }
59 }
60
61 (input_plane, rec_plane)
62 }
63
64 /// Fill data for scaling of one (i.e. no scaling between blocks)
65 fn scaling_one(scales: &mut [u32]) {
66 for a in scales.iter_mut() {
67 *a = DistortionScale::default().0;
68 }
69 }
70
71 /// Fill data for scaling of one
72 fn scaling_random(scales: &mut [u32]) {
73 let mut rng = rng();
74 for a in scales.iter_mut() {
75 *a = rng.random_range(
76 DistortionScale::from(0.5).0..DistortionScale::from(1.5).0,
77 );
78 }
79 }
80
81 /// Fill the max value for scaling
82 /// TODO: Pair with max difference test
83 fn scaling_large(scales: &mut [u32]) {
84 for a in scales.iter_mut() {
85 *a = DistortionScale::from(f64::MAX).0;
86 }
87 }
88
89 #[test]
90 fn weighted_sse_simd_no_scaling() {
91 weighted_sse_simd_tester(8, scaling_one, random_planes::<u8>);
92 }
93
94 #[test]
95 fn weighted_sse_simd_random() {
96 weighted_sse_simd_tester(8, scaling_random, random_planes::<u8>);
97 }
98
99 #[test]
100 fn weighted_sse_simd_large() {
101 weighted_sse_simd_tester(8, scaling_large, max_diff_planes::<u8>);
102 }
103
104 #[test]
105 fn weighted_sse_hbd_simd_no_scaling() {
106 weighted_sse_simd_tester(12, scaling_one, random_planes::<u16>);
107 }
108
109 #[test]
110 fn weighted_sse_hbd_simd_random() {
111 weighted_sse_simd_tester(12, scaling_random, random_planes::<u16>);
112 }
113
114 #[test]
115 fn weighted_sse_hbd_simd_large() {
116 weighted_sse_simd_tester(12, scaling_large, max_diff_planes::<u16>);
117 }
118
119 fn weighted_sse_simd_tester<T: Pixel>(
120 bd: usize, fill_scales: fn(scales: &mut [u32]),
121 gen_planes: fn(bd: usize) -> (Plane<T>, Plane<T>),
122 ) {
123 use BlockSize::*;
124 let blocks = vec![
125 BLOCK_4X4,
126 BLOCK_4X8,
127 BLOCK_8X4,
128 BLOCK_8X8,
129 BLOCK_8X16,
130 BLOCK_16X8,
131 BLOCK_16X16,
132 BLOCK_16X32,
133 BLOCK_32X16,
134 BLOCK_32X32,
135 BLOCK_32X64,
136 BLOCK_64X32,
137 BLOCK_64X64,
138 BLOCK_64X128,
139 BLOCK_128X64,
140 BLOCK_128X128,
141 BLOCK_4X16,
142 BLOCK_16X4,
143 BLOCK_8X32,
144 BLOCK_32X8,
145 BLOCK_16X64,
146 BLOCK_64X16,
147 ];
148
149 const SCALE_STRIDE: usize = 256;
150 let mut scaling_storage = Aligned::new([0u32; 256 * SCALE_STRIDE]);
151 let scaling = &mut scaling_storage.data;
152 fill_scales(scaling);
153
154 let (input_plane, rec_plane) = gen_planes(bd);
155
156 for block in blocks {
157 // Start at block width to test alignment.
158 let area = Area::StartingAt { x: block.width() as isize, y: 40 };
159
160 let input_region = input_plane.region(area);
161 let rec_region = rec_plane.region(area);
162
163 let rust = rust::get_weighted_sse(
164 &input_region,
165 &rec_region,
166 scaling,
167 SCALE_STRIDE,
168 block.width(),
169 block.height(),
170 bd,
171 CpuFeatureLevel::default(),
172 );
173
174 let simd = get_weighted_sse(
175 &input_region,
176 &rec_region,
177 scaling,
178 SCALE_STRIDE,
179 block.width(),
180 block.height(),
181 bd,
182 CpuFeatureLevel::default(),
183 );
184
185 assert!(
186 simd == rust,
187 "Weighted SSE {}: Assembly doesn't match reference code. {} (asm) != {} (ref)",
188 block,
189 simd,
190 rust,
191 );
192 }
193 }
194 }