Skip to content

Commit 14dd1be

Browse files
committed
Add nightly-arm-aes Cargo feature
1 parent dba28d4 commit 14dd1be

File tree

7 files changed

+41
-18
lines changed

7 files changed

+41
-18
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

src/hash_quality_test.rs

Lines changed: 8 additions & 5 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());
@@ -438,6 +440,7 @@ mod fallback_tests {
438440
#[cfg(any(
439441
all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "aes", not(miri)),
440442
all(target_arch = "aarch64", target_feature = "aes", not(miri)),
443+
all(feature = "nightly-arm-aes", target_arch = "arm", target_feature = "aes", not(miri)),
441444
))]
442445
#[cfg(test)]
443446
mod aes_tests {

src/lib.rs

Lines changed: 6 additions & 1 deletion
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,6 +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))]
101+
#![cfg_attr(feature = "nightly-arm-aes", feature(stdarch_arm_neon_intrinsics))]
98102

99103
#[macro_use]
100104
mod convert;
@@ -105,6 +109,7 @@ cfg_if::cfg_if! {
105109
if #[cfg(any(
106110
all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "aes", not(miri)),
107111
all(target_arch = "aarch64", target_feature = "aes", not(miri)),
112+
all(feature = "nightly-arm-aes", target_arch = "arm", target_feature = "aes", not(miri)),
108113
))] {
109114
mod aes_hash;
110115
pub use crate::aes_hash::AHasher;

src/operations.rs

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

113-
#[cfg(all(target_arch = "aarch64", target_feature = "aes", not(miri)))]
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)),
116+
))]
114117
#[allow(unused)]
115118
#[inline(always)]
116119
pub(crate) fn aesenc(value: u128, xor: u128) -> u128 {
@@ -123,7 +126,10 @@ pub(crate) fn aesenc(value: u128, xor: u128) -> u128 {
123126
xor ^ value
124127
}
125128

126-
#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "aes", not(miri)))]
129+
#[cfg(any(
130+
all(target_arch = "aarch64", target_feature = "aes", not(miri)),
131+
all(feature = "nightly-arm-aes", target_arch = "arm", target_feature = "aes", not(miri)),
132+
))]
127133
#[allow(unused)]
128134
#[inline(always)]
129135
pub(crate) fn aesdec(value: u128, xor: u128) -> u128 {

src/random_state.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ cfg_if::cfg_if! {
33
if #[cfg(any(
44
all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "aes", not(miri)),
55
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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ const AHASH_IMPL: &str = if cfg!(any(
1515
not(miri),
1616
),
1717
all(target_arch = "aarch64", target_feature = "aes", not(miri)),
18+
all(
19+
feature = "nightly-arm-aes",
20+
target_arch = "arm",
21+
target_feature = "aes",
22+
not(miri)
23+
),
1824
)) {
1925
"aeshash"
2026
} else {

0 commit comments

Comments
 (0)