11cb0ef41Sopenharmony_ci/* Copyright 2016 Google Inc. All Rights Reserved. 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ci Distributed under MIT license. 41cb0ef41Sopenharmony_ci See file LICENSE for detail or copy at https://opensource.org/licenses/MIT 51cb0ef41Sopenharmony_ci*/ 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ci/* Macros for compiler / platform specific features and build options. 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ci Build options are: 101cb0ef41Sopenharmony_ci * BROTLI_BUILD_32_BIT disables 64-bit optimizations 111cb0ef41Sopenharmony_ci * BROTLI_BUILD_64_BIT forces to use 64-bit optimizations 121cb0ef41Sopenharmony_ci * BROTLI_BUILD_BIG_ENDIAN forces to use big-endian optimizations 131cb0ef41Sopenharmony_ci * BROTLI_BUILD_ENDIAN_NEUTRAL disables endian-aware optimizations 141cb0ef41Sopenharmony_ci * BROTLI_BUILD_LITTLE_ENDIAN forces to use little-endian optimizations 151cb0ef41Sopenharmony_ci * BROTLI_BUILD_PORTABLE disables dangerous optimizations, like unaligned 161cb0ef41Sopenharmony_ci read and overlapping memcpy; this reduces decompression speed by 5% 171cb0ef41Sopenharmony_ci * BROTLI_BUILD_NO_RBIT disables "rbit" optimization for ARM CPUs 181cb0ef41Sopenharmony_ci * BROTLI_DEBUG dumps file name and line number when decoder detects stream 191cb0ef41Sopenharmony_ci or memory error 201cb0ef41Sopenharmony_ci * BROTLI_ENABLE_LOG enables asserts and dumps various state information 211cb0ef41Sopenharmony_ci*/ 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ci#ifndef BROTLI_COMMON_PLATFORM_H_ 241cb0ef41Sopenharmony_ci#define BROTLI_COMMON_PLATFORM_H_ 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ci#include <string.h> /* memcpy */ 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ci#include <brotli/port.h> 291cb0ef41Sopenharmony_ci#include <brotli/types.h> 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_ci#if defined(OS_LINUX) || defined(OS_CYGWIN) || defined(__EMSCRIPTEN__) 321cb0ef41Sopenharmony_ci#include <endian.h> 331cb0ef41Sopenharmony_ci#elif defined(OS_FREEBSD) 341cb0ef41Sopenharmony_ci#include <machine/endian.h> 351cb0ef41Sopenharmony_ci#elif defined(OS_MACOSX) 361cb0ef41Sopenharmony_ci#include <machine/endian.h> 371cb0ef41Sopenharmony_ci/* Let's try and follow the Linux convention */ 381cb0ef41Sopenharmony_ci#define BROTLI_X_BYTE_ORDER BYTE_ORDER 391cb0ef41Sopenharmony_ci#define BROTLI_X_LITTLE_ENDIAN LITTLE_ENDIAN 401cb0ef41Sopenharmony_ci#define BROTLI_X_BIG_ENDIAN BIG_ENDIAN 411cb0ef41Sopenharmony_ci#endif 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_ci#if BROTLI_MSVC_VERSION_CHECK(12, 0, 0) 441cb0ef41Sopenharmony_ci#include <intrin.h> 451cb0ef41Sopenharmony_ci#endif 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_ci#if defined(BROTLI_ENABLE_LOG) || defined(BROTLI_DEBUG) 481cb0ef41Sopenharmony_ci#include <assert.h> 491cb0ef41Sopenharmony_ci#include <stdio.h> 501cb0ef41Sopenharmony_ci#endif 511cb0ef41Sopenharmony_ci 521cb0ef41Sopenharmony_ci/* The following macros were borrowed from https://github.com/nemequ/hedley 531cb0ef41Sopenharmony_ci * with permission of original author - Evan Nemerson <evan@nemerson.com> */ 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_ci/* >>> >>> >>> hedley macros */ 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_ci/* Define "BROTLI_PREDICT_TRUE" and "BROTLI_PREDICT_FALSE" macros for capable 581cb0ef41Sopenharmony_ci compilers. 591cb0ef41Sopenharmony_ci 601cb0ef41Sopenharmony_ciTo apply compiler hint, enclose the branching condition into macros, like this: 611cb0ef41Sopenharmony_ci 621cb0ef41Sopenharmony_ci if (BROTLI_PREDICT_TRUE(zero == 0)) { 631cb0ef41Sopenharmony_ci // main execution path 641cb0ef41Sopenharmony_ci } else { 651cb0ef41Sopenharmony_ci // compiler should place this code outside of main execution path 661cb0ef41Sopenharmony_ci } 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_ciOR: 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_ci if (BROTLI_PREDICT_FALSE(something_rare_or_unexpected_happens)) { 711cb0ef41Sopenharmony_ci // compiler should place this code outside of main execution path 721cb0ef41Sopenharmony_ci } 731cb0ef41Sopenharmony_ci 741cb0ef41Sopenharmony_ci*/ 751cb0ef41Sopenharmony_ci#if BROTLI_GNUC_HAS_BUILTIN(__builtin_expect, 3, 0, 0) || \ 761cb0ef41Sopenharmony_ci BROTLI_INTEL_VERSION_CHECK(16, 0, 0) || \ 771cb0ef41Sopenharmony_ci BROTLI_SUNPRO_VERSION_CHECK(5, 15, 0) || \ 781cb0ef41Sopenharmony_ci BROTLI_ARM_VERSION_CHECK(4, 1, 0) || \ 791cb0ef41Sopenharmony_ci BROTLI_IBM_VERSION_CHECK(10, 1, 0) || \ 801cb0ef41Sopenharmony_ci BROTLI_TI_VERSION_CHECK(7, 3, 0) || \ 811cb0ef41Sopenharmony_ci BROTLI_TINYC_VERSION_CHECK(0, 9, 27) 821cb0ef41Sopenharmony_ci#define BROTLI_PREDICT_TRUE(x) (__builtin_expect(!!(x), 1)) 831cb0ef41Sopenharmony_ci#define BROTLI_PREDICT_FALSE(x) (__builtin_expect(x, 0)) 841cb0ef41Sopenharmony_ci#else 851cb0ef41Sopenharmony_ci#define BROTLI_PREDICT_FALSE(x) (x) 861cb0ef41Sopenharmony_ci#define BROTLI_PREDICT_TRUE(x) (x) 871cb0ef41Sopenharmony_ci#endif 881cb0ef41Sopenharmony_ci 891cb0ef41Sopenharmony_ci#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \ 901cb0ef41Sopenharmony_ci !defined(__cplusplus) 911cb0ef41Sopenharmony_ci#define BROTLI_RESTRICT restrict 921cb0ef41Sopenharmony_ci#elif BROTLI_GNUC_VERSION_CHECK(3, 1, 0) || \ 931cb0ef41Sopenharmony_ci BROTLI_MSVC_VERSION_CHECK(14, 0, 0) || \ 941cb0ef41Sopenharmony_ci BROTLI_INTEL_VERSION_CHECK(16, 0, 0) || \ 951cb0ef41Sopenharmony_ci BROTLI_ARM_VERSION_CHECK(4, 1, 0) || \ 961cb0ef41Sopenharmony_ci BROTLI_IBM_VERSION_CHECK(10, 1, 0) || \ 971cb0ef41Sopenharmony_ci BROTLI_PGI_VERSION_CHECK(17, 10, 0) || \ 981cb0ef41Sopenharmony_ci BROTLI_TI_VERSION_CHECK(8, 0, 0) || \ 991cb0ef41Sopenharmony_ci BROTLI_IAR_VERSION_CHECK(8, 0, 0) || \ 1001cb0ef41Sopenharmony_ci (BROTLI_SUNPRO_VERSION_CHECK(5, 14, 0) && defined(__cplusplus)) 1011cb0ef41Sopenharmony_ci#define BROTLI_RESTRICT __restrict 1021cb0ef41Sopenharmony_ci#elif BROTLI_SUNPRO_VERSION_CHECK(5, 3, 0) && !defined(__cplusplus) 1031cb0ef41Sopenharmony_ci#define BROTLI_RESTRICT _Restrict 1041cb0ef41Sopenharmony_ci#else 1051cb0ef41Sopenharmony_ci#define BROTLI_RESTRICT 1061cb0ef41Sopenharmony_ci#endif 1071cb0ef41Sopenharmony_ci 1081cb0ef41Sopenharmony_ci#if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \ 1091cb0ef41Sopenharmony_ci (defined(__cplusplus) && (__cplusplus >= 199711L)) 1101cb0ef41Sopenharmony_ci#define BROTLI_MAYBE_INLINE inline 1111cb0ef41Sopenharmony_ci#elif defined(__GNUC_STDC_INLINE__) || defined(__GNUC_GNU_INLINE__) || \ 1121cb0ef41Sopenharmony_ci BROTLI_ARM_VERSION_CHECK(6, 2, 0) 1131cb0ef41Sopenharmony_ci#define BROTLI_MAYBE_INLINE __inline__ 1141cb0ef41Sopenharmony_ci#elif BROTLI_MSVC_VERSION_CHECK(12, 0, 0) || \ 1151cb0ef41Sopenharmony_ci BROTLI_ARM_VERSION_CHECK(4, 1, 0) || BROTLI_TI_VERSION_CHECK(8, 0, 0) 1161cb0ef41Sopenharmony_ci#define BROTLI_MAYBE_INLINE __inline 1171cb0ef41Sopenharmony_ci#else 1181cb0ef41Sopenharmony_ci#define BROTLI_MAYBE_INLINE 1191cb0ef41Sopenharmony_ci#endif 1201cb0ef41Sopenharmony_ci 1211cb0ef41Sopenharmony_ci#if BROTLI_GNUC_HAS_ATTRIBUTE(always_inline, 4, 0, 0) || \ 1221cb0ef41Sopenharmony_ci BROTLI_INTEL_VERSION_CHECK(16, 0, 0) || \ 1231cb0ef41Sopenharmony_ci BROTLI_SUNPRO_VERSION_CHECK(5, 11, 0) || \ 1241cb0ef41Sopenharmony_ci BROTLI_ARM_VERSION_CHECK(4, 1, 0) || \ 1251cb0ef41Sopenharmony_ci BROTLI_IBM_VERSION_CHECK(10, 1, 0) || \ 1261cb0ef41Sopenharmony_ci BROTLI_TI_VERSION_CHECK(8, 0, 0) || \ 1271cb0ef41Sopenharmony_ci (BROTLI_TI_VERSION_CHECK(7, 3, 0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) 1281cb0ef41Sopenharmony_ci#define BROTLI_INLINE BROTLI_MAYBE_INLINE __attribute__((__always_inline__)) 1291cb0ef41Sopenharmony_ci#elif BROTLI_MSVC_VERSION_CHECK(12, 0, 0) 1301cb0ef41Sopenharmony_ci#define BROTLI_INLINE BROTLI_MAYBE_INLINE __forceinline 1311cb0ef41Sopenharmony_ci#elif BROTLI_TI_VERSION_CHECK(7, 0, 0) && defined(__cplusplus) 1321cb0ef41Sopenharmony_ci#define BROTLI_INLINE BROTLI_MAYBE_INLINE _Pragma("FUNC_ALWAYS_INLINE;") 1331cb0ef41Sopenharmony_ci#elif BROTLI_IAR_VERSION_CHECK(8, 0, 0) 1341cb0ef41Sopenharmony_ci#define BROTLI_INLINE BROTLI_MAYBE_INLINE _Pragma("inline=forced") 1351cb0ef41Sopenharmony_ci#else 1361cb0ef41Sopenharmony_ci#define BROTLI_INLINE BROTLI_MAYBE_INLINE 1371cb0ef41Sopenharmony_ci#endif 1381cb0ef41Sopenharmony_ci 1391cb0ef41Sopenharmony_ci#if BROTLI_GNUC_HAS_ATTRIBUTE(noinline, 4, 0, 0) || \ 1401cb0ef41Sopenharmony_ci BROTLI_INTEL_VERSION_CHECK(16, 0, 0) || \ 1411cb0ef41Sopenharmony_ci BROTLI_SUNPRO_VERSION_CHECK(5, 11, 0) || \ 1421cb0ef41Sopenharmony_ci BROTLI_ARM_VERSION_CHECK(4, 1, 0) || \ 1431cb0ef41Sopenharmony_ci BROTLI_IBM_VERSION_CHECK(10, 1, 0) || \ 1441cb0ef41Sopenharmony_ci BROTLI_TI_VERSION_CHECK(8, 0, 0) || \ 1451cb0ef41Sopenharmony_ci (BROTLI_TI_VERSION_CHECK(7, 3, 0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) 1461cb0ef41Sopenharmony_ci#define BROTLI_NOINLINE __attribute__((__noinline__)) 1471cb0ef41Sopenharmony_ci#elif BROTLI_MSVC_VERSION_CHECK(13, 10, 0) 1481cb0ef41Sopenharmony_ci#define BROTLI_NOINLINE __declspec(noinline) 1491cb0ef41Sopenharmony_ci#elif BROTLI_PGI_VERSION_CHECK(10, 2, 0) 1501cb0ef41Sopenharmony_ci#define BROTLI_NOINLINE _Pragma("noinline") 1511cb0ef41Sopenharmony_ci#elif BROTLI_TI_VERSION_CHECK(6, 0, 0) && defined(__cplusplus) 1521cb0ef41Sopenharmony_ci#define BROTLI_NOINLINE _Pragma("FUNC_CANNOT_INLINE;") 1531cb0ef41Sopenharmony_ci#elif BROTLI_IAR_VERSION_CHECK(8, 0, 0) 1541cb0ef41Sopenharmony_ci#define BROTLI_NOINLINE _Pragma("inline=never") 1551cb0ef41Sopenharmony_ci#else 1561cb0ef41Sopenharmony_ci#define BROTLI_NOINLINE 1571cb0ef41Sopenharmony_ci#endif 1581cb0ef41Sopenharmony_ci 1591cb0ef41Sopenharmony_ci/* BROTLI_INTERNAL could be defined to override visibility, e.g. for tests. */ 1601cb0ef41Sopenharmony_ci#if !defined(BROTLI_INTERNAL) 1611cb0ef41Sopenharmony_ci#if defined(_WIN32) || defined(__CYGWIN__) 1621cb0ef41Sopenharmony_ci#define BROTLI_INTERNAL 1631cb0ef41Sopenharmony_ci#elif BROTLI_GNUC_VERSION_CHECK(3, 3, 0) || \ 1641cb0ef41Sopenharmony_ci BROTLI_TI_VERSION_CHECK(8, 0, 0) || \ 1651cb0ef41Sopenharmony_ci BROTLI_INTEL_VERSION_CHECK(16, 0, 0) || \ 1661cb0ef41Sopenharmony_ci BROTLI_ARM_VERSION_CHECK(4, 1, 0) || \ 1671cb0ef41Sopenharmony_ci BROTLI_IBM_VERSION_CHECK(13, 1, 0) || \ 1681cb0ef41Sopenharmony_ci BROTLI_SUNPRO_VERSION_CHECK(5, 11, 0) || \ 1691cb0ef41Sopenharmony_ci (BROTLI_TI_VERSION_CHECK(7, 3, 0) && \ 1701cb0ef41Sopenharmony_ci defined(__TI_GNU_ATTRIBUTE_SUPPORT__) && defined(__TI_EABI__)) 1711cb0ef41Sopenharmony_ci#define BROTLI_INTERNAL __attribute__ ((visibility ("hidden"))) 1721cb0ef41Sopenharmony_ci#else 1731cb0ef41Sopenharmony_ci#define BROTLI_INTERNAL 1741cb0ef41Sopenharmony_ci#endif 1751cb0ef41Sopenharmony_ci#endif 1761cb0ef41Sopenharmony_ci 1771cb0ef41Sopenharmony_ci/* <<< <<< <<< end of hedley macros. */ 1781cb0ef41Sopenharmony_ci 1791cb0ef41Sopenharmony_ci#if BROTLI_GNUC_HAS_ATTRIBUTE(unused, 2, 7, 0) || \ 1801cb0ef41Sopenharmony_ci BROTLI_INTEL_VERSION_CHECK(16, 0, 0) 1811cb0ef41Sopenharmony_ci#define BROTLI_UNUSED_FUNCTION static BROTLI_INLINE __attribute__ ((unused)) 1821cb0ef41Sopenharmony_ci#else 1831cb0ef41Sopenharmony_ci#define BROTLI_UNUSED_FUNCTION static BROTLI_INLINE 1841cb0ef41Sopenharmony_ci#endif 1851cb0ef41Sopenharmony_ci 1861cb0ef41Sopenharmony_ci#if BROTLI_GNUC_HAS_ATTRIBUTE(aligned, 2, 7, 0) 1871cb0ef41Sopenharmony_ci#define BROTLI_ALIGNED(N) __attribute__((aligned(N))) 1881cb0ef41Sopenharmony_ci#else 1891cb0ef41Sopenharmony_ci#define BROTLI_ALIGNED(N) 1901cb0ef41Sopenharmony_ci#endif 1911cb0ef41Sopenharmony_ci 1921cb0ef41Sopenharmony_ci#if (defined(__ARM_ARCH) && (__ARM_ARCH == 7)) || \ 1931cb0ef41Sopenharmony_ci (defined(M_ARM) && (M_ARM == 7)) 1941cb0ef41Sopenharmony_ci#define BROTLI_TARGET_ARMV7 1951cb0ef41Sopenharmony_ci#endif /* ARMv7 */ 1961cb0ef41Sopenharmony_ci 1971cb0ef41Sopenharmony_ci#if (defined(__ARM_ARCH) && (__ARM_ARCH == 8)) || \ 1981cb0ef41Sopenharmony_ci defined(__aarch64__) || defined(__ARM64_ARCH_8__) 1991cb0ef41Sopenharmony_ci#define BROTLI_TARGET_ARMV8_ANY 2001cb0ef41Sopenharmony_ci 2011cb0ef41Sopenharmony_ci#if defined(__ARM_32BIT_STATE) 2021cb0ef41Sopenharmony_ci#define BROTLI_TARGET_ARMV8_32 2031cb0ef41Sopenharmony_ci#elif defined(__ARM_64BIT_STATE) 2041cb0ef41Sopenharmony_ci#define BROTLI_TARGET_ARMV8_64 2051cb0ef41Sopenharmony_ci#endif 2061cb0ef41Sopenharmony_ci 2071cb0ef41Sopenharmony_ci#endif /* ARMv8 */ 2081cb0ef41Sopenharmony_ci 2091cb0ef41Sopenharmony_ci#if defined(__ARM_NEON__) || defined(__ARM_NEON) 2101cb0ef41Sopenharmony_ci#define BROTLI_TARGET_NEON 2111cb0ef41Sopenharmony_ci#endif 2121cb0ef41Sopenharmony_ci 2131cb0ef41Sopenharmony_ci#if defined(__i386) || defined(_M_IX86) 2141cb0ef41Sopenharmony_ci#define BROTLI_TARGET_X86 2151cb0ef41Sopenharmony_ci#endif 2161cb0ef41Sopenharmony_ci 2171cb0ef41Sopenharmony_ci#if defined(__x86_64__) || defined(_M_X64) 2181cb0ef41Sopenharmony_ci#define BROTLI_TARGET_X64 2191cb0ef41Sopenharmony_ci#endif 2201cb0ef41Sopenharmony_ci 2211cb0ef41Sopenharmony_ci#if defined(__PPC64__) 2221cb0ef41Sopenharmony_ci#define BROTLI_TARGET_POWERPC64 2231cb0ef41Sopenharmony_ci#endif 2241cb0ef41Sopenharmony_ci 2251cb0ef41Sopenharmony_ci#if defined(__riscv) && defined(__riscv_xlen) && __riscv_xlen == 64 2261cb0ef41Sopenharmony_ci#define BROTLI_TARGET_RISCV64 2271cb0ef41Sopenharmony_ci#endif 2281cb0ef41Sopenharmony_ci 2291cb0ef41Sopenharmony_ci#if defined(BROTLI_BUILD_64_BIT) 2301cb0ef41Sopenharmony_ci#define BROTLI_64_BITS 1 2311cb0ef41Sopenharmony_ci#elif defined(BROTLI_BUILD_32_BIT) 2321cb0ef41Sopenharmony_ci#define BROTLI_64_BITS 0 2331cb0ef41Sopenharmony_ci#elif defined(BROTLI_TARGET_X64) || defined(BROTLI_TARGET_ARMV8_64) || \ 2341cb0ef41Sopenharmony_ci defined(BROTLI_TARGET_POWERPC64) || defined(BROTLI_TARGET_RISCV64) 2351cb0ef41Sopenharmony_ci#define BROTLI_64_BITS 1 2361cb0ef41Sopenharmony_ci#else 2371cb0ef41Sopenharmony_ci#define BROTLI_64_BITS 0 2381cb0ef41Sopenharmony_ci#endif 2391cb0ef41Sopenharmony_ci 2401cb0ef41Sopenharmony_ci#if (BROTLI_64_BITS) 2411cb0ef41Sopenharmony_ci#define brotli_reg_t uint64_t 2421cb0ef41Sopenharmony_ci#else 2431cb0ef41Sopenharmony_ci#define brotli_reg_t uint32_t 2441cb0ef41Sopenharmony_ci#endif 2451cb0ef41Sopenharmony_ci 2461cb0ef41Sopenharmony_ci#if defined(BROTLI_BUILD_BIG_ENDIAN) 2471cb0ef41Sopenharmony_ci#define BROTLI_BIG_ENDIAN 1 2481cb0ef41Sopenharmony_ci#elif defined(BROTLI_BUILD_LITTLE_ENDIAN) 2491cb0ef41Sopenharmony_ci#define BROTLI_LITTLE_ENDIAN 1 2501cb0ef41Sopenharmony_ci#elif defined(BROTLI_BUILD_ENDIAN_NEUTRAL) 2511cb0ef41Sopenharmony_ci/* Just break elif chain. */ 2521cb0ef41Sopenharmony_ci#elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) 2531cb0ef41Sopenharmony_ci#define BROTLI_LITTLE_ENDIAN 1 2541cb0ef41Sopenharmony_ci#elif defined(_WIN32) || defined(BROTLI_TARGET_X64) 2551cb0ef41Sopenharmony_ci/* Win32 & x64 can currently always be assumed to be little endian */ 2561cb0ef41Sopenharmony_ci#define BROTLI_LITTLE_ENDIAN 1 2571cb0ef41Sopenharmony_ci#elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) 2581cb0ef41Sopenharmony_ci#define BROTLI_BIG_ENDIAN 1 2591cb0ef41Sopenharmony_ci#elif defined(BROTLI_X_BYTE_ORDER) 2601cb0ef41Sopenharmony_ci#if BROTLI_X_BYTE_ORDER == BROTLI_X_LITTLE_ENDIAN 2611cb0ef41Sopenharmony_ci#define BROTLI_LITTLE_ENDIAN 1 2621cb0ef41Sopenharmony_ci#elif BROTLI_X_BYTE_ORDER == BROTLI_X_BIG_ENDIAN 2631cb0ef41Sopenharmony_ci#define BROTLI_BIG_ENDIAN 1 2641cb0ef41Sopenharmony_ci#endif 2651cb0ef41Sopenharmony_ci#endif /* BROTLI_X_BYTE_ORDER */ 2661cb0ef41Sopenharmony_ci 2671cb0ef41Sopenharmony_ci#if !defined(BROTLI_LITTLE_ENDIAN) 2681cb0ef41Sopenharmony_ci#define BROTLI_LITTLE_ENDIAN 0 2691cb0ef41Sopenharmony_ci#endif 2701cb0ef41Sopenharmony_ci 2711cb0ef41Sopenharmony_ci#if !defined(BROTLI_BIG_ENDIAN) 2721cb0ef41Sopenharmony_ci#define BROTLI_BIG_ENDIAN 0 2731cb0ef41Sopenharmony_ci#endif 2741cb0ef41Sopenharmony_ci 2751cb0ef41Sopenharmony_ci#if defined(BROTLI_X_BYTE_ORDER) 2761cb0ef41Sopenharmony_ci#undef BROTLI_X_BYTE_ORDER 2771cb0ef41Sopenharmony_ci#undef BROTLI_X_LITTLE_ENDIAN 2781cb0ef41Sopenharmony_ci#undef BROTLI_X_BIG_ENDIAN 2791cb0ef41Sopenharmony_ci#endif 2801cb0ef41Sopenharmony_ci 2811cb0ef41Sopenharmony_ci#if defined(BROTLI_BUILD_PORTABLE) 2821cb0ef41Sopenharmony_ci#define BROTLI_ALIGNED_READ (!!1) 2831cb0ef41Sopenharmony_ci#elif defined(BROTLI_TARGET_X86) || defined(BROTLI_TARGET_X64) || \ 2841cb0ef41Sopenharmony_ci defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_ANY) || \ 2851cb0ef41Sopenharmony_ci defined(BROTLI_TARGET_RISCV64) 2861cb0ef41Sopenharmony_ci/* Allow unaligned read only for white-listed CPUs. */ 2871cb0ef41Sopenharmony_ci#define BROTLI_ALIGNED_READ (!!0) 2881cb0ef41Sopenharmony_ci#else 2891cb0ef41Sopenharmony_ci#define BROTLI_ALIGNED_READ (!!1) 2901cb0ef41Sopenharmony_ci#endif 2911cb0ef41Sopenharmony_ci 2921cb0ef41Sopenharmony_ci#if BROTLI_ALIGNED_READ 2931cb0ef41Sopenharmony_ci/* Portable unaligned memory access: read / write values via memcpy. */ 2941cb0ef41Sopenharmony_cistatic BROTLI_INLINE uint16_t BrotliUnalignedRead16(const void* p) { 2951cb0ef41Sopenharmony_ci uint16_t t; 2961cb0ef41Sopenharmony_ci memcpy(&t, p, sizeof t); 2971cb0ef41Sopenharmony_ci return t; 2981cb0ef41Sopenharmony_ci} 2991cb0ef41Sopenharmony_cistatic BROTLI_INLINE uint32_t BrotliUnalignedRead32(const void* p) { 3001cb0ef41Sopenharmony_ci uint32_t t; 3011cb0ef41Sopenharmony_ci memcpy(&t, p, sizeof t); 3021cb0ef41Sopenharmony_ci return t; 3031cb0ef41Sopenharmony_ci} 3041cb0ef41Sopenharmony_cistatic BROTLI_INLINE uint64_t BrotliUnalignedRead64(const void* p) { 3051cb0ef41Sopenharmony_ci uint64_t t; 3061cb0ef41Sopenharmony_ci memcpy(&t, p, sizeof t); 3071cb0ef41Sopenharmony_ci return t; 3081cb0ef41Sopenharmony_ci} 3091cb0ef41Sopenharmony_cistatic BROTLI_INLINE void BrotliUnalignedWrite64(void* p, uint64_t v) { 3101cb0ef41Sopenharmony_ci memcpy(p, &v, sizeof v); 3111cb0ef41Sopenharmony_ci} 3121cb0ef41Sopenharmony_ci#else /* BROTLI_ALIGNED_READ */ 3131cb0ef41Sopenharmony_ci/* Unaligned memory access is allowed: just cast pointer to requested type. */ 3141cb0ef41Sopenharmony_ci#if BROTLI_SANITIZED 3151cb0ef41Sopenharmony_ci/* Consider we have an unaligned load/store of 4 bytes from address 0x...05. 3161cb0ef41Sopenharmony_ci AddressSanitizer will treat it as a 3-byte access to the range 05:07 and 3171cb0ef41Sopenharmony_ci will miss a bug if 08 is the first unaddressable byte. 3181cb0ef41Sopenharmony_ci ThreadSanitizer will also treat this as a 3-byte access to 05:07 and will 3191cb0ef41Sopenharmony_ci miss a race between this access and some other accesses to 08. 3201cb0ef41Sopenharmony_ci MemorySanitizer will correctly propagate the shadow on unaligned stores 3211cb0ef41Sopenharmony_ci and correctly report bugs on unaligned loads, but it may not properly 3221cb0ef41Sopenharmony_ci update and report the origin of the uninitialized memory. 3231cb0ef41Sopenharmony_ci For all three tools, replacing an unaligned access with a tool-specific 3241cb0ef41Sopenharmony_ci callback solves the problem. */ 3251cb0ef41Sopenharmony_ci#if defined(__cplusplus) 3261cb0ef41Sopenharmony_ciextern "C" { 3271cb0ef41Sopenharmony_ci#endif /* __cplusplus */ 3281cb0ef41Sopenharmony_ci uint16_t __sanitizer_unaligned_load16(const void* p); 3291cb0ef41Sopenharmony_ci uint32_t __sanitizer_unaligned_load32(const void* p); 3301cb0ef41Sopenharmony_ci uint64_t __sanitizer_unaligned_load64(const void* p); 3311cb0ef41Sopenharmony_ci void __sanitizer_unaligned_store64(void* p, uint64_t v); 3321cb0ef41Sopenharmony_ci#if defined(__cplusplus) 3331cb0ef41Sopenharmony_ci} /* extern "C" */ 3341cb0ef41Sopenharmony_ci#endif /* __cplusplus */ 3351cb0ef41Sopenharmony_ci#define BrotliUnalignedRead16 __sanitizer_unaligned_load16 3361cb0ef41Sopenharmony_ci#define BrotliUnalignedRead32 __sanitizer_unaligned_load32 3371cb0ef41Sopenharmony_ci#define BrotliUnalignedRead64 __sanitizer_unaligned_load64 3381cb0ef41Sopenharmony_ci#define BrotliUnalignedWrite64 __sanitizer_unaligned_store64 3391cb0ef41Sopenharmony_ci#else /* BROTLI_SANITIZED */ 3401cb0ef41Sopenharmony_cistatic BROTLI_INLINE uint16_t BrotliUnalignedRead16(const void* p) { 3411cb0ef41Sopenharmony_ci return *(const uint16_t*)p; 3421cb0ef41Sopenharmony_ci} 3431cb0ef41Sopenharmony_cistatic BROTLI_INLINE uint32_t BrotliUnalignedRead32(const void* p) { 3441cb0ef41Sopenharmony_ci return *(const uint32_t*)p; 3451cb0ef41Sopenharmony_ci} 3461cb0ef41Sopenharmony_ci#if (BROTLI_64_BITS) 3471cb0ef41Sopenharmony_cistatic BROTLI_INLINE uint64_t BrotliUnalignedRead64(const void* p) { 3481cb0ef41Sopenharmony_ci return *(const uint64_t*)p; 3491cb0ef41Sopenharmony_ci} 3501cb0ef41Sopenharmony_cistatic BROTLI_INLINE void BrotliUnalignedWrite64(void* p, uint64_t v) { 3511cb0ef41Sopenharmony_ci *(uint64_t*)p = v; 3521cb0ef41Sopenharmony_ci} 3531cb0ef41Sopenharmony_ci#else /* BROTLI_64_BITS */ 3541cb0ef41Sopenharmony_ci/* Avoid emitting LDRD / STRD, which require properly aligned address. */ 3551cb0ef41Sopenharmony_ci/* If __attribute__(aligned) is available, use that. Otherwise, memcpy. */ 3561cb0ef41Sopenharmony_ci 3571cb0ef41Sopenharmony_ci#if BROTLI_GNUC_HAS_ATTRIBUTE(aligned, 2, 7, 0) 3581cb0ef41Sopenharmony_citypedef BROTLI_ALIGNED(1) uint64_t brotli_unaligned_uint64_t; 3591cb0ef41Sopenharmony_ci 3601cb0ef41Sopenharmony_cistatic BROTLI_INLINE uint64_t BrotliUnalignedRead64(const void* p) { 3611cb0ef41Sopenharmony_ci return (uint64_t) ((const brotli_unaligned_uint64_t*) p)[0]; 3621cb0ef41Sopenharmony_ci} 3631cb0ef41Sopenharmony_cistatic BROTLI_INLINE void BrotliUnalignedWrite64(void* p, uint64_t v) { 3641cb0ef41Sopenharmony_ci brotli_unaligned_uint64_t* dwords = (brotli_unaligned_uint64_t*) p; 3651cb0ef41Sopenharmony_ci dwords[0] = (brotli_unaligned_uint64_t) v; 3661cb0ef41Sopenharmony_ci} 3671cb0ef41Sopenharmony_ci#else /* BROTLI_GNUC_HAS_ATTRIBUTE(aligned, 2, 7, 0) */ 3681cb0ef41Sopenharmony_cistatic BROTLI_INLINE uint64_t BrotliUnalignedRead64(const void* p) { 3691cb0ef41Sopenharmony_ci uint64_t v; 3701cb0ef41Sopenharmony_ci memcpy(&v, p, sizeof(uint64_t)); 3711cb0ef41Sopenharmony_ci return v; 3721cb0ef41Sopenharmony_ci} 3731cb0ef41Sopenharmony_ci 3741cb0ef41Sopenharmony_cistatic BROTLI_INLINE void BrotliUnalignedWrite64(void* p, uint64_t v) { 3751cb0ef41Sopenharmony_ci memcpy(p, &v, sizeof(uint64_t)); 3761cb0ef41Sopenharmony_ci} 3771cb0ef41Sopenharmony_ci#endif /* BROTLI_GNUC_HAS_ATTRIBUTE(aligned, 2, 7, 0) */ 3781cb0ef41Sopenharmony_ci#endif /* BROTLI_64_BITS */ 3791cb0ef41Sopenharmony_ci#endif /* BROTLI_SANITIZED */ 3801cb0ef41Sopenharmony_ci#endif /* BROTLI_ALIGNED_READ */ 3811cb0ef41Sopenharmony_ci 3821cb0ef41Sopenharmony_ci#if BROTLI_LITTLE_ENDIAN 3831cb0ef41Sopenharmony_ci/* Straight endianness. Just read / write values. */ 3841cb0ef41Sopenharmony_ci#define BROTLI_UNALIGNED_LOAD16LE BrotliUnalignedRead16 3851cb0ef41Sopenharmony_ci#define BROTLI_UNALIGNED_LOAD32LE BrotliUnalignedRead32 3861cb0ef41Sopenharmony_ci#define BROTLI_UNALIGNED_LOAD64LE BrotliUnalignedRead64 3871cb0ef41Sopenharmony_ci#define BROTLI_UNALIGNED_STORE64LE BrotliUnalignedWrite64 3881cb0ef41Sopenharmony_ci#elif BROTLI_BIG_ENDIAN /* BROTLI_LITTLE_ENDIAN */ 3891cb0ef41Sopenharmony_ci/* Explain compiler to byte-swap values. */ 3901cb0ef41Sopenharmony_ci#define BROTLI_BSWAP16_(V) ((uint16_t)( \ 3911cb0ef41Sopenharmony_ci (((V) & 0xFFU) << 8) | \ 3921cb0ef41Sopenharmony_ci (((V) >> 8) & 0xFFU))) 3931cb0ef41Sopenharmony_cistatic BROTLI_INLINE uint16_t BROTLI_UNALIGNED_LOAD16LE(const void* p) { 3941cb0ef41Sopenharmony_ci uint16_t value = BrotliUnalignedRead16(p); 3951cb0ef41Sopenharmony_ci return BROTLI_BSWAP16_(value); 3961cb0ef41Sopenharmony_ci} 3971cb0ef41Sopenharmony_ci#define BROTLI_BSWAP32_(V) ( \ 3981cb0ef41Sopenharmony_ci (((V) & 0xFFU) << 24) | (((V) & 0xFF00U) << 8) | \ 3991cb0ef41Sopenharmony_ci (((V) >> 8) & 0xFF00U) | (((V) >> 24) & 0xFFU)) 4001cb0ef41Sopenharmony_cistatic BROTLI_INLINE uint32_t BROTLI_UNALIGNED_LOAD32LE(const void* p) { 4011cb0ef41Sopenharmony_ci uint32_t value = BrotliUnalignedRead32(p); 4021cb0ef41Sopenharmony_ci return BROTLI_BSWAP32_(value); 4031cb0ef41Sopenharmony_ci} 4041cb0ef41Sopenharmony_ci#define BROTLI_BSWAP64_(V) ( \ 4051cb0ef41Sopenharmony_ci (((V) & 0xFFU) << 56) | (((V) & 0xFF00U) << 40) | \ 4061cb0ef41Sopenharmony_ci (((V) & 0xFF0000U) << 24) | (((V) & 0xFF000000U) << 8) | \ 4071cb0ef41Sopenharmony_ci (((V) >> 8) & 0xFF000000U) | (((V) >> 24) & 0xFF0000U) | \ 4081cb0ef41Sopenharmony_ci (((V) >> 40) & 0xFF00U) | (((V) >> 56) & 0xFFU)) 4091cb0ef41Sopenharmony_cistatic BROTLI_INLINE uint64_t BROTLI_UNALIGNED_LOAD64LE(const void* p) { 4101cb0ef41Sopenharmony_ci uint64_t value = BrotliUnalignedRead64(p); 4111cb0ef41Sopenharmony_ci return BROTLI_BSWAP64_(value); 4121cb0ef41Sopenharmony_ci} 4131cb0ef41Sopenharmony_cistatic BROTLI_INLINE void BROTLI_UNALIGNED_STORE64LE(void* p, uint64_t v) { 4141cb0ef41Sopenharmony_ci uint64_t value = BROTLI_BSWAP64_(v); 4151cb0ef41Sopenharmony_ci BrotliUnalignedWrite64(p, value); 4161cb0ef41Sopenharmony_ci} 4171cb0ef41Sopenharmony_ci#else /* BROTLI_LITTLE_ENDIAN */ 4181cb0ef41Sopenharmony_ci/* Read / store values byte-wise; hopefully compiler will understand. */ 4191cb0ef41Sopenharmony_cistatic BROTLI_INLINE uint16_t BROTLI_UNALIGNED_LOAD16LE(const void* p) { 4201cb0ef41Sopenharmony_ci const uint8_t* in = (const uint8_t*)p; 4211cb0ef41Sopenharmony_ci return (uint16_t)(in[0] | (in[1] << 8)); 4221cb0ef41Sopenharmony_ci} 4231cb0ef41Sopenharmony_cistatic BROTLI_INLINE uint32_t BROTLI_UNALIGNED_LOAD32LE(const void* p) { 4241cb0ef41Sopenharmony_ci const uint8_t* in = (const uint8_t*)p; 4251cb0ef41Sopenharmony_ci uint32_t value = (uint32_t)(in[0]); 4261cb0ef41Sopenharmony_ci value |= (uint32_t)(in[1]) << 8; 4271cb0ef41Sopenharmony_ci value |= (uint32_t)(in[2]) << 16; 4281cb0ef41Sopenharmony_ci value |= (uint32_t)(in[3]) << 24; 4291cb0ef41Sopenharmony_ci return value; 4301cb0ef41Sopenharmony_ci} 4311cb0ef41Sopenharmony_cistatic BROTLI_INLINE uint64_t BROTLI_UNALIGNED_LOAD64LE(const void* p) { 4321cb0ef41Sopenharmony_ci const uint8_t* in = (const uint8_t*)p; 4331cb0ef41Sopenharmony_ci uint64_t value = (uint64_t)(in[0]); 4341cb0ef41Sopenharmony_ci value |= (uint64_t)(in[1]) << 8; 4351cb0ef41Sopenharmony_ci value |= (uint64_t)(in[2]) << 16; 4361cb0ef41Sopenharmony_ci value |= (uint64_t)(in[3]) << 24; 4371cb0ef41Sopenharmony_ci value |= (uint64_t)(in[4]) << 32; 4381cb0ef41Sopenharmony_ci value |= (uint64_t)(in[5]) << 40; 4391cb0ef41Sopenharmony_ci value |= (uint64_t)(in[6]) << 48; 4401cb0ef41Sopenharmony_ci value |= (uint64_t)(in[7]) << 56; 4411cb0ef41Sopenharmony_ci return value; 4421cb0ef41Sopenharmony_ci} 4431cb0ef41Sopenharmony_cistatic BROTLI_INLINE void BROTLI_UNALIGNED_STORE64LE(void* p, uint64_t v) { 4441cb0ef41Sopenharmony_ci uint8_t* out = (uint8_t*)p; 4451cb0ef41Sopenharmony_ci out[0] = (uint8_t)v; 4461cb0ef41Sopenharmony_ci out[1] = (uint8_t)(v >> 8); 4471cb0ef41Sopenharmony_ci out[2] = (uint8_t)(v >> 16); 4481cb0ef41Sopenharmony_ci out[3] = (uint8_t)(v >> 24); 4491cb0ef41Sopenharmony_ci out[4] = (uint8_t)(v >> 32); 4501cb0ef41Sopenharmony_ci out[5] = (uint8_t)(v >> 40); 4511cb0ef41Sopenharmony_ci out[6] = (uint8_t)(v >> 48); 4521cb0ef41Sopenharmony_ci out[7] = (uint8_t)(v >> 56); 4531cb0ef41Sopenharmony_ci} 4541cb0ef41Sopenharmony_ci#endif /* BROTLI_LITTLE_ENDIAN */ 4551cb0ef41Sopenharmony_ci 4561cb0ef41Sopenharmony_ci/* BROTLI_IS_CONSTANT macros returns true for compile-time constants. */ 4571cb0ef41Sopenharmony_ci#if BROTLI_GNUC_HAS_BUILTIN(__builtin_constant_p, 3, 0, 1) || \ 4581cb0ef41Sopenharmony_ci BROTLI_INTEL_VERSION_CHECK(16, 0, 0) 4591cb0ef41Sopenharmony_ci#define BROTLI_IS_CONSTANT(x) (!!__builtin_constant_p(x)) 4601cb0ef41Sopenharmony_ci#else 4611cb0ef41Sopenharmony_ci#define BROTLI_IS_CONSTANT(x) (!!0) 4621cb0ef41Sopenharmony_ci#endif 4631cb0ef41Sopenharmony_ci 4641cb0ef41Sopenharmony_ci#if defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_ANY) 4651cb0ef41Sopenharmony_ci#define BROTLI_HAS_UBFX (!!1) 4661cb0ef41Sopenharmony_ci#else 4671cb0ef41Sopenharmony_ci#define BROTLI_HAS_UBFX (!!0) 4681cb0ef41Sopenharmony_ci#endif 4691cb0ef41Sopenharmony_ci 4701cb0ef41Sopenharmony_ci#if defined(BROTLI_ENABLE_LOG) 4711cb0ef41Sopenharmony_ci#define BROTLI_LOG(x) printf x 4721cb0ef41Sopenharmony_ci#else 4731cb0ef41Sopenharmony_ci#define BROTLI_LOG(x) 4741cb0ef41Sopenharmony_ci#endif 4751cb0ef41Sopenharmony_ci 4761cb0ef41Sopenharmony_ci#if defined(BROTLI_DEBUG) || defined(BROTLI_ENABLE_LOG) 4771cb0ef41Sopenharmony_ci#define BROTLI_DCHECK(x) assert(x) 4781cb0ef41Sopenharmony_cistatic BROTLI_INLINE void BrotliDump(const char* f, int l, const char* fn) { 4791cb0ef41Sopenharmony_ci fprintf(stderr, "%s:%d (%s)\n", f, l, fn); 4801cb0ef41Sopenharmony_ci fflush(stderr); 4811cb0ef41Sopenharmony_ci} 4821cb0ef41Sopenharmony_ci#define BROTLI_DUMP() BrotliDump(__FILE__, __LINE__, __FUNCTION__) 4831cb0ef41Sopenharmony_ci#else 4841cb0ef41Sopenharmony_ci#define BROTLI_DCHECK(x) 4851cb0ef41Sopenharmony_ci#define BROTLI_DUMP() (void)(0) 4861cb0ef41Sopenharmony_ci#endif 4871cb0ef41Sopenharmony_ci 4881cb0ef41Sopenharmony_ci/* TODO: add appropriate icc/sunpro/arm/ibm/ti checks. */ 4891cb0ef41Sopenharmony_ci#if (BROTLI_GNUC_VERSION_CHECK(3, 0, 0) || defined(__llvm__)) && \ 4901cb0ef41Sopenharmony_ci !defined(BROTLI_BUILD_NO_RBIT) 4911cb0ef41Sopenharmony_ci#if defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_ANY) 4921cb0ef41Sopenharmony_ci/* TODO: detect ARMv6T2 and enable this code for it. */ 4931cb0ef41Sopenharmony_cistatic BROTLI_INLINE brotli_reg_t BrotliRBit(brotli_reg_t input) { 4941cb0ef41Sopenharmony_ci brotli_reg_t output; 4951cb0ef41Sopenharmony_ci __asm__("rbit %0, %1\n" : "=r"(output) : "r"(input)); 4961cb0ef41Sopenharmony_ci return output; 4971cb0ef41Sopenharmony_ci} 4981cb0ef41Sopenharmony_ci#define BROTLI_RBIT(x) BrotliRBit(x) 4991cb0ef41Sopenharmony_ci#endif /* armv7 / armv8 */ 5001cb0ef41Sopenharmony_ci#endif /* gcc || clang */ 5011cb0ef41Sopenharmony_ci#if !defined(BROTLI_RBIT) 5021cb0ef41Sopenharmony_cistatic BROTLI_INLINE void BrotliRBit(void) { /* Should break build if used. */ } 5031cb0ef41Sopenharmony_ci#endif /* BROTLI_RBIT */ 5041cb0ef41Sopenharmony_ci 5051cb0ef41Sopenharmony_ci#define BROTLI_REPEAT(N, X) { \ 5061cb0ef41Sopenharmony_ci if ((N & 1) != 0) {X;} \ 5071cb0ef41Sopenharmony_ci if ((N & 2) != 0) {X; X;} \ 5081cb0ef41Sopenharmony_ci if ((N & 4) != 0) {X; X; X; X;} \ 5091cb0ef41Sopenharmony_ci} 5101cb0ef41Sopenharmony_ci 5111cb0ef41Sopenharmony_ci#define BROTLI_UNUSED(X) (void)(X) 5121cb0ef41Sopenharmony_ci 5131cb0ef41Sopenharmony_ci#define BROTLI_MIN_MAX(T) \ 5141cb0ef41Sopenharmony_ci static BROTLI_INLINE T brotli_min_ ## T (T a, T b) { return a < b ? a : b; } \ 5151cb0ef41Sopenharmony_ci static BROTLI_INLINE T brotli_max_ ## T (T a, T b) { return a > b ? a : b; } 5161cb0ef41Sopenharmony_ciBROTLI_MIN_MAX(double) BROTLI_MIN_MAX(float) BROTLI_MIN_MAX(int) 5171cb0ef41Sopenharmony_ciBROTLI_MIN_MAX(size_t) BROTLI_MIN_MAX(uint32_t) BROTLI_MIN_MAX(uint8_t) 5181cb0ef41Sopenharmony_ci#undef BROTLI_MIN_MAX 5191cb0ef41Sopenharmony_ci#define BROTLI_MIN(T, A, B) (brotli_min_ ## T((A), (B))) 5201cb0ef41Sopenharmony_ci#define BROTLI_MAX(T, A, B) (brotli_max_ ## T((A), (B))) 5211cb0ef41Sopenharmony_ci 5221cb0ef41Sopenharmony_ci#define BROTLI_SWAP(T, A, I, J) { \ 5231cb0ef41Sopenharmony_ci T __brotli_swap_tmp = (A)[(I)]; \ 5241cb0ef41Sopenharmony_ci (A)[(I)] = (A)[(J)]; \ 5251cb0ef41Sopenharmony_ci (A)[(J)] = __brotli_swap_tmp; \ 5261cb0ef41Sopenharmony_ci} 5271cb0ef41Sopenharmony_ci 5281cb0ef41Sopenharmony_ci#if BROTLI_64_BITS 5291cb0ef41Sopenharmony_ci#if BROTLI_GNUC_HAS_BUILTIN(__builtin_ctzll, 3, 4, 0) || \ 5301cb0ef41Sopenharmony_ci BROTLI_INTEL_VERSION_CHECK(16, 0, 0) 5311cb0ef41Sopenharmony_ci#define BROTLI_TZCNT64 __builtin_ctzll 5321cb0ef41Sopenharmony_ci#elif BROTLI_MSVC_VERSION_CHECK(12, 0, 0) 5331cb0ef41Sopenharmony_ci#if defined(BROTLI_TARGET_X64) 5341cb0ef41Sopenharmony_ci#define BROTLI_TZCNT64 _tzcnt_u64 5351cb0ef41Sopenharmony_ci#else /* BROTLI_TARGET_X64 */ 5361cb0ef41Sopenharmony_cistatic BROTLI_INLINE uint32_t BrotliBsf64Msvc(uint64_t x) { 5371cb0ef41Sopenharmony_ci uint32_t lsb; 5381cb0ef41Sopenharmony_ci _BitScanForward64(&lsb, x); 5391cb0ef41Sopenharmony_ci return lsb; 5401cb0ef41Sopenharmony_ci} 5411cb0ef41Sopenharmony_ci#define BROTLI_TZCNT64 BrotliBsf64Msvc 5421cb0ef41Sopenharmony_ci#endif /* BROTLI_TARGET_X64 */ 5431cb0ef41Sopenharmony_ci#endif /* __builtin_ctzll */ 5441cb0ef41Sopenharmony_ci#endif /* BROTLI_64_BITS */ 5451cb0ef41Sopenharmony_ci 5461cb0ef41Sopenharmony_ci#if BROTLI_GNUC_HAS_BUILTIN(__builtin_clz, 3, 4, 0) || \ 5471cb0ef41Sopenharmony_ci BROTLI_INTEL_VERSION_CHECK(16, 0, 0) 5481cb0ef41Sopenharmony_ci#define BROTLI_BSR32(x) (31u ^ (uint32_t)__builtin_clz(x)) 5491cb0ef41Sopenharmony_ci#elif BROTLI_MSVC_VERSION_CHECK(12, 0, 0) 5501cb0ef41Sopenharmony_cistatic BROTLI_INLINE uint32_t BrotliBsr32Msvc(uint32_t x) { 5511cb0ef41Sopenharmony_ci unsigned long msb; 5521cb0ef41Sopenharmony_ci _BitScanReverse(&msb, x); 5531cb0ef41Sopenharmony_ci return (uint32_t)msb; 5541cb0ef41Sopenharmony_ci} 5551cb0ef41Sopenharmony_ci#define BROTLI_BSR32 BrotliBsr32Msvc 5561cb0ef41Sopenharmony_ci#endif /* __builtin_clz */ 5571cb0ef41Sopenharmony_ci 5581cb0ef41Sopenharmony_ci/* Default brotli_alloc_func */ 5591cb0ef41Sopenharmony_ciBROTLI_COMMON_API void* BrotliDefaultAllocFunc(void* opaque, size_t size); 5601cb0ef41Sopenharmony_ci 5611cb0ef41Sopenharmony_ci/* Default brotli_free_func */ 5621cb0ef41Sopenharmony_ciBROTLI_COMMON_API void BrotliDefaultFreeFunc(void* opaque, void* address); 5631cb0ef41Sopenharmony_ci 5641cb0ef41Sopenharmony_ciBROTLI_UNUSED_FUNCTION void BrotliSuppressUnusedFunctions(void) { 5651cb0ef41Sopenharmony_ci BROTLI_UNUSED(&BrotliSuppressUnusedFunctions); 5661cb0ef41Sopenharmony_ci BROTLI_UNUSED(&BrotliUnalignedRead16); 5671cb0ef41Sopenharmony_ci BROTLI_UNUSED(&BrotliUnalignedRead32); 5681cb0ef41Sopenharmony_ci BROTLI_UNUSED(&BrotliUnalignedRead64); 5691cb0ef41Sopenharmony_ci BROTLI_UNUSED(&BrotliUnalignedWrite64); 5701cb0ef41Sopenharmony_ci BROTLI_UNUSED(&BROTLI_UNALIGNED_LOAD16LE); 5711cb0ef41Sopenharmony_ci BROTLI_UNUSED(&BROTLI_UNALIGNED_LOAD32LE); 5721cb0ef41Sopenharmony_ci BROTLI_UNUSED(&BROTLI_UNALIGNED_LOAD64LE); 5731cb0ef41Sopenharmony_ci BROTLI_UNUSED(&BROTLI_UNALIGNED_STORE64LE); 5741cb0ef41Sopenharmony_ci BROTLI_UNUSED(&BrotliRBit); 5751cb0ef41Sopenharmony_ci BROTLI_UNUSED(&brotli_min_double); 5761cb0ef41Sopenharmony_ci BROTLI_UNUSED(&brotli_max_double); 5771cb0ef41Sopenharmony_ci BROTLI_UNUSED(&brotli_min_float); 5781cb0ef41Sopenharmony_ci BROTLI_UNUSED(&brotli_max_float); 5791cb0ef41Sopenharmony_ci BROTLI_UNUSED(&brotli_min_int); 5801cb0ef41Sopenharmony_ci BROTLI_UNUSED(&brotli_max_int); 5811cb0ef41Sopenharmony_ci BROTLI_UNUSED(&brotli_min_size_t); 5821cb0ef41Sopenharmony_ci BROTLI_UNUSED(&brotli_max_size_t); 5831cb0ef41Sopenharmony_ci BROTLI_UNUSED(&brotli_min_uint32_t); 5841cb0ef41Sopenharmony_ci BROTLI_UNUSED(&brotli_max_uint32_t); 5851cb0ef41Sopenharmony_ci BROTLI_UNUSED(&brotli_min_uint8_t); 5861cb0ef41Sopenharmony_ci BROTLI_UNUSED(&brotli_max_uint8_t); 5871cb0ef41Sopenharmony_ci BROTLI_UNUSED(&BrotliDefaultAllocFunc); 5881cb0ef41Sopenharmony_ci BROTLI_UNUSED(&BrotliDefaultFreeFunc); 5891cb0ef41Sopenharmony_ci#if defined(BROTLI_DEBUG) || defined(BROTLI_ENABLE_LOG) 5901cb0ef41Sopenharmony_ci BROTLI_UNUSED(&BrotliDump); 5911cb0ef41Sopenharmony_ci#endif 5921cb0ef41Sopenharmony_ci} 5931cb0ef41Sopenharmony_ci 5941cb0ef41Sopenharmony_ci#endif /* BROTLI_COMMON_PLATFORM_H_ */ 595