|
| 1 | +//! Run-time feature detection on PowerPC64. |
| 2 | +use super::{bit, linux}; |
| 3 | + |
| 4 | +#[macro_export] |
| 5 | +#[doc(hidden)] |
| 6 | +macro_rules! __unstable_detect_feature { |
| 7 | + ("altivec") => { |
| 8 | + $crate::vendor::__unstable_detect_feature($crate::vendor::__Feature::altivec{}) |
| 9 | + }; |
| 10 | + ("vsx") => { |
| 11 | + $crate::vendor::__unstable_detect_feature($crate::vendor::__Feature::vsx{}) |
| 12 | + }; |
| 13 | + ("power8") => { |
| 14 | + $crate::vendor::__unstable_detect_feature($crate::vendor::__Feature::power8{}) |
| 15 | + }; |
| 16 | + ($t:tt) => { compile_error!(concat!("unknown PowerPC target feature: ", $t)) }; |
| 17 | +} |
| 18 | + |
| 19 | +/// PowerPC CPU Feature enum. Each variant denotes a position in a bitset |
| 20 | +/// for a particular feature. |
| 21 | +/// |
| 22 | +/// PLEASE: do not use this, it is an implementation detail subject to change. |
| 23 | +#[doc(hidden)] |
| 24 | +#[allow(non_camel_case_types)] |
| 25 | +#[repr(u8)] |
| 26 | +pub enum __Feature { |
| 27 | + /// Altivec |
| 28 | + altivec, |
| 29 | + /// VSX |
| 30 | + vsx, |
| 31 | + /// Power8 |
| 32 | + power8, |
| 33 | +} |
| 34 | + |
| 35 | +pub fn detect_features<T: linux::FeatureQuery>(mut x: T) -> usize { |
| 36 | + let mut value: usize = 0; |
| 37 | + { |
| 38 | + let mut enable_feature = |f| { |
| 39 | + if x.has_feature(&f) { |
| 40 | + value = bit::set(value, f as u32); |
| 41 | + } |
| 42 | + }; |
| 43 | + enable_feature(__Feature::altivec); |
| 44 | + enable_feature(__Feature::vsx); |
| 45 | + enable_feature(__Feature::power8); |
| 46 | + } |
| 47 | + value |
| 48 | +} |
| 49 | + |
| 50 | +/// Probe the ELF Auxiliary vector for hardware capabilities |
| 51 | +/// |
| 52 | +/// The values are part of the platform-specific [asm/cputable.h][cputable] |
| 53 | +/// |
| 54 | +/// [cputable]: https://github.com/torvalds/linux/blob/master/arch/powerpc/include/uapi/asm/cputable.h |
| 55 | +impl linux::FeatureQuery for linux::AuxVec { |
| 56 | + fn has_feature(&mut self, x: &__Feature) -> bool { |
| 57 | + use self::__Feature::*; |
| 58 | + match *x { |
| 59 | + altivec => self.lookup(linux::AT::HWCAP) |
| 60 | + .map(|caps| caps & 0x10000000 != 0) |
| 61 | + .unwrap_or(false), |
| 62 | + vsx => self.lookup(linux::AT::HWCAP) |
| 63 | + .map(|caps| caps & 0x00000080 != 0) |
| 64 | + .unwrap_or(false), |
| 65 | + power8 => self.lookup(linux::AT::HWCAP2) |
| 66 | + .map(|caps| caps & 0x80000000 != 0) |
| 67 | + .unwrap_or(false), |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +/// Check for altivec support only |
| 73 | +/// |
| 74 | +/// PowerPC's /proc/cpuinfo lacks a proper Feature field, |
| 75 | +/// but `altivec` support is indicated in the `cpu` field. |
| 76 | +impl linux::FeatureQuery for linux::CpuInfo { |
| 77 | + fn has_feature(&mut self, x: &__Feature) -> bool { |
| 78 | + use self::__Feature::*; |
| 79 | + match *x { |
| 80 | + altivec => self.field("cpu").has("altivec"), |
| 81 | + _ => false |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +#[cfg(test)] |
| 87 | +mod tests { |
| 88 | + #[test] |
| 89 | + fn detect_feature() { |
| 90 | + println!("altivec {}", __unstable_detect_feature!("altivec")); |
| 91 | + println!("vsx {}", __unstable_detect_feature!("vsx")); |
| 92 | + println!("power8 {}", __unstable_detect_feature!("power8")); |
| 93 | + } |
| 94 | +} |
0 commit comments