| 1 |
// Copyright (c) 2019-2022, 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 |
use arg_enum_proc_macro::ArgEnum; |
| 11 |
use std::env; |
| 12 |
use std::str::FromStr; |
| 13 |
|
| 14 |
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, ArgEnum)] |
| 15 |
pub enum CpuFeatureLevel { |
| 16 |
RUST, |
| 17 |
NEON, |
| 18 |
} |
| 19 |
|
| 20 |
impl CpuFeatureLevel { |
| 21 |
#[cfg(test)] |
| 22 |
pub(crate) const fn all() -> &'static [Self] { |
| 23 |
use CpuFeatureLevel::*; |
| 24 |
&[RUST, NEON] |
| 25 |
} |
| 26 |
|
| 27 |
pub const fn len() -> usize { |
| 28 |
CpuFeatureLevel::NEON as usize + 1 |
| 29 |
} |
| 30 |
|
| 31 |
#[inline(always)] |
| 32 |
pub fn as_index(self) -> usize { |
| 33 |
self as usize |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
impl Default for CpuFeatureLevel { |
| 38 |
fn default() -> CpuFeatureLevel { |
| 39 |
let detected = CpuFeatureLevel::NEON; |
| 40 |
let manual: CpuFeatureLevel = match env::var("RAV1E_CPU_TARGET") { |
| 41 |
Ok(feature) => CpuFeatureLevel::from_str(&feature).unwrap_or(detected), |
| 42 |
Err(_e) => detected, |
| 43 |
}; |
| 44 |
if manual > detected { |
| 45 |
detected |
| 46 |
} else { |
| 47 |
manual |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
// Create a static lookup table for CPUFeatureLevel enums |
| 53 |
// Note: keys are CpuFeatureLevels without any prefix (no CpuFeatureLevel::) |
| 54 |
macro_rules! cpu_function_lookup_table { |
| 55 |
// version for default visibility |
| 56 |
($name:ident: [$type:ty], default: $empty:expr, [$(($key:ident, $value:expr)),*]) => { |
| 57 |
static $name: [$type; crate::cpu_features::CpuFeatureLevel::len()] = { |
| 58 |
use crate::cpu_features::CpuFeatureLevel; |
| 59 |
#[allow(unused_mut)] |
| 60 |
let mut out: [$type; CpuFeatureLevel::len()] = [$empty; CpuFeatureLevel::len()]; |
| 61 |
|
| 62 |
// Can't use out[0][.] == $empty in static as of rust 1.40 |
| 63 |
#[allow(unused_mut)] |
| 64 |
let mut set: [bool; CpuFeatureLevel::len()] = [false; CpuFeatureLevel::len()]; |
| 65 |
|
| 66 |
#[allow(unused_imports)] |
| 67 |
use CpuFeatureLevel::*; |
| 68 |
$( |
| 69 |
out[$key as usize] = $value; |
| 70 |
set[$key as usize] = true; |
| 71 |
)* |
| 72 |
cpu_function_lookup_table!(waterfall_cpu_features(out, set, [NEON])); |
| 73 |
out |
| 74 |
}; |
| 75 |
}; |
| 76 |
|
| 77 |
($pub:vis, $name:ident: [$type:ty], default: $empty:expr, [$(($key:ident, $value:expr)),*]) => { |
| 78 |
$pub cpu_function_lookup_table!($name: [$type], default: $empty, [$(($key, $value)),*]); |
| 79 |
}; |
| 80 |
// Fill empty output functions with the existent functions they support. |
| 81 |
// cpus should be in order of lowest cpu level to highest |
| 82 |
// Used like an internal function |
| 83 |
// Put in here to avoid adding more public macros |
| 84 |
(waterfall_cpu_features($out:ident, $set:ident, [$($cpu:ident),*])) => { |
| 85 |
// Use an array to emulate if statements (not supported in static as of |
| 86 |
// rust 1.40). Setting best[0] (false) and best[1] (true) is equivalent to |
| 87 |
// doing nothing and overriding our value respectively. |
| 88 |
#[allow(unused_assignments)] |
| 89 |
let mut best = [$out[0], $out[0]]; |
| 90 |
$( |
| 91 |
// If the current entry has a function, update out best function. |
| 92 |
best[$set[$cpu as usize] as usize] = $out[$cpu as usize]; |
| 93 |
// Update our current entry. Does nothing if it already had a function. |
| 94 |
$out[$cpu as usize] = best[1]; |
| 95 |
)* |
| 96 |
}; |
| 97 |
|
| 98 |
|
| 99 |
// use $name_$key as our values |
| 100 |
($pub:vis, $name:ident: [$type:ty], default: $empty:expr, [$($key:ident),*]) => { |
| 101 |
paste::item!{ |
| 102 |
cpu_function_lookup_table!( |
| 103 |
$pub, $name: [$type], default: $empty, [$(($key, [<$name _$key>])),*] |
| 104 |
); |
| 105 |
} |
| 106 |
}; |
| 107 |
|
| 108 |
// version for default visibility |
| 109 |
($name:ident: [$type:ty], default: $empty:expr, [$($key:ident),*]) => { |
| 110 |
paste::item!{ |
| 111 |
cpu_function_lookup_table!( |
| 112 |
$name: [$type], default: $empty, [$(($key, [<$name _$key>])),*] |
| 113 |
); |
| 114 |
} |
| 115 |
}; |
| 116 |
} |