| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
#ifndef LZMA_CRC32_ARM64_H |
| 15 |
#define LZMA_CRC32_ARM64_H |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#ifndef _MSC_VER |
| 20 |
# include <arm_acle.h> |
| 21 |
#endif |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
#if defined(CRC32_GENERIC) && defined(CRC32_ARCH_OPTIMIZED) |
| 26 |
# if defined(HAVE_GETAUXVAL) || defined(HAVE_ELF_AUX_INFO) |
| 27 |
# include <sys/auxv.h> |
| 28 |
# elif defined(_WIN32) |
| 29 |
# include <processthreadsapi.h> |
| 30 |
# elif defined(__APPLE__) && defined(HAVE_SYSCTLBYNAME) |
| 31 |
# include <sys/sysctl.h> |
| 32 |
# endif |
| 33 |
#endif |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
#if (defined(__GNUC__) || defined(__clang__)) && !defined(__EDG__) |
| 40 |
# define crc_attr_target __attribute__((__target__("+crc"))) |
| 41 |
#else |
| 42 |
# define crc_attr_target |
| 43 |
#endif |
| 44 |
|
| 45 |
|
| 46 |
crc_attr_target |
| 47 |
static uint32_t |
| 48 |
crc32_arch_optimized(const uint8_t *buf, size_t size, uint32_t crc) |
| 49 |
{ |
| 50 |
crc = ~crc; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
const size_t align_amount = my_min(size, (0U - (uintptr_t)buf) & 7); |
| 55 |
|
| 56 |
for (const uint8_t *limit = buf + align_amount; buf < limit; ++buf) |
| 57 |
crc = __crc32b(crc, *buf); |
| 58 |
|
| 59 |
size -= align_amount; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
for (const uint8_t *limit = buf + (size & ~(size_t)7); |
| 66 |
buf < limit; buf += 8) |
| 67 |
crc = __crc32d(crc, aligned_read64le(buf)); |
| 68 |
|
| 69 |
|
| 70 |
for (const uint8_t *limit = buf + (size & 7); buf < limit; ++buf) |
| 71 |
crc = __crc32b(crc, *buf); |
| 72 |
|
| 73 |
return ~crc; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
#if defined(CRC32_GENERIC) && defined(CRC32_ARCH_OPTIMIZED) |
| 78 |
static inline bool |
| 79 |
is_arch_extension_supported(void) |
| 80 |
{ |
| 81 |
#if defined(HAVE_GETAUXVAL) |
| 82 |
return (getauxval(AT_HWCAP) & HWCAP_CRC32) != 0; |
| 83 |
|
| 84 |
#elif defined(HAVE_ELF_AUX_INFO) |
| 85 |
unsigned long feature_flags; |
| 86 |
|
| 87 |
if (elf_aux_info(AT_HWCAP, &feature_flags, sizeof(feature_flags)) != 0) |
| 88 |
return false; |
| 89 |
|
| 90 |
return (feature_flags & HWCAP_CRC32) != 0; |
| 91 |
|
| 92 |
#elif defined(_WIN32) |
| 93 |
return IsProcessorFeaturePresent( |
| 94 |
PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE); |
| 95 |
|
| 96 |
#elif defined(__APPLE__) && defined(HAVE_SYSCTLBYNAME) |
| 97 |
int has_crc32 = 0; |
| 98 |
size_t size = sizeof(has_crc32); |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
if (sysctlbyname("hw.optional.armv8_crc32", &has_crc32, |
| 105 |
&size, NULL, 0) != 0) |
| 106 |
return false; |
| 107 |
|
| 108 |
return has_crc32; |
| 109 |
|
| 110 |
#else |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
# error Runtime detection method unavailable. |
| 118 |
#endif |
| 119 |
} |
| 120 |
#endif |
| 121 |
|
| 122 |
#endif |