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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
use arch::detect::Feature;
use arch::detect::cache;
use arch::detect::bit;
use super::auxvec;
use super::cpuinfo;
#[inline]
pub fn check_for(x: Feature) -> bool {
cache::test(x as u32, detect_features)
}
fn detect_features() -> cache::Initializer {
if let Ok(auxv) = auxvec::auxv() {
let hwcap: AtHwcap = auxv.into();
return hwcap.cache();
}
if let Ok(c) = cpuinfo::CpuInfo::new() {
let hwcap: AtHwcap = c.into();
return hwcap.cache();
}
cache::Initializer::default()
}
struct AtHwcap {
fp: bool,
asimd: bool,
aes: bool,
pmull: bool,
sha1: bool,
sha2: bool,
crc32: bool,
atomics: bool,
fphp: bool,
asimdhp: bool,
asimdrdm: bool,
lrcpc: bool,
asimddp: bool,
sve: bool,
}
impl From<auxvec::AuxVec> for AtHwcap {
fn from(auxv: auxvec::AuxVec) -> Self {
AtHwcap {
fp: bit::test(auxv.hwcap, 0),
asimd: bit::test(auxv.hwcap, 1),
aes: bit::test(auxv.hwcap, 3),
pmull: bit::test(auxv.hwcap, 4),
sha1: bit::test(auxv.hwcap, 5),
sha2: bit::test(auxv.hwcap, 6),
crc32: bit::test(auxv.hwcap, 7),
atomics: bit::test(auxv.hwcap, 8),
fphp: bit::test(auxv.hwcap, 9),
asimdhp: bit::test(auxv.hwcap, 10),
asimdrdm: bit::test(auxv.hwcap, 12),
lrcpc: bit::test(auxv.hwcap, 15),
asimddp: bit::test(auxv.hwcap, 20),
sve: bit::test(auxv.hwcap, 22),
}
}
}
impl From<cpuinfo::CpuInfo> for AtHwcap {
fn from(c: cpuinfo::CpuInfo) -> Self {
let f = &c.field("Features");
AtHwcap {
fp: f.has("fp"),
asimd: f.has("asimd"),
aes: f.has("aes"),
pmull: f.has("pmull"),
sha1: f.has("sha1"),
sha2: f.has("sha2"),
crc32: f.has("crc32"),
atomics: f.has("atomics"),
fphp: f.has("fphp"),
asimdhp: f.has("asimdhp"),
asimdrdm: f.has("asimdrdm"),
lrcpc: f.has("lrcpc"),
asimddp: f.has("asimddp"),
sve: f.has("sve"),
}
}
}
impl AtHwcap {
fn cache(self) -> cache::Initializer {
let mut value = cache::Initializer::default();
{
let mut enable_feature = |f, enable| {
if enable {
value.set(f as u32);
}
};
enable_feature(Feature::fp, self.fp);
enable_feature(Feature::fp16, self.fp && self.fphp);
enable_feature(Feature::pmull, self.pmull);
enable_feature(Feature::crc, self.crc32);
enable_feature(Feature::lse, self.atomics);
enable_feature(Feature::rcpc, self.lrcpc);
let asimd = self.fp && self.asimd && (!self.fphp | self.asimdhp);
enable_feature(Feature::asimd, asimd);
enable_feature(Feature::rdm, self.asimdrdm && asimd);
enable_feature(Feature::dotprod, self.asimddp && asimd);
enable_feature(Feature::sve, self.sve && asimd);
enable_feature(Feature::crypto, self.aes && self.pmull && self.sha1 && self.sha2);
}
value
}
}