Skip to content

Commit 4828b53

Browse files
committed
Avoid [u64; 4] -> [u128; 2] conversion.
Avoid the need to implement this conversion by just hard-coding the constant twice.
1 parent b8f78a4 commit 4828b53

File tree

4 files changed

+18
-16
lines changed

4 files changed

+18
-16
lines changed

src/aes_hash.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::convert::*;
22
use crate::operations::*;
3-
use crate::random_state::PI;
3+
use crate::random_state::PI_U128X2;
44
use crate::RandomState;
55
use core::hash::Hasher;
66

@@ -49,9 +49,8 @@ impl AHasher {
4949
/// ```
5050
#[inline]
5151
pub(crate) fn new_with_keys(key1: u128, key2: u128) -> Self {
52-
let pi: [u128; 2] = PI.convert();
53-
let key1 = key1 ^ pi[0];
54-
let key2 = key2 ^ pi[1];
52+
let key1 = key1 ^ PI_U128X2[0];
53+
let key2 = key2 ^ PI_U128X2[1];
5554
Self {
5655
enc: key1,
5756
sum: key2,

src/convert.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ macro_rules! convert_primitive_bytes {
3737
}
3838

3939
convert!([u128; 4], [u8; 64]);
40-
convert!([u128; 2], [u64; 4]);
4140
convert!([u128; 2], [u8; 32]);
4241
convert!(u128, [u64; 2]);
4342
convert_primitive_bytes!(u128, [u8; 16]);

src/fallback_hash.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::convert::*;
22
use crate::operations::folded_multiply;
33
use crate::operations::read_small;
44
use crate::operations::MULTIPLE;
5-
use crate::random_state::PI;
5+
use crate::random_state::PI_U128X2;
66
use crate::RandomState;
77
use core::hash::Hasher;
88

@@ -31,9 +31,8 @@ impl AHasher {
3131
#[inline]
3232
#[allow(dead_code)] // Is not called if non-fallback hash is used.
3333
pub(crate) fn new_with_keys(key1: u128, key2: u128) -> AHasher {
34-
let pi: [u128; 2] = PI.convert();
35-
let key1: [u64; 2] = (key1 ^ pi[0]).convert();
36-
let key2: [u64; 2] = (key2 ^ pi[1]).convert();
34+
let key1: [u64; 2] = (key1 ^ PI_U128X2[0]).convert();
35+
let key2: [u64; 2] = (key2 ^ PI_U128X2[1]).convert();
3736
AHasher {
3837
buffer: key1[0],
3938
pad: key1[1],

src/random_state.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,18 @@ use core::fmt;
3030
use core::hash::BuildHasher;
3131
use core::hash::Hasher;
3232

33-
pub(crate) const PI: [u64; 4] = [
33+
pub(crate) const PI_U64X4: [u64; 4] = [
3434
0x243f_6a88_85a3_08d3,
3535
0x1319_8a2e_0370_7344,
3636
0xa409_3822_299f_31d0,
3737
0x082e_fa98_ec4e_6c89,
3838
];
3939

40+
pub(crate) const PI_U128X2: [u128; 2] = [
41+
0x1319_8a2e_0370_7344_243f_6a88_85a3_08d3,
42+
0x082e_fa98_ec4e_6c89_a409_3822_299f_31d0,
43+
];
44+
4045
pub(crate) const PI2: [u64; 4] = [
4146
0x4528_21e6_38d0_1377,
4247
0xbe54_66cf_34e9_0c6c,
@@ -101,7 +106,7 @@ cfg_if::cfg_if! {
101106
} else {
102107
#[inline]
103108
fn get_fixed_seeds() -> &'static [[u64; 4]; 2] {
104-
&[PI, PI2]
109+
&[PI_U64X4, PI2]
105110
}
106111
}
107112
}
@@ -135,14 +140,14 @@ struct DefaultRandomSource {
135140
impl DefaultRandomSource {
136141
fn new() -> DefaultRandomSource {
137142
DefaultRandomSource {
138-
counter: AtomicUsize::new(&PI as *const _ as usize),
143+
counter: AtomicUsize::new(&PI_U64X4 as *const _ as usize),
139144
}
140145
}
141146

142147
#[cfg(all(target_arch = "arm", target_os = "none"))]
143148
const fn default() -> DefaultRandomSource {
144149
DefaultRandomSource {
145-
counter: AtomicUsize::new(PI[3] as usize),
150+
counter: AtomicUsize::new(PI_U64X4[3] as usize),
146151
}
147152
}
148153
}
@@ -501,19 +506,19 @@ mod test {
501506
#[cfg(all(feature = "runtime-rng", not(all(feature = "compile-time-rng", test))))]
502507
#[test]
503508
fn test_not_pi() {
504-
assert_ne!(PI, get_fixed_seeds()[0]);
509+
assert_ne!(PI_U64X4, get_fixed_seeds()[0]);
505510
}
506511

507512
#[cfg(all(feature = "compile-time-rng", any(not(feature = "runtime-rng"), test)))]
508513
#[test]
509514
fn test_not_pi_const() {
510-
assert_ne!(PI, get_fixed_seeds()[0]);
515+
assert_ne!(PI_U64X4, get_fixed_seeds()[0]);
511516
}
512517

513518
#[cfg(all(not(feature = "runtime-rng"), not(feature = "compile-time-rng")))]
514519
#[test]
515520
fn test_pi() {
516-
assert_eq!(PI, get_fixed_seeds()[0]);
521+
assert_eq!(PI_U64X4, get_fixed_seeds()[0]);
517522
}
518523

519524
#[test]

0 commit comments

Comments
 (0)