Skip to content

Commit c2a6849

Browse files
committed
feat: use is_x86_feature_detected! when std is enabled
1 parent 5c7478f commit c2a6849

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ rustdoc-args = ["--cfg", "docsrs"]
2121
cfg-if = "1"
2222
hex = { version = "~0.4.2", optional = true, default-features = false }
2323
serde = { version = "1.0", optional = true, default-features = false }
24-
proptest = { version = "1.3.1", default-features = false, optional = true }
24+
25+
proptest = { version = "1.4", optional = true, default-features = false }
2526

2627
[target.'cfg(any(target_arch = "x86", target_arch = "x86_64"))'.dependencies]
2728
cpufeatures = "0.2"
@@ -38,14 +39,14 @@ default = ["std"]
3839
std = ["hex?/std", "serde?/std", "proptest?/std", "alloc"]
3940
alloc = ["hex?/alloc", "serde?/alloc", "proptest?/alloc"]
4041

41-
# Serde support. Use with `#[serde(with = "const_hex")]`
42+
# Serde support. Use with `#[serde(with = "const_hex")]`.
4243
serde = ["hex?/serde", "dep:serde"]
4344

4445
# Use `hex`'s traits instead of our own.
4546
# This should not be needed most of the time.
4647
hex = ["dep:hex"]
4748

48-
# Forces generic implementation, overriding any specialized implementation (e.g. x86 or aarch64)
49+
# Forces generic implementation, overriding any specialized implementation (e.g. x86 or aarch64).
4950
force-generic = []
5051

5152
# Support for the `portable-simd` nightly feature.

src/arch/x86.rs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,33 @@ const CHUNK_SIZE_AVX: usize = core::mem::size_of::<__m256i>();
1414

1515
const T_MASK: i32 = 65535;
1616

17-
cpufeatures::new!(cpuid_sse2, "sse2");
18-
cpufeatures::new!(cpuid_ssse3, "sse2", "ssse3");
19-
cpufeatures::new!(cpuid_avx2, "avx2");
17+
cfg_if::cfg_if! {
18+
if #[cfg(feature = "std")] {
19+
#[inline(always)]
20+
pub(super) fn has_sse2() -> bool {
21+
is_x86_feature_detected!("sse2")
22+
}
23+
#[inline(always)]
24+
pub(super) fn has_ssse3() -> bool {
25+
is_x86_feature_detected!("ssse3")
26+
}
27+
#[inline(always)]
28+
pub(super) fn has_avx2() -> bool {
29+
is_x86_feature_detected!("avx2")
30+
}
31+
} else {
32+
cpufeatures::new!(cpuid_sse2, "sse2");
33+
use cpuid_sse2::get as has_sse2;
34+
cpufeatures::new!(cpuid_ssse3, "ssse3");
35+
use cpuid_ssse3::get as has_ssse3;
36+
cpufeatures::new!(cpuid_avx2, "avx2");
37+
use cpuid_avx2::get as has_avx2;
38+
}
39+
}
2040

2141
#[inline]
2242
pub(crate) unsafe fn encode<const UPPER: bool>(input: &[u8], output: *mut u8) {
23-
if input.len() < CHUNK_SIZE_SSE || !cpuid_ssse3::get() {
43+
if !has_ssse3() || input.len() < CHUNK_SIZE_SSE {
2444
return generic::encode::<UPPER>(input, output);
2545
}
2646
encode_ssse3::<UPPER>(input, output);
@@ -65,7 +85,7 @@ unsafe fn encode_ssse3<const UPPER: bool>(input: &[u8], output: *mut u8) {
6585

6686
#[inline]
6787
pub(crate) fn check(input: &[u8]) -> bool {
68-
if input.len() < CHUNK_SIZE_SSE || !cpuid_sse2::get() {
88+
if !has_sse2() || input.len() < CHUNK_SIZE_SSE {
6989
return generic::check(input);
7090
}
7191
unsafe { check_sse2(input) }
@@ -111,14 +131,13 @@ unsafe fn check_sse2(input: &[u8]) -> bool {
111131

112132
#[inline]
113133
pub(crate) unsafe fn decode_unchecked(input: &[u8], output: &mut [u8]) {
114-
if input.len() < CHUNK_SIZE_AVX || !cpuid_avx2::get() {
134+
if !has_avx2() || input.len() < CHUNK_SIZE_AVX {
115135
return generic::decode_unchecked(input, output);
116136
}
117137
decode_avx2(input, output);
118138
}
119139

120140
/// Modified from [`faster-hex`](https://github.com/nervosnetwork/faster-hex/blob/856aba7b141a5fe16113fae110d535065882f25a/src/decode.rs).
121-
#[inline(never)]
122141
#[target_feature(enable = "avx2")]
123142
unsafe fn decode_avx2(mut input: &[u8], mut output: &mut [u8]) {
124143
#[rustfmt::skip]

0 commit comments

Comments
 (0)