Skip to content

Commit

Permalink
xxhsum with auto-dispatch displays the locally detected vector extension
Browse files Browse the repository at this point in the history
using command -V
ex: compiled as 64-bit x86 autoVec (AVX2 detected) little endian with GCC 13.3.0
  • Loading branch information
Cyan4973 committed Dec 26, 2024
1 parent 2bf8313 commit 6c666ad
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 2 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ LIBXXH = libxxhash.$(SHARED_EXT_VER)
XXHSUM_SRC_DIR = cli
XXHSUM_SPLIT_SRCS = $(XXHSUM_SRC_DIR)/xxhsum.c \
$(XXHSUM_SRC_DIR)/xsum_os_specific.c \
$(XXHSUM_SRC_DIR)/xsum_arch.c \
$(XXHSUM_SRC_DIR)/xsum_output.c \
$(XXHSUM_SRC_DIR)/xsum_sanity_check.c \
$(XXHSUM_SRC_DIR)/xsum_bench.c
Expand Down
51 changes: 51 additions & 0 deletions cli/xsum_arch.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* xxhsum - Command line interface for xxhash algorithms
* Copyright (C) 2013-2021 Yann Collet
*
* GPL v2 License
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* You can contact the author at:
* - xxHash homepage: https://www.xxhash.com
* - xxHash source repository: https://github.com/Cyan4973/xxHash
*/

#if ((defined(__x86_64__) || defined(_M_AMD64) || defined(_M_X64)) && !defined(_M_ARM64EC)) || defined(__i386__) || defined(_M_IX86) || defined(_M_IX86_FP)
#if defined(XXHSUM_DISPATCH)

#include "../xxh_x86dispatch.h"
#define XXH_INLINE_ALL /* XXH_* vector types */
#include "../xxhash.h"

const char* XSUM_autox86(void)
{
int vecVersion = XXH_featureTest();
switch(vecVersion) {
case XXH_SCALAR:
return "x86 autoVec (scalar: no vector extension detected)";
case XXH_SSE2:
return "x86 autoVec (SSE2 detected)";
case XXH_AVX2:
return "x86 autoVec (AVX2 detected)";
case XXH_AVX512:
return "x86 autoVec (AVX512 detected)";
default:;
}
return " autoVec (error detecting vector extension)";
}

#endif /* XXHSUM_DISPATCH */
#endif /* x86 */
3 changes: 2 additions & 1 deletion cli/xsum_arch.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@
/* Try to detect the architecture. */
#if defined(XSUM_ARCH_X86)
# if defined(XXHSUM_DISPATCH)
# define XSUM_ARCH XSUM_ARCH_X86 " autoVec"
const char* XSUM_autox86(void);
# define XSUM_ARCH XSUM_autox86()
# elif defined(__AVX512F__)
# define XSUM_ARCH XSUM_ARCH_X86 " + AVX512"
# elif defined(__AVX2__)
Expand Down
2 changes: 1 addition & 1 deletion xxh_x86dispatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ static xxh_u64 XXH_xgetbv(void)
* @return The best @ref XXH_VECTOR implementation.
* @see XXH_VECTOR_TYPES
*/
static int XXH_featureTest(void)
int XXH_featureTest(void)
{
xxh_u32 abcd[4];
xxh_u32 max_leaves;
Expand Down
8 changes: 8 additions & 0 deletions xxh_x86dispatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@
extern "C" {
#endif

/*!
* @brief Returns the best XXH3 implementation for x86
*
* @return The best @ref XXH_VECTOR implementation.
* @see XXH_VECTOR_TYPES
*/
XXH_PUBLIC_API int XXH_featureTest(void);

XXH_PUBLIC_API XXH64_hash_t XXH3_64bits_dispatch(XXH_NOESCAPE const void* input, size_t len);
XXH_PUBLIC_API XXH64_hash_t XXH3_64bits_withSeed_dispatch(XXH_NOESCAPE const void* input, size_t len, XXH64_hash_t seed);
XXH_PUBLIC_API XXH64_hash_t XXH3_64bits_withSecret_dispatch(XXH_NOESCAPE const void* input, size_t len, XXH_NOESCAPE const void* secret, size_t secretLen);
Expand Down

0 comments on commit 6c666ad

Please sign in to comment.