Home | History | Annotate | Line # | Download | only in common
      1 /* Copyright 2016 Google Inc. All Rights Reserved.
      2 
      3    Distributed under MIT license.
      4    See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
      5 */
      6 
      7 /* Macros for compiler / platform specific features and build options.
      8 
      9    Build options are:
     10     * BROTLI_BUILD_32_BIT disables 64-bit optimizations
     11     * BROTLI_BUILD_64_BIT forces to use 64-bit optimizations
     12     * BROTLI_BUILD_BIG_ENDIAN forces to use big-endian optimizations
     13     * BROTLI_BUILD_ENDIAN_NEUTRAL disables endian-aware optimizations
     14     * BROTLI_BUILD_LITTLE_ENDIAN forces to use little-endian optimizations
     15     * BROTLI_BUILD_PORTABLE disables dangerous optimizations, like unaligned
     16       read and overlapping memcpy; this reduces decompression speed by 5%
     17     * BROTLI_BUILD_NO_RBIT disables "rbit" optimization for ARM CPUs
     18     * BROTLI_DEBUG dumps file name and line number when decoder detects stream
     19       or memory error
     20     * BROTLI_ENABLE_LOG enables asserts and dumps various state information
     21 */
     22 
     23 #ifndef BROTLI_COMMON_PLATFORM_H_
     24 #define BROTLI_COMMON_PLATFORM_H_
     25 
     26 #include <string.h>  /* memcpy */
     27 
     28 #include <brotli/port.h>
     29 #include <brotli/types.h>
     30 
     31 #if defined(OS_LINUX) || defined(OS_CYGWIN) || defined(__EMSCRIPTEN__)
     32 #include <endian.h>
     33 #elif defined(OS_FREEBSD)
     34 #include <machine/endian.h>
     35 #elif defined(OS_MACOSX)
     36 #include <machine/endian.h>
     37 /* Let's try and follow the Linux convention */
     38 #define BROTLI_X_BYTE_ORDER BYTE_ORDER
     39 #define BROTLI_X_LITTLE_ENDIAN LITTLE_ENDIAN
     40 #define BROTLI_X_BIG_ENDIAN BIG_ENDIAN
     41 #endif
     42 
     43 #if BROTLI_MSVC_VERSION_CHECK(12, 0, 0)
     44 #include <intrin.h>
     45 #endif
     46 
     47 #if defined(BROTLI_ENABLE_LOG) || defined(BROTLI_DEBUG)
     48 #include <assert.h>
     49 #include <stdio.h>
     50 #endif
     51 
     52 /* The following macros were borrowed from https://github.com/nemequ/hedley
     53  * with permission of original author - Evan Nemerson <evan (at) nemerson.com> */
     54 
     55 /* >>> >>> >>> hedley macros */
     56 
     57 /* Define "BROTLI_PREDICT_TRUE" and "BROTLI_PREDICT_FALSE" macros for capable
     58    compilers.
     59 
     60 To apply compiler hint, enclose the branching condition into macros, like this:
     61 
     62   if (BROTLI_PREDICT_TRUE(zero == 0)) {
     63     // main execution path
     64   } else {
     65     // compiler should place this code outside of main execution path
     66   }
     67 
     68 OR:
     69 
     70   if (BROTLI_PREDICT_FALSE(something_rare_or_unexpected_happens)) {
     71     // compiler should place this code outside of main execution path
     72   }
     73 
     74 */
     75 #if BROTLI_GNUC_HAS_BUILTIN(__builtin_expect, 3, 0, 0) || \
     76     BROTLI_INTEL_VERSION_CHECK(16, 0, 0) ||               \
     77     BROTLI_SUNPRO_VERSION_CHECK(5, 15, 0) ||              \
     78     BROTLI_ARM_VERSION_CHECK(4, 1, 0) ||                  \
     79     BROTLI_IBM_VERSION_CHECK(10, 1, 0) ||                 \
     80     BROTLI_TI_VERSION_CHECK(7, 3, 0) ||                   \
     81     BROTLI_TINYC_VERSION_CHECK(0, 9, 27)
     82 #define BROTLI_PREDICT_TRUE(x) (__builtin_expect(!!(x), 1))
     83 #define BROTLI_PREDICT_FALSE(x) (__builtin_expect(x, 0))
     84 #else
     85 #define BROTLI_PREDICT_FALSE(x) (x)
     86 #define BROTLI_PREDICT_TRUE(x) (x)
     87 #endif
     88 
     89 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \
     90     !defined(__cplusplus)
     91 #define BROTLI_RESTRICT restrict
     92 #elif BROTLI_GNUC_VERSION_CHECK(3, 1, 0) ||                         \
     93     BROTLI_MSVC_VERSION_CHECK(14, 0, 0) ||                          \
     94     BROTLI_INTEL_VERSION_CHECK(16, 0, 0) ||                         \
     95     BROTLI_ARM_VERSION_CHECK(4, 1, 0) ||                            \
     96     BROTLI_IBM_VERSION_CHECK(10, 1, 0) ||                           \
     97     BROTLI_PGI_VERSION_CHECK(17, 10, 0) ||                          \
     98     BROTLI_TI_VERSION_CHECK(8, 0, 0) ||                             \
     99     BROTLI_IAR_VERSION_CHECK(8, 0, 0) ||                            \
    100     (BROTLI_SUNPRO_VERSION_CHECK(5, 14, 0) && defined(__cplusplus))
    101 #define BROTLI_RESTRICT __restrict
    102 #elif BROTLI_SUNPRO_VERSION_CHECK(5, 3, 0) && !defined(__cplusplus)
    103 #define BROTLI_RESTRICT _Restrict
    104 #else
    105 #define BROTLI_RESTRICT
    106 #endif
    107 
    108 #if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \
    109     (defined(__cplusplus) && (__cplusplus >= 199711L))
    110 #define BROTLI_MAYBE_INLINE inline
    111 #elif defined(__GNUC_STDC_INLINE__) || defined(__GNUC_GNU_INLINE__) || \
    112     BROTLI_ARM_VERSION_CHECK(6, 2, 0)
    113 #define BROTLI_MAYBE_INLINE __inline__
    114 #elif BROTLI_MSVC_VERSION_CHECK(12, 0, 0) || \
    115     BROTLI_ARM_VERSION_CHECK(4, 1, 0) || BROTLI_TI_VERSION_CHECK(8, 0, 0)
    116 #define BROTLI_MAYBE_INLINE __inline
    117 #else
    118 #define BROTLI_MAYBE_INLINE
    119 #endif
    120 
    121 #if BROTLI_GNUC_HAS_ATTRIBUTE(always_inline, 4, 0, 0) ||                       \
    122     BROTLI_INTEL_VERSION_CHECK(16, 0, 0) ||                                    \
    123     BROTLI_SUNPRO_VERSION_CHECK(5, 11, 0) ||                                   \
    124     BROTLI_ARM_VERSION_CHECK(4, 1, 0) ||                                       \
    125     BROTLI_IBM_VERSION_CHECK(10, 1, 0) ||                                      \
    126     BROTLI_TI_VERSION_CHECK(8, 0, 0) ||                                        \
    127     (BROTLI_TI_VERSION_CHECK(7, 3, 0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__))
    128 #define BROTLI_INLINE BROTLI_MAYBE_INLINE __attribute__((__always_inline__))
    129 #elif BROTLI_MSVC_VERSION_CHECK(12, 0, 0)
    130 #define BROTLI_INLINE BROTLI_MAYBE_INLINE __forceinline
    131 #elif BROTLI_TI_VERSION_CHECK(7, 0, 0) && defined(__cplusplus)
    132 #define BROTLI_INLINE BROTLI_MAYBE_INLINE _Pragma("FUNC_ALWAYS_INLINE;")
    133 #elif BROTLI_IAR_VERSION_CHECK(8, 0, 0)
    134 #define BROTLI_INLINE BROTLI_MAYBE_INLINE _Pragma("inline=forced")
    135 #else
    136 #define BROTLI_INLINE BROTLI_MAYBE_INLINE
    137 #endif
    138 
    139 #if BROTLI_GNUC_HAS_ATTRIBUTE(noinline, 4, 0, 0) ||                            \
    140     BROTLI_INTEL_VERSION_CHECK(16, 0, 0) ||                                    \
    141     BROTLI_SUNPRO_VERSION_CHECK(5, 11, 0) ||                                   \
    142     BROTLI_ARM_VERSION_CHECK(4, 1, 0) ||                                       \
    143     BROTLI_IBM_VERSION_CHECK(10, 1, 0) ||                                      \
    144     BROTLI_TI_VERSION_CHECK(8, 0, 0) ||                                        \
    145     (BROTLI_TI_VERSION_CHECK(7, 3, 0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__))
    146 #define BROTLI_NOINLINE __attribute__((__noinline__))
    147 #elif BROTLI_MSVC_VERSION_CHECK(13, 10, 0)
    148 #define BROTLI_NOINLINE __declspec(noinline)
    149 #elif BROTLI_PGI_VERSION_CHECK(10, 2, 0)
    150 #define BROTLI_NOINLINE _Pragma("noinline")
    151 #elif BROTLI_TI_VERSION_CHECK(6, 0, 0) && defined(__cplusplus)
    152 #define BROTLI_NOINLINE _Pragma("FUNC_CANNOT_INLINE;")
    153 #elif BROTLI_IAR_VERSION_CHECK(8, 0, 0)
    154 #define BROTLI_NOINLINE _Pragma("inline=never")
    155 #else
    156 #define BROTLI_NOINLINE
    157 #endif
    158 
    159 /* BROTLI_INTERNAL could be defined to override visibility, e.g. for tests. */
    160 #if !defined(BROTLI_INTERNAL)
    161 #if defined(_WIN32) || defined(__CYGWIN__)
    162 #define BROTLI_INTERNAL
    163 #elif BROTLI_GNUC_VERSION_CHECK(3, 3, 0) ||                         \
    164     BROTLI_TI_VERSION_CHECK(8, 0, 0) ||                             \
    165     BROTLI_INTEL_VERSION_CHECK(16, 0, 0) ||                         \
    166     BROTLI_ARM_VERSION_CHECK(4, 1, 0) ||                            \
    167     BROTLI_IBM_VERSION_CHECK(13, 1, 0) ||                           \
    168     BROTLI_SUNPRO_VERSION_CHECK(5, 11, 0) ||                        \
    169     (BROTLI_TI_VERSION_CHECK(7, 3, 0) &&                            \
    170      defined(__TI_GNU_ATTRIBUTE_SUPPORT__) && defined(__TI_EABI__))
    171 #define BROTLI_INTERNAL __attribute__ ((visibility ("hidden")))
    172 #else
    173 #define BROTLI_INTERNAL
    174 #endif
    175 #endif
    176 
    177 /* <<< <<< <<< end of hedley macros. */
    178 
    179 #if BROTLI_GNUC_HAS_ATTRIBUTE(unused, 2, 7, 0) || \
    180     BROTLI_INTEL_VERSION_CHECK(16, 0, 0)
    181 #define BROTLI_UNUSED_FUNCTION static BROTLI_INLINE __attribute__ ((unused))
    182 #else
    183 #define BROTLI_UNUSED_FUNCTION static BROTLI_INLINE
    184 #endif
    185 
    186 #if BROTLI_GNUC_HAS_ATTRIBUTE(aligned, 2, 7, 0)
    187 #define BROTLI_ALIGNED(N) __attribute__((aligned(N)))
    188 #else
    189 #define BROTLI_ALIGNED(N)
    190 #endif
    191 
    192 #if (defined(__ARM_ARCH) && (__ARM_ARCH == 7)) || \
    193     (defined(M_ARM) && (M_ARM == 7))
    194 #define BROTLI_TARGET_ARMV7
    195 #endif  /* ARMv7 */
    196 
    197 #if (defined(__ARM_ARCH) && (__ARM_ARCH == 8)) || \
    198     defined(__aarch64__) || defined(__ARM64_ARCH_8__)
    199 #define BROTLI_TARGET_ARMV8_ANY
    200 
    201 #if defined(__ARM_32BIT_STATE)
    202 #define BROTLI_TARGET_ARMV8_32
    203 #elif defined(__ARM_64BIT_STATE)
    204 #define BROTLI_TARGET_ARMV8_64
    205 #endif
    206 
    207 #endif  /* ARMv8 */
    208 
    209 #if defined(__ARM_NEON__) || defined(__ARM_NEON)
    210 #define BROTLI_TARGET_NEON
    211 #endif
    212 
    213 #if defined(__i386) || defined(_M_IX86)
    214 #define BROTLI_TARGET_X86
    215 #endif
    216 
    217 #if defined(__x86_64__) || defined(_M_X64)
    218 #define BROTLI_TARGET_X64
    219 #endif
    220 
    221 #if defined(__PPC64__)
    222 #define BROTLI_TARGET_POWERPC64
    223 #endif
    224 
    225 #if defined(__riscv) && defined(__riscv_xlen) && __riscv_xlen == 64
    226 #define BROTLI_TARGET_RISCV64
    227 #endif
    228 
    229 #if defined(BROTLI_BUILD_64_BIT)
    230 #define BROTLI_64_BITS 1
    231 #elif defined(BROTLI_BUILD_32_BIT)
    232 #define BROTLI_64_BITS 0
    233 #elif defined(BROTLI_TARGET_X64) || defined(BROTLI_TARGET_ARMV8_64) || \
    234     defined(BROTLI_TARGET_POWERPC64) || defined(BROTLI_TARGET_RISCV64)
    235 #define BROTLI_64_BITS 1
    236 #else
    237 #define BROTLI_64_BITS 0
    238 #endif
    239 
    240 #if (BROTLI_64_BITS)
    241 #define brotli_reg_t uint64_t
    242 #else
    243 #define brotli_reg_t uint32_t
    244 #endif
    245 
    246 #if defined(BROTLI_BUILD_BIG_ENDIAN)
    247 #define BROTLI_BIG_ENDIAN 1
    248 #elif defined(BROTLI_BUILD_LITTLE_ENDIAN)
    249 #define BROTLI_LITTLE_ENDIAN 1
    250 #elif defined(BROTLI_BUILD_ENDIAN_NEUTRAL)
    251 /* Just break elif chain. */
    252 #elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
    253 #define BROTLI_LITTLE_ENDIAN 1
    254 #elif defined(_WIN32) || defined(BROTLI_TARGET_X64)
    255 /* Win32 & x64 can currently always be assumed to be little endian */
    256 #define BROTLI_LITTLE_ENDIAN 1
    257 #elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
    258 #define BROTLI_BIG_ENDIAN 1
    259 #elif defined(BROTLI_X_BYTE_ORDER)
    260 #if BROTLI_X_BYTE_ORDER == BROTLI_X_LITTLE_ENDIAN
    261 #define BROTLI_LITTLE_ENDIAN 1
    262 #elif BROTLI_X_BYTE_ORDER == BROTLI_X_BIG_ENDIAN
    263 #define BROTLI_BIG_ENDIAN 1
    264 #endif
    265 #endif  /* BROTLI_X_BYTE_ORDER */
    266 
    267 #if !defined(BROTLI_LITTLE_ENDIAN)
    268 #define BROTLI_LITTLE_ENDIAN 0
    269 #endif
    270 
    271 #if !defined(BROTLI_BIG_ENDIAN)
    272 #define BROTLI_BIG_ENDIAN 0
    273 #endif
    274 
    275 #if defined(BROTLI_X_BYTE_ORDER)
    276 #undef BROTLI_X_BYTE_ORDER
    277 #undef BROTLI_X_LITTLE_ENDIAN
    278 #undef BROTLI_X_BIG_ENDIAN
    279 #endif
    280 
    281 #if defined(BROTLI_BUILD_PORTABLE)
    282 #define BROTLI_ALIGNED_READ (!!1)
    283 #elif defined(BROTLI_TARGET_X86) || defined(BROTLI_TARGET_X64) || \
    284     defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_ANY) || \
    285     defined(BROTLI_TARGET_RISCV64)
    286 /* Allow unaligned read only for white-listed CPUs. */
    287 #define BROTLI_ALIGNED_READ (!!0)
    288 #else
    289 #define BROTLI_ALIGNED_READ (!!1)
    290 #endif
    291 
    292 #if BROTLI_ALIGNED_READ
    293 /* Portable unaligned memory access: read / write values via memcpy. */
    294 static BROTLI_INLINE uint16_t BrotliUnalignedRead16(const void* p) {
    295   uint16_t t;
    296   memcpy(&t, p, sizeof t);
    297   return t;
    298 }
    299 static BROTLI_INLINE uint32_t BrotliUnalignedRead32(const void* p) {
    300   uint32_t t;
    301   memcpy(&t, p, sizeof t);
    302   return t;
    303 }
    304 static BROTLI_INLINE uint64_t BrotliUnalignedRead64(const void* p) {
    305   uint64_t t;
    306   memcpy(&t, p, sizeof t);
    307   return t;
    308 }
    309 static BROTLI_INLINE void BrotliUnalignedWrite64(void* p, uint64_t v) {
    310   memcpy(p, &v, sizeof v);
    311 }
    312 #else  /* BROTLI_ALIGNED_READ */
    313 /* Unaligned memory access is allowed: just cast pointer to requested type. */
    314 #if BROTLI_SANITIZED
    315 /* Consider we have an unaligned load/store of 4 bytes from address 0x...05.
    316    AddressSanitizer will treat it as a 3-byte access to the range 05:07 and
    317    will miss a bug if 08 is the first unaddressable byte.
    318    ThreadSanitizer will also treat this as a 3-byte access to 05:07 and will
    319    miss a race between this access and some other accesses to 08.
    320    MemorySanitizer will correctly propagate the shadow on unaligned stores
    321    and correctly report bugs on unaligned loads, but it may not properly
    322    update and report the origin of the uninitialized memory.
    323    For all three tools, replacing an unaligned access with a tool-specific
    324    callback solves the problem. */
    325 #if defined(__cplusplus)
    326 extern "C" {
    327 #endif  /* __cplusplus */
    328   uint16_t __sanitizer_unaligned_load16(const void* p);
    329   uint32_t __sanitizer_unaligned_load32(const void* p);
    330   uint64_t __sanitizer_unaligned_load64(const void* p);
    331   void __sanitizer_unaligned_store64(void* p, uint64_t v);
    332 #if defined(__cplusplus)
    333 }  /* extern "C" */
    334 #endif  /* __cplusplus */
    335 #define BrotliUnalignedRead16 __sanitizer_unaligned_load16
    336 #define BrotliUnalignedRead32 __sanitizer_unaligned_load32
    337 #define BrotliUnalignedRead64 __sanitizer_unaligned_load64
    338 #define BrotliUnalignedWrite64 __sanitizer_unaligned_store64
    339 #else  /* BROTLI_SANITIZED */
    340 static BROTLI_INLINE uint16_t BrotliUnalignedRead16(const void* p) {
    341   return *(const uint16_t*)p;
    342 }
    343 static BROTLI_INLINE uint32_t BrotliUnalignedRead32(const void* p) {
    344   return *(const uint32_t*)p;
    345 }
    346 #if (BROTLI_64_BITS)
    347 static BROTLI_INLINE uint64_t BrotliUnalignedRead64(const void* p) {
    348   return *(const uint64_t*)p;
    349 }
    350 static BROTLI_INLINE void BrotliUnalignedWrite64(void* p, uint64_t v) {
    351   *(uint64_t*)p = v;
    352 }
    353 #else  /* BROTLI_64_BITS */
    354 /* Avoid emitting LDRD / STRD, which require properly aligned address. */
    355 /* If __attribute__(aligned) is available, use that. Otherwise, memcpy. */
    356 
    357 #if BROTLI_GNUC_HAS_ATTRIBUTE(aligned, 2, 7, 0)
    358 typedef BROTLI_ALIGNED(1) uint64_t brotli_unaligned_uint64_t;
    359 
    360 static BROTLI_INLINE uint64_t BrotliUnalignedRead64(const void* p) {
    361   return (uint64_t) ((const brotli_unaligned_uint64_t*) p)[0];
    362 }
    363 static BROTLI_INLINE void BrotliUnalignedWrite64(void* p, uint64_t v) {
    364   brotli_unaligned_uint64_t* dwords = (brotli_unaligned_uint64_t*) p;
    365   dwords[0] = (brotli_unaligned_uint64_t) v;
    366 }
    367 #else /* BROTLI_GNUC_HAS_ATTRIBUTE(aligned, 2, 7, 0) */
    368 static BROTLI_INLINE uint64_t BrotliUnalignedRead64(const void* p) {
    369   uint64_t v;
    370   memcpy(&v, p, sizeof(uint64_t));
    371   return v;
    372 }
    373 
    374 static BROTLI_INLINE void BrotliUnalignedWrite64(void* p, uint64_t v) {
    375   memcpy(p, &v, sizeof(uint64_t));
    376 }
    377 #endif  /* BROTLI_GNUC_HAS_ATTRIBUTE(aligned, 2, 7, 0) */
    378 #endif  /* BROTLI_64_BITS */
    379 #endif  /* BROTLI_SANITIZED */
    380 #endif  /* BROTLI_ALIGNED_READ */
    381 
    382 #if BROTLI_LITTLE_ENDIAN
    383 /* Straight endianness. Just read / write values. */
    384 #define BROTLI_UNALIGNED_LOAD16LE BrotliUnalignedRead16
    385 #define BROTLI_UNALIGNED_LOAD32LE BrotliUnalignedRead32
    386 #define BROTLI_UNALIGNED_LOAD64LE BrotliUnalignedRead64
    387 #define BROTLI_UNALIGNED_STORE64LE BrotliUnalignedWrite64
    388 #elif BROTLI_BIG_ENDIAN  /* BROTLI_LITTLE_ENDIAN */
    389 /* Explain compiler to byte-swap values. */
    390 #define BROTLI_BSWAP16_(V) ((uint16_t)( \
    391   (((V) & 0xFFU) << 8) | \
    392   (((V) >> 8) & 0xFFU)))
    393 static BROTLI_INLINE uint16_t BROTLI_UNALIGNED_LOAD16LE(const void* p) {
    394   uint16_t value = BrotliUnalignedRead16(p);
    395   return BROTLI_BSWAP16_(value);
    396 }
    397 #define BROTLI_BSWAP32_(V) ( \
    398   (((V) & 0xFFU) << 24) | (((V) & 0xFF00U) << 8) | \
    399   (((V) >> 8) & 0xFF00U) | (((V) >> 24) & 0xFFU))
    400 static BROTLI_INLINE uint32_t BROTLI_UNALIGNED_LOAD32LE(const void* p) {
    401   uint32_t value = BrotliUnalignedRead32(p);
    402   return BROTLI_BSWAP32_(value);
    403 }
    404 #define BROTLI_BSWAP64_(V) ( \
    405   (((V) & 0xFFU) << 56) | (((V) & 0xFF00U) << 40) | \
    406   (((V) & 0xFF0000U) << 24) | (((V) & 0xFF000000U) << 8) | \
    407   (((V) >> 8) & 0xFF000000U) | (((V) >> 24) & 0xFF0000U) | \
    408   (((V) >> 40) & 0xFF00U) | (((V) >> 56) & 0xFFU))
    409 static BROTLI_INLINE uint64_t BROTLI_UNALIGNED_LOAD64LE(const void* p) {
    410   uint64_t value = BrotliUnalignedRead64(p);
    411   return BROTLI_BSWAP64_(value);
    412 }
    413 static BROTLI_INLINE void BROTLI_UNALIGNED_STORE64LE(void* p, uint64_t v) {
    414   uint64_t value = BROTLI_BSWAP64_(v);
    415   BrotliUnalignedWrite64(p, value);
    416 }
    417 #else  /* BROTLI_LITTLE_ENDIAN */
    418 /* Read / store values byte-wise; hopefully compiler will understand. */
    419 static BROTLI_INLINE uint16_t BROTLI_UNALIGNED_LOAD16LE(const void* p) {
    420   const uint8_t* in = (const uint8_t*)p;
    421   return (uint16_t)(in[0] | (in[1] << 8));
    422 }
    423 static BROTLI_INLINE uint32_t BROTLI_UNALIGNED_LOAD32LE(const void* p) {
    424   const uint8_t* in = (const uint8_t*)p;
    425   uint32_t value = (uint32_t)(in[0]);
    426   value |= (uint32_t)(in[1]) << 8;
    427   value |= (uint32_t)(in[2]) << 16;
    428   value |= (uint32_t)(in[3]) << 24;
    429   return value;
    430 }
    431 static BROTLI_INLINE uint64_t BROTLI_UNALIGNED_LOAD64LE(const void* p) {
    432   const uint8_t* in = (const uint8_t*)p;
    433   uint64_t value = (uint64_t)(in[0]);
    434   value |= (uint64_t)(in[1]) << 8;
    435   value |= (uint64_t)(in[2]) << 16;
    436   value |= (uint64_t)(in[3]) << 24;
    437   value |= (uint64_t)(in[4]) << 32;
    438   value |= (uint64_t)(in[5]) << 40;
    439   value |= (uint64_t)(in[6]) << 48;
    440   value |= (uint64_t)(in[7]) << 56;
    441   return value;
    442 }
    443 static BROTLI_INLINE void BROTLI_UNALIGNED_STORE64LE(void* p, uint64_t v) {
    444   uint8_t* out = (uint8_t*)p;
    445   out[0] = (uint8_t)v;
    446   out[1] = (uint8_t)(v >> 8);
    447   out[2] = (uint8_t)(v >> 16);
    448   out[3] = (uint8_t)(v >> 24);
    449   out[4] = (uint8_t)(v >> 32);
    450   out[5] = (uint8_t)(v >> 40);
    451   out[6] = (uint8_t)(v >> 48);
    452   out[7] = (uint8_t)(v >> 56);
    453 }
    454 #endif  /* BROTLI_LITTLE_ENDIAN */
    455 
    456 /* BROTLI_IS_CONSTANT macros returns true for compile-time constants. */
    457 #if BROTLI_GNUC_HAS_BUILTIN(__builtin_constant_p, 3, 0, 1) || \
    458     BROTLI_INTEL_VERSION_CHECK(16, 0, 0)
    459 #define BROTLI_IS_CONSTANT(x) (!!__builtin_constant_p(x))
    460 #else
    461 #define BROTLI_IS_CONSTANT(x) (!!0)
    462 #endif
    463 
    464 #if defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_ANY)
    465 #define BROTLI_HAS_UBFX (!!1)
    466 #else
    467 #define BROTLI_HAS_UBFX (!!0)
    468 #endif
    469 
    470 #if defined(BROTLI_ENABLE_LOG)
    471 #define BROTLI_LOG(x) printf x
    472 #else
    473 #define BROTLI_LOG(x)
    474 #endif
    475 
    476 #if defined(BROTLI_DEBUG) || defined(BROTLI_ENABLE_LOG)
    477 #define BROTLI_DCHECK(x) assert(x)
    478 static BROTLI_INLINE void BrotliDump(const char* f, int l, const char* fn) {
    479   fprintf(stderr, "%s:%d (%s)\n", f, l, fn);
    480   fflush(stderr);
    481 }
    482 #define BROTLI_DUMP() BrotliDump(__FILE__, __LINE__, __FUNCTION__)
    483 #else
    484 #define BROTLI_DCHECK(x)
    485 #define BROTLI_DUMP() (void)(0)
    486 #endif
    487 
    488 /* TODO: add appropriate icc/sunpro/arm/ibm/ti checks. */
    489 #if (BROTLI_GNUC_VERSION_CHECK(3, 0, 0) || defined(__llvm__)) && \
    490     !defined(BROTLI_BUILD_NO_RBIT)
    491 #if defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_ANY)
    492 /* TODO: detect ARMv6T2 and enable this code for it. */
    493 static BROTLI_INLINE brotli_reg_t BrotliRBit(brotli_reg_t input) {
    494   brotli_reg_t output;
    495   __asm__("rbit %0, %1\n" : "=r"(output) : "r"(input));
    496   return output;
    497 }
    498 #define BROTLI_RBIT(x) BrotliRBit(x)
    499 #endif  /* armv7 / armv8 */
    500 #endif  /* gcc || clang */
    501 #if !defined(BROTLI_RBIT)
    502 static BROTLI_INLINE void BrotliRBit(void) { /* Should break build if used. */ }
    503 #endif  /* BROTLI_RBIT */
    504 
    505 #define BROTLI_REPEAT(N, X) {     \
    506   if ((N & 1) != 0) {X;}          \
    507   if ((N & 2) != 0) {X; X;}       \
    508   if ((N & 4) != 0) {X; X; X; X;} \
    509 }
    510 
    511 #define BROTLI_UNUSED(X) (void)(X)
    512 
    513 #define BROTLI_MIN_MAX(T)                                                      \
    514   static BROTLI_INLINE T brotli_min_ ## T (T a, T b) { return a < b ? a : b; } \
    515   static BROTLI_INLINE T brotli_max_ ## T (T a, T b) { return a > b ? a : b; }
    516 BROTLI_MIN_MAX(double) BROTLI_MIN_MAX(float) BROTLI_MIN_MAX(int)
    517 BROTLI_MIN_MAX(size_t) BROTLI_MIN_MAX(uint32_t) BROTLI_MIN_MAX(uint8_t)
    518 #undef BROTLI_MIN_MAX
    519 #define BROTLI_MIN(T, A, B) (brotli_min_ ## T((A), (B)))
    520 #define BROTLI_MAX(T, A, B) (brotli_max_ ## T((A), (B)))
    521 
    522 #define BROTLI_SWAP(T, A, I, J) { \
    523   T __brotli_swap_tmp = (A)[(I)]; \
    524   (A)[(I)] = (A)[(J)];            \
    525   (A)[(J)] = __brotli_swap_tmp;   \
    526 }
    527 
    528 #if BROTLI_64_BITS
    529 #if BROTLI_GNUC_HAS_BUILTIN(__builtin_ctzll, 3, 4, 0) || \
    530     BROTLI_INTEL_VERSION_CHECK(16, 0, 0)
    531 #define BROTLI_TZCNT64 __builtin_ctzll
    532 #elif BROTLI_MSVC_VERSION_CHECK(12, 0, 0)
    533 #if defined(BROTLI_TARGET_X64)
    534 #define BROTLI_TZCNT64 _tzcnt_u64
    535 #else /* BROTLI_TARGET_X64 */
    536 static BROTLI_INLINE uint32_t BrotliBsf64Msvc(uint64_t x) {
    537   uint32_t lsb;
    538   _BitScanForward64(&lsb, x);
    539   return lsb;
    540 }
    541 #define BROTLI_TZCNT64 BrotliBsf64Msvc
    542 #endif /* BROTLI_TARGET_X64 */
    543 #endif /* __builtin_ctzll */
    544 #endif /* BROTLI_64_BITS */
    545 
    546 #if BROTLI_GNUC_HAS_BUILTIN(__builtin_clz, 3, 4, 0) || \
    547     BROTLI_INTEL_VERSION_CHECK(16, 0, 0)
    548 #define BROTLI_BSR32(x) (31u ^ (uint32_t)__builtin_clz(x))
    549 #elif BROTLI_MSVC_VERSION_CHECK(12, 0, 0)
    550 static BROTLI_INLINE uint32_t BrotliBsr32Msvc(uint32_t x) {
    551   unsigned long msb;
    552   _BitScanReverse(&msb, x);
    553   return (uint32_t)msb;
    554 }
    555 #define BROTLI_BSR32 BrotliBsr32Msvc
    556 #endif /* __builtin_clz */
    557 
    558 /* Default brotli_alloc_func */
    559 BROTLI_COMMON_API void* BrotliDefaultAllocFunc(void* opaque, size_t size);
    560 
    561 /* Default brotli_free_func */
    562 BROTLI_COMMON_API void BrotliDefaultFreeFunc(void* opaque, void* address);
    563 
    564 BROTLI_UNUSED_FUNCTION void BrotliSuppressUnusedFunctions(void) {
    565   BROTLI_UNUSED(&BrotliSuppressUnusedFunctions);
    566   BROTLI_UNUSED(&BrotliUnalignedRead16);
    567   BROTLI_UNUSED(&BrotliUnalignedRead32);
    568   BROTLI_UNUSED(&BrotliUnalignedRead64);
    569   BROTLI_UNUSED(&BrotliUnalignedWrite64);
    570   BROTLI_UNUSED(&BROTLI_UNALIGNED_LOAD16LE);
    571   BROTLI_UNUSED(&BROTLI_UNALIGNED_LOAD32LE);
    572   BROTLI_UNUSED(&BROTLI_UNALIGNED_LOAD64LE);
    573   BROTLI_UNUSED(&BROTLI_UNALIGNED_STORE64LE);
    574   BROTLI_UNUSED(&BrotliRBit);
    575   BROTLI_UNUSED(&brotli_min_double);
    576   BROTLI_UNUSED(&brotli_max_double);
    577   BROTLI_UNUSED(&brotli_min_float);
    578   BROTLI_UNUSED(&brotli_max_float);
    579   BROTLI_UNUSED(&brotli_min_int);
    580   BROTLI_UNUSED(&brotli_max_int);
    581   BROTLI_UNUSED(&brotli_min_size_t);
    582   BROTLI_UNUSED(&brotli_max_size_t);
    583   BROTLI_UNUSED(&brotli_min_uint32_t);
    584   BROTLI_UNUSED(&brotli_max_uint32_t);
    585   BROTLI_UNUSED(&brotli_min_uint8_t);
    586   BROTLI_UNUSED(&brotli_max_uint8_t);
    587   BROTLI_UNUSED(&BrotliDefaultAllocFunc);
    588   BROTLI_UNUSED(&BrotliDefaultFreeFunc);
    589 #if defined(BROTLI_DEBUG) || defined(BROTLI_ENABLE_LOG)
    590   BROTLI_UNUSED(&BrotliDump);
    591 #endif
    592 }
    593 
    594 #endif  /* BROTLI_COMMON_PLATFORM_H_ */
    595