Skip to content

Commit d8b0404

Browse files
committed
fix: apply same rotation on big endian (#330)
job: +check_debian Signed-off-by: usamoi <usamoi@outlook.com>
1 parent a360f1a commit d8b0404

2 files changed

Lines changed: 29 additions & 9 deletions

File tree

crates/rabitq/build.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ fn main() -> Result<(), Box<dyn Error>> {
2020
use rand::{Rng, SeedableRng};
2121
use rand_chacha::ChaCha12Rng;
2222
let mut rng = ChaCha12Rng::from_seed([7; 32]);
23-
let bits = (0..262144).map(|_| rng.random::<u8>()).collect::<Vec<_>>();
23+
let mut bits = (0..262144).map(|_| rng.random::<u8>()).collect::<Vec<_>>();
24+
if var("CARGO_CFG_TARGET_ENDIAN")?.as_str() == "big" {
25+
for chunk in bits.as_chunks_mut::<8>().0 {
26+
chunk.reverse();
27+
}
28+
}
2429
let out_dir = var("OUT_DIR")?;
2530
std::fs::write(PathBuf::from(out_dir).join("bits"), bits)?;
2631
Ok(())

crates/rabitq/src/rotate.rs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,29 @@
1414

1515
const BITS: &[u8; 262144] = include_bytes!(concat!(env!("OUT_DIR"), "/bits"));
1616

17-
const _: () = assert!(BITS[0] == 246);
18-
const _: () = assert!(BITS[1] == 133);
19-
const _: () = assert!(BITS[2] == 163);
20-
const _: () = assert!(BITS[3] == 106);
21-
const _: () = assert!(BITS[4] == 54);
22-
const _: () = assert!(BITS[5] == 126);
23-
const _: () = assert!(BITS[6] == 9);
24-
const _: () = assert!(BITS[7] == 115);
17+
#[cfg(target_endian = "little")]
18+
const _: () = {
19+
assert!(BITS[0] == 246);
20+
assert!(BITS[1] == 133);
21+
assert!(BITS[2] == 163);
22+
assert!(BITS[3] == 106);
23+
assert!(BITS[4] == 54);
24+
assert!(BITS[5] == 126);
25+
assert!(BITS[6] == 9);
26+
assert!(BITS[7] == 115);
27+
};
28+
29+
#[cfg(target_endian = "big")]
30+
const _: () = {
31+
assert!(BITS[7] == 246);
32+
assert!(BITS[6] == 133);
33+
assert!(BITS[5] == 163);
34+
assert!(BITS[4] == 106);
35+
assert!(BITS[3] == 54);
36+
assert!(BITS[2] == 126);
37+
assert!(BITS[1] == 9);
38+
assert!(BITS[0] == 115);
39+
};
2540

2641
static BITS_0: [u64; 1024] = zerocopy::transmute!(BITS.as_chunks::<8192>().0[0]);
2742
static BITS_1: [u64; 1024] = zerocopy::transmute!(BITS.as_chunks::<8192>().0[1]);

0 commit comments

Comments
 (0)