Wrap BMI2 instruction usage in support checks.
A previous version of this was submitted and rolled back due to breakage -- an attempt to accommodate Visual Studio resulted in compiler errors on GCC/Clang with -mavx2 but without -mbmi2. This version makes the BMI2 support check more strict, to avoid the errors. A previous CL introduced _bzhi_u32 (part of Intel's BMI2 instruction set, released in Haswell) gated by a check for the __BMI2__ preprocessor macro. This works for Clang and GCC, but does not work on Visual Studio, and may not work on other compilers. This CL plumbs the BMI2 support checks through the CMake configuration used by the open source build. It also replaces the <x86intrin.h> header, which does not exist on Visual Studio, with the more scoped headers <tmmintrin.h> (for SSSE3) and <immintrin.h> (for BMI2/AVX2). Asides from fixing the open source build, the more scoped headers make it slightly less likely that newer intrinsics will creep in without proper gating.
This commit is contained in:
27
snappy.cc
27
snappy.cc
@@ -41,8 +41,31 @@
|
||||
#endif
|
||||
#endif // !defined(SNAPPY_HAVE_SSSE3)
|
||||
|
||||
#if !defined(SNAPPY_HAVE_BMI2)
|
||||
// __BMI2__ is defined by GCC and Clang. Visual Studio doesn't target BMI2
|
||||
// specifically, but it does define __AVX2__ when AVX2 support is available.
|
||||
// Fortunately, AVX2 was introduced in Haswell, just like BMI2.
|
||||
//
|
||||
// BMI2 is not defined as a subset of AVX2 (unlike SSSE3 and AVX above). So,
|
||||
// GCC and Clang can build code with AVX2 enabled but BMI2 disabled, in which
|
||||
// case issuing BMI2 instructions results in a compiler error.
|
||||
#if defined(__BMI2__) || (defined(_MSC_VER) && defined(__AVX2__))
|
||||
#define SNAPPY_HAVE_BMI2 1
|
||||
#else
|
||||
#define SNAPPY_HAVE_BMI2 0
|
||||
#endif
|
||||
#endif // !defined(SNAPPY_HAVE_BMI2)
|
||||
|
||||
#if SNAPPY_HAVE_SSSE3
|
||||
#include <x86intrin.h>
|
||||
// Please do not replace with <x86intrin.h>. or with headers that assume more
|
||||
// advanced SSE versions without checking with all the OWNERS.
|
||||
#include <tmmintrin.h>
|
||||
#endif
|
||||
|
||||
#if SNAPPY_HAVE_BMI2
|
||||
// Please do not replace with <x86intrin.h>. or with headers that assume more
|
||||
// advanced SSE versions without checking with all the OWNERS.
|
||||
#include <immintrin.h>
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
@@ -699,7 +722,7 @@ static inline uint32 ExtractLowBytes(uint32 v, int n) {
|
||||
assert(n <= 4);
|
||||
// TODO(b/121042345): Remove !defined(MEMORY_SANITIZER) once MSan
|
||||
// handles _bzhi_u32() correctly.
|
||||
#if defined(__BMI2__) && !defined(MEMORY_SANITIZER)
|
||||
#if SNAPPY_HAVE_BMI2 && !defined(MEMORY_SANITIZER)
|
||||
return _bzhi_u32(v, 8 * n);
|
||||
#else
|
||||
// This needs to be wider than uint32 otherwise `mask << 32` will be
|
||||
|
||||
Reference in New Issue
Block a user