Skip to content

Commit be95083

Browse files
sayantnAmanieu
authored andcommitted
Add detection for SHA512, SM3 and SM4
Cannot cross-verify with `cupid` because they do not have these features yet.
1 parent 3dd9579 commit be95083

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/mod.rs

+12
Original file line numberDiff line numberDiff line change
@@ -325,3 +325,15 @@ mod nvptx;
325325
#[cfg(any(target_arch = "loongarch64", doc))]
326326
#[doc(cfg(target_arch = "loongarch64"))]
327327
mod loongarch64;
328+
329+
// TODO: remove after merge of rustc #126704
330+
#[unstable(feature = "sha512_sm_x86", issue = "126624")]
331+
unsafe fn dummy() {
332+
// This has to be here until PR #126704 gets merged into rustc,
333+
// because otherwise rustc cannot compile because aarch64 also has
334+
// a target feature named sm4, and that is stable. For `doc` env this
335+
// gets compiled also in x86, but in x86 the feature sm4 is unstable
336+
// So we need `feature(sha512_sm_x86)` somewhere, but if we place it without
337+
// any unstable attr, rustc cannot compile stage0, because it doesn't know about
338+
// this feature yet.
339+
}

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"`
@@ -138,6 +141,12 @@ features! {
138141
/// AVX (Advanced Vector Extensions)
139142
@FEATURE: #[stable(feature = "simd_x86", since = "1.27.0")] avx2: "avx2";
140143
/// AVX2 (Advanced Vector Extensions 2)
144+
@FEATURE: #[unstable(feature = "sha512_sm_x86", issue = "126624")] sha512: "sha512";
145+
/// SHA512
146+
@FEATURE: #[unstable(feature = "sha512_sm_x86", issue = "126624")] sm3: "sm3";
147+
/// SM3
148+
@FEATURE: #[unstable(feature = "sha512_sm_x86", issue = "126624")] sm4: "sm4";
149+
/// SM4
141150
@FEATURE: #[stable(feature = "simd_x86", since = "1.27.0")] avx512f: "avx512f" ;
142151
/// AVX-512 F (Foundation)
143152
@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
@@ -217,6 +217,10 @@ pub(crate) fn detect_features() -> cache::Initializer {
217217
enable(extended_features_edx_leaf_1, 5, Feature::avxneconvert);
218218
enable(extended_features_edx_leaf_1, 10, Feature::avxvnniint16);
219219

220+
enable(extended_features_eax_leaf_1, 0, Feature::sha512);
221+
enable(extended_features_eax_leaf_1, 1, Feature::sm3);
222+
enable(extended_features_eax_leaf_1, 2, Feature::sm4);
223+
220224
// For AVX-512 the OS also needs to support saving/restoring
221225
// the extended state, only then we enable AVX-512 support:
222226
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, avx512_target_feature)]
3+
#![feature(stdarch_internal, avx512_target_feature, 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)