| 1 |
/* |
| 2 |
* arm/cpu_features.h - feature detection for ARM CPUs |
| 3 |
* |
| 4 |
* Copyright 2018 Eric Biggers |
| 5 |
* |
| 6 |
* Permission is hereby granted, free of charge, to any person |
| 7 |
* obtaining a copy of this software and associated documentation |
| 8 |
* files (the "Software"), to deal in the Software without |
| 9 |
* restriction, including without limitation the rights to use, |
| 10 |
* copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 |
* copies of the Software, and to permit persons to whom the |
| 12 |
* Software is furnished to do so, subject to the following |
| 13 |
* conditions: |
| 14 |
* |
| 15 |
* The above copyright notice and this permission notice shall be |
| 16 |
* included in all copies or substantial portions of the Software. |
| 17 |
* |
| 18 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 19 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 20 |
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 21 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 22 |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 23 |
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 24 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 25 |
* OTHER DEALINGS IN THE SOFTWARE. |
| 26 |
*/ |
| 27 |
|
| 28 |
#ifndef LIB_ARM_CPU_FEATURES_H |
| 29 |
#define LIB_ARM_CPU_FEATURES_H |
| 30 |
|
| 31 |
#include "../lib_common.h" |
| 32 |
|
| 33 |
#if defined(ARCH_ARM32) || defined(ARCH_ARM64) |
| 34 |
|
| 35 |
#define ARM_CPU_FEATURE_NEON (1 << 0) |
| 36 |
#define ARM_CPU_FEATURE_PMULL (1 << 1) |
| 37 |
/* |
| 38 |
* PREFER_PMULL indicates that the CPU has very high pmull throughput, and so |
| 39 |
* the 12x wide pmull-based CRC-32 implementation is likely to be faster than an |
| 40 |
* implementation based on the crc32 instructions. |
| 41 |
*/ |
| 42 |
#define ARM_CPU_FEATURE_PREFER_PMULL (1 << 2) |
| 43 |
#define ARM_CPU_FEATURE_CRC32 (1 << 3) |
| 44 |
#define ARM_CPU_FEATURE_SHA3 (1 << 4) |
| 45 |
#define ARM_CPU_FEATURE_DOTPROD (1 << 5) |
| 46 |
|
| 47 |
#if !defined(FREESTANDING) && \ |
| 48 |
(defined(__GNUC__) || defined(__clang__) || defined(_MSC_VER)) && \ |
| 49 |
(defined(__linux__) || \ |
| 50 |
(defined(__APPLE__) && defined(ARCH_ARM64)) || \ |
| 51 |
(defined(_WIN32) && defined(ARCH_ARM64))) |
| 52 |
/* Runtime ARM CPU feature detection is supported. */ |
| 53 |
# define ARM_CPU_FEATURES_KNOWN (1U << 31) |
| 54 |
extern volatile u32 libdeflate_arm_cpu_features; |
| 55 |
|
| 56 |
void libdeflate_init_arm_cpu_features(void); |
| 57 |
|
| 58 |
static inline u32 get_arm_cpu_features(void) |
| 59 |
{ |
| 60 |
if (libdeflate_arm_cpu_features == 0) |
| 61 |
libdeflate_init_arm_cpu_features(); |
| 62 |
return libdeflate_arm_cpu_features; |
| 63 |
} |
| 64 |
#else |
| 65 |
static inline u32 get_arm_cpu_features(void) { return 0; } |
| 66 |
#endif |
| 67 |
|
| 68 |
/* NEON */ |
| 69 |
#if defined(__ARM_NEON) || (defined(_MSC_VER) && defined(ARCH_ARM64)) |
| 70 |
# define HAVE_NEON(features) 1 |
| 71 |
# define HAVE_NEON_NATIVE 1 |
| 72 |
#else |
| 73 |
# define HAVE_NEON(features) ((features) & ARM_CPU_FEATURE_NEON) |
| 74 |
# define HAVE_NEON_NATIVE 0 |
| 75 |
#endif |
| 76 |
/* |
| 77 |
* With both gcc and clang, NEON intrinsics require that the main target has |
| 78 |
* NEON enabled already. Exception: with gcc 6.1 and later (r230411 for arm32, |
| 79 |
* r226563 for arm64), hardware floating point support is sufficient. |
| 80 |
*/ |
| 81 |
#if (defined(__GNUC__) || defined(__clang__) || defined(_MSC_VER)) && \ |
| 82 |
(HAVE_NEON_NATIVE || (GCC_PREREQ(6, 1) && defined(__ARM_FP))) |
| 83 |
# define HAVE_NEON_INTRIN 1 |
| 84 |
# include <arm_neon.h> |
| 85 |
#else |
| 86 |
# define HAVE_NEON_INTRIN 0 |
| 87 |
#endif |
| 88 |
|
| 89 |
/* PMULL */ |
| 90 |
#ifdef __ARM_FEATURE_CRYPTO |
| 91 |
# define HAVE_PMULL(features) 1 |
| 92 |
#else |
| 93 |
# define HAVE_PMULL(features) ((features) & ARM_CPU_FEATURE_PMULL) |
| 94 |
#endif |
| 95 |
#if defined(ARCH_ARM64) && HAVE_NEON_INTRIN && \ |
| 96 |
(GCC_PREREQ(7, 1) || defined(__clang__) || defined(_MSC_VER)) && \ |
| 97 |
CPU_IS_LITTLE_ENDIAN() /* untested on big endian */ |
| 98 |
# define HAVE_PMULL_INTRIN 1 |
| 99 |
/* Work around MSVC's vmull_p64() taking poly64x1_t instead of poly64_t */ |
| 100 |
# ifdef _MSC_VER |
| 101 |
# define compat_vmull_p64(a, b) vmull_p64(vcreate_p64(a), vcreate_p64(b)) |
| 102 |
# else |
| 103 |
# define compat_vmull_p64(a, b) vmull_p64((a), (b)) |
| 104 |
# endif |
| 105 |
#else |
| 106 |
# define HAVE_PMULL_INTRIN 0 |
| 107 |
#endif |
| 108 |
|
| 109 |
/* CRC32 */ |
| 110 |
#ifdef __ARM_FEATURE_CRC32 |
| 111 |
# define HAVE_CRC32(features) 1 |
| 112 |
#else |
| 113 |
# define HAVE_CRC32(features) ((features) & ARM_CPU_FEATURE_CRC32) |
| 114 |
#endif |
| 115 |
#if defined(ARCH_ARM64) && \ |
| 116 |
(defined(__GNUC__) || defined(__clang__) || defined(_MSC_VER)) |
| 117 |
# define HAVE_CRC32_INTRIN 1 |
| 118 |
# if defined(__GNUC__) || defined(__clang__) |
| 119 |
# include <arm_acle.h> |
| 120 |
# endif |
| 121 |
/* |
| 122 |
* Use an inline assembly fallback for clang 15 and earlier, which only |
| 123 |
* defined the crc32 intrinsics when crc32 is enabled in the main target. |
| 124 |
*/ |
| 125 |
# if defined(__clang__) && !CLANG_PREREQ(16, 0, 16000000) && \ |
| 126 |
!defined(__ARM_FEATURE_CRC32) |
| 127 |
# undef __crc32b |
| 128 |
# define __crc32b(a, b) \ |
| 129 |
({ uint32_t res; \ |
| 130 |
__asm__("crc32b %w0, %w1, %w2" \ |
| 131 |
: "=r" (res) : "r" (a), "r" (b)); \ |
| 132 |
res; }) |
| 133 |
# undef __crc32h |
| 134 |
# define __crc32h(a, b) \ |
| 135 |
({ uint32_t res; \ |
| 136 |
__asm__("crc32h %w0, %w1, %w2" \ |
| 137 |
: "=r" (res) : "r" (a), "r" (b)); \ |
| 138 |
res; }) |
| 139 |
# undef __crc32w |
| 140 |
# define __crc32w(a, b) \ |
| 141 |
({ uint32_t res; \ |
| 142 |
__asm__("crc32w %w0, %w1, %w2" \ |
| 143 |
: "=r" (res) : "r" (a), "r" (b)); \ |
| 144 |
res; }) |
| 145 |
# undef __crc32d |
| 146 |
# define __crc32d(a, b) \ |
| 147 |
({ uint32_t res; \ |
| 148 |
__asm__("crc32x %w0, %w1, %2" \ |
| 149 |
: "=r" (res) : "r" (a), "r" (b)); \ |
| 150 |
res; }) |
| 151 |
# pragma clang diagnostic ignored "-Wgnu-statement-expression" |
| 152 |
# endif |
| 153 |
#else |
| 154 |
# define HAVE_CRC32_INTRIN 0 |
| 155 |
#endif |
| 156 |
|
| 157 |
/* SHA3 (needed for the eor3 instruction) */ |
| 158 |
#ifdef __ARM_FEATURE_SHA3 |
| 159 |
# define HAVE_SHA3(features) 1 |
| 160 |
#else |
| 161 |
# define HAVE_SHA3(features) ((features) & ARM_CPU_FEATURE_SHA3) |
| 162 |
#endif |
| 163 |
#if defined(ARCH_ARM64) && HAVE_NEON_INTRIN && \ |
| 164 |
(GCC_PREREQ(9, 1) /* r268049 */ || \ |
| 165 |
CLANG_PREREQ(7, 0, 10010463) /* r338010 */) |
| 166 |
# define HAVE_SHA3_INTRIN 1 |
| 167 |
/* |
| 168 |
* Use an inline assembly fallback for clang 15 and earlier, which only |
| 169 |
* defined the sha3 intrinsics when sha3 is enabled in the main target. |
| 170 |
*/ |
| 171 |
# if defined(__clang__) && !CLANG_PREREQ(16, 0, 16000000) && \ |
| 172 |
!defined(__ARM_FEATURE_SHA3) |
| 173 |
# undef veor3q_u8 |
| 174 |
# define veor3q_u8(a, b, c) \ |
| 175 |
({ uint8x16_t res; \ |
| 176 |
__asm__("eor3 %0.16b, %1.16b, %2.16b, %3.16b" \ |
| 177 |
: "=w" (res) : "w" (a), "w" (b), "w" (c)); \ |
| 178 |
res; }) |
| 179 |
# pragma clang diagnostic ignored "-Wgnu-statement-expression" |
| 180 |
# endif |
| 181 |
#else |
| 182 |
# define HAVE_SHA3_INTRIN 0 |
| 183 |
#endif |
| 184 |
|
| 185 |
/* dotprod */ |
| 186 |
#ifdef __ARM_FEATURE_DOTPROD |
| 187 |
# define HAVE_DOTPROD(features) 1 |
| 188 |
#else |
| 189 |
# define HAVE_DOTPROD(features) ((features) & ARM_CPU_FEATURE_DOTPROD) |
| 190 |
#endif |
| 191 |
#if defined(ARCH_ARM64) && HAVE_NEON_INTRIN && \ |
| 192 |
(GCC_PREREQ(8, 1) || CLANG_PREREQ(7, 0, 10010000) || defined(_MSC_VER)) |
| 193 |
# define HAVE_DOTPROD_INTRIN 1 |
| 194 |
/* |
| 195 |
* Use an inline assembly fallback for clang 15 and earlier, which only |
| 196 |
* defined the dotprod intrinsics when dotprod is enabled in the main target. |
| 197 |
*/ |
| 198 |
# if defined(__clang__) && !CLANG_PREREQ(16, 0, 16000000) && \ |
| 199 |
!defined(__ARM_FEATURE_DOTPROD) |
| 200 |
# undef vdotq_u32 |
| 201 |
# define vdotq_u32(a, b, c) \ |
| 202 |
({ uint32x4_t res = (a); \ |
| 203 |
__asm__("udot %0.4s, %1.16b, %2.16b" \ |
| 204 |
: "+w" (res) : "w" (b), "w" (c)); \ |
| 205 |
res; }) |
| 206 |
# pragma clang diagnostic ignored "-Wgnu-statement-expression" |
| 207 |
# endif |
| 208 |
#else |
| 209 |
# define HAVE_DOTPROD_INTRIN 0 |
| 210 |
#endif |
| 211 |
|
| 212 |
#endif /* ARCH_ARM32 || ARCH_ARM64 */ |
| 213 |
|
| 214 |
#endif /* LIB_ARM_CPU_FEATURES_H */ |