Path: blob/master/thirdparty/brotli/common/platform.h
21438 views
/* Copyright 2016 Google Inc. All Rights Reserved.12Distributed under MIT license.3See file LICENSE for detail or copy at https://opensource.org/licenses/MIT4*/56/* Macros for compiler / platform specific features and build options.78Build options are:9* BROTLI_BUILD_32_BIT disables 64-bit optimizations10* BROTLI_BUILD_64_BIT forces to use 64-bit optimizations11* BROTLI_BUILD_BIG_ENDIAN forces to use big-endian optimizations12* BROTLI_BUILD_ENDIAN_NEUTRAL disables endian-aware optimizations13* BROTLI_BUILD_LITTLE_ENDIAN forces to use little-endian optimizations14* BROTLI_BUILD_NO_RBIT disables "rbit" optimization for ARM CPUs15* BROTLI_BUILD_NO_UNALIGNED_READ_FAST forces off the fast-unaligned-read16optimizations (mainly for testing purposes)17* BROTLI_DEBUG dumps file name and line number when decoder detects stream18or memory error19* BROTLI_ENABLE_LOG enables asserts and dumps various state information20* BROTLI_ENABLE_DUMP overrides default "dump" behaviour21*/2223#ifndef BROTLI_COMMON_PLATFORM_H_24#define BROTLI_COMMON_PLATFORM_H_2526#include <string.h> /* IWYU pragma: export memcmp, memcpy, memset */27#include <stdlib.h> /* IWYU pragma: export exit, free, malloc */28#include <sys/types.h> /* should include endian.h for us */2930#include <brotli/port.h> /* IWYU pragma: export */31#include <brotli/types.h> /* IWYU pragma: export */3233#if BROTLI_MSVC_VERSION_CHECK(18, 0, 0)34#include <intrin.h>35#endif3637#if defined(BROTLI_ENABLE_LOG) || defined(BROTLI_DEBUG)38#include <assert.h>39#include <stdio.h>40#endif4142/* The following macros were borrowed from https://github.com/nemequ/hedley43* with permission of original author - Evan Nemerson <[email protected]> */4445/* >>> >>> >>> hedley macros */4647/* Define "BROTLI_PREDICT_TRUE" and "BROTLI_PREDICT_FALSE" macros for capable48compilers.4950To apply compiler hint, enclose the branching condition into macros, like this:5152if (BROTLI_PREDICT_TRUE(zero == 0)) {53// main execution path54} else {55// compiler should place this code outside of main execution path56}5758OR:5960if (BROTLI_PREDICT_FALSE(something_rare_or_unexpected_happens)) {61// compiler should place this code outside of main execution path62}6364*/65#if BROTLI_GNUC_HAS_BUILTIN(__builtin_expect, 3, 0, 0) || \66BROTLI_INTEL_VERSION_CHECK(16, 0, 0) || \67BROTLI_SUNPRO_VERSION_CHECK(5, 15, 0) || \68BROTLI_ARM_VERSION_CHECK(4, 1, 0) || \69BROTLI_IBM_VERSION_CHECK(10, 1, 0) || \70BROTLI_TI_VERSION_CHECK(7, 3, 0) || \71BROTLI_TINYC_VERSION_CHECK(0, 9, 27)72#define BROTLI_PREDICT_TRUE(x) (__builtin_expect(!!(x), 1))73#define BROTLI_PREDICT_FALSE(x) (__builtin_expect(x, 0))74#else75#define BROTLI_PREDICT_FALSE(x) (x)76#define BROTLI_PREDICT_TRUE(x) (x)77#endif7879#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \80!defined(__cplusplus)81#define BROTLI_RESTRICT restrict82#elif BROTLI_GNUC_VERSION_CHECK(3, 1, 0) || \83BROTLI_MSVC_VERSION_CHECK(14, 0, 0) || \84BROTLI_INTEL_VERSION_CHECK(16, 0, 0) || \85BROTLI_ARM_VERSION_CHECK(4, 1, 0) || \86BROTLI_IBM_VERSION_CHECK(10, 1, 0) || \87BROTLI_PGI_VERSION_CHECK(17, 10, 0) || \88BROTLI_TI_VERSION_CHECK(8, 0, 0) || \89BROTLI_IAR_VERSION_CHECK(8, 0, 0) || \90(BROTLI_SUNPRO_VERSION_CHECK(5, 14, 0) && defined(__cplusplus))91#define BROTLI_RESTRICT __restrict92#elif BROTLI_SUNPRO_VERSION_CHECK(5, 3, 0) && !defined(__cplusplus)93#define BROTLI_RESTRICT _Restrict94#else95#define BROTLI_RESTRICT96#endif9798#if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \99(defined(__cplusplus) && (__cplusplus >= 199711L))100#define BROTLI_MAYBE_INLINE inline101#elif defined(__GNUC_STDC_INLINE__) || defined(__GNUC_GNU_INLINE__) || \102BROTLI_ARM_VERSION_CHECK(6, 2, 0)103#define BROTLI_MAYBE_INLINE __inline__104#elif BROTLI_MSVC_VERSION_CHECK(12, 0, 0) || \105BROTLI_ARM_VERSION_CHECK(4, 1, 0) || BROTLI_TI_VERSION_CHECK(8, 0, 0)106#define BROTLI_MAYBE_INLINE __inline107#else108#define BROTLI_MAYBE_INLINE109#endif110111#if BROTLI_GNUC_HAS_ATTRIBUTE(always_inline, 4, 0, 0) || \112BROTLI_INTEL_VERSION_CHECK(16, 0, 0) || \113BROTLI_SUNPRO_VERSION_CHECK(5, 11, 0) || \114BROTLI_ARM_VERSION_CHECK(4, 1, 0) || \115BROTLI_IBM_VERSION_CHECK(10, 1, 0) || \116BROTLI_TI_VERSION_CHECK(8, 0, 0) || \117(BROTLI_TI_VERSION_CHECK(7, 3, 0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__))118#define BROTLI_INLINE BROTLI_MAYBE_INLINE __attribute__((__always_inline__))119#elif BROTLI_MSVC_VERSION_CHECK(12, 0, 0)120#define BROTLI_INLINE BROTLI_MAYBE_INLINE __forceinline121#elif BROTLI_TI_VERSION_CHECK(7, 0, 0) && defined(__cplusplus)122#define BROTLI_INLINE BROTLI_MAYBE_INLINE _Pragma("FUNC_ALWAYS_INLINE;")123#elif BROTLI_IAR_VERSION_CHECK(8, 0, 0)124#define BROTLI_INLINE BROTLI_MAYBE_INLINE _Pragma("inline=forced")125#else126#define BROTLI_INLINE BROTLI_MAYBE_INLINE127#endif128129#if BROTLI_GNUC_HAS_ATTRIBUTE(noinline, 4, 0, 0) || \130BROTLI_INTEL_VERSION_CHECK(16, 0, 0) || \131BROTLI_SUNPRO_VERSION_CHECK(5, 11, 0) || \132BROTLI_ARM_VERSION_CHECK(4, 1, 0) || \133BROTLI_IBM_VERSION_CHECK(10, 1, 0) || \134BROTLI_TI_VERSION_CHECK(8, 0, 0) || \135(BROTLI_TI_VERSION_CHECK(7, 3, 0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__))136#define BROTLI_NOINLINE __attribute__((__noinline__))137#elif BROTLI_MSVC_VERSION_CHECK(13, 10, 0)138#define BROTLI_NOINLINE __declspec(noinline)139#elif BROTLI_PGI_VERSION_CHECK(10, 2, 0)140#define BROTLI_NOINLINE _Pragma("noinline")141#elif BROTLI_TI_VERSION_CHECK(6, 0, 0) && defined(__cplusplus)142#define BROTLI_NOINLINE _Pragma("FUNC_CANNOT_INLINE;")143#elif BROTLI_IAR_VERSION_CHECK(8, 0, 0)144#define BROTLI_NOINLINE _Pragma("inline=never")145#else146#define BROTLI_NOINLINE147#endif148149/* <<< <<< <<< end of hedley macros. */150151#if BROTLI_GNUC_HAS_ATTRIBUTE(unused, 2, 7, 0) || \152BROTLI_INTEL_VERSION_CHECK(16, 0, 0)153#define BROTLI_UNUSED_FUNCTION static BROTLI_INLINE __attribute__ ((unused))154#else155#define BROTLI_UNUSED_FUNCTION static BROTLI_INLINE156#endif157158#if BROTLI_GNUC_HAS_ATTRIBUTE(aligned, 2, 7, 0)159#define BROTLI_ALIGNED(N) __attribute__((aligned(N)))160#else161#define BROTLI_ALIGNED(N)162#endif163164#if (defined(__ARM_ARCH) && (__ARM_ARCH == 7)) || \165(defined(M_ARM) && (M_ARM == 7))166#define BROTLI_TARGET_ARMV7167#endif /* ARMv7 */168169#if (defined(__ARM_ARCH) && (__ARM_ARCH == 8)) || \170defined(__aarch64__) || defined(__ARM64_ARCH_8__)171#define BROTLI_TARGET_ARMV8_ANY172173#if defined(__ARM_32BIT_STATE)174#define BROTLI_TARGET_ARMV8_32175#elif defined(__ARM_64BIT_STATE)176#define BROTLI_TARGET_ARMV8_64177#endif178179#endif /* ARMv8 */180181#if defined(__ARM_NEON__) || defined(__ARM_NEON)182#define BROTLI_TARGET_NEON183#endif184185#if defined(__i386) || defined(_M_IX86)186#define BROTLI_TARGET_X86187#endif188189#if defined(__x86_64__) || defined(_M_X64)190#define BROTLI_TARGET_X64191#endif192193#if defined(__PPC64__)194#define BROTLI_TARGET_POWERPC64195#endif196197#if defined(__riscv) && defined(__riscv_xlen) && __riscv_xlen == 64198#define BROTLI_TARGET_RISCV64199#endif200201#if defined(__loongarch_lp64)202#define BROTLI_TARGET_LOONGARCH64203#endif204205/* This does not seem to be an indicator of z/Architecture (64-bit); neither206that allows to use unaligned loads. */207#if defined(__s390x__)208#define BROTLI_TARGET_S390X209#endif210211#if defined(__mips64)212#define BROTLI_TARGET_MIPS64213#endif214215#if defined(__ia64__) || defined(_M_IA64)216#define BROTLI_TARGET_IA64217#endif218219#if defined(BROTLI_TARGET_X64) || defined(BROTLI_TARGET_ARMV8_64) || \220defined(BROTLI_TARGET_POWERPC64) || defined(BROTLI_TARGET_RISCV64) || \221defined(BROTLI_TARGET_LOONGARCH64) || defined(BROTLI_TARGET_MIPS64)222#define BROTLI_TARGET_64_BITS 1223#else224#define BROTLI_TARGET_64_BITS 0225#endif226227#if defined(BROTLI_BUILD_64_BIT)228#define BROTLI_64_BITS 1229#elif defined(BROTLI_BUILD_32_BIT)230#define BROTLI_64_BITS 0231#else232#define BROTLI_64_BITS BROTLI_TARGET_64_BITS233#endif234235#if (BROTLI_64_BITS)236#define brotli_reg_t uint64_t237#else238#define brotli_reg_t uint32_t239#endif240241#if defined(BROTLI_BUILD_BIG_ENDIAN)242#define BROTLI_BIG_ENDIAN 1243#elif defined(BROTLI_BUILD_LITTLE_ENDIAN)244#define BROTLI_LITTLE_ENDIAN 1245#elif defined(BROTLI_BUILD_ENDIAN_NEUTRAL)246/* Just break elif chain. */247#elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)248#define BROTLI_LITTLE_ENDIAN 1249#elif defined(_WIN32) || defined(BROTLI_TARGET_X64)250/* Win32 & x64 can currently always be assumed to be little endian */251#define BROTLI_LITTLE_ENDIAN 1252#elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)253#define BROTLI_BIG_ENDIAN 1254/* Likely target platform is iOS / OSX. */255#elif defined(BYTE_ORDER) && (BYTE_ORDER == LITTLE_ENDIAN)256#define BROTLI_LITTLE_ENDIAN 1257#elif defined(BYTE_ORDER) && (BYTE_ORDER == BIG_ENDIAN)258#define BROTLI_BIG_ENDIAN 1259#endif260261#if !defined(BROTLI_LITTLE_ENDIAN)262#define BROTLI_LITTLE_ENDIAN 0263#endif264265#if !defined(BROTLI_BIG_ENDIAN)266#define BROTLI_BIG_ENDIAN 0267#endif268269#if defined(BROTLI_BUILD_NO_UNALIGNED_READ_FAST)270#define BROTLI_UNALIGNED_READ_FAST (!!0)271#elif defined(BROTLI_TARGET_X86) || defined(BROTLI_TARGET_X64) || \272defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_ANY) || \273defined(BROTLI_TARGET_RISCV64) || defined(BROTLI_TARGET_LOONGARCH64)274/* These targets are known to generate efficient code for unaligned reads275* (e.g. a single instruction, not multiple 1-byte loads, shifted and or'd276* together). */277#define BROTLI_UNALIGNED_READ_FAST (!!1)278#else279#define BROTLI_UNALIGNED_READ_FAST (!!0)280#endif281282/* Portable unaligned memory access: read / write values via memcpy. */283#if !defined(BROTLI_USE_PACKED_FOR_UNALIGNED)284#if defined(__mips__) && (!defined(__mips_isa_rev) || __mips_isa_rev < 6)285#define BROTLI_USE_PACKED_FOR_UNALIGNED 1286#else287#define BROTLI_USE_PACKED_FOR_UNALIGNED 0288#endif289#endif /* defined(BROTLI_USE_PACKED_FOR_UNALIGNED) */290291#if BROTLI_USE_PACKED_FOR_UNALIGNED292293typedef union BrotliPackedValue {294uint16_t u16;295uint32_t u32;296uint64_t u64;297size_t szt;298} __attribute__ ((packed)) BrotliPackedValue;299300static BROTLI_INLINE uint16_t BrotliUnalignedRead16(const void* p) {301const BrotliPackedValue* address = (const BrotliPackedValue*)p;302return address->u16;303}304static BROTLI_INLINE uint32_t BrotliUnalignedRead32(const void* p) {305const BrotliPackedValue* address = (const BrotliPackedValue*)p;306return address->u32;307}308static BROTLI_INLINE uint64_t BrotliUnalignedRead64(const void* p) {309const BrotliPackedValue* address = (const BrotliPackedValue*)p;310return address->u64;311}312static BROTLI_INLINE size_t BrotliUnalignedReadSizeT(const void* p) {313const BrotliPackedValue* address = (const BrotliPackedValue*)p;314return address->szt;315}316static BROTLI_INLINE void BrotliUnalignedWrite64(void* p, uint64_t v) {317BrotliPackedValue* address = (BrotliPackedValue*)p;318address->u64 = v;319}320321#else /* not BROTLI_USE_PACKED_FOR_UNALIGNED */322323static BROTLI_INLINE uint16_t BrotliUnalignedRead16(const void* p) {324uint16_t t;325memcpy(&t, p, sizeof t);326return t;327}328static BROTLI_INLINE uint32_t BrotliUnalignedRead32(const void* p) {329uint32_t t;330memcpy(&t, p, sizeof t);331return t;332}333static BROTLI_INLINE uint64_t BrotliUnalignedRead64(const void* p) {334uint64_t t;335memcpy(&t, p, sizeof t);336return t;337}338static BROTLI_INLINE size_t BrotliUnalignedReadSizeT(const void* p) {339size_t t;340memcpy(&t, p, sizeof t);341return t;342}343static BROTLI_INLINE void BrotliUnalignedWrite64(void* p, uint64_t v) {344memcpy(p, &v, sizeof v);345}346347#endif /* BROTLI_USE_PACKED_FOR_UNALIGNED */348349#if BROTLI_GNUC_HAS_BUILTIN(__builtin_bswap16, 4, 3, 0)350#define BROTLI_BSWAP16(V) ((uint16_t)__builtin_bswap16(V))351#else352#define BROTLI_BSWAP16(V) ((uint16_t)( \353(((V) & 0xFFU) << 8) | \354(((V) >> 8) & 0xFFU)))355#endif356357#if BROTLI_GNUC_HAS_BUILTIN(__builtin_bswap32, 4, 3, 0)358#define BROTLI_BSWAP32(V) ((uint32_t)__builtin_bswap32(V))359#else360#define BROTLI_BSWAP32(V) ((uint32_t)( \361(((V) & 0xFFU) << 24) | (((V) & 0xFF00U) << 8) | \362(((V) >> 8) & 0xFF00U) | (((V) >> 24) & 0xFFU)))363#endif364365#if BROTLI_GNUC_HAS_BUILTIN(__builtin_bswap64, 4, 3, 0)366#define BROTLI_BSWAP64(V) ((uint64_t)__builtin_bswap64(V))367#else368#define BROTLI_BSWAP64(V) ((uint64_t)( \369(((V) & 0xFFU) << 56) | (((V) & 0xFF00U) << 40) | \370(((V) & 0xFF0000U) << 24) | (((V) & 0xFF000000U) << 8) | \371(((V) >> 8) & 0xFF000000U) | (((V) >> 24) & 0xFF0000U) | \372(((V) >> 40) & 0xFF00U) | (((V) >> 56) & 0xFFU)))373#endif374375#if BROTLI_LITTLE_ENDIAN376/* Straight endianness. Just read / write values. */377#define BROTLI_UNALIGNED_LOAD16LE BrotliUnalignedRead16378#define BROTLI_UNALIGNED_LOAD32LE BrotliUnalignedRead32379#define BROTLI_UNALIGNED_LOAD64LE BrotliUnalignedRead64380#define BROTLI_UNALIGNED_STORE64LE BrotliUnalignedWrite64381#elif BROTLI_BIG_ENDIAN /* BROTLI_LITTLE_ENDIAN */382static BROTLI_INLINE uint16_t BROTLI_UNALIGNED_LOAD16LE(const void* p) {383uint16_t value = BrotliUnalignedRead16(p);384return BROTLI_BSWAP16(value);385}386static BROTLI_INLINE uint32_t BROTLI_UNALIGNED_LOAD32LE(const void* p) {387uint32_t value = BrotliUnalignedRead32(p);388return BROTLI_BSWAP32(value);389}390static BROTLI_INLINE uint64_t BROTLI_UNALIGNED_LOAD64LE(const void* p) {391uint64_t value = BrotliUnalignedRead64(p);392return BROTLI_BSWAP64(value);393}394static BROTLI_INLINE void BROTLI_UNALIGNED_STORE64LE(void* p, uint64_t v) {395uint64_t value = BROTLI_BSWAP64(v);396BrotliUnalignedWrite64(p, value);397}398#else /* BROTLI_LITTLE_ENDIAN */399/* Read / store values byte-wise; hopefully compiler will understand. */400static BROTLI_INLINE uint16_t BROTLI_UNALIGNED_LOAD16LE(const void* p) {401const uint8_t* in = (const uint8_t*)p;402return (uint16_t)(in[0] | (in[1] << 8));403}404static BROTLI_INLINE uint32_t BROTLI_UNALIGNED_LOAD32LE(const void* p) {405const uint8_t* in = (const uint8_t*)p;406uint32_t value = (uint32_t)(in[0]);407value |= (uint32_t)(in[1]) << 8;408value |= (uint32_t)(in[2]) << 16;409value |= (uint32_t)(in[3]) << 24;410return value;411}412static BROTLI_INLINE uint64_t BROTLI_UNALIGNED_LOAD64LE(const void* p) {413const uint8_t* in = (const uint8_t*)p;414uint64_t value = (uint64_t)(in[0]);415value |= (uint64_t)(in[1]) << 8;416value |= (uint64_t)(in[2]) << 16;417value |= (uint64_t)(in[3]) << 24;418value |= (uint64_t)(in[4]) << 32;419value |= (uint64_t)(in[5]) << 40;420value |= (uint64_t)(in[6]) << 48;421value |= (uint64_t)(in[7]) << 56;422return value;423}424static BROTLI_INLINE void BROTLI_UNALIGNED_STORE64LE(void* p, uint64_t v) {425uint8_t* out = (uint8_t*)p;426out[0] = (uint8_t)v;427out[1] = (uint8_t)(v >> 8);428out[2] = (uint8_t)(v >> 16);429out[3] = (uint8_t)(v >> 24);430out[4] = (uint8_t)(v >> 32);431out[5] = (uint8_t)(v >> 40);432out[6] = (uint8_t)(v >> 48);433out[7] = (uint8_t)(v >> 56);434}435#endif /* BROTLI_LITTLE_ENDIAN */436437static BROTLI_INLINE void* BROTLI_UNALIGNED_LOAD_PTR(const void* p) {438void* v;439memcpy(&v, p, sizeof(void*));440return v;441}442443static BROTLI_INLINE void BROTLI_UNALIGNED_STORE_PTR(void* p, const void* v) {444memcpy(p, &v, sizeof(void*));445}446447/* BROTLI_IS_CONSTANT macros returns true for compile-time constants. */448#if BROTLI_GNUC_HAS_BUILTIN(__builtin_constant_p, 3, 0, 1) || \449BROTLI_INTEL_VERSION_CHECK(16, 0, 0)450#define BROTLI_IS_CONSTANT(x) (!!__builtin_constant_p(x))451#else452#define BROTLI_IS_CONSTANT(x) (!!0)453#endif454455#if defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_ANY)456#define BROTLI_HAS_UBFX (!!1)457#else458#define BROTLI_HAS_UBFX (!!0)459#endif460461#if defined(BROTLI_ENABLE_LOG)462#define BROTLI_LOG(x) printf x463#else464#define BROTLI_LOG(x)465#endif466467#if defined(BROTLI_DEBUG) || defined(BROTLI_ENABLE_LOG)468#define BROTLI_ENABLE_DUMP_DEFAULT 1469#define BROTLI_DCHECK(x) assert(x)470#else471#define BROTLI_ENABLE_DUMP_DEFAULT 0472#define BROTLI_DCHECK(x)473#endif474475#if !defined(BROTLI_ENABLE_DUMP)476#define BROTLI_ENABLE_DUMP BROTLI_ENABLE_DUMP_DEFAULT477#endif478479#if BROTLI_ENABLE_DUMP480static BROTLI_INLINE void BrotliDump(const char* f, int l, const char* fn) {481fprintf(stderr, "%s:%d (%s)\n", f, l, fn);482fflush(stderr);483}484#define BROTLI_DUMP() BrotliDump(__FILE__, __LINE__, __FUNCTION__)485#else486#define BROTLI_DUMP() (void)(0)487#endif488489/* BrotliRBit assumes brotli_reg_t fits native CPU register type. */490#if (BROTLI_64_BITS == BROTLI_TARGET_64_BITS)491/* TODO(eustas): add appropriate icc/sunpro/arm/ibm/ti checks. */492#if (BROTLI_GNUC_VERSION_CHECK(3, 0, 0) || defined(__llvm__)) && \493!defined(BROTLI_BUILD_NO_RBIT)494#if defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_ANY)495/* TODO(eustas): detect ARMv6T2 and enable this code for it. */496static BROTLI_INLINE brotli_reg_t BrotliRBit(brotli_reg_t input) {497brotli_reg_t output;498__asm__("rbit %0, %1\n" : "=r"(output) : "r"(input));499return output;500}501#define BROTLI_RBIT(x) BrotliRBit(x)502#endif /* armv7 / armv8 */503#endif /* gcc || clang */504#endif /* brotli_reg_t is native */505#if !defined(BROTLI_RBIT)506static BROTLI_INLINE void BrotliRBit(void) { /* Should break build if used. */ }507#endif /* BROTLI_RBIT */508509#define BROTLI_REPEAT_4(X) {X; X; X; X;}510#define BROTLI_REPEAT_5(X) {X; X; X; X; X;}511#define BROTLI_REPEAT_6(X) {X; X; X; X; X; X;}512513#define BROTLI_UNUSED(X) (void)(X)514515#define BROTLI_MIN_MAX(T) \516static BROTLI_INLINE T brotli_min_ ## T (T a, T b) { return a < b ? a : b; } \517static BROTLI_INLINE T brotli_max_ ## T (T a, T b) { return a > b ? a : b; }518BROTLI_MIN_MAX(double) BROTLI_MIN_MAX(float) BROTLI_MIN_MAX(int)519BROTLI_MIN_MAX(size_t) BROTLI_MIN_MAX(uint32_t) BROTLI_MIN_MAX(uint8_t)520#undef BROTLI_MIN_MAX521#define BROTLI_MIN(T, A, B) (brotli_min_ ## T((A), (B)))522#define BROTLI_MAX(T, A, B) (brotli_max_ ## T((A), (B)))523524#define BROTLI_SWAP(T, A, I, J) { \525T __brotli_swap_tmp = (A)[(I)]; \526(A)[(I)] = (A)[(J)]; \527(A)[(J)] = __brotli_swap_tmp; \528}529530#if BROTLI_64_BITS531#if BROTLI_GNUC_HAS_BUILTIN(__builtin_ctzll, 3, 4, 0) || \532BROTLI_INTEL_VERSION_CHECK(16, 0, 0)533#define BROTLI_TZCNT64 __builtin_ctzll534#elif BROTLI_MSVC_VERSION_CHECK(18, 0, 0)535#if defined(BROTLI_TARGET_X64) && !defined(_M_ARM64EC)536#define BROTLI_TZCNT64 _tzcnt_u64537#else /* BROTLI_TARGET_X64 */538static BROTLI_INLINE uint32_t BrotliBsf64Msvc(uint64_t x) {539uint32_t lsb;540_BitScanForward64(&lsb, x);541return lsb;542}543#define BROTLI_TZCNT64 BrotliBsf64Msvc544#endif /* BROTLI_TARGET_X64 */545#endif /* __builtin_ctzll */546#endif /* BROTLI_64_BITS */547548#if BROTLI_GNUC_HAS_BUILTIN(__builtin_clz, 3, 4, 0) || \549BROTLI_INTEL_VERSION_CHECK(16, 0, 0)550#define BROTLI_BSR32(x) (31u ^ (uint32_t)__builtin_clz(x))551#elif BROTLI_MSVC_VERSION_CHECK(18, 0, 0)552static BROTLI_INLINE uint32_t BrotliBsr32Msvc(uint32_t x) {553unsigned long msb;554_BitScanReverse(&msb, x);555return (uint32_t)msb;556}557#define BROTLI_BSR32 BrotliBsr32Msvc558#endif /* __builtin_clz */559560/* Default brotli_alloc_func */561BROTLI_COMMON_API void* BrotliDefaultAllocFunc(void* opaque, size_t size);562563/* Default brotli_free_func */564BROTLI_COMMON_API void BrotliDefaultFreeFunc(void* opaque, void* address);565566/* Circular logical rotates. */567static BROTLI_INLINE uint16_t BrotliRotateRight16(uint16_t const value,568size_t count) {569count &= 0x0F; /* for fickle pattern recognition */570return (value >> count) | (uint16_t)(value << ((0U - count) & 0x0F));571}572static BROTLI_INLINE uint32_t BrotliRotateRight32(uint32_t const value,573size_t count) {574count &= 0x1F; /* for fickle pattern recognition */575return (value >> count) | (uint32_t)(value << ((0U - count) & 0x1F));576}577static BROTLI_INLINE uint64_t BrotliRotateRight64(uint64_t const value,578size_t count) {579count &= 0x3F; /* for fickle pattern recognition */580return (value >> count) | (uint64_t)(value << ((0U - count) & 0x3F));581}582583BROTLI_UNUSED_FUNCTION void BrotliSuppressUnusedFunctions(void) {584BROTLI_UNUSED(&BrotliSuppressUnusedFunctions);585BROTLI_UNUSED(&BrotliUnalignedRead16);586BROTLI_UNUSED(&BrotliUnalignedRead32);587BROTLI_UNUSED(&BrotliUnalignedRead64);588BROTLI_UNUSED(&BrotliUnalignedReadSizeT);589BROTLI_UNUSED(&BrotliUnalignedWrite64);590BROTLI_UNUSED(&BROTLI_UNALIGNED_LOAD16LE);591BROTLI_UNUSED(&BROTLI_UNALIGNED_LOAD32LE);592BROTLI_UNUSED(&BROTLI_UNALIGNED_LOAD64LE);593BROTLI_UNUSED(&BROTLI_UNALIGNED_STORE64LE);594BROTLI_UNUSED(&BROTLI_UNALIGNED_LOAD_PTR);595BROTLI_UNUSED(&BROTLI_UNALIGNED_STORE_PTR);596BROTLI_UNUSED(&BrotliRBit);597BROTLI_UNUSED(&brotli_min_double);598BROTLI_UNUSED(&brotli_max_double);599BROTLI_UNUSED(&brotli_min_float);600BROTLI_UNUSED(&brotli_max_float);601BROTLI_UNUSED(&brotli_min_int);602BROTLI_UNUSED(&brotli_max_int);603BROTLI_UNUSED(&brotli_min_size_t);604BROTLI_UNUSED(&brotli_max_size_t);605BROTLI_UNUSED(&brotli_min_uint32_t);606BROTLI_UNUSED(&brotli_max_uint32_t);607BROTLI_UNUSED(&brotli_min_uint8_t);608BROTLI_UNUSED(&brotli_max_uint8_t);609BROTLI_UNUSED(&BrotliDefaultAllocFunc);610BROTLI_UNUSED(&BrotliDefaultFreeFunc);611BROTLI_UNUSED(&BrotliRotateRight16);612BROTLI_UNUSED(&BrotliRotateRight32);613BROTLI_UNUSED(&BrotliRotateRight64);614#if BROTLI_ENABLE_DUMP615BROTLI_UNUSED(&BrotliDump);616#endif617618#if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_I86)) && \619!defined(_M_ARM64EC)620/* _mm_prefetch() is not defined outside of x86/x64 */621/* https://msdn.microsoft.com/fr-fr/library/84szxsww(v=vs.90).aspx */622#include <mmintrin.h>623#define PREFETCH_L1(ptr) _mm_prefetch((const char*)(ptr), _MM_HINT_T0)624#define PREFETCH_L2(ptr) _mm_prefetch((const char*)(ptr), _MM_HINT_T1)625#elif BROTLI_GNUC_HAS_BUILTIN(__builtin_prefetch, 3, 1, 0)626#define PREFETCH_L1(ptr) \627__builtin_prefetch((ptr), 0 /* rw==read */, 3 /* locality */)628#define PREFETCH_L2(ptr) \629__builtin_prefetch((ptr), 0 /* rw==read */, 2 /* locality */)630#elif defined(__aarch64__)631#define PREFETCH_L1(ptr) \632do { \633__asm__ __volatile__("prfm pldl1keep, %0" ::"Q"(*(ptr))); \634} while (0)635#define PREFETCH_L2(ptr) \636do { \637__asm__ __volatile__("prfm pldl2keep, %0" ::"Q"(*(ptr))); \638} while (0)639#else640#define PREFETCH_L1(ptr) \641do { \642(void)(ptr); \643} while (0) /* disabled */644#define PREFETCH_L2(ptr) \645do { \646(void)(ptr); \647} while (0) /* disabled */648#endif649650/* The SIMD matchers are only faster at certain quality levels. */651#if defined(_M_X64) && defined(BROTLI_TZCNT64)652#define BROTLI_MAX_SIMD_QUALITY 7653#elif defined(BROTLI_TZCNT64)654#define BROTLI_MAX_SIMD_QUALITY 6655#endif656}657658#if defined(_MSC_VER)659#define BROTLI_CRASH() __debugbreak(), (void)abort()660#elif BROTLI_GNUC_HAS_BUILTIN(__builtin_trap, 3, 0, 0)661#define BROTLI_CRASH() (void)__builtin_trap()662#else663#define BROTLI_CRASH() (void)abort()664#endif665666/* Make BROTLI_TEST=0 act same as undefined. */667#if defined(BROTLI_TEST) && ((1-BROTLI_TEST-1) == 0)668#undef BROTLI_TEST669#endif670671#if !defined(BROTLI_MODEL) && BROTLI_GNUC_HAS_ATTRIBUTE(model, 3, 0, 3) && \672!defined(BROTLI_TARGET_IA64) && !defined(BROTLI_TARGET_LOONGARCH64)673#define BROTLI_MODEL(M) __attribute__((model(M)))674#else675#define BROTLI_MODEL(M) /* M */676#endif677678#if !defined(BROTLI_COLD) && BROTLI_GNUC_HAS_ATTRIBUTE(cold, 4, 3, 0)679#define BROTLI_COLD __attribute__((cold))680#else681#define BROTLI_COLD /* cold */682#endif683684#endif /* BROTLI_COMMON_PLATFORM_H_ */685686687