| 1 |
// Copyright (c) 2019-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 |
/// For classifying the number of rows and columns in a transform. Used to |
| 11 |
/// select the operations to perform for different vector lengths. |
| 12 |
#[derive(Debug, Clone, Copy)] |
| 13 |
pub enum SizeClass1D { |
| 14 |
X4, |
| 15 |
X8UP, |
| 16 |
} |
| 17 |
|
| 18 |
impl SizeClass1D { |
| 19 |
#[inline] |
| 20 |
pub fn from_length(len: usize) -> Self { |
| 21 |
assert!(len.is_power_of_two()); |
| 22 |
use SizeClass1D::*; |
| 23 |
match len { |
| 24 |
4 => X4, |
| 25 |
_ => X8UP, |
| 26 |
} |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
pub fn cast<const N: usize, T>(x: &[T]) -> &[T; N] { |
| 31 |
// SAFETY: we perform a bounds check with [..N], |
| 32 |
// so casting to *const [T; N] is valid because the bounds |
| 33 |
// check guarantees that x has N elements |
| 34 |
unsafe { &*(&x[..N] as *const [T] as *const [T; N]) } |
| 35 |
} |
| 36 |
|
| 37 |
pub fn cast_mut<const N: usize, T>(x: &mut [T]) -> &mut [T; N] { |
| 38 |
// SAFETY: we perform a bounds check with [..N], |
| 39 |
// so casting to *mut [T; N] is valid because the bounds |
| 40 |
// check guarantees that x has N elements |
| 41 |
unsafe { &mut *(&mut x[..N] as *mut [T] as *mut [T; N]) } |
| 42 |
} |
| 43 |
|
| 44 |
#[cfg(test)] |
| 45 |
mod test { |
| 46 |
use crate::cpu_features::*; |
| 47 |
use crate::transform::{forward_transform, get_valid_txfm_types, TxSize}; |
| 48 |
use crate::util::slice_assume_init_mut; |
| 49 |
use rand::Rng; |
| 50 |
use std::mem::MaybeUninit; |
| 51 |
|
| 52 |
// Ensure that the simd results match the rust code |
| 53 |
#[test] |
| 54 |
fn test_forward_transform() { |
| 55 |
for &cpu in |
| 56 |
&CpuFeatureLevel::all()[1..=CpuFeatureLevel::default().as_index()] |
| 57 |
{ |
| 58 |
println!("Testing {:?}", cpu); |
| 59 |
test_forward_transform_simd(cpu); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
fn test_forward_transform_simd(cpu: CpuFeatureLevel) { |
| 64 |
let mut rng = rand::rng(); |
| 65 |
|
| 66 |
let tx_sizes = { |
| 67 |
use TxSize::*; |
| 68 |
[ |
| 69 |
TX_4X4, TX_8X8, TX_16X16, TX_32X32, TX_64X64, TX_4X8, TX_8X4, TX_8X16, |
| 70 |
TX_16X8, TX_16X32, TX_32X16, TX_32X64, TX_64X32, TX_4X16, TX_16X4, |
| 71 |
TX_8X32, TX_32X8, TX_16X64, TX_64X16, |
| 72 |
] |
| 73 |
}; |
| 74 |
|
| 75 |
for &tx_size in &tx_sizes { |
| 76 |
let area = tx_size.area(); |
| 77 |
|
| 78 |
let input: Vec<i16> = |
| 79 |
(0..area).map(|_| rng.random_range(-255..256)).collect(); |
| 80 |
|
| 81 |
for &tx_type in get_valid_txfm_types(tx_size) { |
| 82 |
let mut output_ref = vec![MaybeUninit::new(0i16); area]; |
| 83 |
let mut output_simd = vec![MaybeUninit::new(0i16); area]; |
| 84 |
|
| 85 |
println!("Testing combination {:?}, {:?}", tx_size, tx_type); |
| 86 |
forward_transform( |
| 87 |
&input[..], |
| 88 |
&mut output_ref[..], |
| 89 |
tx_size.width(), |
| 90 |
tx_size, |
| 91 |
tx_type, |
| 92 |
8, |
| 93 |
CpuFeatureLevel::RUST, |
| 94 |
); |
| 95 |
let output_ref = unsafe { slice_assume_init_mut(&mut output_ref[..]) }; |
| 96 |
forward_transform( |
| 97 |
&input[..], |
| 98 |
&mut output_simd[..], |
| 99 |
tx_size.width(), |
| 100 |
tx_size, |
| 101 |
tx_type, |
| 102 |
8, |
| 103 |
cpu, |
| 104 |
); |
| 105 |
let output_simd = |
| 106 |
unsafe { slice_assume_init_mut(&mut output_simd[..]) }; |
| 107 |
assert_eq!(output_ref, output_simd) |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
} |