24 #ifndef INC_CHECKSUMEX_HPP_4824BBD5_CC92_4894_B227_01821AE7FAFD
25 #define INC_CHECKSUMEX_HPP_4824BBD5_CC92_4894_B227_01821AE7FAFD
28 #if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__)
29 #if __has_include(<byteswap.h>)
31 #elif defined(__i386__) && __has_include(<intrin_x86.h>)
32 #include <intrin_x86.h>
33 #elif defined(_PPC_) && __has_include(<intrin_ppc.h>)
34 #include <intrin_ppc.h>
35 #elif defined(_MIPS_) && __has_include(<intrin_mips.h>)
36 #include <intrin_mips.h>
37 #elif defined(__x86_64__) && __has_include(<intrin_x86_64.h>)
38 #include <intrin_x86_64.h>
40 #define LIBXCKS_CHECKSUMEX_NOINTRIN
42 #elif defined(_MSC_VER)
45 #define LIBXCKS_CHECKSUMEX_NOINTRIN
55 #define LIBXCKS_BIG_ENDIAN 4321
56 #define LIBXCKS_LITTLE_ENDIAN 1234
58 #ifdef WORDS_BIGENDIAN
59 #define LIBXCKS_BYTE_ORDER LIBXCKS_BIG_ENDIAN
61 #define LIBXCKS_BYTE_ORDER LIBXCKS_LITTLE_ENDIAN
86 std::string
toString(
const bool hexInUpperCase =
false)
const override;
95 inline static uint32_t
swapOnLE(
const uint32_t value)
97 #if (LIBXCKS_BYTE_ORDER == LIBXCKS_BIG_ENDIAN)
100 #if !defined(LIBXCKS_CHECKSUMEX_NOINTRIN) && (defined(__clang__) || defined(__GNUC__) || defined(__GNUG__))
101 return __builtin_bswap32(value);
102 #elif !defined(LIBXCKS_CHECKSUMEX_NOINTRIN) && defined(_MSC_VER)
103 return _byteswap_ulong(value);
105 return ((uint32_t) ( \
106 (((uint32_t) (value) & (uint32_t) 0x000000ffU) << 24) | \
107 (((uint32_t) (value) & (uint32_t) 0x0000ff00U) << 8) | \
108 (((uint32_t) (value) & (uint32_t) 0x00ff0000U) >> 8) | \
109 (((uint32_t) (value) & (uint32_t) 0xff000000U) >> 24)));
120 inline static uint32_t
swapOnBE(
const uint32_t value)
122 #if (LIBXCKS_BYTE_ORDER == LIBXCKS_LITTLE_ENDIAN)
125 #if !defined(LIBXCKS_CHECKSUMEX_NOINTRIN) && (defined(__clang__) || defined(__GNUC__) || defined(__GNUG__))
126 return __builtin_bswap32(value);
127 #elif !defined(LIBXCKS_CHECKSUMEX_NOINTRIN) && defined(_MSC_VER)
128 return _byteswap_ulong(value);
130 return ((uint32_t) ( \
131 (((uint32_t) (value) & (uint32_t) 0x000000ffU) << 24) | \
132 (((uint32_t) (value) & (uint32_t) 0x0000ff00U) << 8) | \
133 (((uint32_t) (value) & (uint32_t) 0x00ff0000U) >> 8) | \
134 (((uint32_t) (value) & (uint32_t) 0xff000000U) >> 24)));
145 inline static uint64_t
swapOnLE(
const uint64_t value)
147 #if (LIBXCKS_BYTE_ORDER == LIBXCKS_BIG_ENDIAN)
150 #if !defined(LIBXCKS_CHECKSUMEX_NOINTRIN) && (defined(__clang__) || defined(__GNUC__) || defined(__GNUG__))
151 return __builtin_bswap64(value);
152 #elif !defined(LIBXCKS_CHECKSUMEX_NOINTRIN) && defined(_MSC_VER)
153 return _byteswap_uint64(value);
155 return ((uint64_t) ( \
156 (((uint64_t) (value) & (uint64_t) UINT64_C(0x00000000000000ff)) << 56) | \
157 (((uint64_t) (value) & (uint64_t) UINT64_C(0x000000000000ff00)) << 40) | \
158 (((uint64_t) (value) & (uint64_t) UINT64_C(0x0000000000ff0000)) << 24) | \
159 (((uint64_t) (value) & (uint64_t) UINT64_C(0x00000000ff000000)) << 8) | \
160 (((uint64_t) (value) & (uint64_t) UINT64_C(0x000000ff00000000)) >> 8) | \
161 (((uint64_t) (value) & (uint64_t) UINT64_C(0x0000ff0000000000)) >> 24) | \
162 (((uint64_t) (value) & (uint64_t) UINT64_C(0x00ff000000000000)) >> 40) | \
163 (((uint64_t) (value) & (uint64_t) UINT64_C(0xff00000000000000)) >> 56)));
174 inline static uint64_t
swapOnBE(
const uint64_t value)
176 #if (LIBXCKS_BYTE_ORDER == LIBXCKS_LITTLE_ENDIAN)
179 #if !defined(LIBXCKS_CHECKSUMEX_NOINTRIN) && (defined(__clang__) || defined(__GNUC__) || defined(__GNUG__))
180 return __builtin_bswap64(value);
181 #elif !defined(LIBXCKS_CHECKSUMEX_NOINTRIN) && defined(_MSC_VER)
182 return _byteswap_uint64(value);
184 return ((uint64_t) ( \
185 (((uint64_t) (value) & (uint64_t) UINT64_C(0x00000000000000ff)) << 56) | \
186 (((uint64_t) (value) & (uint64_t) UINT64_C(0x000000000000ff00)) << 40) | \
187 (((uint64_t) (value) & (uint64_t) UINT64_C(0x0000000000ff0000)) << 24) | \
188 (((uint64_t) (value) & (uint64_t) UINT64_C(0x00000000ff000000)) << 8) | \
189 (((uint64_t) (value) & (uint64_t) UINT64_C(0x000000ff00000000)) >> 8) | \
190 (((uint64_t) (value) & (uint64_t) UINT64_C(0x0000ff0000000000)) >> 24) | \
191 (((uint64_t) (value) & (uint64_t) UINT64_C(0x00ff000000000000)) >> 40) | \
192 (((uint64_t) (value) & (uint64_t) UINT64_C(0xff00000000000000)) >> 56)));
Interface for classes that compute checksums.
Computes a hash from a byte stream.
virtual void reset()=0
Resets the checksum to initial value.
static uint64_t swapOnBE(const uint64_t value)
Swaps bytes on big endian architectures.
std::string toString(const bool hexInUpperCase=false) const override
Returns the hash value has a string.
static uint64_t swapOnLE(const uint64_t value)
Swaps bytes on little endian architectures.
static uint32_t swapOnLE(const uint32_t value)
Swaps bytes on little endian architectures.
static uint32_t swapOnBE(const uint32_t value)
Swaps bytes on big endian architectures.
Computes a checksum from a byte stream.