Adenilson Cavalcanti | 5de00af | 2020-01-08 22:12:31 +0000 | [diff] [blame] | 1 | /* cpu_features.c -- Processor features detection. |
| 2 | * |
| 3 | * Copyright 2018 The Chromium Authors. All rights reserved. |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the Chromium source repository LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "cpu_features.h" |
| 9 | #include "zutil.h" |
| 10 | |
| 11 | #include <stdint.h> |
| 12 | #if defined(_MSC_VER) |
| 13 | #include <intrin.h> |
| 14 | #elif defined(ADLER32_SIMD_SSSE3) |
| 15 | #include <cpuid.h> |
| 16 | #endif |
| 17 | |
| 18 | /* TODO(cavalcantii): remove checks for x86_flags on deflate. |
| 19 | */ |
| 20 | int ZLIB_INTERNAL arm_cpu_enable_crc32 = 0; |
| 21 | int ZLIB_INTERNAL arm_cpu_enable_pmull = 0; |
| 22 | int ZLIB_INTERNAL x86_cpu_enable_ssse3 = 0; |
| 23 | int ZLIB_INTERNAL x86_cpu_enable_simd = 0; |
| 24 | |
Richard Townsend | c2eb8a7 | 2020-02-14 01:15:01 +0000 | [diff] [blame] | 25 | #ifndef CPU_NO_SIMD |
| 26 | |
Adenilson Cavalcanti | 5de00af | 2020-01-08 22:12:31 +0000 | [diff] [blame] | 27 | #if defined(ARMV8_OS_ANDROID) || defined(ARMV8_OS_LINUX) || defined(ARMV8_OS_FUCHSIA) |
| 28 | #include <pthread.h> |
| 29 | #endif |
| 30 | |
| 31 | #if defined(ARMV8_OS_ANDROID) |
| 32 | #include <cpu-features.h> |
| 33 | #elif defined(ARMV8_OS_LINUX) |
| 34 | #include <asm/hwcap.h> |
| 35 | #include <sys/auxv.h> |
| 36 | #elif defined(ARMV8_OS_FUCHSIA) |
| 37 | #include <zircon/features.h> |
| 38 | #include <zircon/syscalls.h> |
| 39 | #include <zircon/types.h> |
Hans Wennborg | 2a6432e | 2020-01-24 22:31:29 +0000 | [diff] [blame] | 40 | #elif defined(ARMV8_OS_WINDOWS) || defined(X86_WINDOWS) |
Adenilson Cavalcanti | 5de00af | 2020-01-08 22:12:31 +0000 | [diff] [blame] | 41 | #include <windows.h> |
Hans Wennborg | 2a6432e | 2020-01-24 22:31:29 +0000 | [diff] [blame] | 42 | #elif !defined(_MSC_VER) |
Nico Weber | 51dd31c | 2020-01-24 20:43:07 +0000 | [diff] [blame] | 43 | #include <pthread.h> |
Hans Wennborg | 2a6432e | 2020-01-24 22:31:29 +0000 | [diff] [blame] | 44 | #else |
| 45 | #error cpu_features.c CPU feature detection in not defined for your platform |
Adenilson Cavalcanti | 5de00af | 2020-01-08 22:12:31 +0000 | [diff] [blame] | 46 | #endif |
| 47 | |
| 48 | #if !defined(CPU_NO_SIMD) && !defined(ARM_OS_IOS) |
| 49 | static void _cpu_check_features(void); |
| 50 | #endif |
| 51 | |
| 52 | #if defined(ARMV8_OS_ANDROID) || defined(ARMV8_OS_LINUX) || defined(ARMV8_OS_FUCHSIA) || defined(X86_NOT_WINDOWS) |
| 53 | static pthread_once_t cpu_check_inited_once = PTHREAD_ONCE_INIT; |
| 54 | void ZLIB_INTERNAL cpu_check_features(void) |
| 55 | { |
| 56 | pthread_once(&cpu_check_inited_once, _cpu_check_features); |
| 57 | } |
Hans Wennborg | 2a6432e | 2020-01-24 22:31:29 +0000 | [diff] [blame] | 58 | #elif defined(ARMV8_OS_WINDOWS) || defined(X86_WINDOWS) |
Adenilson Cavalcanti | 5de00af | 2020-01-08 22:12:31 +0000 | [diff] [blame] | 59 | static INIT_ONCE cpu_check_inited_once = INIT_ONCE_STATIC_INIT; |
| 60 | static BOOL CALLBACK _cpu_check_features_forwarder(PINIT_ONCE once, PVOID param, PVOID* context) |
| 61 | { |
| 62 | _cpu_check_features(); |
Adenilson Cavalcanti | 5de00af | 2020-01-08 22:12:31 +0000 | [diff] [blame] | 63 | return TRUE; |
| 64 | } |
| 65 | void ZLIB_INTERNAL cpu_check_features(void) |
| 66 | { |
| 67 | InitOnceExecuteOnce(&cpu_check_inited_once, _cpu_check_features_forwarder, |
| 68 | NULL, NULL); |
| 69 | } |
| 70 | #endif |
| 71 | |
| 72 | #if (defined(__ARM_NEON__) || defined(__ARM_NEON)) |
| 73 | /* |
| 74 | * iOS@ARM is a special case where we always have NEON but don't check |
| 75 | * for crypto extensions. |
| 76 | */ |
| 77 | #ifndef ARM_OS_IOS |
| 78 | /* |
| 79 | * See http://bit.ly/2CcoEsr for run-time detection of ARM features and also |
| 80 | * crbug.com/931275 for android_getCpuFeatures() use in the Android sandbox. |
| 81 | */ |
| 82 | static void _cpu_check_features(void) |
| 83 | { |
| 84 | #if defined(ARMV8_OS_ANDROID) && defined(__aarch64__) |
| 85 | uint64_t features = android_getCpuFeatures(); |
| 86 | arm_cpu_enable_crc32 = !!(features & ANDROID_CPU_ARM64_FEATURE_CRC32); |
| 87 | arm_cpu_enable_pmull = !!(features & ANDROID_CPU_ARM64_FEATURE_PMULL); |
| 88 | #elif defined(ARMV8_OS_ANDROID) /* aarch32 */ |
| 89 | uint64_t features = android_getCpuFeatures(); |
| 90 | arm_cpu_enable_crc32 = !!(features & ANDROID_CPU_ARM_FEATURE_CRC32); |
| 91 | arm_cpu_enable_pmull = !!(features & ANDROID_CPU_ARM_FEATURE_PMULL); |
| 92 | #elif defined(ARMV8_OS_LINUX) && defined(__aarch64__) |
| 93 | unsigned long features = getauxval(AT_HWCAP); |
| 94 | arm_cpu_enable_crc32 = !!(features & HWCAP_CRC32); |
| 95 | arm_cpu_enable_pmull = !!(features & HWCAP_PMULL); |
| 96 | #elif defined(ARMV8_OS_LINUX) && (defined(__ARM_NEON) || defined(__ARM_NEON__)) |
| 97 | /* Query HWCAP2 for ARMV8-A SoCs running in aarch32 mode */ |
| 98 | unsigned long features = getauxval(AT_HWCAP2); |
| 99 | arm_cpu_enable_crc32 = !!(features & HWCAP2_CRC32); |
| 100 | arm_cpu_enable_pmull = !!(features & HWCAP2_PMULL); |
| 101 | #elif defined(ARMV8_OS_FUCHSIA) |
| 102 | uint32_t features; |
| 103 | zx_status_t rc = zx_system_get_features(ZX_FEATURE_KIND_CPU, &features); |
| 104 | if (rc != ZX_OK || (features & ZX_ARM64_FEATURE_ISA_ASIMD) == 0) |
| 105 | return; /* Report nothing if ASIMD(NEON) is missing */ |
| 106 | arm_cpu_enable_crc32 = !!(features & ZX_ARM64_FEATURE_ISA_CRC32); |
| 107 | arm_cpu_enable_pmull = !!(features & ZX_ARM64_FEATURE_ISA_PMULL); |
| 108 | #elif defined(ARMV8_OS_WINDOWS) |
| 109 | arm_cpu_enable_crc32 = IsProcessorFeaturePresent(PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE); |
| 110 | arm_cpu_enable_pmull = IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE); |
| 111 | #endif |
| 112 | } |
| 113 | #endif |
| 114 | #elif defined(X86_NOT_WINDOWS) || defined(X86_WINDOWS) |
| 115 | /* |
| 116 | * iOS@x86 (i.e. emulator) is another special case where we disable |
| 117 | * SIMD optimizations. |
| 118 | */ |
| 119 | #ifndef CPU_NO_SIMD |
| 120 | /* On x86 we simply use a instruction to check the CPU features. |
| 121 | * (i.e. CPUID). |
| 122 | */ |
| 123 | static void _cpu_check_features(void) |
| 124 | { |
| 125 | int x86_cpu_has_sse2; |
| 126 | int x86_cpu_has_ssse3; |
| 127 | int x86_cpu_has_sse42; |
| 128 | int x86_cpu_has_pclmulqdq; |
| 129 | int abcd[4]; |
| 130 | #ifdef _MSC_VER |
| 131 | __cpuid(abcd, 1); |
| 132 | #else |
| 133 | __cpuid(1, abcd[0], abcd[1], abcd[2], abcd[3]); |
| 134 | #endif |
| 135 | x86_cpu_has_sse2 = abcd[3] & 0x4000000; |
| 136 | x86_cpu_has_ssse3 = abcd[2] & 0x000200; |
| 137 | x86_cpu_has_sse42 = abcd[2] & 0x100000; |
| 138 | x86_cpu_has_pclmulqdq = abcd[2] & 0x2; |
| 139 | |
| 140 | x86_cpu_enable_ssse3 = x86_cpu_has_ssse3; |
| 141 | |
| 142 | x86_cpu_enable_simd = x86_cpu_has_sse2 && |
| 143 | x86_cpu_has_sse42 && |
| 144 | x86_cpu_has_pclmulqdq; |
| 145 | } |
| 146 | #endif |
| 147 | #endif |
Richard Townsend | c2eb8a7 | 2020-02-14 01:15:01 +0000 | [diff] [blame] | 148 | #endif |