|
| 1 | + |
| 2 | +extern "C" { |
| 3 | + #[link_name = "llvm.aarch64.crc32b"] |
| 4 | + fn crc32b_(crc: u32, data: u32) -> u32; |
| 5 | + #[link_name = "llvm.aarch64.crc32h"] |
| 6 | + fn crc32h_(crc: u32, data: u32) -> u32; |
| 7 | + #[link_name = "llvm.aarch64.crc32w"] |
| 8 | + fn crc32w_(crc: u32, data: u32) -> u32; |
| 9 | + #[link_name = "llvm.aarch64.crc32x"] |
| 10 | + fn crc32x_(crc: u32, data: u64) -> u32; |
| 11 | + |
| 12 | + #[link_name = "llvm.aarch64.crc32cb"] |
| 13 | + fn crc32cb_(crc: u32, data: u32) -> u32; |
| 14 | + #[link_name = "llvm.aarch64.crc32ch"] |
| 15 | + fn crc32ch_(crc: u32, data: u32) -> u32; |
| 16 | + #[link_name = "llvm.aarch64.crc32cw"] |
| 17 | + fn crc32cw_(crc: u32, data: u32) -> u32; |
| 18 | + #[link_name = "llvm.aarch64.crc32cx"] |
| 19 | + fn crc32cx_(crc: u32, data: u64) -> u32; |
| 20 | +} |
| 21 | + |
| 22 | +#[cfg(test)] |
| 23 | +use stdsimd_test::assert_instr; |
| 24 | + |
| 25 | +/// CRC32 single round checksum for bytes (8 bits). |
| 26 | +#[inline] |
| 27 | +#[target_feature(enable = "crc")] |
| 28 | +#[cfg_attr(test, assert_instr(crc32b))] |
| 29 | +pub unsafe fn crc32b(crc: u32, data: u8) -> u32 { |
| 30 | + crc32b_(crc, data as u32) |
| 31 | +} |
| 32 | + |
| 33 | +/// CRC32 single round checksum for half words (16 bits). |
| 34 | +#[inline] |
| 35 | +#[target_feature(enable = "crc")] |
| 36 | +#[cfg_attr(test, assert_instr(crc32h))] |
| 37 | +pub unsafe fn crc32h(crc: u32, data: u16) -> u32 { |
| 38 | + crc32h_(crc, data as u32) |
| 39 | +} |
| 40 | + |
| 41 | +/// CRC32 single round checksum for words (32 bits). |
| 42 | +#[inline] |
| 43 | +#[target_feature(enable = "crc")] |
| 44 | +#[cfg_attr(test, assert_instr(crc32w))] |
| 45 | +pub unsafe fn crc32w(crc: u32, data: u32) -> u32 { |
| 46 | + crc32w_(crc, data) |
| 47 | +} |
| 48 | + |
| 49 | +/// CRC32 single round checksum for quad words (64 bits). |
| 50 | +#[inline] |
| 51 | +#[target_feature(enable = "crc")] |
| 52 | +#[cfg_attr(test, assert_instr(crc32x))] |
| 53 | +pub unsafe fn crc32x(crc: u32, data: u64) -> u32 { |
| 54 | + crc32x_(crc, data) |
| 55 | +} |
| 56 | + |
| 57 | +/// CRC32-C single round checksum for bytes (8 bits). |
| 58 | +#[inline] |
| 59 | +#[target_feature(enable = "crc")] |
| 60 | +#[cfg_attr(test, assert_instr(crc32cb))] |
| 61 | +pub unsafe fn crc32cb(crc: u32, data: u8) -> u32 { |
| 62 | + crc32cb_(crc, data as u32) |
| 63 | +} |
| 64 | + |
| 65 | +/// CRC32-C single round checksum for half words (16 bits). |
| 66 | +#[inline] |
| 67 | +#[target_feature(enable = "crc")] |
| 68 | +#[cfg_attr(test, assert_instr(crc32ch))] |
| 69 | +pub unsafe fn crc32ch(crc: u32, data: u16) -> u32 { |
| 70 | + crc32ch_(crc, data as u32) |
| 71 | +} |
| 72 | + |
| 73 | +/// CRC32-C single round checksum for words (32 bits). |
| 74 | +#[inline] |
| 75 | +#[target_feature(enable = "crc")] |
| 76 | +#[cfg_attr(test, assert_instr(crc32cw))] |
| 77 | +pub unsafe fn crc32cw(crc: u32, data: u32) -> u32 { |
| 78 | + crc32cw_(crc, data) |
| 79 | +} |
| 80 | + |
| 81 | +/// CRC32-C single round checksum for quad words (64 bits). |
| 82 | +#[inline] |
| 83 | +#[target_feature(enable = "crc")] |
| 84 | +#[cfg_attr(test, assert_instr(crc32cx))] |
| 85 | +pub unsafe fn crc32cx(crc: u32, data: u64) -> u32 { |
| 86 | + crc32cx_(crc, data) |
| 87 | +} |
| 88 | + |
| 89 | +#[cfg(test)] |
| 90 | +mod tests { |
| 91 | + use coresimd::aarch64::*; |
| 92 | + use coresimd::simd::*; |
| 93 | + use std::mem; |
| 94 | + use stdsimd_test::simd_test; |
| 95 | + |
| 96 | + #[simd_test(enable = "crc")] |
| 97 | + unsafe fn test_crc32b() { |
| 98 | + assert_eq!(crc32b(0, 0), 0); |
| 99 | + assert_eq!(crc32b(0, 255), 755167117); |
| 100 | + } |
| 101 | + |
| 102 | + #[simd_test(enable = "crc")] |
| 103 | + unsafe fn test_crc32h() { |
| 104 | + assert_eq!(crc32h(0, 0), 0); |
| 105 | + assert_eq!(crc32h(0, 16384), 1994146192); |
| 106 | + } |
| 107 | + |
| 108 | + #[simd_test(enable = "crc")] |
| 109 | + unsafe fn test_crc32w() { |
| 110 | + assert_eq!(crc32w(0, 0), 0); |
| 111 | + assert_eq!(crc32w(0, 4294967295), 3736805603); |
| 112 | + } |
| 113 | + |
| 114 | + #[simd_test(enable = "crc")] |
| 115 | + unsafe fn test_crc32x() { |
| 116 | + assert_eq!(crc32x(0, 0), 0); |
| 117 | + assert_eq!(crc32x(0, 18446744073709551615), 1147535477); |
| 118 | + } |
| 119 | + |
| 120 | + #[simd_test(enable = "crc")] |
| 121 | + unsafe fn test_crc32cb() { |
| 122 | + assert_eq!(crc32cb(0, 0), 0); |
| 123 | + assert_eq!(crc32cb(0, 255), 2910671697); |
| 124 | + } |
| 125 | + |
| 126 | + #[simd_test(enable = "crc")] |
| 127 | + unsafe fn test_crc32ch() { |
| 128 | + assert_eq!(crc32ch(0, 0), 0); |
| 129 | + assert_eq!(crc32ch(0, 16384), 1098587580); |
| 130 | + } |
| 131 | + |
| 132 | + #[simd_test(enable = "crc")] |
| 133 | + unsafe fn test_crc32cw() { |
| 134 | + assert_eq!(crc32cw(0, 0), 0); |
| 135 | + assert_eq!(crc32cw(0, 4294967295), 3080238136); |
| 136 | + } |
| 137 | + |
| 138 | + #[simd_test(enable = "crc")] |
| 139 | + unsafe fn test_crc32cx() { |
| 140 | + assert_eq!(crc32cx(0, 0), 0); |
| 141 | + assert_eq!(crc32cx(0, 18446744073709551615), 3293575501); |
| 142 | + } |
| 143 | + |
| 144 | +} |
0 commit comments