| 1 |
#[cfg(test)] |
| 2 |
const MAX_COMPARE_SIZE: usize = 256; |
| 3 |
|
| 4 |
pub fn compare256_slice(src0: &[u8], src1: &[u8]) -> usize { |
| 5 |
let src0 = first_chunk::<_, 256>(src0).unwrap(); |
| 6 |
let src1 = first_chunk::<_, 256>(src1).unwrap(); |
| 7 |
|
| 8 |
compare256(src0, src1) |
| 9 |
} |
| 10 |
|
| 11 |
fn compare256(src0: &[u8; 256], src1: &[u8; 256]) -> usize { |
| 12 |
#[cfg(feature = "avx512")] |
| 13 |
#[cfg(target_arch = "x86_64")] |
| 14 |
if cfg!(target_feature = "avx512vl") && cfg!(target_feature = "avx512bw") { |
| 15 |
return unsafe { avx512::compare256(src0, src1) }; |
| 16 |
} |
| 17 |
|
| 18 |
#[cfg(target_arch = "x86_64")] |
| 19 |
if crate::cpu_features::is_enabled_avx2_and_bmi2() { |
| 20 |
return unsafe { avx2::compare256(src0, src1) }; |
| 21 |
} |
| 22 |
|
| 23 |
#[cfg(target_arch = "aarch64")] |
| 24 |
if crate::cpu_features::is_enabled_neon() { |
| 25 |
return unsafe { neon::compare256(src0, src1) }; |
| 26 |
} |
| 27 |
|
| 28 |
#[cfg(all(target_arch = "loongarch64", feature = "lsx"))] |
| 29 |
if crate::cpu_features::is_enabled_lsx() { |
| 30 |
return unsafe { lsx::compare256(src0, src1) }; |
| 31 |
} |
| 32 |
|
| 33 |
#[cfg(target_arch = "wasm32")] |
| 34 |
if crate::cpu_features::is_enabled_simd128() { |
| 35 |
return wasm32::compare256(src0, src1); |
| 36 |
} |
| 37 |
|
| 38 |
rust::compare256(src0, src1) |
| 39 |
} |
| 40 |
|
| 41 |
pub fn compare256_rle_slice(byte: u8, src: &[u8]) -> usize { |
| 42 |
rust::compare256_rle(byte, src) |
| 43 |
} |
| 44 |
|
| 45 |
#[inline] |
| 46 |
pub const fn first_chunk<T, const N: usize>(slice: &[T]) -> Option<&[T; N]> { |
| 47 |
if slice.len() < N { |
| 48 |
None |
| 49 |
} else { |
| 50 |
// SAFETY: We explicitly check for the correct number of elements, |
| 51 |
// and do not let the reference outlive the slice. |
| 52 |
Some(unsafe { &*(slice.as_ptr() as *const [T; N]) }) |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
mod rust { |
| 57 |
|
| 58 |
pub fn compare256(src0: &[u8; 256], src1: &[u8; 256]) -> usize { |
| 59 |
// only unrolls 4 iterations; zlib-ng unrolls 8 |
| 60 |
src0.iter().zip(src1).take_while(|(x, y)| x == y).count() |
| 61 |
} |
| 62 |
|
| 63 |
// run-length encoding |
| 64 |
pub fn compare256_rle(byte: u8, src: &[u8]) -> usize { |
| 65 |
assert!(src.len() >= 256, "too short {}", src.len()); |
| 66 |
|
| 67 |
let sv = u64::from_ne_bytes([byte; 8]); |
| 68 |
let mut len = 0; |
| 69 |
|
| 70 |
// this optimizes well because we statically limit the slice to 256 bytes. |
| 71 |
// the loop gets unrolled 4 times automatically. |
| 72 |
for chunk in src[..256].chunks_exact(8) { |
| 73 |
let mv = u64::from_le_bytes(chunk.try_into().unwrap()); |
| 74 |
|
| 75 |
let diff = sv ^ mv; |
| 76 |
|
| 77 |
if diff > 0 { |
| 78 |
let match_byte = diff.trailing_zeros() / 8; |
| 79 |
return len + match_byte as usize; |
| 80 |
} |
| 81 |
|
| 82 |
len += 8 |
| 83 |
} |
| 84 |
|
| 85 |
256 |
| 86 |
} |
| 87 |
|
| 88 |
#[test] |
| 89 |
fn test_compare256() { |
| 90 |
let str1 = [b'a'; super::MAX_COMPARE_SIZE]; |
| 91 |
let mut str2 = [b'a'; super::MAX_COMPARE_SIZE]; |
| 92 |
|
| 93 |
for i in 0..str1.len() { |
| 94 |
str2[i] = 0; |
| 95 |
|
| 96 |
let match_len = compare256(&str1, &str2); |
| 97 |
assert_eq!(match_len, i); |
| 98 |
|
| 99 |
str2[i] = b'a'; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
#[test] |
| 104 |
fn test_compare256_rle() { |
| 105 |
let mut string = [b'a'; super::MAX_COMPARE_SIZE]; |
| 106 |
|
| 107 |
for i in 0..string.len() { |
| 108 |
string[i] = 0; |
| 109 |
|
| 110 |
let match_len = compare256_rle(b'a', &string); |
| 111 |
assert_eq!(match_len, i); |
| 112 |
|
| 113 |
string[i] = b'a'; |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
#[cfg(target_arch = "aarch64")] |
| 119 |
mod neon { |
| 120 |
use core::arch::aarch64::{ |
| 121 |
uint8x16x4_t, vceqq_u8, vget_lane_u64, vld4q_u8, vreinterpret_u64_u8, vreinterpretq_u16_u8, |
| 122 |
vrev16q_u8, vshrn_n_u16, vsriq_n_u8, |
| 123 |
}; |
| 124 |
|
| 125 |
/// # Safety |
| 126 |
/// |
| 127 |
/// Behavior is undefined if the `neon` target feature is not enabled |
| 128 |
#[target_feature(enable = "neon")] |
| 129 |
pub unsafe fn compare256(src0: &[u8; 256], src1: &[u8; 256]) -> usize { |
| 130 |
type Chunk = uint8x16x4_t; |
| 131 |
let src0 = src0.chunks_exact(core::mem::size_of::<Chunk>()); |
| 132 |
let src1 = src1.chunks_exact(core::mem::size_of::<Chunk>()); |
| 133 |
|
| 134 |
let mut len = 0; |
| 135 |
|
| 136 |
for (a, b) in src0.zip(src1) { |
| 137 |
unsafe { |
| 138 |
// Load 4 vectors *deinterleaved* from the two slices |
| 139 |
// e.g. the first vector contains the 0, 4, 8, ... bytes of the input, the |
| 140 |
// second vector contains the 1, 5, 9, ... bytes of the input, etc. |
| 141 |
let a: Chunk = vld4q_u8(a.as_ptr()); |
| 142 |
let b: Chunk = vld4q_u8(b.as_ptr()); |
| 143 |
|
| 144 |
// Compare each vector element-wise, each resulting vector will contain |
| 145 |
// 0xFF for equal bytes, and 0x00 for unequal bytes. |
| 146 |
let cmp0 = vceqq_u8(a.0, b.0); |
| 147 |
let cmp1 = vceqq_u8(a.1, b.1); |
| 148 |
let cmp2 = vceqq_u8(a.2, b.2); |
| 149 |
let cmp3 = vceqq_u8(a.3, b.3); |
| 150 |
|
| 151 |
// Pack bits from the 4 vectors into a single vector to convert to a 64-bit integer. |
| 152 |
|
| 153 |
// shift the second vector right by one, insert the top bit from the first vector |
| 154 |
// The top two bits each element of the result are from the first and second vector |
| 155 |
let first_two_bits = vsriq_n_u8::<1>(cmp1, cmp0); |
| 156 |
|
| 157 |
// shift the fourth vector right by one, insert the top bit from the third vector |
| 158 |
// The top two bits each element of the result are from the third and fourth vector |
| 159 |
let last_two_bits = vsriq_n_u8::<1>(cmp3, cmp2); |
| 160 |
|
| 161 |
// shift last_two_bits (the top two bits of which are from the third and fourth |
| 162 |
// vector) right by 2, insert the top two bits from first_two_bits (the top two |
| 163 |
// bits of which are from the first and second vector). |
| 164 |
// The top four bits of each element of the result are from the |
| 165 |
// first, second, third, and fourth vector |
| 166 |
let first_four_bits = vsriq_n_u8::<2>(last_two_bits, first_two_bits); |
| 167 |
|
| 168 |
// duplicate the top 4 bits into the bottom 4 bits of each element. |
| 169 |
let bitmask_vector = vsriq_n_u8::<4>(first_four_bits, first_four_bits); |
| 170 |
|
| 171 |
// Reinterpret as 16-bit integers, and shift right by 4 bits narrowing: |
| 172 |
// shifting right by 4 bits means the top 4 bits of each 16 bit element contains the |
| 173 |
// low 4 bits of the 0th 8-bit element and the high 4 bits of the 1nth 8-bit |
| 174 |
// element. Narrowing takes the top 8 bits of each (16-bit) element. |
| 175 |
let bitmask_vector = if cfg!(target_endian = "big") { |
| 176 |
// We interpret this as u16 below; on BE swap the bytes around. |
| 177 |
vrev16q_u8(bitmask_vector) |
| 178 |
} else { |
| 179 |
bitmask_vector |
| 180 |
}; |
| 181 |
|
| 182 |
let result_vector = vshrn_n_u16::<4>(vreinterpretq_u16_u8(bitmask_vector)); |
| 183 |
|
| 184 |
// Convert the vector to a 64-bit integer, where each bit represents whether |
| 185 |
// the corresponding byte in the original vectors was equal. |
| 186 |
let bitmask = vget_lane_u64::<0>(vreinterpret_u64_u8(result_vector)); |
| 187 |
|
| 188 |
// We reinterpreted the vector as a 64-bit integer, so endianness matters. |
| 189 |
// We want things to be in little-endian (where the least significant bit is in the |
| 190 |
// first byte), but in big-endian, the first vector element will be the most |
| 191 |
// significant byte, so we need to convert to little-endian. |
| 192 |
let bitmask = bitmask.to_le(); |
| 193 |
if bitmask != u64::MAX { |
| 194 |
// Find the first byte that is not equal, which is the first bit that is not set |
| 195 |
let match_byte = bitmask.trailing_ones(); |
| 196 |
return len + match_byte as usize; |
| 197 |
} |
| 198 |
|
| 199 |
len += core::mem::size_of::<Chunk>(); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
256 |
| 204 |
} |
| 205 |
|
| 206 |
#[test] |
| 207 |
fn test_compare256() { |
| 208 |
if crate::cpu_features::is_enabled_neon() { |
| 209 |
let str1 = [b'a'; super::MAX_COMPARE_SIZE]; |
| 210 |
let mut str2 = [b'a'; super::MAX_COMPARE_SIZE]; |
| 211 |
|
| 212 |
for i in 0..str1.len() { |
| 213 |
str2[i] = 0; |
| 214 |
|
| 215 |
let match_len = unsafe { compare256(&str1, &str2) }; |
| 216 |
assert_eq!(match_len, i); |
| 217 |
|
| 218 |
str2[i] = b'a'; |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
#[cfg(target_arch = "x86_64")] |
| 225 |
mod avx2 { |
| 226 |
use core::arch::x86_64::{ |
| 227 |
__m256i, _mm256_cmpeq_epi8, _mm256_loadu_si256, _mm256_movemask_epi8, |
| 228 |
}; |
| 229 |
|
| 230 |
/// # Safety |
| 231 |
/// |
| 232 |
/// Behavior is undefined if the `avx` target feature is not enabled |
| 233 |
#[target_feature(enable = "avx2")] |
| 234 |
#[target_feature(enable = "bmi2")] |
| 235 |
#[target_feature(enable = "bmi1")] |
| 236 |
pub unsafe fn compare256(src0: &[u8; 256], src1: &[u8; 256]) -> usize { |
| 237 |
let src0 = src0.chunks_exact(32); |
| 238 |
let src1 = src1.chunks_exact(32); |
| 239 |
|
| 240 |
let mut len = 0; |
| 241 |
|
| 242 |
unsafe { |
| 243 |
for (chunk0, chunk1) in src0.zip(src1) { |
| 244 |
let ymm_src0 = _mm256_loadu_si256(chunk0.as_ptr() as *const __m256i); |
| 245 |
let ymm_src1 = _mm256_loadu_si256(chunk1.as_ptr() as *const __m256i); |
| 246 |
|
| 247 |
// element-wise compare of the 8-bit elements |
| 248 |
let ymm_cmp = _mm256_cmpeq_epi8(ymm_src0, ymm_src1); |
| 249 |
|
| 250 |
// turn an 32 * 8-bit vector into a 32-bit integer. |
| 251 |
// a bit in the output is one if the corresponding element is non-zero. |
| 252 |
let mask = _mm256_movemask_epi8(ymm_cmp) as u32; |
| 253 |
|
| 254 |
if mask != 0xFFFFFFFF { |
| 255 |
let match_byte = mask.trailing_ones(); |
| 256 |
return len + match_byte as usize; |
| 257 |
} |
| 258 |
|
| 259 |
len += 32; |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
256 |
| 264 |
} |
| 265 |
|
| 266 |
#[test] |
| 267 |
fn test_compare256() { |
| 268 |
if crate::cpu_features::is_enabled_avx2_and_bmi2() { |
| 269 |
let str1 = [b'a'; super::MAX_COMPARE_SIZE]; |
| 270 |
let mut str2 = [b'a'; super::MAX_COMPARE_SIZE]; |
| 271 |
|
| 272 |
for i in 0..str1.len() { |
| 273 |
str2[i] = 0; |
| 274 |
|
| 275 |
let match_len = unsafe { compare256(&str1, &str2) }; |
| 276 |
assert_eq!(match_len, i); |
| 277 |
|
| 278 |
str2[i] = b'a'; |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
#[cfg(feature = "avx512")] |
| 285 |
#[cfg(target_arch = "x86_64")] |
| 286 |
mod avx512 { |
| 287 |
use core::arch::x86_64::{ |
| 288 |
_mm512_cmpeq_epu8_mask, _mm512_loadu_si512, _mm_cmpeq_epu8_mask, _mm_loadu_si128, |
| 289 |
}; |
| 290 |
|
| 291 |
/// # Safety |
| 292 |
/// |
| 293 |
/// Behavior is undefined if the `avx` target feature is not enabled |
| 294 |
#[target_feature(enable = "avx512vl")] |
| 295 |
#[target_feature(enable = "avx512bw")] |
| 296 |
pub unsafe fn compare256(src0: &[u8; 256], src1: &[u8; 256]) -> usize { |
| 297 |
// First do a 16byte round before increasing to 64bytes, this reduces the |
| 298 |
// penalty for the short matches, and those are usually the most common ones. |
| 299 |
// This requires us to overlap on the last round, giving a small penalty |
| 300 |
// on matches of 192+ bytes (Still faster than AVX2 though). |
| 301 |
|
| 302 |
unsafe { |
| 303 |
// 16 bytes |
| 304 |
let xmm_src0_0 = _mm_loadu_si128(src0.as_ptr().cast()); |
| 305 |
let xmm_src1_0 = _mm_loadu_si128(src1.as_ptr().cast()); |
| 306 |
let mask_0 = u32::from(_mm_cmpeq_epu8_mask(xmm_src0_0, xmm_src1_0)); // zero-extended to use __builtin_ctz |
| 307 |
if mask_0 != 0x0000FFFF { |
| 308 |
// There is potential for using __builtin_ctzg/__builtin_ctzs/_tzcnt_u16/__tzcnt_u16 here |
| 309 |
let match_byte = mask_0.trailing_ones(); |
| 310 |
return match_byte as usize; |
| 311 |
} |
| 312 |
|
| 313 |
// 64 bytes |
| 314 |
let zmm_src0_1 = _mm512_loadu_si512(src0[16..].as_ptr().cast()); |
| 315 |
let zmm_src1_1 = _mm512_loadu_si512(src1[16..].as_ptr().cast()); |
| 316 |
let mask_1 = _mm512_cmpeq_epu8_mask(zmm_src0_1, zmm_src1_1); |
| 317 |
if mask_1 != 0xFFFFFFFFFFFFFFFF { |
| 318 |
let match_byte = mask_1.trailing_ones(); |
| 319 |
return 16 + match_byte as usize; |
| 320 |
} |
| 321 |
|
| 322 |
// 64 bytes |
| 323 |
let zmm_src0_2 = _mm512_loadu_si512(src0[80..].as_ptr().cast()); |
| 324 |
let zmm_src1_2 = _mm512_loadu_si512(src1[80..].as_ptr().cast()); |
| 325 |
let mask_2 = _mm512_cmpeq_epu8_mask(zmm_src0_2, zmm_src1_2); |
| 326 |
if mask_2 != 0xFFFFFFFFFFFFFFFF { |
| 327 |
let match_byte = mask_2.trailing_ones(); |
| 328 |
return 80 + match_byte as usize; |
| 329 |
} |
| 330 |
|
| 331 |
// 64 bytes |
| 332 |
let zmm_src0_3 = _mm512_loadu_si512(src0[144..].as_ptr().cast()); |
| 333 |
let zmm_src1_3 = _mm512_loadu_si512(src1[144..].as_ptr().cast()); |
| 334 |
let mask_3 = _mm512_cmpeq_epu8_mask(zmm_src0_3, zmm_src1_3); |
| 335 |
if mask_3 != 0xFFFFFFFFFFFFFFFF { |
| 336 |
let match_byte = mask_3.trailing_ones(); |
| 337 |
return 144 + match_byte as usize; |
| 338 |
} |
| 339 |
|
| 340 |
// 64 bytes (overlaps the previous 16 bytes for fast tail processing) |
| 341 |
let zmm_src0_4 = _mm512_loadu_si512(src0[192..].as_ptr().cast()); |
| 342 |
let zmm_src1_4 = _mm512_loadu_si512(src1[192..].as_ptr().cast()); |
| 343 |
let mask_4 = _mm512_cmpeq_epu8_mask(zmm_src0_4, zmm_src1_4); |
| 344 |
if mask_4 != 0xFFFFFFFFFFFFFFFF { |
| 345 |
let match_byte = mask_4.trailing_ones(); |
| 346 |
return 192 + match_byte as usize; |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
256 |
| 351 |
} |
| 352 |
|
| 353 |
#[test] |
| 354 |
fn test_compare256() { |
| 355 |
if cfg!(target_feature = "avx512vl") && cfg!(target_feature = "avx512bw") { |
| 356 |
let str1 = [b'a'; super::MAX_COMPARE_SIZE]; |
| 357 |
let mut str2 = [b'a'; super::MAX_COMPARE_SIZE]; |
| 358 |
|
| 359 |
for i in 0..str1.len() { |
| 360 |
str2[i] = 0; |
| 361 |
|
| 362 |
let match_len = unsafe { compare256(&str1, &str2) }; |
| 363 |
assert_eq!(match_len, i); |
| 364 |
|
| 365 |
str2[i] = b'a'; |
| 366 |
} |
| 367 |
} |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
#[cfg(all(target_arch = "loongarch64", feature = "lsx"))] |
| 372 |
mod lsx { |
| 373 |
use core::arch::loongarch64::{lsx_vld, lsx_vmskltz_b, lsx_vpickve2gr_w, lsx_vseq_b}; |
| 374 |
|
| 375 |
#[target_feature(enable = "lsx")] |
| 376 |
pub fn compare256(src0: &[u8; 256], src1: &[u8; 256]) -> usize { |
| 377 |
let src0 = src0.chunks_exact(16); |
| 378 |
let src1 = src1.chunks_exact(16); |
| 379 |
|
| 380 |
let mut len = 0; |
| 381 |
|
| 382 |
unsafe { |
| 383 |
for (chunk0, chunk1) in src0.zip(src1) { |
| 384 |
let a = lsx_vld::<0>(chunk0.as_ptr() as *const i8); |
| 385 |
let b = lsx_vld::<0>(chunk1.as_ptr() as *const i8); |
| 386 |
|
| 387 |
// element-wise compare of the 8-bit elements |
| 388 |
// 0xff if equal, otherwise 0 |
| 389 |
let v = lsx_vseq_b(a, b); |
| 390 |
|
| 391 |
// bootleg movemask implementation |
| 392 |
let mask = lsx_vpickve2gr_w::<0>(lsx_vmskltz_b(v)); |
| 393 |
|
| 394 |
if mask != 0xFFFF { |
| 395 |
let match_byte = mask.trailing_ones(); |
| 396 |
return len + match_byte as usize; |
| 397 |
} |
| 398 |
|
| 399 |
len += 16; |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
256 |
| 404 |
} |
| 405 |
|
| 406 |
#[test] |
| 407 |
fn test_compare256() { |
| 408 |
if crate::cpu_features::is_enabled_lsx() { |
| 409 |
let str1 = [b'a'; super::MAX_COMPARE_SIZE]; |
| 410 |
let mut str2 = [b'a'; super::MAX_COMPARE_SIZE]; |
| 411 |
|
| 412 |
for i in 0..str1.len() { |
| 413 |
str2[i] = 0; |
| 414 |
|
| 415 |
let match_len = unsafe { compare256(&str1, &str2) }; |
| 416 |
assert_eq!(match_len, i); |
| 417 |
|
| 418 |
str2[i] = b'a'; |
| 419 |
} |
| 420 |
} |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
#[cfg(target_arch = "wasm32")] |
| 425 |
mod wasm32 { |
| 426 |
use core::arch::wasm32::{u8x16_bitmask, u8x16_eq, v128, v128_load}; |
| 427 |
|
| 428 |
#[target_feature(enable = "simd128")] |
| 429 |
pub fn compare256(src0: &[u8; 256], src1: &[u8; 256]) -> usize { |
| 430 |
let src0 = src0.chunks_exact(16); |
| 431 |
let src1 = src1.chunks_exact(16); |
| 432 |
|
| 433 |
let mut len = 0; |
| 434 |
|
| 435 |
for (chunk0, chunk1) in src0.zip(src1) { |
| 436 |
// SAFETY: these are valid pointers to slice data. |
| 437 |
let v128_src0 = unsafe { v128_load(chunk0.as_ptr() as *const v128) }; |
| 438 |
let v128_src1 = unsafe { v128_load(chunk1.as_ptr() as *const v128) }; |
| 439 |
|
| 440 |
let v128_cmp = u8x16_eq(v128_src0, v128_src1); |
| 441 |
let mask = u8x16_bitmask(v128_cmp); |
| 442 |
|
| 443 |
if mask != 0xFFFF { |
| 444 |
let match_byte = mask.trailing_ones(); |
| 445 |
return len + match_byte as usize; |
| 446 |
} |
| 447 |
|
| 448 |
len += 16; |
| 449 |
} |
| 450 |
|
| 451 |
256 |
| 452 |
} |
| 453 |
|
| 454 |
#[test] |
| 455 |
fn test_compare256() { |
| 456 |
if crate::cpu_features::is_enabled_simd128() { |
| 457 |
let str1 = [b'a'; super::MAX_COMPARE_SIZE]; |
| 458 |
let mut str2 = [b'a'; super::MAX_COMPARE_SIZE]; |
| 459 |
|
| 460 |
for i in 0..str1.len() { |
| 461 |
str2[i] = 0; |
| 462 |
|
| 463 |
let match_len = unsafe { compare256(&str1, &str2) }; |
| 464 |
assert_eq!(match_len, i); |
| 465 |
|
| 466 |
str2[i] = b'a'; |
| 467 |
} |
| 468 |
} |
| 469 |
} |
| 470 |
} |