Skip to content

Commit 921e76b

Browse files
committed
Add detection for SHA512, SM3 and SM4
Cannot cross-verify with `cupid` because they do not have these features yet.
1 parent 86098df commit 921e76b

File tree

6 files changed

+38
-2
lines changed

6 files changed

+38
-2
lines changed

crates/core_arch/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
asm_const,
3434
target_feature_11,
3535
generic_arg_infer,
36-
asm_experimental_arch
36+
asm_experimental_arch,
37+
sha512_sm_x86
3738
)]
3839
#![cfg_attr(test, feature(test, abi_vectorcall, stdarch_internal))]
3940
#![deny(clippy::missing_inline_in_public_items)]

crates/core_arch/src/x86/sha.rs

+12
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,18 @@ pub unsafe fn _mm_sha256rnds2_epu32(a: __m128i, b: __m128i, k: __m128i) -> __m12
118118
transmute(sha256rnds2(a.as_i32x4(), b.as_i32x4(), k.as_i32x4()))
119119
}
120120

121+
// TODO: remove after merge of rustc #126704
122+
#[unstable(feature = "sha512_sm_x86", issue = "126624")]
123+
unsafe fn dummy() {
124+
// This has to be here until PR #126704 gets merged into rustc,
125+
// because otherwise rustc cannot compile because aarch64 also has
126+
// a target feature named sm4, and that is stable. For `doc` env this
127+
// gets compiled also in x86, but in x86 the feature sm4 is unstable
128+
// So we need `feature(sha512_sm_x86)` somewhere, but if we place it without
129+
// any unstable attr, rustc cannot compile stage0, because it doesn't know about
130+
// this feature yet.
131+
}
132+
121133
#[cfg(test)]
122134
mod tests {
123135
use std::{

crates/std_detect/src/detect/arch/x86.rs

+9
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ features! {
5757
/// * `"sha"`
5858
/// * `"avx"`
5959
/// * `"avx2"`
60+
/// * `"sha512"`
61+
/// * `"sm3"`
62+
/// * `"sm4"`
6063
/// * `"avx512f"`
6164
/// * `"avx512cd"`
6265
/// * `"avx512er"`
@@ -133,6 +136,12 @@ features! {
133136
/// AVX (Advanced Vector Extensions)
134137
@FEATURE: #[stable(feature = "simd_x86", since = "1.27.0")] avx2: "avx2";
135138
/// AVX2 (Advanced Vector Extensions 2)
139+
@FEATURE: #[unstable(feature = "sha512_sm_x86", issue = "126624")] sha512: "sha512";
140+
/// SHA512
141+
@FEATURE: #[unstable(feature = "sha512_sm_x86", issue = "126624")] sm3: "sm3";
142+
/// SM3
143+
@FEATURE: #[unstable(feature = "sha512_sm_x86", issue = "126624")] sm4: "sm4";
144+
/// SM4
136145
@FEATURE: #[stable(feature = "simd_x86", since = "1.27.0")] avx512f: "avx512f" ;
137146
/// AVX-512 F (Foundation)
138147
@FEATURE: #[stable(feature = "simd_x86", since = "1.27.0")] avx512cd: "avx512cd" ;

crates/std_detect/src/detect/os/x86.rs

+4
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,10 @@ pub(crate) fn detect_features() -> cache::Initializer {
203203
enable(proc_info_ecx, 28, Feature::avx);
204204
enable(extended_features_ebx, 5, Feature::avx2);
205205

206+
enable(extended_features_eax_leaf_1, 0, Feature::sha512);
207+
enable(extended_features_eax_leaf_1, 1, Feature::sm3);
208+
enable(extended_features_eax_leaf_1, 2, Feature::sm4);
209+
206210
// For AVX-512 the OS also needs to support saving/restoring
207211
// the extended state, only then we enable AVX-512 support:
208212
if os_avx512_support {

crates/std_detect/tests/cpu-detection.rs

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
#![cfg_attr(target_arch = "arm", feature(stdarch_arm_feature_detection))]
44
#![cfg_attr(target_arch = "powerpc", feature(stdarch_powerpc_feature_detection))]
55
#![cfg_attr(target_arch = "powerpc64", feature(stdarch_powerpc_feature_detection))]
6+
#![cfg_attr(
7+
any(target_arch = "x86", target_arch = "x86_64"),
8+
feature(sha512_sm_x86)
9+
)]
610
#![allow(clippy::unwrap_used, clippy::use_debug, clippy::print_stdout)]
711

812
#[cfg_attr(
@@ -210,6 +214,9 @@ fn x86_all() {
210214
println!("sha: {:?}", is_x86_feature_detected!("sha"));
211215
println!("avx: {:?}", is_x86_feature_detected!("avx"));
212216
println!("avx2: {:?}", is_x86_feature_detected!("avx2"));
217+
println!("sha512: {:?}", is_x86_feature_detected!("sha512"));
218+
println!("sm3: {:?}", is_x86_feature_detected!("sm3"));
219+
println!("sm4: {:?}", is_x86_feature_detected!("sm4"));
213220
println!("avx512f: {:?}", is_x86_feature_detected!("avx512f"));
214221
println!("avx512cd: {:?}", is_x86_feature_detected!("avx512cd"));
215222
println!("avx512er: {:?}", is_x86_feature_detected!("avx512er"));

crates/std_detect/tests/x86-specific.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![cfg(any(target_arch = "x86", target_arch = "x86_64"))]
22
#![allow(internal_features)]
3-
#![feature(stdarch_internal)]
3+
#![feature(stdarch_internal, sha512_sm_x86)]
44

55
extern crate cupid;
66
#[macro_use]
@@ -24,6 +24,9 @@ fn dump() {
2424
println!("f16c: {:?}", is_x86_feature_detected!("f16c"));
2525
println!("avx: {:?}", is_x86_feature_detected!("avx"));
2626
println!("avx2: {:?}", is_x86_feature_detected!("avx2"));
27+
println!("sha512: {:?}", is_x86_feature_detected!("sha512"));
28+
println!("sm3: {:?}", is_x86_feature_detected!("sm3"));
29+
println!("sm4: {:?}", is_x86_feature_detected!("sm4"));
2730
println!("avx512f {:?}", is_x86_feature_detected!("avx512f"));
2831
println!("avx512cd {:?}", is_x86_feature_detected!("avx512cd"));
2932
println!("avx512er {:?}", is_x86_feature_detected!("avx512er"));

0 commit comments

Comments
 (0)