From ee17f4c6d295e82733673f824e7dba81a33e245b Mon Sep 17 00:00:00 2001 From: Pavel P Date: Sat, 18 Jan 2025 21:32:30 +0200 Subject: [PATCH 1/5] Use _bzhi_u32 for 32-bit builds when building with STATIC_BMI2 `_bzhi_u64` is available only for 64-bit builds, while `BIT_getLowerBits` expects `nbBits` to be less than `BIT_MASK_SIZE` (`BIT_MASK_SIZE` is 32) --- lib/common/bitstream.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/common/bitstream.h b/lib/common/bitstream.h index a1dac889d3..c9c645416f 100644 --- a/lib/common/bitstream.h +++ b/lib/common/bitstream.h @@ -162,7 +162,11 @@ MEM_STATIC size_t BIT_initCStream(BIT_CStream_t* bitC, FORCE_INLINE_TEMPLATE size_t BIT_getLowerBits(size_t bitContainer, U32 const nbBits) { #if defined(STATIC_BMI2) && STATIC_BMI2 == 1 && !defined(ZSTD_NO_INTRINSICS) - return _bzhi_u64(bitContainer, nbBits); +#if defined(__x86_64__) || defined(_M_X64) + return _bzhi_u64(bitContainer, nbBits); +#else + return _bzhi_u32(bitContainer, nbBits); +#endif #else assert(nbBits < BIT_MASK_SIZE); return bitContainer & BIT_mask[nbBits]; From 936927a427704ca21dfd07c666c4123737c8fb03 Mon Sep 17 00:00:00 2001 From: Pavel P Date: Sat, 18 Jan 2025 23:27:19 +0200 Subject: [PATCH 2/5] handle 32bit size_t when building for x64 --- lib/common/bitstream.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/common/bitstream.h b/lib/common/bitstream.h index c9c645416f..c0db11d405 100644 --- a/lib/common/bitstream.h +++ b/lib/common/bitstream.h @@ -162,11 +162,11 @@ MEM_STATIC size_t BIT_initCStream(BIT_CStream_t* bitC, FORCE_INLINE_TEMPLATE size_t BIT_getLowerBits(size_t bitContainer, U32 const nbBits) { #if defined(STATIC_BMI2) && STATIC_BMI2 == 1 && !defined(ZSTD_NO_INTRINSICS) -#if defined(__x86_64__) || defined(_M_X64) - return _bzhi_u64(bitContainer, nbBits); -#else +# if defined(__x86_64__) || defined(_M_X64) + return sizeof(size_t) == sizeof(U64) ? _bzhi_u64(bitContainer, nbBits) : _bzhi_u32((U32)bitContainer, nbBits); +# else return _bzhi_u32(bitContainer, nbBits); -#endif +# endif #else assert(nbBits < BIT_MASK_SIZE); return bitContainer & BIT_mask[nbBits]; From 26e5fb36149b9d155a3640ca0800199fc13711e8 Mon Sep 17 00:00:00 2001 From: Pavel P Date: Sat, 18 Jan 2025 23:37:50 +0200 Subject: [PATCH 3/5] handle 32bit size_t when building for x64 --- lib/common/bitstream.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/common/bitstream.h b/lib/common/bitstream.h index c0db11d405..d976c54783 100644 --- a/lib/common/bitstream.h +++ b/lib/common/bitstream.h @@ -162,9 +162,10 @@ MEM_STATIC size_t BIT_initCStream(BIT_CStream_t* bitC, FORCE_INLINE_TEMPLATE size_t BIT_getLowerBits(size_t bitContainer, U32 const nbBits) { #if defined(STATIC_BMI2) && STATIC_BMI2 == 1 && !defined(ZSTD_NO_INTRINSICS) -# if defined(__x86_64__) || defined(_M_X64) - return sizeof(size_t) == sizeof(U64) ? _bzhi_u64(bitContainer, nbBits) : _bzhi_u32((U32)bitContainer, nbBits); +# if (defined(__x86_64__) || defined(_M_X64)) && !defined(__ILP32__) + return _bzhi_u64(bitContainer, nbBits); # else + DEBUG_STATIC_ASSERT(sizeof(size_t) == sizeof(U32)); return _bzhi_u32(bitContainer, nbBits); # endif #else From 462484d5dcbab964474bf4704df4d188e5c31818 Mon Sep 17 00:00:00 2001 From: Pavel P Date: Sun, 19 Jan 2025 02:34:23 +0200 Subject: [PATCH 4/5] change to BitContainerType --- lib/common/bitstream.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/common/bitstream.h b/lib/common/bitstream.h index d976c54783..582f9dd721 100644 --- a/lib/common/bitstream.h +++ b/lib/common/bitstream.h @@ -159,7 +159,7 @@ MEM_STATIC size_t BIT_initCStream(BIT_CStream_t* bitC, return 0; } -FORCE_INLINE_TEMPLATE size_t BIT_getLowerBits(size_t bitContainer, U32 const nbBits) +FORCE_INLINE_TEMPLATE size_t BIT_getLowerBits(BitContainerType bitContainer, U32 const nbBits) { #if defined(STATIC_BMI2) && STATIC_BMI2 == 1 && !defined(ZSTD_NO_INTRINSICS) # if (defined(__x86_64__) || defined(_M_X64)) && !defined(__ILP32__) From fcd684b9b45917dbb90e8122faf782e4028fdb42 Mon Sep 17 00:00:00 2001 From: Pavel P Date: Sun, 19 Jan 2025 02:37:35 +0200 Subject: [PATCH 5/5] update sizeof check --- lib/common/bitstream.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/common/bitstream.h b/lib/common/bitstream.h index 582f9dd721..bc7118b375 100644 --- a/lib/common/bitstream.h +++ b/lib/common/bitstream.h @@ -165,7 +165,7 @@ FORCE_INLINE_TEMPLATE size_t BIT_getLowerBits(BitContainerType bitContainer, U32 # if (defined(__x86_64__) || defined(_M_X64)) && !defined(__ILP32__) return _bzhi_u64(bitContainer, nbBits); # else - DEBUG_STATIC_ASSERT(sizeof(size_t) == sizeof(U32)); + DEBUG_STATIC_ASSERT(sizeof(bitContainer) == sizeof(U32)); return _bzhi_u32(bitContainer, nbBits); # endif #else