Skip to content

Commit 72f809a

Browse files
committed
Add conditional compilation attributes
1 parent c2c976e commit 72f809a

File tree

4 files changed

+70
-17
lines changed

4 files changed

+70
-17
lines changed

src/crc32/fusion/aarch64.rs

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ pub fn crc32_iso_hdlc(crc: u32, data: &[u8]) -> u32 {
6363
}
6464

6565
#[inline]
66+
#[cfg(target_feature = "sha3")]
6667
#[target_feature(enable = "neon,aes")]
6768
unsafe fn clmul_lo_eor3(a: uint64x2_t, b: uint64x2_t) -> uint64x2_t {
6869
// Polynomial multiply low parts - convert u128 result to uint64x2_t
@@ -71,6 +72,7 @@ unsafe fn clmul_lo_eor3(a: uint64x2_t, b: uint64x2_t) -> uint64x2_t {
7172
}
7273

7374
#[inline]
75+
#[cfg(target_feature = "sha3")]
7476
#[target_feature(enable = "neon,aes")]
7577
unsafe fn clmul_hi_eor3(a: uint64x2_t, b: uint64x2_t) -> uint64x2_t {
7678
// Polynomial multiply high parts - convert u128 result to uint64x2_t
@@ -79,6 +81,7 @@ unsafe fn clmul_hi_eor3(a: uint64x2_t, b: uint64x2_t) -> uint64x2_t {
7981
}
8082

8183
#[inline]
84+
#[cfg(target_feature = "sha3")]
8285
#[target_feature(enable = "neon,aes")]
8386
unsafe fn clmul_scalar(a: u32, b: u32) -> uint64x2_t {
8487
// Polynomial multiply scalars - convert u128 result to uint64x2_t
@@ -88,6 +91,7 @@ unsafe fn clmul_scalar(a: u32, b: u32) -> uint64x2_t {
8891

8992
// x^n mod P, in log(n) time
9093
#[inline]
94+
#[cfg(target_feature = "sha3")]
9195
#[target_feature(enable = "neon,aes")]
9296
fn xnmodp_crc32_iscsi(mut n: u64) -> u32 {
9397
let mut stack = !1u64;
@@ -125,13 +129,15 @@ fn xnmodp_crc32_iscsi(mut n: u64) -> u32 {
125129
}
126130

127131
#[inline]
132+
#[cfg(target_feature = "sha3")]
128133
#[target_feature(enable = "neon,aes")]
129134
unsafe fn crc_shift_iscsi(crc: u32, nbytes: usize) -> uint64x2_t {
130135
clmul_scalar(crc, xnmodp_crc32_iscsi((nbytes * 8 - 33) as u64))
131136
}
132137

133138
// x^n mod P, in log(n) time
134139
#[inline]
140+
#[cfg(target_feature = "sha3")]
135141
#[target_feature(enable = "neon,aes")]
136142
fn xnmodp_iso_hdlc(mut n: u64) -> u32 {
137143
let mut stack = !1u64;
@@ -169,6 +175,7 @@ fn xnmodp_iso_hdlc(mut n: u64) -> u32 {
169175
}
170176

171177
#[inline]
178+
#[cfg(target_feature = "sha3")]
172179
#[target_feature(enable = "neon,aes")]
173180
unsafe fn crc_shift_iso_hdlc(crc: u32, nbytes: usize) -> uint64x2_t {
174181
clmul_scalar(crc, xnmodp_iso_hdlc((nbytes * 8 - 33) as u64))
@@ -179,6 +186,7 @@ unsafe fn crc_shift_iso_hdlc(crc: u32, nbytes: usize) -> uint64x2_t {
179186
///
180187
/// ./generate -i neon_eor3 -p crc32c -a v9s3x2e_s3
181188
#[inline]
189+
#[cfg(target_feature = "sha3")]
182190
#[target_feature(enable = "neon,aes,sha3")]
183191
unsafe fn crc32_iscsi_eor3_v9s3x2e_s3(mut crc0: u32, mut buf: *const u8, mut len: usize) -> u32 {
184192
// Align to 8-byte boundary
@@ -555,6 +563,7 @@ unsafe fn crc32_iscsi_v12e_v1(mut crc0: u32, mut buf: *const u8, mut len: usize)
555563
///
556564
/// ./generate -i neon_eor3 -p crc32 -a v9s3x2e_s3
557565
#[inline]
566+
#[cfg(target_feature = "sha3")]
558567
#[target_feature(enable = "neon,aes,sha3")]
559568
unsafe fn crc32_iso_hdlc_eor3_v9s3x2e_s3(mut crc0: u32, mut buf: *const u8, mut len: usize) -> u32 {
560569
// Align to 8-byte boundary
@@ -916,7 +925,6 @@ mod tests {
916925
use crate::test::consts::TEST_CHECK_STRING;
917926
use crc::{Crc, Table};
918927
use rand::{rng, Rng};
919-
use std::arch::is_aarch64_feature_detected;
920928

921929
const RUST_CRC32_ISO_HDLC: Crc<u32, Table<16>> =
922930
Crc::<u32, Table<16>>::new(&crc::CRC_32_ISO_HDLC);
@@ -985,6 +993,7 @@ mod tests {
985993
}
986994
}
987995

996+
#[cfg(target_feature = "sha3")]
988997
fn crc32_iso_hdlc_random(len: usize) {
989998
let mut data = vec![0u8; len];
990999
rng().fill(&mut data[..]);
@@ -994,13 +1003,11 @@ mod tests {
9941003
assert_eq!(crc32_iso_hdlc(0xffffffff, &data) ^ 0xffffffff, checksum);
9951004

9961005
unsafe {
997-
if is_aarch64_feature_detected!("sha3") {
998-
assert_eq!(
999-
crc32_iso_hdlc_eor3_v9s3x2e_s3(0xffffffff, data.as_ptr(), data.len())
1000-
^ 0xffffffff,
1001-
checksum
1002-
);
1003-
}
1006+
assert_eq!(
1007+
crc32_iso_hdlc_eor3_v9s3x2e_s3(0xffffffff, data.as_ptr(), data.len())
1008+
^ 0xffffffff,
1009+
checksum
1010+
);
10041011

10051012
assert_eq!(
10061013
crc32_iso_hdlc_v12e_v1(0xffffffff, data.as_ptr(), data.len()) ^ 0xffffffff,
@@ -1009,6 +1016,24 @@ mod tests {
10091016
}
10101017
}
10111018

1019+
#[cfg(not(target_feature = "sha3"))]
1020+
fn crc32_iso_hdlc_random(len: usize) {
1021+
let mut data = vec![0u8; len];
1022+
rng().fill(&mut data[..]);
1023+
1024+
let checksum = RUST_CRC32_ISO_HDLC.checksum(&data);
1025+
1026+
assert_eq!(crc32_iso_hdlc(0xffffffff, &data) ^ 0xffffffff, checksum);
1027+
1028+
unsafe {
1029+
assert_eq!(
1030+
crc32_iso_hdlc_v12e_v1(0xffffffff, data.as_ptr(), data.len()) ^ 0xffffffff,
1031+
checksum
1032+
);
1033+
}
1034+
}
1035+
1036+
#[cfg(target_feature = "sha3")]
10121037
fn crc32_iscsi_random(len: usize) {
10131038
let mut data = vec![0u8; len];
10141039
rng().fill(&mut data[..]);
@@ -1018,13 +1043,28 @@ mod tests {
10181043
assert_eq!(crc32_iscsi(0xffffffff, &data) ^ 0xffffffff, checksum);
10191044

10201045
unsafe {
1021-
if is_aarch64_feature_detected!("sha3") {
1022-
assert_eq!(
1023-
crc32_iscsi_eor3_v9s3x2e_s3(0xffffffff, data.as_ptr(), data.len()) ^ 0xffffffff,
1024-
checksum
1025-
);
1026-
}
1046+
assert_eq!(
1047+
crc32_iscsi_eor3_v9s3x2e_s3(0xffffffff, data.as_ptr(), data.len()) ^ 0xffffffff,
1048+
checksum
1049+
);
1050+
1051+
assert_eq!(
1052+
crc32_iscsi_v12e_v1(0xffffffff, data.as_ptr(), data.len()) ^ 0xffffffff,
1053+
checksum
1054+
);
1055+
}
1056+
}
1057+
1058+
#[cfg(not(target_feature = "sha3"))]
1059+
fn crc32_iscsi_random(len: usize) {
1060+
let mut data = vec![0u8; len];
1061+
rng().fill(&mut data[..]);
1062+
1063+
let checksum = RUST_CRC32_ISCSI.checksum(&data);
10271064

1065+
assert_eq!(crc32_iscsi(0xffffffff, &data) ^ 0xffffffff, checksum);
1066+
1067+
unsafe {
10281068
assert_eq!(
10291069
crc32_iscsi_v12e_v1(0xffffffff, data.as_ptr(), data.len()) ^ 0xffffffff,
10301070
checksum

src/crc32/fusion/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ pub(crate) fn crc32_iso_hdlc(state: u32, data: &[u8]) -> u32 {
1717
#[cfg(target_arch = "aarch64")]
1818
return aarch64::crc32_iso_hdlc(state, data);
1919

20-
#[cfg(target_arch = "x86_64")]
21-
panic!("CRC-32/ISO-HDLC with fusion is not supported on x86_64");
20+
#[cfg(not(target_arch = "aarch64"))]
21+
panic!("CRC-32/ISO-HDLC with fusion is only supported on AArch64 architecture");
2222
}
2323

2424
#[inline(always)]
@@ -28,4 +28,7 @@ pub(crate) fn crc32_iscsi(state: u32, data: &[u8]) -> u32 {
2828

2929
#[cfg(target_arch = "x86_64")]
3030
return x86::crc32_iscsi(state, data);
31+
32+
#[cfg(all(not(target_arch = "aarch64"), not(target_arch = "x86_64")))]
33+
panic!("CRC-32/ISCSI with fusion is only supported on AArch64 and X86_64 architectures");
3134
}

src/crc32/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@
44
55
pub mod algorithm;
66
pub mod consts;
7+
8+
#[cfg(any(target_arch = "x86", target_arch = "x86_64", target_arch = "aarch64"))]
79
pub(crate) mod fusion;

src/lib.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,10 @@ use crate::crc32::consts::{
109109
CRC32_AIXM, CRC32_AUTOSAR, CRC32_BASE91_D, CRC32_BZIP2, CRC32_CD_ROM_EDC, CRC32_CKSUM,
110110
CRC32_ISCSI, CRC32_ISO_HDLC, CRC32_JAMCRC, CRC32_MEF, CRC32_MPEG_2, CRC32_XFER,
111111
};
112+
113+
#[cfg(any(target_arch = "x86", target_arch = "x86_64", target_arch = "aarch64"))]
112114
use crate::crc32::fusion;
115+
113116
use crate::crc64::consts::{
114117
CRC64_ECMA_182, CRC64_GO_ISO, CRC64_MS, CRC64_NVME, CRC64_REDIS, CRC64_WE, CRC64_XZ,
115118
};
@@ -480,7 +483,12 @@ fn get_calculator_params(algorithm: CrcAlgorithm) -> (CalculatorFn, CrcParams) {
480483
#[inline(always)]
481484
fn crc32_iscsi_calculator(state: u64, data: &[u8], _params: CrcParams) -> u64 {
482485
// both aarch64 and x86 have native CRC-32/ISCSI support, so we can use fusion
483-
fusion::crc32_iscsi(state as u32, data) as u64
486+
#[cfg(any(target_arch = "aarch64", target_arch = "x86_64"))]
487+
return fusion::crc32_iscsi(state as u32, data) as u64;
488+
489+
#[cfg(all(not(target_arch = "aarch64"), not(target_arch = "x86_64")))]
490+
// fallback to traditional calculation if not aarch64 or x86_64
491+
Calculator::calculate(state, data, _params)
484492
}
485493

486494
/// Calculates the CRC-32/ISO-HDLC ("crc32" in many, but not all, implementations) checksum.

0 commit comments

Comments
 (0)