diff --git a/Makefile b/Makefile index eac72fb4..d87da54c 100644 --- a/Makefile +++ b/Makefile @@ -31,11 +31,13 @@ LIBVER_PATCH:=`sed -n '/define XXH_VERSION_RELEASE/s/.*[[:blank:]]\([0-9][0-9]*\ LIBVER := $(LIBVER_MAJOR).$(LIBVER_MINOR).$(LIBVER_PATCH) CFLAGS ?= -O3 -CFLAGS += -std=c99 -Wall -Wextra -Wcast-qual -Wcast-align -Wshadow -Wstrict-aliasing=1 -Wswitch-enum -Wdeclaration-after-statement -Wstrict-prototypes -Wundef -pedantic +CFLAGS += -std=c99 -Wall -Wextra -Wcast-qual -Wcast-align -Wshadow \ + -Wstrict-aliasing=1 -Wswitch-enum -Wdeclaration-after-statement \ + -Wstrict-prototypes -Wundef -pedantic FLAGS := $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(MOREFLAGS) XXHSUM_VERSION=$(LIBVER) -MD2ROFF =ronn -MD2ROFF_FLAGS = --roff --warnings --manual="User Commands" --organization="xxhsum $(XXHSUM_VERSION)" +MD2ROFF = ronn +MD2ROFF_FLAGS = --roff --warnings --manual="User Commands" --organization="xxhsum $(XXHSUM_VERSION)" # Define *.exe as extension for Windows systems ifneq (,$(filter Windows%,$(OS))) @@ -101,7 +103,7 @@ test-xxhsum-c: xxhsum echo "00000000 test-expects-file-not-found" | ./xxhsum -c -; test $$? -eq 1 clean-xxhsum-c: - @rm -f .test.xxh32 .test.xxh64 + @$(RM) -f .test.xxh32 .test.xxh64 armtest: clean @echo ---- test ARM compilation ---- @@ -123,19 +125,25 @@ staticAnalyze: clean @echo ---- static analyzer - scan-build ---- CFLAGS="-g -Werror" scan-build --status-bugs -v $(MAKE) all +namespaceTest: + $(CC) -c xxhash.c + $(CC) -DXXH_NAMESPACE=TEST_ -c xxhash.c -o xxhash2.o + $(CC) xxhash.o xxhash2.o xxhsum.c -o xxhsum2 # will fail if one namespace missing (symbol collision) + $(RM) *.o xxhsum2 # clean + xxhsum.1: xxhsum.1.md cat $^ | $(MD2ROFF) $(MD2ROFF_FLAGS) | sed -n '/^\.\\\".*/!p' > $@ man: xxhsum.1 clean-man: - rm xxhsum.1 + $(RM) xxhsum.1 preview-man: clean-man man man ./xxhsum.1 -test-all: clean all test test32 test-xxhsum-c clean-xxhsum-c armtest clangtest gpptest sanitize staticAnalyze +test-all: clean all namespaceTest test test32 test-xxhsum-c clean-xxhsum-c armtest clangtest gpptest sanitize staticAnalyze clean: clean-xxhsum-c - @rm -f core *.o xxhsum$(EXT) xxhsum32$(EXT) xxhsum_inlinedXXH$(EXT) xxh32sum xxh64sum + @$(RM) -f core *.o xxhsum$(EXT) xxhsum32$(EXT) xxhsum_inlinedXXH$(EXT) xxh32sum xxh64sum @echo cleaning completed diff --git a/cmake_unofficial/CMakeLists.txt b/cmake_unofficial/CMakeLists.txt index 825193e1..f3cb72c6 100644 --- a/cmake_unofficial/CMakeLists.txt +++ b/cmake_unofficial/CMakeLists.txt @@ -6,11 +6,14 @@ project(xxhash) set(XXHASH_LIB_VERSION "0.42.0") set(XXHASH_LIB_SOVERSION "0") -add_library(xxhash SHARED ../xxhash.c) -set_target_properties(xxhash PROPERTIES COMPILE_DEFINITIONS "XXHASH_EXPORT" - VERSION "${XXHASH_LIB_VERSION}" - SOVERSION "${XXHASH_LIB_SOVERSION}") -set(install_libs xxhash) +set(BUILD_SHARED_LIBS ON CACHE BOOL "Set to ON to build shared libraries") +if(BUILD_SHARED_LIBS) + add_library(xxhash SHARED ../xxhash.c) + set_target_properties(xxhash PROPERTIES COMPILE_DEFINITIONS "XXHASH_EXPORT" + VERSION "${XXHASH_LIB_VERSION}" + SOVERSION "${XXHASH_LIB_SOVERSION}") + LIST(APPEND install_libs xxhash) +endif(BUILD_SHARED_LIBS) set(BUILD_STATIC_LIBS ON CACHE BOOL "Set to ON to build static libraries") if(BUILD_STATIC_LIBS) @@ -21,4 +24,9 @@ endif(BUILD_STATIC_LIBS) INSTALL(FILES ../xxhash.h DESTINATION include) -INSTALL(TARGETS ${install_libs} DESTINATION lib) +INSTALL( + TARGETS ${install_libs} + RUNTIME DESTINATION bin + ARCHIVE DESTINATION lib + LIBRARY DESTINATION lib +) diff --git a/xxhash.c b/xxhash.c index 9238b159..17a54f17 100644 --- a/xxhash.c +++ b/xxhash.c @@ -132,7 +132,7 @@ static void* XXH_memcpy(void* dest, const void* src, size_t size) { return memcp ***************************************/ #ifndef MEM_MODULE # define MEM_MODULE -# if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */ +# if !defined (__VMS) && (defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) ) # include typedef uint8_t BYTE; typedef uint16_t U16; @@ -144,7 +144,7 @@ static void* XXH_memcpy(void* dest, const void* src, size_t size) { return memcp typedef unsigned short U16; typedef unsigned int U32; typedef signed int S32; - typedef unsigned long long U64; + typedef unsigned long long U64; /* if your compiler doesn't support unsigned long long, replace by another 64-bit type here. Note that xxhash.h will also need to be updated. */ # endif #endif @@ -559,8 +559,7 @@ XXH_PUBLIC_API XXH_errorcode XXH64_freeState(XXH64_state_t* statePtr) XXH_PUBLIC_API XXH_errorcode XXH32_reset(XXH32_state_t* statePtr, unsigned int seed) { XXH32_state_t state; /* using a local state to memcpy() in order to avoid strict-aliasing warnings */ - memset(&state, 0, sizeof(state)); - state.seed = seed; + memset(&state, 0, sizeof(state)-4); /* do not write into reserved, for future removal */ state.v1 = seed + PRIME32_1 + PRIME32_2; state.v2 = seed + PRIME32_2; state.v3 = seed + 0; @@ -573,8 +572,7 @@ XXH_PUBLIC_API XXH_errorcode XXH32_reset(XXH32_state_t* statePtr, unsigned int s XXH_PUBLIC_API XXH_errorcode XXH64_reset(XXH64_state_t* statePtr, unsigned long long seed) { XXH64_state_t state; /* using a local state to memcpy() in order to avoid strict-aliasing warnings */ - memset(&state, 0, sizeof(state)); - state.seed = seed; + memset(&state, 0, sizeof(state)-8); /* do not write into reserved, for future removal */ state.v1 = seed + PRIME64_1 + PRIME64_2; state.v2 = seed + PRIME64_2; state.v3 = seed + 0; @@ -593,7 +591,8 @@ FORCE_INLINE XXH_errorcode XXH32_update_endian (XXH32_state_t* state, const void if (input==NULL) return XXH_ERROR; #endif - state->total_len += len; + state->total_len_32 += len; + state->large_len |= (len>=16) | (state->total_len_32>=16); if (state->memsize + len < 16) { /* fill in tmp buffer */ XXH_memcpy((BYTE*)(state->mem32) + state->memsize, input, len); @@ -659,13 +658,13 @@ FORCE_INLINE U32 XXH32_digest_endian (const XXH32_state_t* state, XXH_endianess const BYTE* const bEnd = (const BYTE*)(state->mem32) + state->memsize; U32 h32; - if (state->total_len >= 16) { + if (state->large_len) { h32 = XXH_rotl32(state->v1, 1) + XXH_rotl32(state->v2, 7) + XXH_rotl32(state->v3, 12) + XXH_rotl32(state->v4, 18); } else { - h32 = state->seed + PRIME32_5; + h32 = state->v3 /* == seed */ + PRIME32_5; } - h32 += (U32) state->total_len; + h32 += state->total_len_32; while (p+4<=bEnd) { h32 += XXH_readLE32(p, endian) * PRIME32_3; @@ -788,7 +787,7 @@ FORCE_INLINE U64 XXH64_digest_endian (const XXH64_state_t* state, XXH_endianess h64 = XXH64_mergeRound(h64, v3); h64 = XXH64_mergeRound(h64, v4); } else { - h64 = state->seed + PRIME64_5; + h64 = state->v3 + PRIME64_5; } h64 += (U64) state->total_len; diff --git a/xxhash.h b/xxhash.h index 4d7feffc..75e2ed20 100644 --- a/xxhash.h +++ b/xxhash.h @@ -88,15 +88,15 @@ typedef enum { XXH_OK=0, XXH_ERROR } XXH_errorcode; * Methodology : * #define XXH_PRIVATE_API * #include "xxhash.h" -* `xxhash.c` is automatically included, so the file is still needed, -* but it's not useful to compile and link it anymore. +* `xxhash.c` is automatically included. +* It's not useful to compile and link it as a separate module anymore. */ #ifdef XXH_PRIVATE_API # ifndef XXH_STATIC_LINKING_ONLY # define XXH_STATIC_LINKING_ONLY # endif # if defined(__GNUC__) -# define XXH_PUBLIC_API static __attribute__((unused)) +# define XXH_PUBLIC_API static __inline __attribute__((unused)) # elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) # define XXH_PUBLIC_API static inline # elif defined(_MSC_VER) @@ -137,6 +137,10 @@ regular symbol name will be automatically translated by this header. # define XXH64_digest XXH_NAME2(XXH_NAMESPACE, XXH64_digest) # define XXH32_copyState XXH_NAME2(XXH_NAMESPACE, XXH32_copyState) # define XXH64_copyState XXH_NAME2(XXH_NAMESPACE, XXH64_copyState) +# define XXH32_canonicalFromHash XXH_NAME2(XXH_NAMESPACE, XXH32_canonicalFromHash) +# define XXH64_canonicalFromHash XXH_NAME2(XXH_NAMESPACE, XXH64_canonicalFromHash) +# define XXH32_hashFromCanonical XXH_NAME2(XXH_NAMESPACE, XXH32_hashFromCanonical) +# define XXH64_hashFromCanonical XXH_NAME2(XXH_NAMESPACE, XXH64_hashFromCanonical) #endif @@ -145,7 +149,7 @@ regular symbol name will be automatically translated by this header. ***************************************/ #define XXH_VERSION_MAJOR 0 #define XXH_VERSION_MINOR 6 -#define XXH_VERSION_RELEASE 1 +#define XXH_VERSION_RELEASE 2 #define XXH_VERSION_NUMBER (XXH_VERSION_MAJOR *100*100 + XXH_VERSION_MINOR *100 + XXH_VERSION_RELEASE) XXH_PUBLIC_API unsigned XXH_versionNumber (void); @@ -254,32 +258,36 @@ XXH_PUBLIC_API XXH64_hash_t XXH64_hashFromCanonical(const XXH64_canonical_t* src /* ================================================================================================ This section contains definitions which are not guaranteed to remain stable. - They could change in a future version, becoming incompatible with a different version of the library. + They may change in future versions, becoming incompatible with a different version of the library. They shall only be used with static linking. + Never use these definitions in association with dynamic linking ! =================================================================================================== */ -/* These definitions allow allocating XXH state statically (on stack) */ +/* These definitions are only meant to allow allocation of XXH state + statically, on stack, or in a struct for example. + Do not use members directly. */ struct XXH32_state_s { - unsigned long long total_len; - unsigned seed; + unsigned total_len_32; + unsigned large_len; unsigned v1; unsigned v2; unsigned v3; unsigned v4; unsigned mem32[4]; /* buffer defined as U32 for alignment */ unsigned memsize; + unsigned reserved; /* never read nor write, will be removed in a future version */ }; /* typedef'd to XXH32_state_t */ struct XXH64_state_s { unsigned long long total_len; - unsigned long long seed; unsigned long long v1; unsigned long long v2; unsigned long long v3; unsigned long long v4; unsigned long long mem64[4]; /* buffer defined as U64 for alignment */ unsigned memsize; + unsigned reserved[2]; /* never read nor write, will be removed in a future version */ }; /* typedef'd to XXH64_state_t */ diff --git a/xxhsum.c b/xxhsum.c index 0d99da70..bbe94007 100644 --- a/xxhsum.c +++ b/xxhsum.c @@ -55,11 +55,7 @@ #include /* stat64 */ #include /* clock_t, clock, CLOCKS_PER_SEC */ -#ifdef XXHSUM_INCLUDE_XXHC /* compile xxhsum with xxhash as private (no public symbol) */ -# define XXH_INCLUDE_BODY -#else -# define XXH_STATIC_LINKING_ONLY /* *_state_t */ -#endif +#define XXH_STATIC_LINKING_ONLY /* *_state_t */ #include "xxhash.h" @@ -124,9 +120,8 @@ static const int g_nbBits = (int)(sizeof(void*)*8); static const char g_lename[] = "little endian"; static const char g_bename[] = "big endian"; #define ENDIAN_NAME (BMK_isLittleEndian() ? g_lename : g_bename) -#define COMPILED __DATE__ static const char author[] = "Yann Collet"; -#define WELCOME_MESSAGE(exename) "%s %s (%i-bits %s), by %s (%s) \n", exename, PROGRAM_VERSION, g_nbBits, ENDIAN_NAME, author, COMPILED +#define WELCOME_MESSAGE(exename) "%s %s (%i-bits %s), by %s \n", exename, PROGRAM_VERSION, g_nbBits, ENDIAN_NAME, author #define NBLOOPS 3 /* Default number of benchmark iterations */ #define TIMELOOP_S 1 @@ -1131,7 +1126,7 @@ static int badusage(const char* exename) int main(int argc, const char** argv) { int i, filenamesStart=0; - const char* exename = argv[0]; + const char* const exename = argv[0]; U32 benchmarkMode = 0; U32 fileCheckMode = 0; U32 strictMode = 0;