1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
use std::mem::{self, MaybeUninit};
use bitflags::bitflags;
use foreign_types::{foreign_type, ForeignType};
use crate::{error::AsResult, ffi, Result};
#[repr(u32)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Tune {
Generic = ffi::HS_TUNE_FAMILY_GENERIC,
SandyBridge = ffi::HS_TUNE_FAMILY_SNB,
IvyBridge = ffi::HS_TUNE_FAMILY_IVB,
Haswell = ffi::HS_TUNE_FAMILY_HSW,
Silvermont = ffi::HS_TUNE_FAMILY_SLM,
Broadwell = ffi::HS_TUNE_FAMILY_BDW,
Skylake = ffi::HS_TUNE_FAMILY_SKL,
SkylakeServer = ffi::HS_TUNE_FAMILY_SKX,
Goldmont = ffi::HS_TUNE_FAMILY_GLM,
#[cfg(feature = "v5_4")]
Icelake = ffi::HS_TUNE_FAMILY_ICL,
#[cfg(feature = "v5_4")]
IcelakeServer = ffi::HS_TUNE_FAMILY_ICX,
}
impl Default for Tune {
fn default() -> Self {
Self::Generic
}
}
bitflags! {
#[derive(Default)]
pub struct CpuFeatures: u64 {
const AVX2 = ffi::HS_CPU_FEATURES_AVX2 as u64;
const AVX512 = ffi::HS_CPU_FEATURES_AVX512 as u64;
#[cfg(feature = "v5_4")]
const AVX512VBMI = ffi::HS_CPU_FEATURES_AVX512VBMI as u64;
}
}
foreign_type! {
pub unsafe type Platform: Send + Sync {
type CType = ffi::hs_platform_info_t;
fn drop = free_platform_info;
}
}
unsafe fn free_platform_info(p: *mut ffi::hs_platform_info_t) {
mem::drop(Box::from_raw(p));
}
impl Platform {
pub fn is_valid() -> Result<()> {
unsafe { ffi::hs_valid_platform().ok() }
}
pub fn host() -> Result<Platform> {
let mut platform = MaybeUninit::zeroed();
unsafe {
ffi::hs_populate_platform(platform.as_mut_ptr())
.map(|_| Platform::from_ptr(Box::into_raw(Box::new(platform.assume_init()))))
}
}
pub fn new(tune: Tune, cpu_features: CpuFeatures) -> Platform {
unsafe {
Platform::from_ptr(Box::into_raw(Box::new(ffi::hs_platform_info_t {
tune: tune as u32,
cpu_features: cpu_features.bits(),
reserved1: 0,
reserved2: 0,
})))
}
}
}
#[cfg(test)]
pub mod tests {
use super::*;
#[test]
pub fn test_platform() {
assert!(Platform::is_valid().is_ok())
}
}