Skip to content

Commit da79cc1

Browse files
authored
Merge pull request #924 from Cyan4973/xxh64_noVector
disable XXH64 autovectorization
2 parents 337b83a + 8cf3346 commit da79cc1

File tree

2 files changed

+52
-23
lines changed

2 files changed

+52
-23
lines changed

README.md

+34-22
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,15 @@ A more detailed analysis is documented [in the wiki](https://github.com/Cyan4973
9595

9696
The following macros can be set at compilation time to modify libxxhash's behavior. They are generally disabled by default.
9797

98-
- `XXH_INLINE_ALL`: Make all functions `inline`, with implementations being directly included within `xxhash.h`.
98+
- `XXH_INLINE_ALL`: Make all functions `inline`, implementations is directly included from within `xxhash.h`.
9999
Inlining functions is beneficial for speed on small keys.
100100
It's _extremely effective_ when key length is expressed as _a compile time constant_,
101101
with performance improvements observed in the +200% range .
102102
See [this article](https://fastcompression.blogspot.com/2018/03/xxhash-for-small-keys-impressive-power.html) for details.
103103
- `XXH_PRIVATE_API`: same outcome as `XXH_INLINE_ALL`. Still available for legacy support.
104104
The name underlines that `XXH_*` symbol names will not be exported.
105+
- `XXH_STATIC_LINKING_ONLY`: gives access to internal state declaration, required for static allocation.
106+
Incompatible with dynamic linking, due to risks of ABI changes.
105107
- `XXH_NAMESPACE`: Prefixes all symbols with the value of `XXH_NAMESPACE`.
106108
This macro can only use compilable character set.
107109
Useful to evade symbol naming collisions,
@@ -117,40 +119,50 @@ The following macros can be set at compilation time to modify libxxhash's behavi
117119
Method `1` uses a gcc-specific `packed` attribute, which can provide better performance for some targets.
118120
Method `2` forces unaligned reads, which is not standard compliant, but might sometimes be the only way to extract better read performance.
119121
Method `3` uses a byteshift operation, which is best for old compilers which don't inline `memcpy()` or big-endian systems without a byteswap instruction.
120-
- `XXH_VECTOR` : manually select a vector instruction set (default: auto-selected at compilation time). Available instruction sets are `XXH_SCALAR`, `XXH_SSE2`, `XXH_AVX2`, `XXH_AVX512`, `XXH_NEON` and `XXH_VSX`. Compiler may require additional flags to ensure proper support (for example, `gcc` on linux will require `-mavx2` for `AVX2`, and `-mavx512f` for `AVX512`).
121-
- `XXH_NO_PREFETCH` : disable prefetching. Some platforms or situations may perform better without prefetching. XXH3 only.
122-
- `XXH_PREFETCH_DIST` : select prefetching distance. For close-to-metal adaptation to specific hardware platforms. XXH3 only.
122+
- `XXH_CPU_LITTLE_ENDIAN`: By default, endianness is determined by a runtime test resolved at compile time.
123+
If, for some reason, the compiler cannot simplify the runtime test, it can cost performance.
124+
It's possible to skip auto-detection and simply state that the architecture is little-endian by setting this macro to 1.
125+
Setting it to 0 states big-endian.
126+
- `XXH_NO_STDLIB`: Disable invocation of `<stdlib.h>` functions, notably `malloc()` and `free()`.
127+
`libxxhash`'s `XXH*_createState()` will always fail and return `NULL`.
128+
But one-shot hashing (like `XXH32()`) or streaming using statically allocated states
129+
still work as expected.
130+
This build flag is useful for embedded environments without dynamic allocation.
123131
- `XXH_NO_STREAM`: Disables the streaming API, limiting it to single shot variants only.
124-
- `XXH_SIZE_OPT`: `0`: default, optimize for speed
125-
`1`: default for `-Os` and `-Oz`: disables some speed hacks for size optimization
126-
`2`: makes code as small as possible, performance may cry
127132
- `XXH_NO_INLINE_HINTS`: By default, xxHash uses `__attribute__((always_inline))` and `__forceinline` to improve performance at the cost of code size.
128133
Defining this macro to 1 will mark all internal functions as `static`, allowing the compiler to decide whether to inline a function or not.
129134
This is very useful when optimizing for smallest binary size,
130135
and is automatically defined when compiling with `-O0`, `-Os`, `-Oz`, or `-fno-inline` on GCC and Clang.
131-
This may also increase performance depending on compiler and architecture.
136+
This might also increase performance depending on compiler and architecture.
137+
- `XXH_SIZE_OPT`: `0`: default, optimize for speed
138+
`1`: default for `-Os` and `-Oz`: disables some speed hacks for size optimization
139+
`2`: makes code as small as possible, performance may cry
140+
- `XXH_DEBUGLEVEL` : When set to any value >= 1, enables `assert()` statements.
141+
This (slightly) slows down execution, but may help finding bugs during debugging sessions.
142+
- `XXH_ENABLE_AUTOVECTORIZE`: Auto-vectorization may be triggered for XXH32 and XXH64, depending on cpu vector capabilities and compiler version.
143+
Note: auto-vectorization tends to be triggered more easily with recent versions of `clang`.
144+
For XXH32, SSE4.1 or equivalent (NEON) is enough, while XXH64 requires AVX512.
145+
Unfortunately, auto-vectorization is not always a good idea, and often degrades performance.
146+
For this reason, the xxhash source code tries to prevent auto-vectorization by default.
147+
That being said, systems evolve, and this conclusion is not forthcoming.
148+
For example, it has been reported that recent Zen4 cpus are more likely to improve performance with vectorization.
149+
Therefore, should you prefer or want to test vectorized code, just enable this flag:
150+
it will remove the no-vectorization protection code, thus making it more likely for XXH32 and XXH64 to be auto-vectorized.
132151
- `XXH32_ENDJMP`: Switch multi-branch finalization stage of XXH32 by a single jump.
133152
This is generally undesirable for performance, especially when hashing inputs of random sizes.
134153
But depending on exact architecture and compiler, a jump might provide slightly better performance on small inputs. Disabled by default.
135-
- `XXH_NO_STDLIB`: Disable invocation of `<stdlib.h>` functions, notably `malloc()` and `free()`.
136-
`libxxhash`'s `XXH*_createState()` will always fail and return `NULL`.
137-
But one-shot hashing (like `XXH32()`) or streaming using statically allocated states
138-
still work as expected.
139-
This build flag is useful for embedded environments without dynamic allocation.
140-
- `XXH_STATIC_LINKING_ONLY`: gives access to internal state declaration, required for static allocation.
141-
Incompatible with dynamic linking, due to risks of ABI changes.
154+
- `XXH_IMPORT`: MSVC specific: should only be defined for dynamic linking, as it prevents linkage errors.
155+
156+
#### Build modifiers specific for XXH3
157+
- `XXH_VECTOR` : manually select a vector instruction set (default: auto-selected at compilation time). Available instruction sets are `XXH_SCALAR`, `XXH_SSE2`, `XXH_AVX2`, `XXH_AVX512`, `XXH_NEON` and `XXH_VSX`. Compiler may require additional flags to ensure proper support (for example, `gcc` on x86_64 requires `-mavx2` for `AVX2`, or `-mavx512f` for `AVX512`).
158+
- `XXH_PREFETCH_DIST` : select prefetching distance. For close-to-metal adaptation to specific hardware platforms. XXH3 only.
159+
- `XXH_NO_PREFETCH` : disable prefetching. Some platforms or situations may perform better without prefetching. XXH3 only.
142160
- `XXH_NO_XXH3` : removes symbols related to `XXH3` (both 64 & 128 bits) from generated binary.
143161
Useful to reduce binary size, notably for applications which do not employ `XXH3`.
144162
- `XXH_NO_LONG_LONG`: removes compilation of algorithms relying on 64-bit types (`XXH3` and `XXH64`). Only `XXH32` will be compiled.
145163
Useful for targets (architectures and compilers) without 64-bit support.
146-
- `XXH_IMPORT`: MSVC specific: should only be defined for dynamic linking, as it prevents linkage errors.
147-
- `XXH_CPU_LITTLE_ENDIAN`: By default, endianness is determined by a runtime test resolved at compile time.
148-
If, for some reason, the compiler cannot simplify the runtime test, it can cost performance.
149-
It's possible to skip auto-detection and simply state that the architecture is little-endian by setting this macro to 1.
150-
Setting it to 0 states big-endian.
151-
- `XXH_DEBUGLEVEL` : When set to any value >= 1, enables `assert()` statements.
152-
This (slightly) slows down execution, but may help finding bugs during debugging sessions.
153164

165+
#### Makefile variables
154166
When compiling the Command Line Interface `xxhsum` using `make`, the following environment variables can also be set :
155167
- `DISPATCH=1` : use `xxh_x86dispatch.c`, to automatically select between `scalar`, `sse2`, `avx2` or `avx512` instruction set at runtime, depending on local host. This option is only valid for `x86`/`x64` systems.
156168
- `XXH_1ST_SPEED_TARGET` : select an initial speed target, expressed in MB/s, for the first speed test in benchmark mode. Benchmark will adjust the target at subsequent iterations, but the first test is made "blindly" by targeting this speed. Currently conservatively set to 10 MB/s, to support very slow (emulated) platforms.

xxhash.h

+18-1
Original file line numberDiff line numberDiff line change
@@ -2863,7 +2863,7 @@ static xxh_u32 XXH32_round(xxh_u32 acc, xxh_u32 input)
28632863
#if (defined(__SSE4_1__) || defined(__aarch64__) || defined(__wasm_simd128__)) && !defined(XXH_ENABLE_AUTOVECTORIZE)
28642864
/*
28652865
* UGLY HACK:
2866-
* A compiler fence is the only thing that prevents GCC and Clang from
2866+
* A compiler fence is used to prevent GCC and Clang from
28672867
* autovectorizing the XXH32 loop (pragmas and attributes don't work for some
28682868
* reason) without globally disabling SSE4.1.
28692869
*
@@ -3370,6 +3370,23 @@ static xxh_u64 XXH64_round(xxh_u64 acc, xxh_u64 input)
33703370
acc += input * XXH_PRIME64_2;
33713371
acc = XXH_rotl64(acc, 31);
33723372
acc *= XXH_PRIME64_1;
3373+
#if (defined(__AVX512F__)) && !defined(XXH_ENABLE_AUTOVECTORIZE)
3374+
/*
3375+
* DISABLE AUTOVECTORIZATION:
3376+
* A compiler fence is used to prevent GCC and Clang from
3377+
* autovectorizing the XXH64 loop (pragmas and attributes don't work for some
3378+
* reason) without globally disabling AVX512.
3379+
*
3380+
* Autovectorization of XXH64 tends to be detrimental,
3381+
* though the exact outcome may change depending on exact cpu and compiler version.
3382+
* For information, it has been reported as detrimental for Skylake-X,
3383+
* but possibly beneficial for Zen4.
3384+
*
3385+
* The default is to disable auto-vectorization,
3386+
* but you can select to enable it instead using `XXH_ENABLE_AUTOVECTORIZE` build variable.
3387+
*/
3388+
XXH_COMPILER_GUARD(acc);
3389+
#endif
33733390
return acc;
33743391
}
33753392

0 commit comments

Comments
 (0)