Skip to content

Commit b583310

Browse files
authored
Remove build-time check for stdsimd feature (#183)
* Remove build-time check for `stdsimd` feature This is blocking rust-lang/rust#117372, which replaces `stdsimd` with more fine-grained features. However the auto-detection in aHash breaks when bootstrapping Rust because it detects the `stdsimd` feature on the old toolchain which is not present on the newly build libcore. This PR removes the build-time detection of the `stdsimd` feature and instead just uses the ARM AES intrinsics directly since they are now stable, but only on AArch64. * Add nightly-arm-aes Cargo feature
1 parent 7fbcf0f commit b583310

File tree

8 files changed

+42
-47
lines changed

8 files changed

+42
-47
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ no-rng = []
4343
# in case this is being used on an architecture lacking core::sync::atomic::AtomicUsize and friends
4444
atomic-polyfill = [ "dep:atomic-polyfill", "once_cell/atomic-polyfill"]
4545

46+
# Nightly-only support for AES intrinsics on 32-bit ARM
47+
nightly-arm-aes = []
48+
4649
[[bench]]
4750
name = "ahash"
4851
path = "tests/bench.rs"

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ The aHash package has the following flags:
5757
This is done using the [getrandom](https://github.com/rust-random/getrandom) crate.
5858
* `compile-time-rng`: For OS targets without access to a random number generator, `compile-time-rng` provides an alternative.
5959
If `getrandom` is unavailable and `compile-time-rng` is enabled, aHash will generate random numbers at compile time and embed them in the binary.
60+
* `nightly-arm-aes`: To use AES instructions on 32-bit ARM, which requires nightly. This is not needed on AArch64.
6061
This allows for DOS resistance even if there is no random number generator available at runtime (assuming the compiled binary is not public).
6162
This makes the binary non-deterministic. (If non-determinism is a problem see [constrandom's documentation](https://github.com/tkaitchuck/constrandom#deterministic-builds))
6263

build.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ fn main() {
77
if let Some(true) = version_check::supports_feature("specialize") {
88
println!("cargo:rustc-cfg=feature=\"specialize\"");
99
}
10-
if let Some(true) = version_check::supports_feature("stdsimd") {
11-
println!("cargo:rustc-cfg=feature=\"stdsimd\"");
12-
}
1310
let arch = env::var("CARGO_CFG_TARGET_ARCH").expect("CARGO_CFG_TARGET_ARCH was not set");
1411
if arch.eq_ignore_ascii_case("x86_64")
1512
|| arch.eq_ignore_ascii_case("aarch64")

src/hash_quality_test.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use core::hash::{Hash, Hasher};
2-
use std::collections::{HashMap};
2+
use std::collections::HashMap;
33

44
fn assert_sufficiently_different(a: u64, b: u64, tolerance: i32) {
55
let (same_byte_count, same_nibble_count) = count_same_bytes_and_nibbles(a, b);
@@ -64,8 +64,7 @@ fn gen_combinations(options: &[u32; 11], depth: u32, so_far: Vec<u32>, combinati
6464

6565
fn test_no_full_collisions<T: Hasher>(gen_hash: impl Fn() -> T) {
6666
let options: [u32; 11] = [
67-
0x00000000, 0x10000000, 0x20000000, 0x40000000, 0x80000000, 0xF0000000,
68-
1, 2, 4, 8, 15
67+
0x00000000, 0x10000000, 0x20000000, 0x40000000, 0x80000000, 0xF0000000, 1, 2, 4, 8, 15,
6968
];
7069
let mut combinations = Vec::new();
7170
gen_combinations(&options, 7, Vec::new(), &mut combinations);
@@ -342,9 +341,12 @@ fn test_sparse<T: Hasher>(hasher: impl Fn() -> T) {
342341
let mut buf = [0u8; 256];
343342
let mut hashes = HashMap::new();
344343
for idx_1 in 0..256 {
345-
for idx_2 in idx_1+1..256 {
344+
for idx_2 in idx_1 + 1..256 {
346345
for value_1 in [1, 2, 4, 8, 16, 32, 64, 128] {
347-
for value_2 in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 15, 16, 17, 18, 20, 24, 31, 32, 33, 48, 64, 96, 127, 128, 129, 192, 254, 255] {
346+
for value_2 in [
347+
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 15, 16, 17, 18, 20, 24, 31, 32, 33, 48, 64, 96, 127, 128, 129,
348+
192, 254, 255,
349+
] {
348350
buf[idx_1] = value_1;
349351
buf[idx_2] = value_2;
350352
let hash_value = hash_with(&buf, &mut hasher());
@@ -437,12 +439,8 @@ mod fallback_tests {
437439
///Basic sanity tests of the cypto properties of aHash.
438440
#[cfg(any(
439441
all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "aes", not(miri)),
440-
all(
441-
any(target_arch = "arm", target_arch = "aarch64"),
442-
any(target_feature = "aes", target_feature = "crypto"),
443-
not(miri),
444-
feature = "stdsimd"
445-
)
442+
all(target_arch = "aarch64", target_feature = "aes", not(miri)),
443+
all(feature = "nightly-arm-aes", target_arch = "arm", target_feature = "aes", not(miri)),
446444
))]
447445
#[cfg(test)]
448446
mod aes_tests {

src/lib.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
//! But this also means that different computers or computers using different versions of ahash may observe different
1414
//! hash values for the same input.
1515
#![cfg_attr(
16-
all(feature = "std", any(feature = "compile-time-rng", feature = "runtime-rng", feature = "no-rng")),
16+
all(
17+
feature = "std",
18+
any(feature = "compile-time-rng", feature = "runtime-rng", feature = "no-rng")
19+
),
1720
doc = r##"
1821
# Basic Usage
1922
AHash provides an implementation of the [Hasher] trait.
@@ -95,8 +98,7 @@ Note the import of [HashMapExt]. This is needed for the constructor.
9598
#![allow(clippy::pedantic, clippy::cast_lossless, clippy::unreadable_literal)]
9699
#![cfg_attr(all(not(test), not(feature = "std")), no_std)]
97100
#![cfg_attr(feature = "specialize", feature(min_specialization))]
98-
#![cfg_attr(feature = "specialize", feature(build_hasher_simple_hash_one))]
99-
#![cfg_attr(feature = "stdsimd", feature(stdsimd))]
101+
#![cfg_attr(feature = "nightly-arm-aes", feature(stdarch_arm_neon_intrinsics))]
100102

101103
#[macro_use]
102104
mod convert;
@@ -106,11 +108,9 @@ mod fallback_hash;
106108
cfg_if::cfg_if! {
107109
if #[cfg(any(
108110
all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "aes", not(miri)),
109-
all(any(target_arch = "arm", target_arch = "aarch64"),
110-
any(target_feature = "aes", target_feature = "crypto"),
111-
not(miri),
112-
feature = "stdsimd")
113-
))] {
111+
all(target_arch = "aarch64", target_feature = "aes", not(miri)),
112+
all(feature = "nightly-arm-aes", target_arch = "arm", target_feature = "aes", not(miri)),
113+
))] {
114114
mod aes_hash;
115115
pub use crate::aes_hash::AHasher;
116116
} else {

src/operations.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,9 @@ pub(crate) fn aesenc(value: u128, xor: u128) -> u128 {
110110
}
111111
}
112112

113-
#[cfg(all(
114-
any(target_arch = "arm", target_arch = "aarch64"),
115-
any(target_feature = "aes", target_feature = "crypto"),
116-
not(miri),
117-
feature = "stdsimd"
113+
#[cfg(any(
114+
all(target_arch = "aarch64", target_feature = "aes", not(miri)),
115+
all(feature = "nightly-arm-aes", target_arch = "arm", target_feature = "aes", not(miri)),
118116
))]
119117
#[allow(unused)]
120118
#[inline(always)]
@@ -142,11 +140,9 @@ pub(crate) fn aesdec(value: u128, xor: u128) -> u128 {
142140
}
143141
}
144142

145-
#[cfg(all(
146-
any(target_arch = "arm", target_arch = "aarch64"),
147-
any(target_feature = "aes", target_feature = "crypto"),
148-
not(miri),
149-
feature = "stdsimd"
143+
#[cfg(any(
144+
all(target_arch = "aarch64", target_feature = "aes", not(miri)),
145+
all(feature = "nightly-arm-aes", target_arch = "arm", target_feature = "aes", not(miri)),
150146
))]
151147
#[allow(unused)]
152148
#[inline(always)]

src/random_state.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use core::hash::Hash;
22
cfg_if::cfg_if! {
33
if #[cfg(any(
44
all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "aes", not(miri)),
5-
all(any(target_arch = "arm", target_arch = "aarch64"), any(target_feature = "aes", target_feature = "crypto"), not(miri), feature = "stdsimd")
5+
all(target_arch = "aarch64", target_feature = "aes", not(miri)),
6+
all(feature = "nightly-arm-aes", target_arch = "arm", target_feature = "aes", not(miri)),
67
))] {
78
use crate::aes_hash::*;
89
} else {
@@ -230,7 +231,6 @@ impl fmt::Debug for RandomState {
230231
}
231232

232233
impl RandomState {
233-
234234
/// Create a new `RandomState` `BuildHasher` using random keys.
235235
///
236236
/// Each instance will have a unique set of keys derived from [RandomSource].
@@ -317,8 +317,8 @@ impl RandomState {
317317
/// Calculates the hash of a single value. This provides a more convenient (and faster) way to obtain a hash:
318318
/// For example:
319319
#[cfg_attr(
320-
feature = "std",
321-
doc = r##" # Examples
320+
feature = "std",
321+
doc = r##" # Examples
322322
```
323323
use std::hash::BuildHasher;
324324
use ahash::RandomState;
@@ -330,8 +330,8 @@ impl RandomState {
330330
)]
331331
/// This is similar to:
332332
#[cfg_attr(
333-
feature = "std",
334-
doc = r##" # Examples
333+
feature = "std",
334+
doc = r##" # Examples
335335
```
336336
use std::hash::{BuildHasher, Hash, Hasher};
337337
use ahash::RandomState;
@@ -419,12 +419,11 @@ impl BuildHasher for RandomState {
419419
AHasher::from_random_state(self)
420420
}
421421

422-
423422
/// Calculates the hash of a single value. This provides a more convenient (and faster) way to obtain a hash:
424423
/// For example:
425424
#[cfg_attr(
426-
feature = "std",
427-
doc = r##" # Examples
425+
feature = "std",
426+
doc = r##" # Examples
428427
```
429428
use std::hash::BuildHasher;
430429
use ahash::RandomState;
@@ -436,8 +435,8 @@ impl BuildHasher for RandomState {
436435
)]
437436
/// This is similar to:
438437
#[cfg_attr(
439-
feature = "std",
440-
doc = r##" # Examples
438+
feature = "std",
439+
doc = r##" # Examples
441440
```
442441
use std::hash::{BuildHasher, Hash, Hasher};
443442
use ahash::RandomState;

tests/bench.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ const AHASH_IMPL: &str = if cfg!(any(
1414
target_feature = "aes",
1515
not(miri),
1616
),
17+
all(target_arch = "aarch64", target_feature = "aes", not(miri)),
1718
all(
18-
any(target_arch = "arm", target_arch = "aarch64"),
19-
any(target_feature = "aes", target_feature = "crypto"),
20-
not(miri),
21-
feature = "stdsimd",
19+
feature = "nightly-arm-aes",
20+
target_arch = "arm",
21+
target_feature = "aes",
22+
not(miri)
2223
),
2324
)) {
2425
"aeshash"

0 commit comments

Comments
 (0)