2015-05-07 14:53:43 +00:00
|
|
|
/* Copyright 2015 Google Inc. All Rights Reserved.
|
|
|
|
|
2015-12-11 10:11:51 +00:00
|
|
|
Distributed under MIT license.
|
2015-11-27 10:27:11 +00:00
|
|
|
See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
|
2015-05-07 14:53:43 +00:00
|
|
|
*/
|
|
|
|
|
2015-09-25 15:57:19 +00:00
|
|
|
/* Macros for compiler / platform specific features and build options.
|
|
|
|
|
|
|
|
Build options are:
|
2015-10-02 14:43:47 +00:00
|
|
|
* BROTLI_BUILD_32_BIT disables 64-bit optimizations
|
|
|
|
* BROTLI_BUILD_64_BIT forces to use 64-bit optimizations
|
|
|
|
* BROTLI_BUILD_BIG_ENDIAN forces to use big-endian optimizations
|
|
|
|
* BROTLI_BUILD_ENDIAN_NEUTRAL disables endian-aware optimizations
|
2015-11-06 10:46:04 +00:00
|
|
|
* BROTLI_BUILD_LITTLE_ENDIAN forces to use little-endian optimizations
|
|
|
|
* BROTLI_BUILD_MODERN_COMPILER forces to use modern compilers built-ins,
|
|
|
|
features and attributes
|
2015-09-25 15:57:19 +00:00
|
|
|
* BROTLI_BUILD_PORTABLE disables dangerous optimizations, like unaligned
|
2015-09-28 10:38:29 +00:00
|
|
|
read and overlapping memcpy; this reduces decompression speed by 5%
|
2015-09-25 15:57:19 +00:00
|
|
|
* BROTLI_DEBUG dumps file name and line number when decoder detects stream
|
|
|
|
or memory error
|
|
|
|
* BROTLI_DECODE_DEBUG enables asserts and dumps various state information
|
|
|
|
*/
|
2015-05-07 14:53:43 +00:00
|
|
|
|
|
|
|
#ifndef BROTLI_DEC_PORT_H_
|
|
|
|
#define BROTLI_DEC_PORT_H_
|
|
|
|
|
2016-02-18 14:03:44 +00:00
|
|
|
#include <assert.h>
|
2015-05-07 14:53:43 +00:00
|
|
|
|
|
|
|
/* Compatibility with non-clang compilers. */
|
|
|
|
#ifndef __has_builtin
|
|
|
|
#define __has_builtin(x) 0
|
|
|
|
#endif
|
|
|
|
|
2015-08-10 11:35:23 +00:00
|
|
|
#ifndef __has_attribute
|
|
|
|
#define __has_attribute(x) 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef __has_feature
|
|
|
|
#define __has_feature(x) 0
|
|
|
|
#endif
|
|
|
|
|
2015-10-06 09:11:42 +00:00
|
|
|
#if defined(__arm__) || defined(__thumb__) || \
|
|
|
|
defined(_M_ARM) || defined(_M_ARMT)
|
|
|
|
#define BROTLI_TARGET_ARM
|
|
|
|
#if (defined(__ARM_ARCH) && (__ARM_ARCH >= 7)) || \
|
|
|
|
(defined(M_ARM) && (M_ARM >= 7))
|
|
|
|
#define BROTLI_TARGET_ARMV7
|
|
|
|
#endif /* ARMv7 */
|
|
|
|
#if defined(__aarch64__)
|
|
|
|
#define BROTLI_TARGET_ARMV8
|
|
|
|
#endif /* ARMv8 */
|
|
|
|
#endif /* ARM */
|
|
|
|
|
2016-02-18 14:03:44 +00:00
|
|
|
#if defined(__i386) || defined(_M_IX86)
|
|
|
|
#define BROTLI_TARGET_X86
|
|
|
|
#endif
|
|
|
|
|
2015-10-06 09:11:42 +00:00
|
|
|
#if defined(__x86_64__) || defined(_M_X64)
|
|
|
|
#define BROTLI_TARGET_X64
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__PPC64__)
|
|
|
|
#define BROTLI_TARGET_POWERPC64
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__GNUC__) && defined(__GNUC_MINOR__)
|
|
|
|
#define BROTLI_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
|
|
|
|
#else
|
|
|
|
#define BROTLI_GCC_VERSION 0
|
|
|
|
#endif
|
|
|
|
|
2015-11-06 10:46:04 +00:00
|
|
|
#if defined(__ICC)
|
|
|
|
#define BROTLI_ICC_VERSION __ICC
|
|
|
|
#else
|
|
|
|
#define BROTLI_ICC_VERSION 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(BROTLI_BUILD_MODERN_COMPILER)
|
|
|
|
#define BROTLI_MODERN_COMPILER 1
|
|
|
|
#elif (BROTLI_GCC_VERSION > 300) || (BROTLI_ICC_VERSION >= 1600)
|
|
|
|
#define BROTLI_MODERN_COMPILER 1
|
|
|
|
#else
|
|
|
|
#define BROTLI_MODERN_COMPILER 0
|
|
|
|
#endif
|
|
|
|
|
2015-09-25 15:57:19 +00:00
|
|
|
#ifdef BROTLI_BUILD_PORTABLE
|
|
|
|
#define BROTLI_ALIGNED_READ 1
|
2016-02-18 14:03:44 +00:00
|
|
|
#elif defined(BROTLI_TARGET_X86) || defined(BROTLI_TARGET_X64) || \
|
|
|
|
defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8)
|
|
|
|
#define BROTLI_ALIGNED_READ 0 /* Allow unaligned access on whitelisted CPUs. */
|
2015-09-25 15:57:19 +00:00
|
|
|
#else
|
2016-02-18 14:03:44 +00:00
|
|
|
#define BROTLI_ALIGNED_READ 1
|
2015-09-25 15:57:19 +00:00
|
|
|
#endif
|
|
|
|
|
2015-05-07 14:53:43 +00:00
|
|
|
/* Define "PREDICT_TRUE" and "PREDICT_FALSE" macros for capable compilers.
|
|
|
|
|
|
|
|
To apply compiler hint, enclose the branching condition into macros, like this:
|
|
|
|
|
|
|
|
if (PREDICT_TRUE(zero == 0)) {
|
|
|
|
// main execution path
|
|
|
|
} else {
|
|
|
|
// compiler should place this code outside of main execution path
|
|
|
|
}
|
|
|
|
|
|
|
|
OR:
|
|
|
|
|
|
|
|
if (PREDICT_FALSE(something_rare_or_unexpected_happens)) {
|
|
|
|
// compiler should place this code outside of main execution path
|
|
|
|
}
|
|
|
|
|
|
|
|
*/
|
2015-11-06 10:46:04 +00:00
|
|
|
#if BROTLI_MODERN_COMPILER || __has_builtin(__builtin_expect)
|
2015-05-07 14:53:43 +00:00
|
|
|
#define PREDICT_TRUE(x) (__builtin_expect(!!(x), 1))
|
|
|
|
#define PREDICT_FALSE(x) (__builtin_expect(x, 0))
|
|
|
|
#else
|
|
|
|
#define PREDICT_FALSE(x) (x)
|
|
|
|
#define PREDICT_TRUE(x) (x)
|
|
|
|
#endif
|
|
|
|
|
2015-08-10 11:35:23 +00:00
|
|
|
/* IS_CONSTANT macros returns true for compile-time constant expressions. */
|
2015-11-06 10:46:04 +00:00
|
|
|
#if BROTLI_MODERN_COMPILER || __has_builtin(__builtin_constant_p)
|
2015-08-10 11:35:23 +00:00
|
|
|
#define IS_CONSTANT(x) __builtin_constant_p(x)
|
|
|
|
#else
|
|
|
|
#define IS_CONSTANT(x) 0
|
|
|
|
#endif
|
|
|
|
|
2015-11-06 10:46:04 +00:00
|
|
|
#if BROTLI_MODERN_COMPILER || __has_attribute(always_inline)
|
2015-08-10 11:35:23 +00:00
|
|
|
#define ATTRIBUTE_ALWAYS_INLINE __attribute__ ((always_inline))
|
|
|
|
#else
|
|
|
|
#define ATTRIBUTE_ALWAYS_INLINE
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef _MSC_VER
|
2016-02-18 14:03:44 +00:00
|
|
|
#if defined(__cplusplus) || !defined(__STRICT_ANSI__) || \
|
|
|
|
__STDC_VERSION__ >= 199901L
|
2015-08-10 11:35:23 +00:00
|
|
|
#define BROTLI_INLINE inline ATTRIBUTE_ALWAYS_INLINE
|
|
|
|
#else
|
|
|
|
#define BROTLI_INLINE
|
|
|
|
#endif
|
|
|
|
#else /* _MSC_VER */
|
|
|
|
#define BROTLI_INLINE __forceinline
|
|
|
|
#endif /* _MSC_VER */
|
|
|
|
|
2015-05-07 14:53:43 +00:00
|
|
|
#ifdef BROTLI_DECODE_DEBUG
|
|
|
|
#define BROTLI_DCHECK(x) assert(x)
|
|
|
|
#else
|
|
|
|
#define BROTLI_DCHECK(x)
|
|
|
|
#endif
|
|
|
|
|
2015-10-02 14:43:47 +00:00
|
|
|
#if defined(BROTLI_BUILD_64_BIT)
|
|
|
|
#define BROTLI_64_BITS 1
|
|
|
|
#elif defined(BROTLI_BUILD_32_BIT)
|
|
|
|
#define BROTLI_64_BITS 0
|
2015-10-06 09:11:42 +00:00
|
|
|
#elif defined(BROTLI_TARGET_X64) || defined(BROTLI_TARGET_ARMV8) || \
|
|
|
|
defined(BROTLI_TARGET_POWERPC64)
|
2015-08-10 11:35:23 +00:00
|
|
|
#define BROTLI_64_BITS 1
|
|
|
|
#else
|
|
|
|
#define BROTLI_64_BITS 0
|
|
|
|
#endif
|
|
|
|
|
2015-10-02 14:43:47 +00:00
|
|
|
#if defined(BROTLI_BUILD_BIG_ENDIAN)
|
|
|
|
#define BROTLI_LITTLE_ENDIAN 0
|
|
|
|
#define BROTLI_BIG_ENDIAN 1
|
|
|
|
#elif defined(BROTLI_BUILD_LITTLE_ENDIAN)
|
|
|
|
#define BROTLI_LITTLE_ENDIAN 1
|
|
|
|
#define BROTLI_BIG_ENDIAN 0
|
|
|
|
#elif defined(BROTLI_BUILD_ENDIAN_NEUTRAL)
|
|
|
|
#define BROTLI_LITTLE_ENDIAN 0
|
|
|
|
#define BROTLI_BIG_ENDIAN 0
|
|
|
|
#elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
|
2015-08-10 11:35:23 +00:00
|
|
|
#define BROTLI_LITTLE_ENDIAN 1
|
2015-10-02 14:43:47 +00:00
|
|
|
#define BROTLI_BIG_ENDIAN 0
|
2015-08-10 12:41:42 +00:00
|
|
|
#elif defined(_WIN32)
|
|
|
|
/* Win32 can currently always be assumed to be little endian */
|
|
|
|
#define BROTLI_LITTLE_ENDIAN 1
|
2015-10-02 14:43:47 +00:00
|
|
|
#define BROTLI_BIG_ENDIAN 0
|
2015-08-10 11:35:23 +00:00
|
|
|
#else
|
2015-10-02 14:43:47 +00:00
|
|
|
#if (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
|
|
|
|
#define BROTLI_BIG_ENDIAN 1
|
2015-08-10 11:35:23 +00:00
|
|
|
#else
|
2015-10-02 14:43:47 +00:00
|
|
|
#define BROTLI_BIG_ENDIAN 0
|
|
|
|
#endif
|
|
|
|
#define BROTLI_LITTLE_ENDIAN 0
|
2015-08-10 11:35:23 +00:00
|
|
|
#endif
|
|
|
|
|
2015-11-06 10:46:04 +00:00
|
|
|
#if BROTLI_MODERN_COMPILER || __has_attribute(noinline)
|
2016-02-18 14:03:44 +00:00
|
|
|
#define BROTLI_NOINLINE __attribute__((noinline))
|
2015-08-10 11:35:23 +00:00
|
|
|
#else
|
|
|
|
#define BROTLI_NOINLINE
|
|
|
|
#endif
|
|
|
|
|
2016-02-18 14:03:44 +00:00
|
|
|
#define BROTLI_REPEAT(N, X) { \
|
|
|
|
if ((N & 1) != 0) {X;} \
|
|
|
|
if ((N & 2) != 0) {X; X;} \
|
2015-08-28 13:20:24 +00:00
|
|
|
if ((N & 4) != 0) {X; X; X; X;} \
|
|
|
|
}
|
|
|
|
|
2015-11-06 10:46:04 +00:00
|
|
|
#if BROTLI_MODERN_COMPILER || defined(__llvm__)
|
2015-10-06 09:11:42 +00:00
|
|
|
#if defined(BROTLI_TARGET_ARMV7)
|
2015-08-28 13:20:24 +00:00
|
|
|
static BROTLI_INLINE unsigned BrotliRBit(unsigned input) {
|
|
|
|
unsigned output;
|
|
|
|
__asm__("rbit %0, %1\n" : "=r"(output) : "r"(input));
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
#define BROTLI_RBIT(x) BrotliRBit(x)
|
|
|
|
#endif /* armv7 */
|
|
|
|
#endif /* gcc || clang */
|
|
|
|
|
2015-11-06 10:46:04 +00:00
|
|
|
#if defined(BROTLI_TARGET_ARM)
|
|
|
|
#define BROTLI_HAS_UBFX 1
|
|
|
|
#else
|
|
|
|
#define BROTLI_HAS_UBFX 0
|
|
|
|
#endif
|
|
|
|
|
2015-11-23 09:21:09 +00:00
|
|
|
#define BROTLI_ALLOC(S, L) S->alloc_func(S->memory_manager_opaque, L)
|
|
|
|
|
2016-02-18 14:03:44 +00:00
|
|
|
#define BROTLI_FREE(S, X) { \
|
2015-11-23 09:21:09 +00:00
|
|
|
S->free_func(S->memory_manager_opaque, X); \
|
2016-02-18 14:03:44 +00:00
|
|
|
X = NULL; \
|
2015-10-02 14:19:30 +00:00
|
|
|
}
|
|
|
|
|
2015-10-05 08:23:32 +00:00
|
|
|
#define BROTLI_UNUSED(X) (void)(X)
|
|
|
|
|
2015-05-07 14:53:43 +00:00
|
|
|
#endif /* BROTLI_DEC_PORT_H_ */
|