libcrux_platform/
lib.rs

1//! High-level functions to detect available CPU features
2//! at runtime on supported processor architectures and
3//! operation systems
4
5#![no_std]
6
7// Use std for tests
8#[cfg(test)]
9#[macro_use]
10extern crate std;
11
12#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
13mod x86;
14#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
15use x86::{self as cpu_id, Feature};
16
17#[cfg(all(target_arch = "aarch64", target_os = "linux"))]
18mod linux_arm;
19#[cfg(all(target_arch = "aarch64", target_os = "macos"))]
20mod macos_arm;
21
22#[cfg(test)]
23mod test;
24
25// TODO: Check for z14 or z15
26pub fn simd128_support() -> bool {
27    #[cfg(all(target_arch = "aarch64", target_os = "macos"))]
28    {
29        use macos_arm::*;
30        adv_simd()
31    }
32
33    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
34    {
35        cpu_id::supported(Feature::sse2)
36            && cpu_id::supported(Feature::sse3)
37            && cpu_id::supported(Feature::sse4_1)
38            && cpu_id::supported(Feature::avx)
39    }
40
41    #[cfg(all(target_arch = "aarch64", target_os = "linux"))]
42    {
43        use linux_arm::*;
44        adv_simd()
45    }
46
47    #[cfg(not(any(
48        all(target_arch = "aarch64", any(target_os = "linux", target_os = "macos")),
49        target_arch = "x86",
50        target_arch = "x86_64"
51    )))]
52    {
53        false
54    }
55}
56
57pub fn simd256_support() -> bool {
58    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
59    return cpu_id::supported(Feature::avx2);
60
61    #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
62    false
63}
64
65pub fn x25519_support() -> bool {
66    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
67    return cpu_id::supported(Feature::bmi2) && cpu_id::supported(Feature::adx);
68
69    #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
70    false
71}
72
73pub fn bmi2_adx_support() -> bool {
74    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
75    return cpu_id::supported(Feature::bmi2) && cpu_id::supported(Feature::adx);
76
77    #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
78    false
79}
80
81/// Check whether p(cl)mull is supported
82pub fn pmull_support() -> bool {
83    #[cfg(all(target_arch = "aarch64", target_os = "macos"))]
84    {
85        use crate::macos_arm::*;
86        pmull()
87    }
88
89    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
90    {
91        cpu_id::supported(Feature::pclmulqdq)
92    }
93
94    #[cfg(all(target_arch = "aarch64", target_os = "linux"))]
95    {
96        use crate::linux_arm::*;
97        pmull()
98    }
99
100    #[cfg(not(any(
101        all(target_arch = "aarch64", any(target_os = "linux", target_os = "macos")),
102        target_arch = "x86",
103        target_arch = "x86_64"
104    )))]
105    {
106        false
107    }
108}
109
110/// Check whether advanced SIMD features are supported
111pub fn adv_simd_support() -> bool {
112    #[cfg(all(target_arch = "aarch64", target_os = "macos"))]
113    {
114        use macos_arm::*;
115        adv_simd()
116    }
117
118    #[cfg(all(target_arch = "aarch64", target_os = "linux"))]
119    {
120        use linux_arm::*;
121        adv_simd()
122    }
123
124    #[cfg(not(all(target_arch = "aarch64", any(target_os = "linux", target_os = "macos"))))]
125    {
126        false
127    }
128}
129
130/// Check whether AES is supported
131pub fn aes_ni_support() -> bool {
132    #[cfg(all(target_arch = "aarch64", target_os = "macos"))]
133    {
134        use crate::macos_arm::*;
135        aes()
136    }
137
138    #[cfg(all(target_arch = "aarch64", target_os = "linux"))]
139    {
140        use crate::linux_arm::*;
141        aes()
142    }
143
144    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
145    {
146        cpu_id::supported(Feature::avx)
147            && cpu_id::supported(Feature::sse)
148            && cpu_id::supported(Feature::aes)
149            && cpu_id::supported(Feature::pclmulqdq)
150            && cpu_id::supported(Feature::movbe)
151    }
152
153    #[cfg(not(any(
154        all(target_arch = "aarch64", any(target_os = "linux", target_os = "macos")),
155        target_arch = "x86",
156        target_arch = "x86_64"
157    )))]
158    {
159        false
160    }
161}
162
163/// Check whether SHA256 is supported
164pub fn sha256_support() -> bool {
165    #[cfg(all(target_arch = "aarch64", target_os = "macos"))]
166    {
167        use crate::macos_arm::*;
168        sha256()
169    }
170
171    #[cfg(all(target_arch = "aarch64", target_os = "linux"))]
172    {
173        use crate::linux_arm::*;
174        sha256()
175    }
176
177    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
178    {
179        cpu_id::supported(Feature::sha)
180    }
181
182    #[cfg(not(any(
183        all(target_arch = "aarch64", any(target_os = "linux", target_os = "macos")),
184        target_arch = "x86",
185        target_arch = "x86_64"
186    )))]
187    {
188        false
189    }
190}