| 1 |
#ifndef AWS_CHECKSUMS_PRIVATE_CRC_UTIL_H |
| 2 |
#define AWS_CHECKSUMS_PRIVATE_CRC_UTIL_H |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include <aws/common/byte_order.h> |
| 9 |
#include <aws/common/cpuid.h> |
| 10 |
#include <aws/common/stdint.h> |
| 11 |
#include <limits.h> |
| 12 |
#include <stdlib.h> |
| 13 |
|
| 14 |
#define large_buffer_apply_impl(Name, T) \ |
| 15 |
static T aws_large_buffer_apply_##Name( \ |
| 16 |
T (*checksum_fn)(const uint8_t *, int, T), const uint8_t *buffer, size_t length, T previous) { \ |
| 17 |
T val = previous; \ |
| 18 |
while (length > INT_MAX) { \ |
| 19 |
val = checksum_fn(buffer, INT_MAX, val); \ |
| 20 |
buffer += (size_t)INT_MAX; \ |
| 21 |
length -= (size_t)INT_MAX; \ |
| 22 |
} \ |
| 23 |
val = checksum_fn(buffer, (int)length, val); \ |
| 24 |
return val; \ |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
static inline uint32_t aws_bswap32_if_be(uint32_t x) { |
| 29 |
if (!aws_is_big_endian()) { |
| 30 |
return x; |
| 31 |
} |
| 32 |
|
| 33 |
#if _MSC_VER |
| 34 |
return _byteswap_ulong(x); |
| 35 |
#elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)) |
| 36 |
return __builtin_bswap32(x); |
| 37 |
#elif defined(__clang__) && __clang_major__ >= 3 |
| 38 |
return __builtin_bswap32(x); |
| 39 |
#else |
| 40 |
return ( |
| 41 |
((x & 0xff000000u) >> 24) | ((x & 0x00ff0000u) >> 8) | ((x & 0x0000ff00u) << 8) | ((x & 0x000000ffu) << 24)); |
| 42 |
#endif |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
static inline uint64_t aws_bswap64_if_be(uint64_t x) { |
| 47 |
if (!aws_is_big_endian()) { |
| 48 |
return x; |
| 49 |
} |
| 50 |
|
| 51 |
#if _MSC_VER |
| 52 |
return _byteswap_uint64(x); |
| 53 |
#elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)) |
| 54 |
|
| 55 |
return __builtin_bswap64(x); |
| 56 |
#elif defined(__clang__) && __clang_major__ >= 3 |
| 57 |
return __builtin_bswap64(x); |
| 58 |
#else |
| 59 |
return ((x << 56) & 0xff00000000000000ULL) | ((x << 40) & 0x00ff000000000000ULL) | |
| 60 |
((x << 24) & 0x0000ff0000000000ULL) | ((x << 8) & 0x000000ff00000000ULL) | |
| 61 |
((x >> 8) & 0x00000000ff000000ULL) | ((x >> 24) & 0x0000000000ff0000ULL) | |
| 62 |
((x >> 40) & 0x000000000000ff00ULL) | ((x >> 56) & 0x00000000000000ffULL); |
| 63 |
#endif |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
void aws_checksums_crc32_init(void); |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
void aws_checksums_crc64_init(void); |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
extern bool s_detection_performed; |
| 87 |
extern bool s_detected_sse42; |
| 88 |
extern bool s_detected_avx512; |
| 89 |
extern bool s_detected_clmul; |
| 90 |
extern bool s_detected_vpclmulqdq; |
| 91 |
|
| 92 |
static inline void aws_checksums_init_detection_cache(void) { |
| 93 |
s_detected_clmul = aws_cpu_has_feature(AWS_CPU_FEATURE_CLMUL); |
| 94 |
s_detected_sse42 = aws_cpu_has_feature(AWS_CPU_FEATURE_SSE_4_2); |
| 95 |
s_detected_avx512 = aws_cpu_has_feature(AWS_CPU_FEATURE_AVX512); |
| 96 |
s_detected_vpclmulqdq = aws_cpu_has_feature(AWS_CPU_FEATURE_VPCLMULQDQ); |
| 97 |
s_detection_performed = true; |
| 98 |
} |
| 99 |
|
| 100 |
static inline bool aws_cpu_has_clmul_cached(void) { |
| 101 |
if (AWS_UNLIKELY(!s_detection_performed)) { |
| 102 |
aws_checksums_init_detection_cache(); |
| 103 |
} |
| 104 |
return s_detected_clmul; |
| 105 |
} |
| 106 |
|
| 107 |
static inline bool aws_cpu_has_sse42_cached(void) { |
| 108 |
if (AWS_UNLIKELY(!s_detection_performed)) { |
| 109 |
aws_checksums_init_detection_cache(); |
| 110 |
} |
| 111 |
return s_detected_sse42; |
| 112 |
} |
| 113 |
|
| 114 |
static inline bool aws_cpu_has_avx512_cached(void) { |
| 115 |
if (AWS_UNLIKELY(!s_detection_performed)) { |
| 116 |
aws_checksums_init_detection_cache(); |
| 117 |
} |
| 118 |
return s_detected_avx512; |
| 119 |
} |
| 120 |
|
| 121 |
static inline bool aws_cpu_has_vpclmulqdq_cached(void) { |
| 122 |
if (AWS_UNLIKELY(!s_detection_performed)) { |
| 123 |
aws_checksums_init_detection_cache(); |
| 124 |
} |
| 125 |
return s_detected_vpclmulqdq; |
| 126 |
} |
| 127 |
|
| 128 |
#if defined(__SIZEOF_INT128__) |
| 129 |
__uint128_t aws_checksums_multiply_mod_p_reflected(const __uint128_t poly, __uint128_t a, __uint128_t b); |
| 130 |
#endif |
| 131 |
|
| 132 |
#endif |