23#ifndef BROTLI_COMMON_PLATFORM_H_
24#define BROTLI_COMMON_PLATFORM_H_
28#include <brotli/port.h>
29#include <brotli/types.h>
31#if defined(OS_LINUX) || defined(OS_CYGWIN) || defined(__EMSCRIPTEN__)
33#elif defined(OS_FREEBSD)
34#include <machine/endian.h>
35#elif defined(OS_MACOSX)
36#include <machine/endian.h>
38#define BROTLI_X_BYTE_ORDER BYTE_ORDER
39#define BROTLI_X_LITTLE_ENDIAN LITTLE_ENDIAN
40#define BROTLI_X_BIG_ENDIAN BIG_ENDIAN
43#if BROTLI_MSVC_VERSION_CHECK(12, 0, 0)
47#if defined(BROTLI_ENABLE_LOG) || defined(BROTLI_DEBUG)
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))
85#define BROTLI_PREDICT_FALSE(x) (x)
86#define BROTLI_PREDICT_TRUE(x) (x)
89#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \
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
105#define BROTLI_RESTRICT
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
118#define BROTLI_MAYBE_INLINE
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")
136#define BROTLI_INLINE BROTLI_MAYBE_INLINE
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")
156#define BROTLI_NOINLINE
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")))
173#define BROTLI_INTERNAL
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))
183#define BROTLI_UNUSED_FUNCTION static BROTLI_INLINE
186#if BROTLI_GNUC_HAS_ATTRIBUTE(aligned, 2, 7, 0)
187#define BROTLI_ALIGNED(N) __attribute__((aligned(N)))
189#define BROTLI_ALIGNED(N)
192#if (defined(__ARM_ARCH) && (__ARM_ARCH == 7)) || \
193 (defined(M_ARM) && (M_ARM == 7))
194#define BROTLI_TARGET_ARMV7
197#if (defined(__ARM_ARCH) && (__ARM_ARCH == 8)) || \
198 defined(__aarch64__) || defined(__ARM64_ARCH_8__)
199#define BROTLI_TARGET_ARMV8_ANY
201#if defined(__ARM_32BIT_STATE)
202#define BROTLI_TARGET_ARMV8_32
203#elif defined(__ARM_64BIT_STATE)
204#define BROTLI_TARGET_ARMV8_64
209#if defined(__ARM_NEON__) || defined(__ARM_NEON)
210#define BROTLI_TARGET_NEON
213#if defined(__i386) || defined(_M_IX86)
214#define BROTLI_TARGET_X86
217#if defined(__x86_64__) || defined(_M_X64)
218#define BROTLI_TARGET_X64
221#if defined(__PPC64__)
222#define BROTLI_TARGET_POWERPC64
225#if defined(__riscv) && defined(__riscv_xlen) && __riscv_xlen == 64
226#define BROTLI_TARGET_RISCV64
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
237#define BROTLI_64_BITS 0
241#define brotli_reg_t uint64_t
243#define brotli_reg_t uint32_t
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)
252#elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
253#define BROTLI_LITTLE_ENDIAN 1
254#elif defined(_WIN32) || defined(BROTLI_TARGET_X64)
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
267#if !defined(BROTLI_LITTLE_ENDIAN)
268#define BROTLI_LITTLE_ENDIAN 0
271#if !defined(BROTLI_BIG_ENDIAN)
272#define BROTLI_BIG_ENDIAN 0
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
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)
287#define BROTLI_ALIGNED_READ (!!0)
289#define BROTLI_ALIGNED_READ (!!1)
292#if BROTLI_ALIGNED_READ
294static BROTLI_INLINE uint16_t BrotliUnalignedRead16(
const void* p) {
296 memcpy(&t, p,
sizeof t);
299static BROTLI_INLINE uint32_t BrotliUnalignedRead32(
const void* p) {
301 memcpy(&t, p,
sizeof t);
304static BROTLI_INLINE uint64_t BrotliUnalignedRead64(
const void* p) {
306 memcpy(&t, p,
sizeof t);
309static BROTLI_INLINE void BrotliUnalignedWrite64(
void* p, uint64_t v) {
310 memcpy(p, &v,
sizeof v);
325#if defined(__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)
335#define BrotliUnalignedRead16 __sanitizer_unaligned_load16
336#define BrotliUnalignedRead32 __sanitizer_unaligned_load32
337#define BrotliUnalignedRead64 __sanitizer_unaligned_load64
338#define BrotliUnalignedWrite64 __sanitizer_unaligned_store64
340static BROTLI_INLINE uint16_t BrotliUnalignedRead16(
const void* p) {
341 return *(
const uint16_t*)p;
343static BROTLI_INLINE uint32_t BrotliUnalignedRead32(
const void* p) {
344 return *(
const uint32_t*)p;
347static BROTLI_INLINE uint64_t BrotliUnalignedRead64(
const void* p) {
348 return *(
const uint64_t*)p;
350static BROTLI_INLINE void BrotliUnalignedWrite64(
void* p, uint64_t v) {
357#if BROTLI_GNUC_HAS_ATTRIBUTE(aligned, 2, 7, 0)
361 return (uint64_t) ((
const brotli_unaligned_uint64_t*) p)[0];
363static 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;
368static BROTLI_INLINE uint64_t BrotliUnalignedRead64(
const void* p) {
370 memcpy(&v, p,
sizeof(uint64_t));
374static BROTLI_INLINE void BrotliUnalignedWrite64(
void* p, uint64_t v) {
375 memcpy(p, &v,
sizeof(uint64_t));
382#if BROTLI_LITTLE_ENDIAN
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
390#define BROTLI_BSWAP16_(V) ((uint16_t)( \
391 (((V) & 0xFFU) << 8) | \
392 (((V) >> 8) & 0xFFU)))
393static BROTLI_INLINE uint16_t BROTLI_UNALIGNED_LOAD16LE(
const void* p) {
394 uint16_t value = BrotliUnalignedRead16(p);
395 return BROTLI_BSWAP16_(value);
397#define BROTLI_BSWAP32_(V) ( \
398 (((V) & 0xFFU) << 24) | (((V) & 0xFF00U) << 8) | \
399 (((V) >> 8) & 0xFF00U) | (((V) >> 24) & 0xFFU))
400static BROTLI_INLINE uint32_t BROTLI_UNALIGNED_LOAD32LE(
const void* p) {
401 uint32_t value = BrotliUnalignedRead32(p);
402 return BROTLI_BSWAP32_(value);
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))
409static BROTLI_INLINE uint64_t BROTLI_UNALIGNED_LOAD64LE(
const void* p) {
410 uint64_t value = BrotliUnalignedRead64(p);
411 return BROTLI_BSWAP64_(value);
413static BROTLI_INLINE void BROTLI_UNALIGNED_STORE64LE(
void* p, uint64_t v) {
414 uint64_t value = BROTLI_BSWAP64_(v);
415 BrotliUnalignedWrite64(p, value);
419static 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));
423static 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;
431static 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;
443static BROTLI_INLINE void BROTLI_UNALIGNED_STORE64LE(
void* p, uint64_t v) {
444 uint8_t*
out = (uint8_t*)p;
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);
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))
461#define BROTLI_IS_CONSTANT(x) (!!0)
464#if defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_ANY)
465#define BROTLI_HAS_UBFX (!!1)
467#define BROTLI_HAS_UBFX (!!0)
470#if defined(BROTLI_ENABLE_LOG)
471#define BROTLI_LOG(x) printf x
476#if defined(BROTLI_DEBUG) || defined(BROTLI_ENABLE_LOG)
477#define BROTLI_DCHECK(x) assert(x)
478static BROTLI_INLINE void BrotliDump(
const char*
f,
int l,
const char* fn) {
479 fprintf(stderr,
"%s:%d (%s)\n",
f, l, fn);
482#define BROTLI_DUMP() BrotliDump(__FILE__, __LINE__, __FUNCTION__)
484#define BROTLI_DCHECK(x)
485#define BROTLI_DUMP() (void)(0)
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)
495 __asm__(
"rbit %0, %1\n" :
"=r"(
output) :
"r"(
input));
498#define BROTLI_RBIT(x) BrotliRBit(x)
501#if !defined(BROTLI_RBIT)
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;} \
511#define BROTLI_UNUSED(X) (void)(X)
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; }
519#define BROTLI_MIN(T, A, B) (brotli_min_ ## T((A), (B)))
520#define BROTLI_MAX(T, A, B) (brotli_max_ ## T((A), (B)))
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; \
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
538 _BitScanForward64(&lsb, x);
541#define BROTLI_TZCNT64 BrotliBsf64Msvc
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)
552 _BitScanReverse(&msb, x);
553 return (uint32_t)msb;
555#define BROTLI_BSR32 BrotliBsr32Msvc
589#if defined(BROTLI_DEBUG) || defined(BROTLI_ENABLE_LOG)
#define BROTLI_COMMON_API
Definition port.h:283
#define input(b, o, c, n, m)
Definition compress42.c:610
#define output(b, o, c, n)
Definition compress42.c:592
#define const
Definition zconf.h:230
#define f(i)
Definition sha256.c:46
const lzma_allocator const uint8_t * in
Definition block.h:527
const lzma_allocator const uint8_t size_t uint8_t * out
Definition block.h:528