| 1 |
//! # Safety |
| 2 |
//! |
| 3 |
//! The functions in this module should only be executed on aarch64 machines with the Neon |
| 4 |
//! extension. |
| 5 |
use core::arch::aarch64::{ |
| 6 |
uint16x8_t, uint16x8x2_t, uint16x8x4_t, uint8x16_t, vaddq_u32, vaddw_high_u8, vaddw_u8, |
| 7 |
vdupq_n_u16, vdupq_n_u32, vextq_u8, vget_high_u32, vget_lane_u32, vget_low_u16, vget_low_u32, |
| 8 |
vget_low_u8, vld1q_u8_x4, vmlal_high_u16, vmlal_u16, vpadalq_u16, vpadalq_u8, vpadd_u32, |
| 9 |
vpaddlq_u8, vsetq_lane_u32, vshlq_n_u32, |
| 10 |
}; |
| 11 |
|
| 12 |
use crate::adler32::{ |
| 13 |
generic::{adler32_len_1, adler32_len_16}, |
| 14 |
BASE, NMAX, |
| 15 |
}; |
| 16 |
|
| 17 |
const TAPS: [uint16x8x4_t; 2] = unsafe { |
| 18 |
core::mem::transmute::<[u16; 64], [uint16x8x4_t; 2]>([ |
| 19 |
64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, |
| 20 |
41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, |
| 21 |
18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, |
| 22 |
]) |
| 23 |
}; |
| 24 |
|
| 25 |
pub fn adler32_neon(adler: u32, buf: &[u8]) -> u32 { |
| 26 |
assert!(crate::cpu_features::is_enabled_neon()); |
| 27 |
// SAFETY: the assertion above ensures this code is not executed unless the CPU has Neon |
| 28 |
// extensions. |
| 29 |
unsafe { adler32_neon_internal(adler, buf) } |
| 30 |
} |
| 31 |
|
| 32 |
#[target_feature(enable = "neon")] |
| 33 |
unsafe fn adler32_neon_internal(mut adler: u32, buf: &[u8]) -> u32 { |
| 34 |
/* split Adler-32 into component sums */ |
| 35 |
let sum2 = (adler >> 16) & 0xffff; |
| 36 |
adler &= 0xffff; |
| 37 |
|
| 38 |
/* in case user likes doing a byte at a time, keep it fast */ |
| 39 |
if buf.len() == 1 { |
| 40 |
return adler32_len_1(adler, buf, sum2); |
| 41 |
} |
| 42 |
|
| 43 |
/* initial Adler-32 value (deferred check for len == 1 speed) */ |
| 44 |
if buf.is_empty() { |
| 45 |
return adler | (sum2 << 16); |
| 46 |
} |
| 47 |
|
| 48 |
/* in case short lengths are provided, keep it somewhat fast */ |
| 49 |
if buf.len() < 16 { |
| 50 |
return adler32_len_16(adler, buf, sum2); |
| 51 |
} |
| 52 |
|
| 53 |
// Split Adler-32 into component sums, it can be supplied by the caller sites (e.g. in a PNG file). |
| 54 |
let mut pair = (adler, sum2); |
| 55 |
|
| 56 |
// If memory is not SIMD aligned, do scalar sums to an aligned |
| 57 |
// offset, provided that doing so doesn't completely eliminate |
| 58 |
// SIMD operation. Aligned loads are still faster on ARM, even |
| 59 |
// though there's no explicit aligned load instruction |
| 60 |
const _: () = assert!(core::mem::align_of::<uint8x16_t>() == 16); |
| 61 |
let (before, middle, after) = unsafe { buf.align_to::<uint8x16_t>() }; |
| 62 |
|
| 63 |
pair = handle_tail(pair, before); |
| 64 |
|
| 65 |
// NOTE: zlib-ng adjusts the chunk size to account for already having consumed some input. We |
| 66 |
// instead just mod the pair by BASE and then use the full chunk width below. Failing to do so |
| 67 |
// can result in overflow. |
| 68 |
pair.0 %= BASE; |
| 69 |
pair.1 %= BASE; |
| 70 |
|
| 71 |
for chunk in middle.chunks(NMAX as usize / core::mem::size_of::<uint8x16_t>()) { |
| 72 |
pair = unsafe { accum32(pair, chunk) }; |
| 73 |
pair.0 %= BASE; |
| 74 |
pair.1 %= BASE; |
| 75 |
} |
| 76 |
|
| 77 |
if !after.is_empty() { |
| 78 |
pair = handle_tail(pair, after); |
| 79 |
pair.0 %= BASE; |
| 80 |
pair.1 %= BASE; |
| 81 |
} |
| 82 |
|
| 83 |
// D = B * 65536 + A, see: https://en.wikipedia.org/wiki/Adler-32. |
| 84 |
(pair.1 << 16) | pair.0 |
| 85 |
} |
| 86 |
|
| 87 |
fn handle_tail(mut pair: (u32, u32), buf: &[u8]) -> (u32, u32) { |
| 88 |
for x in buf { |
| 89 |
pair.0 += *x as u32; |
| 90 |
pair.1 += pair.0; |
| 91 |
} |
| 92 |
|
| 93 |
pair |
| 94 |
} |
| 95 |
|
| 96 |
#[target_feature(enable = "neon")] |
| 97 |
unsafe fn accum32(s: (u32, u32), buf: &[uint8x16_t]) -> (u32, u32) { |
| 98 |
// No target features 1.1 yet... |
| 99 |
unsafe { |
| 100 |
let mut adacc = vdupq_n_u32(0); |
| 101 |
let mut s2acc = vdupq_n_u32(0); |
| 102 |
|
| 103 |
adacc = vsetq_lane_u32(s.0, adacc, 0); |
| 104 |
s2acc = vsetq_lane_u32(s.1, s2acc, 0); |
| 105 |
|
| 106 |
let mut s3acc = vdupq_n_u32(0); |
| 107 |
let mut adacc_prev = adacc; |
| 108 |
|
| 109 |
let mut s2_0 = vdupq_n_u16(0); |
| 110 |
let mut s2_1 = vdupq_n_u16(0); |
| 111 |
let mut s2_2 = vdupq_n_u16(0); |
| 112 |
let mut s2_3 = vdupq_n_u16(0); |
| 113 |
|
| 114 |
let mut s2_4 = vdupq_n_u16(0); |
| 115 |
let mut s2_5 = vdupq_n_u16(0); |
| 116 |
let mut s2_6 = vdupq_n_u16(0); |
| 117 |
let mut s2_7 = vdupq_n_u16(0); |
| 118 |
|
| 119 |
let mut it = buf.chunks_exact(4); |
| 120 |
|
| 121 |
#[target_feature(enable = "neon")] |
| 122 |
#[inline] |
| 123 |
unsafe fn swap_64bit_lanes_when_be(v: uint8x16_t) -> uint8x16_t { |
| 124 |
crate::cfg_select! { |
| 125 |
target_endian = "big" => { vextq_u8(v, v, 8) } |
| 126 |
target_endian = "little" => { v } |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
for chunk in &mut it { |
| 131 |
// SAFETY: the chunks_exact iterator ensures chunk always references a 16x4 block within buf. |
| 132 |
let d0_d3 = { |
| 133 |
let mut tmp = unsafe { vld1q_u8_x4(chunk.as_ptr() as *const u8) }; |
| 134 |
|
| 135 |
tmp.0 = swap_64bit_lanes_when_be(tmp.0); |
| 136 |
tmp.1 = swap_64bit_lanes_when_be(tmp.1); |
| 137 |
tmp.2 = swap_64bit_lanes_when_be(tmp.2); |
| 138 |
tmp.3 = swap_64bit_lanes_when_be(tmp.3); |
| 139 |
|
| 140 |
tmp |
| 141 |
}; |
| 142 |
|
| 143 |
// Unfortunately it doesn't look like there's a direct sum 8 bit to 32 |
| 144 |
// bit instruction, we'll have to make due summing to 16 bits first |
| 145 |
let hsum = uint16x8x2_t(vpaddlq_u8(d0_d3.0), vpaddlq_u8(d0_d3.1)); |
| 146 |
|
| 147 |
let hsum_fold = uint16x8x2_t(vpadalq_u8(hsum.0, d0_d3.2), vpadalq_u8(hsum.1, d0_d3.3)); |
| 148 |
|
| 149 |
adacc = vpadalq_u16(adacc, hsum_fold.0); |
| 150 |
s3acc = vaddq_u32(s3acc, adacc_prev); |
| 151 |
adacc = vpadalq_u16(adacc, hsum_fold.1); |
| 152 |
|
| 153 |
// If we do straight widening additions to the 16 bit values, we don't incur |
| 154 |
// the usual penalties of a pairwise add. We can defer the multiplications |
| 155 |
// until the very end. These will not overflow because we are incurring at |
| 156 |
// most 408 loop iterations (NMAX / 64), and a given lane is only going to be |
| 157 |
// summed into once. This means for the maximum input size, the largest value |
| 158 |
// we will see is 255 * 102 = 26010, safely under uint16 max |
| 159 |
s2_0 = vaddw_u8(s2_0, vget_low_u8(d0_d3.0)); |
| 160 |
s2_1 = vaddw_high_u8(s2_1, d0_d3.0); |
| 161 |
s2_2 = vaddw_u8(s2_2, vget_low_u8(d0_d3.1)); |
| 162 |
s2_3 = vaddw_high_u8(s2_3, d0_d3.1); |
| 163 |
s2_4 = vaddw_u8(s2_4, vget_low_u8(d0_d3.2)); |
| 164 |
s2_5 = vaddw_high_u8(s2_5, d0_d3.2); |
| 165 |
s2_6 = vaddw_u8(s2_6, vget_low_u8(d0_d3.3)); |
| 166 |
s2_7 = vaddw_high_u8(s2_7, d0_d3.3); |
| 167 |
|
| 168 |
adacc_prev = adacc; |
| 169 |
} |
| 170 |
|
| 171 |
s3acc = vshlq_n_u32(s3acc, 6); |
| 172 |
|
| 173 |
let remainder = it.remainder(); |
| 174 |
|
| 175 |
if !remainder.is_empty() { |
| 176 |
let mut s3acc_0 = vdupq_n_u32(0); |
| 177 |
for d0 in remainder.iter().copied() { |
| 178 |
let d0 = swap_64bit_lanes_when_be(d0); |
| 179 |
let adler: uint16x8_t = vpaddlq_u8(d0); |
| 180 |
s2_6 = vaddw_u8(s2_6, vget_low_u8(d0)); |
| 181 |
s2_7 = vaddw_high_u8(s2_7, d0); |
| 182 |
adacc = vpadalq_u16(adacc, adler); |
| 183 |
s3acc_0 = vaddq_u32(s3acc_0, adacc_prev); |
| 184 |
adacc_prev = adacc; |
| 185 |
} |
| 186 |
|
| 187 |
s3acc_0 = vshlq_n_u32(s3acc_0, 4); |
| 188 |
s3acc = vaddq_u32(s3acc_0, s3acc); |
| 189 |
} |
| 190 |
|
| 191 |
let t0_t3 = TAPS[0]; |
| 192 |
let t4_t7 = TAPS[1]; |
| 193 |
|
| 194 |
let mut s2acc_0 = vdupq_n_u32(0); |
| 195 |
let mut s2acc_1 = vdupq_n_u32(0); |
| 196 |
let mut s2acc_2 = vdupq_n_u32(0); |
| 197 |
|
| 198 |
s2acc = vmlal_high_u16(s2acc, t0_t3.0, s2_0); |
| 199 |
s2acc_0 = vmlal_u16(s2acc_0, vget_low_u16(t0_t3.0), vget_low_u16(s2_0)); |
| 200 |
s2acc_1 = vmlal_high_u16(s2acc_1, t0_t3.1, s2_1); |
| 201 |
s2acc_2 = vmlal_u16(s2acc_2, vget_low_u16(t0_t3.1), vget_low_u16(s2_1)); |
| 202 |
|
| 203 |
s2acc = vmlal_high_u16(s2acc, t0_t3.2, s2_2); |
| 204 |
s2acc_0 = vmlal_u16(s2acc_0, vget_low_u16(t0_t3.2), vget_low_u16(s2_2)); |
| 205 |
s2acc_1 = vmlal_high_u16(s2acc_1, t0_t3.3, s2_3); |
| 206 |
s2acc_2 = vmlal_u16(s2acc_2, vget_low_u16(t0_t3.3), vget_low_u16(s2_3)); |
| 207 |
|
| 208 |
s2acc = vmlal_high_u16(s2acc, t4_t7.0, s2_4); |
| 209 |
s2acc_0 = vmlal_u16(s2acc_0, vget_low_u16(t4_t7.0), vget_low_u16(s2_4)); |
| 210 |
s2acc_1 = vmlal_high_u16(s2acc_1, t4_t7.1, s2_5); |
| 211 |
s2acc_2 = vmlal_u16(s2acc_2, vget_low_u16(t4_t7.1), vget_low_u16(s2_5)); |
| 212 |
|
| 213 |
s2acc = vmlal_high_u16(s2acc, t4_t7.2, s2_6); |
| 214 |
s2acc_0 = vmlal_u16(s2acc_0, vget_low_u16(t4_t7.2), vget_low_u16(s2_6)); |
| 215 |
s2acc_1 = vmlal_high_u16(s2acc_1, t4_t7.3, s2_7); |
| 216 |
s2acc_2 = vmlal_u16(s2acc_2, vget_low_u16(t4_t7.3), vget_low_u16(s2_7)); |
| 217 |
|
| 218 |
s2acc = vaddq_u32(s2acc_0, s2acc); |
| 219 |
s2acc_2 = vaddq_u32(s2acc_1, s2acc_2); |
| 220 |
s2acc = vaddq_u32(s2acc, s2acc_2); |
| 221 |
|
| 222 |
let s2acc = vaddq_u32(s2acc, s3acc); |
| 223 |
let adacc2 = vpadd_u32(vget_low_u32(adacc), vget_high_u32(adacc)); |
| 224 |
let s2acc2 = vpadd_u32(vget_low_u32(s2acc), vget_high_u32(s2acc)); |
| 225 |
let as_ = vpadd_u32(adacc2, s2acc2); |
| 226 |
|
| 227 |
(vget_lane_u32(as_, 0), vget_lane_u32(as_, 1)) |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
#[cfg(all(test, feature = "std", any(miri, target_feature = "neon")))] |
| 232 |
mod tests { |
| 233 |
use super::*; |
| 234 |
|
| 235 |
quickcheck::quickcheck! { |
| 236 |
fn adler32_neon_is_adler32_rust(v: Vec<u8>, start: u32) -> bool { |
| 237 |
let neon = adler32_neon(start, &v); |
| 238 |
let rust = crate::adler32::generic::adler32_rust(start, &v); |
| 239 |
|
| 240 |
rust == neon |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
const INPUT: [u8; 1024] = { |
| 245 |
let mut array = [0; 1024]; |
| 246 |
let mut i = 0; |
| 247 |
while i < array.len() { |
| 248 |
array[i] = i as u8; |
| 249 |
i += 1; |
| 250 |
} |
| 251 |
|
| 252 |
array |
| 253 |
}; |
| 254 |
|
| 255 |
#[test] |
| 256 |
fn start_alignment() { |
| 257 |
// SIMD algorithm is sensitive to alignment; |
| 258 |
for i in 0..16 { |
| 259 |
for start in [crate::ADLER32_INITIAL_VALUE as u32, 42] { |
| 260 |
let neon = adler32_neon(start, &INPUT[i..]); |
| 261 |
let rust = crate::adler32::generic::adler32_rust(start, &INPUT[i..]); |
| 262 |
|
| 263 |
assert_eq!(neon, rust, "offset = {i}, start = {start}"); |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
#[test] |
| 269 |
fn large_input() { |
| 270 |
const DEFAULT: &[u8] = include_bytes!("../deflate/test-data/paper-100k.pdf"); |
| 271 |
|
| 272 |
let neon = adler32_neon(42, DEFAULT); |
| 273 |
let rust = crate::adler32::generic::adler32_rust(42, DEFAULT); |
| 274 |
|
| 275 |
assert_eq!(neon, rust); |
| 276 |
} |
| 277 |
|
| 278 |
// Regression test for a bug where adler32 would not modulo by the base early enough, |
| 279 |
// specifically it ignored the `before` slice in when to mod. |
| 280 |
#[test] |
| 281 |
fn carry_in_with_unaligned_before_no_overflow() { |
| 282 |
let backing = vec![0xffu8; 5568]; |
| 283 |
let buf: &[u8] = &backing[1..1 + 5567]; |
| 284 |
let start: u32 = 0xa4c1_fb51; |
| 285 |
let neon = adler32_neon(start, buf); |
| 286 |
let rust = crate::adler32::generic::adler32_rust(start, buf); |
| 287 |
assert_eq!(neon, rust); |
| 288 |
} |
| 289 |
} |