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(all(any(target_arch = "x86", target_arch = "x86_64"), not(hax)))]
13mod x86;
14
15#[cfg(all(target_arch = "aarch64", target_os = "linux", not(hax)))]
16mod linux_arm;
17#[cfg(all(target_arch = "aarch64", target_os = "macos", not(hax)))]
18mod macos_arm;
19
20#[cfg(test)]
21mod test;
22
23pub use platform::*;
24
25#[cfg(hax)]
26mod platform {
27    pub fn simd128_support() -> bool {
28        false
29    }
30    pub fn simd256_support() -> bool {
31        false
32    }
33    pub fn x25519_support() -> bool {
34        false
35    }
36    pub fn bmi2_adx_support() -> bool {
37        false
38    }
39    pub fn pmull_support() -> bool {
40        false
41    }
42    pub fn adv_simd_support() -> bool {
43        false
44    }
45    pub fn aes_ni_support() -> bool {
46        false
47    }
48    pub fn sha256_support() -> bool {
49        false
50    }
51}
52
53#[cfg(not(hax))]
54mod platform {
55    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
56    use super::x86::{self as cpu_id, Feature};
57
58    // TODO: Check for z14 or z15
59    pub fn simd128_support() -> bool {
60        #[cfg(all(target_arch = "aarch64", target_os = "macos"))]
61        {
62            use crate::macos_arm::*;
63            adv_simd()
64        }
65
66        #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
67        {
68            cpu_id::supported(Feature::sse2)
69                && cpu_id::supported(Feature::sse3)
70                && cpu_id::supported(Feature::sse4_1)
71                && cpu_id::supported(Feature::avx)
72        }
73
74        #[cfg(all(target_arch = "aarch64", target_os = "linux"))]
75        {
76            use crate::linux_arm::*;
77            adv_simd()
78        }
79
80        #[cfg(not(any(
81            all(target_arch = "aarch64", any(target_os = "linux", target_os = "macos")),
82            target_arch = "x86",
83            target_arch = "x86_64"
84        )))]
85        {
86            false
87        }
88    }
89
90    pub fn simd256_support() -> bool {
91        #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
92        return cpu_id::supported(Feature::avx2);
93
94        #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
95        false
96    }
97
98    pub fn x25519_support() -> bool {
99        #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
100        return cpu_id::supported(Feature::bmi2) && cpu_id::supported(Feature::adx);
101
102        #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
103        false
104    }
105
106    pub fn bmi2_adx_support() -> bool {
107        #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
108        return cpu_id::supported(Feature::bmi2) && cpu_id::supported(Feature::adx);
109
110        #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
111        false
112    }
113
114    /// Check whether p(cl)mull is supported
115    pub fn pmull_support() -> bool {
116        #[cfg(all(target_arch = "aarch64", target_os = "macos"))]
117        {
118            use crate::macos_arm::*;
119            pmull()
120        }
121
122        #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
123        {
124            cpu_id::supported(Feature::pclmulqdq)
125        }
126
127        #[cfg(all(target_arch = "aarch64", target_os = "linux"))]
128        {
129            use crate::linux_arm::*;
130            pmull()
131        }
132
133        #[cfg(not(any(
134            all(target_arch = "aarch64", any(target_os = "linux", target_os = "macos")),
135            target_arch = "x86",
136            target_arch = "x86_64"
137        )))]
138        {
139            false
140        }
141    }
142
143    /// Check whether advanced SIMD features are supported
144    pub fn adv_simd_support() -> bool {
145        #[cfg(all(target_arch = "aarch64", target_os = "macos"))]
146        {
147            use crate::macos_arm::*;
148            adv_simd()
149        }
150
151        #[cfg(all(target_arch = "aarch64", target_os = "linux"))]
152        {
153            use crate::linux_arm::*;
154            adv_simd()
155        }
156
157        #[cfg(not(all(target_arch = "aarch64", any(target_os = "linux", target_os = "macos"))))]
158        {
159            false
160        }
161    }
162
163    /// Check whether AES is supported
164    pub fn aes_ni_support() -> bool {
165        #[cfg(all(target_arch = "aarch64", target_os = "macos"))]
166        {
167            use crate::macos_arm::*;
168            aes()
169        }
170
171        #[cfg(all(target_arch = "aarch64", target_os = "linux"))]
172        {
173            use crate::linux_arm::*;
174            aes()
175        }
176
177        #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
178        {
179            cpu_id::supported(Feature::avx)
180                && cpu_id::supported(Feature::sse)
181                && cpu_id::supported(Feature::aes)
182                && cpu_id::supported(Feature::pclmulqdq)
183                && cpu_id::supported(Feature::movbe)
184        }
185
186        #[cfg(not(any(
187            all(target_arch = "aarch64", any(target_os = "linux", target_os = "macos")),
188            target_arch = "x86",
189            target_arch = "x86_64"
190        )))]
191        {
192            false
193        }
194    }
195
196    /// Check whether SHA256 is supported
197    pub fn sha256_support() -> bool {
198        #[cfg(all(target_arch = "aarch64", target_os = "macos"))]
199        {
200            use crate::macos_arm::*;
201            sha256()
202        }
203
204        #[cfg(all(target_arch = "aarch64", target_os = "linux"))]
205        {
206            use crate::linux_arm::*;
207            sha256()
208        }
209
210        #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
211        {
212            cpu_id::supported(Feature::sha)
213        }
214
215        #[cfg(not(any(
216            all(target_arch = "aarch64", any(target_os = "linux", target_os = "macos")),
217            target_arch = "x86",
218            target_arch = "x86_64"
219        )))]
220        {
221            false
222        }
223    }
224}