From 75be20f7fa39d38ea209609cff04925fa1dd673f Mon Sep 17 00:00:00 2001 From: Olivier Giniaux Date: Thu, 19 Oct 2023 00:59:28 +0200 Subject: [PATCH] Change masking technique for partial vector --- src/gxhash.rs | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/gxhash.rs b/src/gxhash.rs index aa38646..706d49f 100644 --- a/src/gxhash.rs +++ b/src/gxhash.rs @@ -117,19 +117,23 @@ mod platform_defs { #[inline] pub unsafe fn get_partial(p: *const state, len: isize) -> state { - const MASK: [u8; size_of::() * 2] = [ - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ]; - + let partial_vector: state; // Safety check - if check_same_page(p) { // false {// - let mask = _mm256_loadu_epi8((MASK.as_ptr() as *const i8).offset(32 - len)); - _mm256_and_si256(_mm256_loadu_si256(p), mask) + if check_same_page(p) { + let indices = _mm256_setr_epi8( + 0, 1, 2, 3, 4, 5, 6, 7, + 8, 9, 10, 11, 12, 13, 14, 15, + 16, 17, 18, 19, 20, 21, 22, 23, + 24, 25, 26, 27, 28, 29, 30, 31 + ); + + let mask = _mm256_cmpgt_epi8(_mm256_set1_epi8(len as i8), indices); + partial_vector = _mm256_and_si256(_mm256_loadu_si256(p), mask); } else { - get_partial_safe(p as *const u8, len as usize) + partial_vector = get_partial_safe(p as *const u8, len as usize) } + // Prevents padded zeroes to introduce bias + _mm256_add_epi32(partial_vector, _mm256_set1_epi32(len as i32)) } #[inline]