Debian Codegraph @

debian/missing-sources/wide/tests/all_tests/t_usefulness.rs

raw
1 #![allow(clippy::excessive_precision)]
2
3 use wide::*;
4
5 use bytemuck::*;
6
7 #[test]
8 fn unpack_modify_and_repack_rgba_values() {
9 let mask = u32x4::from(0xFF);
10 //
11 let input = u32x4::from([0xFF0000FF, 0x00FF00FF, 0x0000FFFF, 0x000000FF]);
12
13 // unpack
14 let r_actual = cast::<_, i32x4>(input >> 24).round_float();
15 let g_actual = cast::<_, i32x4>((input >> 16) & mask).round_float();
16 let b_actual = cast::<_, i32x4>((input >> 8) & mask).round_float();
17 let a_actual = cast::<_, i32x4>(input & mask).round_float();
18 let r_expected = f32x4::from([255.0, 0.0, 0.0, 0.0]);
19 let g_expected = f32x4::from([0.0, 255.0, 0.0, 0.0]);
20 let b_expected = f32x4::from([0.0, 0.0, 255.0, 0.0]);
21 let a_expected = f32x4::from([255.0, 255.0, 255.0, 255.0]);
22 assert_eq!(r_expected, r_actual);
23 assert_eq!(g_expected, g_actual);
24 assert_eq!(b_expected, b_actual);
25 assert_eq!(a_expected, a_actual);
26
27 // modify some of the data
28 let r_new = (r_actual - f32x4::from(1.0)).max(f32x4::from(0.0));
29 let g_new = (g_actual - f32x4::from(1.0)).max(f32x4::from(0.0));
30 let b_new = (b_actual - f32x4::from(1.0)).max(f32x4::from(0.0));
31 let a_new = a_actual;
32
33 // repack
34 let r_u = cast::<i32x4, u32x4>(r_new.round_int());
35 let g_u = cast::<i32x4, u32x4>(g_new.round_int());
36 let b_u = cast::<i32x4, u32x4>(b_new.round_int());
37 let a_u = cast::<i32x4, u32x4>(a_new.round_int());
38 let output_actual = (r_u << 24) | (g_u << 16) | (b_u << 8) | (a_u);
39 let output_expected =
40 u32x4::from([0xFE0000FF, 0x00FE00FF, 0x0000FEFF, 0x000000FF]);
41 assert_eq!(output_expected, output_actual);
42 }
43
44 /// Implement JPEG IDCT using i16x8. This has slightly different behavior than
45 /// the normal 32 bit scalar implementation in libjpeg. It's a bit more accurate
46 /// in some ways (since the constants are encoded in 15 bits instead of 12) but
47 /// is more subject to hitting saturation during intermediate calculations,
48 /// although that should normally not be a problem for photographic JPEGs.
49 ///
50 /// The main downside of this approach is that it is very slow to do saturating
51 /// math on scalar types on some CPUs, so if you need bit-exact behavior on
52 /// different architectures this is not the algorithm for you.
53 #[test]
54 fn test_dequantize_and_idct_i16() {
55 fn to_fixed(x: f32) -> i16 {
56 (x * 32767.0 + 0.5) as i16
57 }
58
59 fn kernel_i16(data: [i16x8; 8]) -> [i16x8; 8] {
60 // kernel x
61 let a2 = data[2];
62 let a6 = data[6];
63
64 let b0 = a2.saturating_add(a6).mul_scale_round_n(to_fixed(0.5411961));
65 let c0 = b0
66 .saturating_sub(a6)
67 .saturating_sub(a6.mul_scale_round_n(to_fixed(0.847759065)));
68 let c1 = b0.saturating_add(a2.mul_scale_round_n(to_fixed(0.765366865)));
69
70 let a0 = data[0];
71 let a4 = data[4];
72 let b1 = a0.saturating_add(a4);
73 let b2 = a0.saturating_sub(a4);
74
75 let x0 = b1.saturating_add(c1);
76 let x1 = b2.saturating_add(c0);
77 let x2 = b2.saturating_sub(c0);
78 let x3 = b1.saturating_sub(c1);
79
80 // kernel t
81 let t0 = data[7];
82 let t1 = data[5];
83 let t2 = data[3];
84 let t3 = data[1];
85
86 let p1 = t0.saturating_add(t3);
87 let p2 = t1.saturating_add(t2);
88 let p3 = t0.saturating_add(t2);
89 let p4 = t1.saturating_add(t3);
90
91 let p5t = p3.saturating_add(p4);
92 let p5 = p5t.saturating_add(p5t.mul_scale_round_n(to_fixed(0.175875602)));
93
94 let e0 = t0.mul_scale_round_n(to_fixed(0.298631336));
95 let e1 = t1
96 .saturating_add(t1)
97 .saturating_add(t1.mul_scale_round_n(to_fixed(0.053119869)));
98
99 let e2 = t2
100 .saturating_add(t2)
101 .saturating_add(t2)
102 .saturating_add(t2.mul_scale_round_n(to_fixed(0.072711026)));
103 let e3 = t3.saturating_add(t3.mul_scale_round_n(to_fixed(0.501321110)));
104
105 let f0 = p5.saturating_sub(p1.mul_scale_round_n(to_fixed(0.899976223)));
106 let f1 = p5
107 .saturating_sub(p2)
108 .saturating_sub(p2)
109 .saturating_sub(p2.mul_scale_round_n(to_fixed(0.562915447)));
110
111 let f2 = p3.mul_scale_round_n(to_fixed(-0.961570560)).saturating_sub(p3);
112 let f3 = p4.mul_scale_round_n(to_fixed(-0.390180644));
113
114 let t3 = f0.saturating_add(f3).saturating_add(e3);
115 let t2 = f1.saturating_add(f2).saturating_add(e2);
116 let t1 = f1.saturating_add(f3).saturating_add(e1);
117 let t0 = f0.saturating_add(f2).saturating_add(e0);
118
119 [
120 x0.saturating_add(t3),
121 x1.saturating_add(t2),
122 x2.saturating_add(t1),
123 x3.saturating_add(t0),
124 x3.saturating_sub(t0),
125 x2.saturating_sub(t1),
126 x1.saturating_sub(t2),
127 x0.saturating_sub(t3),
128 ]
129 }
130
131 #[rustfmt::skip]
132 let coefficients: [i16; 8 * 8] = [
133 -14, -39, 58, -2, 3, 3, 0, 1,
134 11, 27, 4, -3, 3, 0, 1, 0,
135 -6, -13, -9, -1, -2, -1, 0, 0,
136 -4, 0, -1, -2, 0, 0, 0, 0,
137 3, 0, 0, 0, 0, 0, 0, 0,
138 -3, -2, 0, 0, 0, 0, 0, 0,
139 0, 0, 0, 0, 0, 0, 0, 0,
140 0, 0, 0, 0, 0, 0, 0, 0
141 ];
142
143 #[rustfmt::skip]
144 let quantization_table: [i16; 8 * 8] = [
145 8, 6, 5, 8, 12, 20, 26, 31,
146 6, 6, 7, 10, 13, 29, 30, 28,
147 7, 7, 8, 12, 20, 29, 35, 28,
148 7, 9, 11, 15, 26, 44, 40, 31,
149 9, 11, 19, 28, 34, 55, 52, 39,
150 12, 18, 28, 32, 41, 52, 57, 46,
151 25, 32, 39, 44, 52, 61, 60, 51,
152 36, 46, 48, 49, 56, 50, 52, 50
153 ];
154
155 let c: [i16x8; 8] = cast(coefficients);
156 let q: [i16x8; 8] = cast(quantization_table);
157
158 // coefficients normally go up to 1024, shift up by 3 to get extra precision
159 const SHIFT: i16 = 3;
160
161 let data = [
162 (c[0] * q[0]) << SHIFT,
163 (c[1] * q[1]) << SHIFT,
164 (c[2] * q[2]) << SHIFT,
165 (c[3] * q[3]) << SHIFT,
166 (c[4] * q[4]) << SHIFT,
167 (c[5] * q[5]) << SHIFT,
168 (c[6] * q[6]) << SHIFT,
169 (c[7] * q[7]) << SHIFT,
170 ];
171
172 let pass1 = kernel_i16(data);
173 let transpose1 = i16x8::transpose(pass1);
174 let pass2 = kernel_i16(transpose1);
175 let result = i16x8::transpose(pass2);
176
177 // offset to recenter to 0..256 and round properly
178 const ROUND_FACTOR: i16 = 0x2020;
179 let round_factor = i16x8::splat(ROUND_FACTOR);
180
181 let result_adj = [
182 result[0].saturating_add(round_factor) >> (2 * SHIFT),
183 result[1].saturating_add(round_factor) >> (2 * SHIFT),
184 result[2].saturating_add(round_factor) >> (2 * SHIFT),
185 result[3].saturating_add(round_factor) >> (2 * SHIFT),
186 result[4].saturating_add(round_factor) >> (2 * SHIFT),
187 result[5].saturating_add(round_factor) >> (2 * SHIFT),
188 result[6].saturating_add(round_factor) >> (2 * SHIFT),
189 result[7].saturating_add(round_factor) >> (2 * SHIFT),
190 ];
191
192 let output: [i16; 64] = cast(result_adj);
193
194 #[rustfmt::skip]
195 let expected_output = [
196 118, 92, 110, 83, 77, 93, 144, 198,
197 172, 116, 114, 87, 78, 93, 146, 191,
198 194, 107, 91, 76, 71, 93, 160, 198,
199 196, 100, 80, 74, 67, 92, 174, 209,
200 182, 104, 88, 81, 68, 89, 178, 206,
201 105, 64, 59, 59, 63, 94, 183, 201,
202 35, 27, 28, 37, 72, 121, 203, 204,
203 38, 45, 41, 47, 99, 154, 223, 208
204 ];
205
206 assert_eq!(expected_output, output);
207 }
208
209 /// Implement JPEG IDCT using i32x8. This is most similar to the scalar
210 /// libjpeg version which has slightly different rounding propertis than the 16
211 /// bit version. Some decoders are forced to use this if they want bit-by-bit
212 /// compatibility across all architectures.
213 #[test]
214 fn test_dequantize_and_idct_i32() {
215 fn to_fixed(x: f32) -> i32 {
216 (x * 4096.0 + 0.5) as i32
217 }
218
219 fn kernel_i32(
220 [s0, s1, s2, s3, s4, s5, s6, s7]: [i32x8; 8],
221 rounding_factor: i32,
222 shift_right: i32,
223 ) -> [i32x8; 8] {
224 // kernel x
225 let at = (s2 + s6) * to_fixed(0.5411961);
226
227 let a0 = (s0 + s4) << 12; // multiply by 1, ie 4096 in fixed point)
228 let a1 = (s0 - s4) << 12; // multiply by 1, ie 4096 in fixed point)
229 let a2 = at + s6 * to_fixed(-1.847759065);
230 let a3 = at + s2 * to_fixed(0.765366865);
231
232 let x0 = a0 + a3 + rounding_factor; // add rounding factor here to avoid extra addition
233 let x1 = a1 + a2 + rounding_factor;
234 let x2 = a1 - a2 + rounding_factor;
235 let x3 = a0 - a3 + rounding_factor;
236
237 // kernel t
238 let b0 = s7 + s1;
239 let b1 = s5 + s3;
240 let b2 = s7 + s3;
241 let b3 = s5 + s1;
242
243 let ct = (b2 + b3) * to_fixed(1.175875602);
244 let c0 = ct + b0 * to_fixed(-0.899976223);
245 let c1 = ct + b1 * to_fixed(-2.562915447);
246 let c2 = b2 * to_fixed(-1.961570560);
247 let c3 = b3 * to_fixed(-0.390180644);
248
249 let t0 = s7 * to_fixed(0.298631336) + c0 + c2;
250 let t1 = s5 * to_fixed(2.053119869) + c1 + c3;
251 let t2 = s3 * to_fixed(3.072711026) + c1 + c2;
252 let t3 = s1 * to_fixed(1.501321110) + c0 + c3;
253
254 [
255 (x0 + t3) >> shift_right,
256 (x1 + t2) >> shift_right,
257 (x2 + t1) >> shift_right,
258 (x3 + t0) >> shift_right,
259 (x3 - t0) >> shift_right,
260 (x2 - t1) >> shift_right,
261 (x1 - t2) >> shift_right,
262 (x0 - t3) >> shift_right,
263 ]
264 }
265
266 #[rustfmt::skip]
267 let coefficients: [i32; 8 * 8] = [
268 -14, -39, 58, -2, 3, 3, 0, 1,
269 11, 27, 4, -3, 3, 0, 1, 0,
270 -6, -13, -9, -1, -2, -1, 0, 0,
271 -4, 0, -1, -2, 0, 0, 0, 0,
272 3, 0, 0, 0, 0, 0, 0, 0,
273 -3, -2, 0, 0, 0, 0, 0, 0,
274 0, 0, 0, 0, 0, 0, 0, 0,
275 0, 0, 0, 0, 0, 0, 0, 0
276 ];
277
278 #[rustfmt::skip]
279 let quantization_table: [i32; 8 * 8] = [
280 8, 6, 5, 8, 12, 20, 26, 31,
281 6, 6, 7, 10, 13, 29, 30, 28,
282 7, 7, 8, 12, 20, 29, 35, 28,
283 7, 9, 11, 15, 26, 44, 40, 31,
284 9, 11, 19, 28, 34, 55, 52, 39,
285 12, 18, 28, 32, 41, 52, 57, 46,
286 25, 32, 39, 44, 52, 61, 60, 51,
287 36, 46, 48, 49, 56, 50, 52, 50
288 ];
289
290 let c: [i32x8; 8] = cast(coefficients);
291 let q: [i32x8; 8] = cast(quantization_table);
292
293 let scaled = [
294 c[0] * q[0],
295 c[1] * q[1],
296 c[2] * q[2],
297 c[3] * q[3],
298 c[4] * q[4],
299 c[5] * q[5],
300 c[6] * q[6],
301 c[7] * q[7],
302 ];
303
304 // add rounding factor before shifting right
305 let pass1 = kernel_i32(scaled, 1 << 9, 10);
306 let transpose1 = i32x8::transpose(pass1);
307
308 // add rounding factor before shifting right (include rebasing from -128..128
309 // to 0..256)
310 let pass2 = kernel_i32(transpose1, 65536 + (128 << 17), 17);
311 let result = i32x8::transpose(pass2);
312
313 let output: [i32; 64] = cast(result);
314
315 // same as other DCT test with some minor rounding differences
316 #[rustfmt::skip]
317 let expected_output = [
318 118, 92, 110, 83, 77, 93, 144, 198,
319 172, 116, 114, 87, 78, 93, 146, 191,
320 194, 107, 91, 76, 71, 93, 160, 198,
321 196, 100, 80, 74, 67, 92, 174, 209,
322 182, 104, 88, 81, 68, 89, 178, 206,
323 105, 64, 59, 59, 63, 94, 183, 201,
324 35, 27, 28, 37, 72, 121, 203, 204,
325 37, 45, 41, 47, 98, 154, 223, 208];
326
327 assert_eq!(expected_output, output);
328 }
329
330 // Example implementation of a branch-free division algorithm using u32x8.
331
332 /// Ported from libdivide. Example to show how to use the branchfree division
333 /// with this library.
334 fn internal_gen_branch_free_u32(d: u32) -> (u32, u32) {
335 fn div_rem(a: u64, b: u64) -> (u64, u64) {
336 (a / b, a % b)
337 }
338
339 // branchfree cannot be one or zero
340 assert!(d > 1);
341
342 let floor_log_2_d = (32u32 - 1) - d.leading_zeros();
343
344 // Power of 2
345 if (d & (d - 1)) == 0 {
346 // We need to subtract 1 from the shift value in case of an unsigned
347 // branchfree divider because there is a hardcoded right shift by 1
348 // in its division algorithm. Because of this we also need to add back
349 // 1 in its recovery algorithm.
350 (0, floor_log_2_d - 1)
351 } else {
352 let (proposed_m, rem) = div_rem(1u64 << (floor_log_2_d + 32), d as u64);
353
354 let mut proposed_m = proposed_m as u32;
355 let rem = rem as u32;
356 assert!(rem > 0 && rem < d);
357
358 // This power works if e < 2**floor_log_2_d.
359 // We have to use the general 33-bit algorithm. We need to compute
360 // (2**power) / d. However, we already have (2**(power-1))/d and
361 // its remainder. By doubling both, and then correcting the
362 // remainder, we can compute the larger division.
363 // don't care about overflow here - in fact, we expect it
364 proposed_m = proposed_m.wrapping_add(proposed_m);
365 let twice_rem = rem.wrapping_add(rem);
366 if twice_rem >= d || twice_rem < rem {
367 proposed_m += 1;
368 }
369
370 (1 + proposed_m, floor_log_2_d)
371 // result.more's shift should in general be ceil_log_2_d. But if we
372 // used the smaller power, we subtract one from the shift because we're
373 // using the smaller power. If we're using the larger power, we
374 // subtract one from the shift because it's taken care of by the add
375 // indicator. So floor_log_2_d happens to be correct in both cases.
376 }
377 }
378
379 /// Generate magic and shift values for branch-free division.
380 fn generate_branch_free_divide_magic_shift(denom: u32x8) -> (u32x8, u32x8) {
381 let mut magic = u32x8::ZERO;
382 let mut shift = u32x8::ZERO;
383 for i in 0..magic.as_array_ref().len() {
384 let (m, s) = internal_gen_branch_free_u32(denom.as_array_ref()[i]);
385 magic.as_array_mut()[i] = m;
386 shift.as_array_mut()[i] = s;
387 }
388
389 (magic, shift)
390 }
391
392 // using the previously generated magic and shift, calculate the division
393 fn branch_free_divide(numerator: u32x8, magic: u32x8, shift: u32x8) -> u32x8 {
394 let q = u32x8::mul_keep_high(numerator, magic);
395
396 let t = ((numerator - q) >> 1) + q;
397 t >> shift
398 }
399
400 #[test]
401 fn impl_u32x8_branch_free_divide() {
402 crate::test_random_vector_vs_scalar(
403 |a: u32x8, b| {
404 // never divide by 0 or 1 (since the branch free division doesn't support
405 // division by 1)
406 let b = b.max(u32x8::splat(2));
407 let (magic, shift) = generate_branch_free_divide_magic_shift(b);
408 branch_free_divide(a, magic, shift)
409 },
410 |a, b| a / b.max(2),
411 );
412 }