11cb0ef41Sopenharmony_ci/* cpu_features.c -- Processor features detection.
21cb0ef41Sopenharmony_ci *
31cb0ef41Sopenharmony_ci * Copyright 2018 The Chromium Authors
41cb0ef41Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
51cb0ef41Sopenharmony_ci * found in the Chromium source repository LICENSE file.
61cb0ef41Sopenharmony_ci */
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include "cpu_features.h"
91cb0ef41Sopenharmony_ci#include "zutil.h"
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci#include <stdint.h>
121cb0ef41Sopenharmony_ci#if defined(_MSC_VER)
131cb0ef41Sopenharmony_ci#include <intrin.h>
141cb0ef41Sopenharmony_ci#elif defined(ADLER32_SIMD_SSSE3)
151cb0ef41Sopenharmony_ci#include <cpuid.h>
161cb0ef41Sopenharmony_ci#endif
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci/* TODO(cavalcantii): remove checks for x86_flags on deflate.
191cb0ef41Sopenharmony_ci */
201cb0ef41Sopenharmony_ci#if defined(ARMV8_OS_MACOS)
211cb0ef41Sopenharmony_ci/* Crypto extensions (crc32/pmull) are a baseline feature in ARMv8.1-A, and
221cb0ef41Sopenharmony_ci * OSX running on arm64 is new enough that these can be assumed without
231cb0ef41Sopenharmony_ci * runtime detection.
241cb0ef41Sopenharmony_ci */
251cb0ef41Sopenharmony_ciint ZLIB_INTERNAL arm_cpu_enable_crc32 = 1;
261cb0ef41Sopenharmony_ciint ZLIB_INTERNAL arm_cpu_enable_pmull = 1;
271cb0ef41Sopenharmony_ci#else
281cb0ef41Sopenharmony_ciint ZLIB_INTERNAL arm_cpu_enable_crc32 = 0;
291cb0ef41Sopenharmony_ciint ZLIB_INTERNAL arm_cpu_enable_pmull = 0;
301cb0ef41Sopenharmony_ci#endif
311cb0ef41Sopenharmony_ciint ZLIB_INTERNAL x86_cpu_enable_sse2 = 0;
321cb0ef41Sopenharmony_ciint ZLIB_INTERNAL x86_cpu_enable_ssse3 = 0;
331cb0ef41Sopenharmony_ciint ZLIB_INTERNAL x86_cpu_enable_simd = 0;
341cb0ef41Sopenharmony_ciint ZLIB_INTERNAL x86_cpu_enable_avx512 = 0;
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci#ifndef CPU_NO_SIMD
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci#if defined(ARMV8_OS_ANDROID) || defined(ARMV8_OS_LINUX) || defined(ARMV8_OS_FUCHSIA) || defined(ARMV8_OS_IOS)
391cb0ef41Sopenharmony_ci#include <pthread.h>
401cb0ef41Sopenharmony_ci#endif
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci#if defined(ARMV8_OS_ANDROID)
431cb0ef41Sopenharmony_ci#include <cpu-features.h>
441cb0ef41Sopenharmony_ci#elif defined(ARMV8_OS_LINUX)
451cb0ef41Sopenharmony_ci#include <asm/hwcap.h>
461cb0ef41Sopenharmony_ci#include <sys/auxv.h>
471cb0ef41Sopenharmony_ci#elif defined(ARMV8_OS_FUCHSIA)
481cb0ef41Sopenharmony_ci#include <zircon/features.h>
491cb0ef41Sopenharmony_ci#include <zircon/syscalls.h>
501cb0ef41Sopenharmony_ci#include <zircon/types.h>
511cb0ef41Sopenharmony_ci#elif defined(ARMV8_OS_WINDOWS) || defined(X86_WINDOWS)
521cb0ef41Sopenharmony_ci#include <windows.h>
531cb0ef41Sopenharmony_ci#elif defined(ARMV8_OS_IOS)
541cb0ef41Sopenharmony_ci#include <sys/sysctl.h>
551cb0ef41Sopenharmony_ci#elif !defined(_MSC_VER)
561cb0ef41Sopenharmony_ci#include <pthread.h>
571cb0ef41Sopenharmony_ci#else
581cb0ef41Sopenharmony_ci#error cpu_features.c CPU feature detection in not defined for your platform
591cb0ef41Sopenharmony_ci#endif
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci#if !defined(CPU_NO_SIMD) && !defined(ARMV8_OS_MACOS)
621cb0ef41Sopenharmony_cistatic void _cpu_check_features(void);
631cb0ef41Sopenharmony_ci#endif
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci#if defined(ARMV8_OS_ANDROID) || defined(ARMV8_OS_LINUX) || defined(ARMV8_OS_MACOS) || defined(ARMV8_OS_FUCHSIA) || defined(X86_NOT_WINDOWS) || defined(ARMV8_OS_IOS)
661cb0ef41Sopenharmony_ci#if !defined(ARMV8_OS_MACOS)
671cb0ef41Sopenharmony_ci// _cpu_check_features() doesn't need to do anything on mac/arm since all
681cb0ef41Sopenharmony_ci// features are known at build time, so don't call it.
691cb0ef41Sopenharmony_ci// Do provide cpu_check_features() (with a no-op implementation) so that we
701cb0ef41Sopenharmony_ci// don't have to make all callers of it check for mac/arm.
711cb0ef41Sopenharmony_cistatic pthread_once_t cpu_check_inited_once = PTHREAD_ONCE_INIT;
721cb0ef41Sopenharmony_ci#endif
731cb0ef41Sopenharmony_civoid ZLIB_INTERNAL cpu_check_features(void)
741cb0ef41Sopenharmony_ci{
751cb0ef41Sopenharmony_ci#if !defined(ARMV8_OS_MACOS)
761cb0ef41Sopenharmony_ci    pthread_once(&cpu_check_inited_once, _cpu_check_features);
771cb0ef41Sopenharmony_ci#endif
781cb0ef41Sopenharmony_ci}
791cb0ef41Sopenharmony_ci#elif defined(ARMV8_OS_WINDOWS) || defined(X86_WINDOWS)
801cb0ef41Sopenharmony_cistatic INIT_ONCE cpu_check_inited_once = INIT_ONCE_STATIC_INIT;
811cb0ef41Sopenharmony_cistatic BOOL CALLBACK _cpu_check_features_forwarder(PINIT_ONCE once, PVOID param, PVOID* context)
821cb0ef41Sopenharmony_ci{
831cb0ef41Sopenharmony_ci    _cpu_check_features();
841cb0ef41Sopenharmony_ci    return TRUE;
851cb0ef41Sopenharmony_ci}
861cb0ef41Sopenharmony_civoid ZLIB_INTERNAL cpu_check_features(void)
871cb0ef41Sopenharmony_ci{
881cb0ef41Sopenharmony_ci    InitOnceExecuteOnce(&cpu_check_inited_once, _cpu_check_features_forwarder,
891cb0ef41Sopenharmony_ci                        NULL, NULL);
901cb0ef41Sopenharmony_ci}
911cb0ef41Sopenharmony_ci#endif
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ci#if (defined(__ARM_NEON__) || defined(__ARM_NEON))
941cb0ef41Sopenharmony_ci#if !defined(ARMV8_OS_MACOS)
951cb0ef41Sopenharmony_ci/*
961cb0ef41Sopenharmony_ci * See http://bit.ly/2CcoEsr for run-time detection of ARM features and also
971cb0ef41Sopenharmony_ci * crbug.com/931275 for android_getCpuFeatures() use in the Android sandbox.
981cb0ef41Sopenharmony_ci */
991cb0ef41Sopenharmony_cistatic void _cpu_check_features(void)
1001cb0ef41Sopenharmony_ci{
1011cb0ef41Sopenharmony_ci#if defined(ARMV8_OS_ANDROID) && defined(__aarch64__)
1021cb0ef41Sopenharmony_ci    uint64_t features = android_getCpuFeatures();
1031cb0ef41Sopenharmony_ci    arm_cpu_enable_crc32 = !!(features & ANDROID_CPU_ARM64_FEATURE_CRC32);
1041cb0ef41Sopenharmony_ci    arm_cpu_enable_pmull = !!(features & ANDROID_CPU_ARM64_FEATURE_PMULL);
1051cb0ef41Sopenharmony_ci#elif defined(ARMV8_OS_ANDROID) /* aarch32 */
1061cb0ef41Sopenharmony_ci    uint64_t features = android_getCpuFeatures();
1071cb0ef41Sopenharmony_ci    arm_cpu_enable_crc32 = !!(features & ANDROID_CPU_ARM_FEATURE_CRC32);
1081cb0ef41Sopenharmony_ci    arm_cpu_enable_pmull = !!(features & ANDROID_CPU_ARM_FEATURE_PMULL);
1091cb0ef41Sopenharmony_ci#elif defined(ARMV8_OS_LINUX) && defined(__aarch64__)
1101cb0ef41Sopenharmony_ci    unsigned long features = getauxval(AT_HWCAP);
1111cb0ef41Sopenharmony_ci    arm_cpu_enable_crc32 = !!(features & HWCAP_CRC32);
1121cb0ef41Sopenharmony_ci    arm_cpu_enable_pmull = !!(features & HWCAP_PMULL);
1131cb0ef41Sopenharmony_ci#elif defined(ARMV8_OS_LINUX) && (defined(__ARM_NEON) || defined(__ARM_NEON__))
1141cb0ef41Sopenharmony_ci    /* Query HWCAP2 for ARMV8-A SoCs running in aarch32 mode */
1151cb0ef41Sopenharmony_ci    unsigned long features = getauxval(AT_HWCAP2);
1161cb0ef41Sopenharmony_ci    arm_cpu_enable_crc32 = !!(features & HWCAP2_CRC32);
1171cb0ef41Sopenharmony_ci    arm_cpu_enable_pmull = !!(features & HWCAP2_PMULL);
1181cb0ef41Sopenharmony_ci#elif defined(ARMV8_OS_FUCHSIA)
1191cb0ef41Sopenharmony_ci    uint32_t features;
1201cb0ef41Sopenharmony_ci    zx_status_t rc = zx_system_get_features(ZX_FEATURE_KIND_CPU, &features);
1211cb0ef41Sopenharmony_ci    if (rc != ZX_OK || (features & ZX_ARM64_FEATURE_ISA_ASIMD) == 0)
1221cb0ef41Sopenharmony_ci        return;  /* Report nothing if ASIMD(NEON) is missing */
1231cb0ef41Sopenharmony_ci    arm_cpu_enable_crc32 = !!(features & ZX_ARM64_FEATURE_ISA_CRC32);
1241cb0ef41Sopenharmony_ci    arm_cpu_enable_pmull = !!(features & ZX_ARM64_FEATURE_ISA_PMULL);
1251cb0ef41Sopenharmony_ci#elif defined(ARMV8_OS_WINDOWS)
1261cb0ef41Sopenharmony_ci    arm_cpu_enable_crc32 = IsProcessorFeaturePresent(PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE);
1271cb0ef41Sopenharmony_ci    arm_cpu_enable_pmull = IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE);
1281cb0ef41Sopenharmony_ci#elif defined(ARMV8_OS_IOS)
1291cb0ef41Sopenharmony_ci    // Determine what features are supported dynamically. This code is applicable to macOS
1301cb0ef41Sopenharmony_ci    // as well if we wish to do that dynamically on that platform in the future.
1311cb0ef41Sopenharmony_ci    // See https://developer.apple.com/documentation/kernel/1387446-sysctlbyname/determining_instruction_set_characteristics
1321cb0ef41Sopenharmony_ci    int val = 0;
1331cb0ef41Sopenharmony_ci    size_t len = sizeof(val);
1341cb0ef41Sopenharmony_ci    arm_cpu_enable_crc32 = sysctlbyname("hw.optional.armv8_crc32", &val, &len, 0, 0) == 0
1351cb0ef41Sopenharmony_ci               && val != 0;
1361cb0ef41Sopenharmony_ci    val = 0;
1371cb0ef41Sopenharmony_ci    len = sizeof(val);
1381cb0ef41Sopenharmony_ci    arm_cpu_enable_pmull = sysctlbyname("hw.optional.arm.FEAT_PMULL", &val, &len, 0, 0) == 0
1391cb0ef41Sopenharmony_ci               && val != 0;
1401cb0ef41Sopenharmony_ci#endif
1411cb0ef41Sopenharmony_ci}
1421cb0ef41Sopenharmony_ci#endif
1431cb0ef41Sopenharmony_ci#elif defined(X86_NOT_WINDOWS) || defined(X86_WINDOWS)
1441cb0ef41Sopenharmony_ci/*
1451cb0ef41Sopenharmony_ci * iOS@x86 (i.e. emulator) is another special case where we disable
1461cb0ef41Sopenharmony_ci * SIMD optimizations.
1471cb0ef41Sopenharmony_ci */
1481cb0ef41Sopenharmony_ci#ifndef CPU_NO_SIMD
1491cb0ef41Sopenharmony_ci/* On x86 we simply use a instruction to check the CPU features.
1501cb0ef41Sopenharmony_ci * (i.e. CPUID).
1511cb0ef41Sopenharmony_ci */
1521cb0ef41Sopenharmony_ci#ifdef CRC32_SIMD_AVX512_PCLMUL
1531cb0ef41Sopenharmony_ci#include <immintrin.h>
1541cb0ef41Sopenharmony_ci#include <xsaveintrin.h>
1551cb0ef41Sopenharmony_ci#endif
1561cb0ef41Sopenharmony_cistatic void _cpu_check_features(void)
1571cb0ef41Sopenharmony_ci{
1581cb0ef41Sopenharmony_ci    int x86_cpu_has_sse2;
1591cb0ef41Sopenharmony_ci    int x86_cpu_has_ssse3;
1601cb0ef41Sopenharmony_ci    int x86_cpu_has_sse42;
1611cb0ef41Sopenharmony_ci    int x86_cpu_has_pclmulqdq;
1621cb0ef41Sopenharmony_ci    int abcd[4];
1631cb0ef41Sopenharmony_ci
1641cb0ef41Sopenharmony_ci#ifdef _MSC_VER
1651cb0ef41Sopenharmony_ci    __cpuid(abcd, 1);
1661cb0ef41Sopenharmony_ci#else
1671cb0ef41Sopenharmony_ci    __cpuid(1, abcd[0], abcd[1], abcd[2], abcd[3]);
1681cb0ef41Sopenharmony_ci#endif
1691cb0ef41Sopenharmony_ci
1701cb0ef41Sopenharmony_ci    x86_cpu_has_sse2 = abcd[3] & 0x4000000;
1711cb0ef41Sopenharmony_ci    x86_cpu_has_ssse3 = abcd[2] & 0x000200;
1721cb0ef41Sopenharmony_ci    x86_cpu_has_sse42 = abcd[2] & 0x100000;
1731cb0ef41Sopenharmony_ci    x86_cpu_has_pclmulqdq = abcd[2] & 0x2;
1741cb0ef41Sopenharmony_ci
1751cb0ef41Sopenharmony_ci    x86_cpu_enable_sse2 = x86_cpu_has_sse2;
1761cb0ef41Sopenharmony_ci
1771cb0ef41Sopenharmony_ci    x86_cpu_enable_ssse3 = x86_cpu_has_ssse3;
1781cb0ef41Sopenharmony_ci
1791cb0ef41Sopenharmony_ci    x86_cpu_enable_simd = x86_cpu_has_sse2 &&
1801cb0ef41Sopenharmony_ci                          x86_cpu_has_sse42 &&
1811cb0ef41Sopenharmony_ci                          x86_cpu_has_pclmulqdq;
1821cb0ef41Sopenharmony_ci
1831cb0ef41Sopenharmony_ci#ifdef CRC32_SIMD_AVX512_PCLMUL
1841cb0ef41Sopenharmony_ci    x86_cpu_enable_avx512 = _xgetbv(0) & 0x00000040;
1851cb0ef41Sopenharmony_ci#endif
1861cb0ef41Sopenharmony_ci}
1871cb0ef41Sopenharmony_ci#endif
1881cb0ef41Sopenharmony_ci#endif
1891cb0ef41Sopenharmony_ci#endif
190