Debian Codegraph @

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

raw
1 // Copyright (c) 2022-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::cpu_features::CpuFeatureLevel;
13 use crate::dist::*;
14 use crate::frame::*;
15 use crate::tiling::Area;
16 use crate::util::Pixel;
17 use rand::{rng, Rng};
18
19 fn random_planes<T: Pixel>(bd: usize) -> (Plane<T>, Plane<T>) {
20 let mut rng = rng();
21
22 // Two planes with different strides
23 let mut input_plane = Plane::new(640, 480, 0, 0, 128 + 8, 128 + 8);
24 let mut rec_plane = Plane::new(640, 480, 0, 0, 2 * 128 + 8, 2 * 128 + 8);
25
26 for rows in input_plane.as_region_mut().rows_iter_mut() {
27 for c in rows {
28 *c = T::cast_from(rng.random_range(0u16..(1 << bd)));
29 }
30 }
31
32 for rows in rec_plane.as_region_mut().rows_iter_mut() {
33 for c in rows {
34 *c = T::cast_from(rng.random_range(0u16..(1 << bd)));
35 }
36 }
37
38 (input_plane, rec_plane)
39 }
40
41 /// Create planes with the max values for pixels.
42 fn max_planes<T: Pixel>(bd: usize) -> (Plane<T>, Plane<T>) {
43 // Two planes with different strides
44 let mut input_plane = Plane::new(640, 480, 0, 0, 128 + 8, 128 + 8);
45 let mut rec_plane = Plane::new(640, 480, 0, 0, 2 * 128 + 8, 2 * 128 + 8);
46
47 for rows in input_plane.as_region_mut().rows_iter_mut() {
48 for c in rows {
49 *c = T::cast_from((1 << bd) - 1);
50 }
51 }
52
53 for rows in rec_plane.as_region_mut().rows_iter_mut() {
54 for c in rows {
55 *c = T::cast_from((1 << bd) - 1);
56 }
57 }
58
59 (input_plane, rec_plane)
60 }
61
62 /// Create planes with the max difference between the two values.
63 fn max_diff_planes<T: Pixel>(bd: usize) -> (Plane<T>, Plane<T>) {
64 // Two planes with different strides
65 let mut input_plane = Plane::new(640, 480, 0, 0, 128 + 8, 128 + 8);
66 let mut rec_plane = Plane::new(640, 480, 0, 0, 2 * 128 + 8, 2 * 128 + 8);
67
68 for rows in input_plane.as_region_mut().rows_iter_mut() {
69 for c in rows {
70 *c = T::cast_from(0);
71 }
72 }
73
74 for rows in rec_plane.as_region_mut().rows_iter_mut() {
75 for c in rows {
76 *c = T::cast_from((1 << bd) - 1);
77 }
78 }
79
80 (input_plane, rec_plane)
81 }
82
83 #[test]
84 fn cdef_dist_simd_random() {
85 cdef_diff_tester(8, random_planes::<u8>);
86 }
87
88 #[test]
89 fn cdef_dist_simd_random_hbd() {
90 cdef_diff_tester(10, random_planes::<u16>);
91 cdef_diff_tester(12, random_planes::<u16>);
92 }
93
94 #[test]
95 fn cdef_dist_simd_large() {
96 cdef_diff_tester(8, max_planes::<u8>);
97 }
98
99 #[test]
100 fn cdef_dist_simd_large_hbd() {
101 cdef_diff_tester(10, max_planes::<u16>);
102 cdef_diff_tester(12, max_planes::<u16>);
103 }
104
105 #[test]
106 fn cdef_dist_simd_large_diff() {
107 cdef_diff_tester(8, max_diff_planes::<u8>);
108 }
109
110 #[test]
111 fn cdef_dist_simd_large_diff_hbd() {
112 cdef_diff_tester(10, max_diff_planes::<u16>);
113 cdef_diff_tester(12, max_diff_planes::<u16>);
114 }
115
116 fn cdef_diff_tester<T: Pixel>(
117 bd: usize, gen_planes: fn(bd: usize) -> (Plane<T>, Plane<T>),
118 ) {
119 let (src_plane, dst_plane) = gen_planes(bd);
120
121 let mut fail = false;
122
123 for w in 1..=8 {
124 for h in 1..=8 {
125 // Test alignment by choosing starting location based on width.
126 let area = Area::StartingAt { x: if w <= 4 { 4 } else { 8 }, y: 40 };
127
128 let src_region = src_plane.region(area);
129 let dst_region = dst_plane.region(area);
130
131 let rust = rust::cdef_dist_kernel(
132 &src_region,
133 &dst_region,
134 w,
135 h,
136 bd,
137 CpuFeatureLevel::default(),
138 );
139
140 let simd = cdef_dist_kernel(
141 &src_region,
142 &dst_region,
143 w,
144 h,
145 bd,
146 CpuFeatureLevel::default(),
147 );
148
149 if simd != rust {
150 eprintln!(
151 "CDEF Distortion {}x{}: Assembly doesn't match reference code \
152 \t {} (asm) != {} (ref)",
153 w, h, simd, rust
154 );
155 fail = true;
156 }
157 }
158
159 if fail {
160 panic!();
161 }
162 }
163 }
164 }