24#ifndef BROTLI_COMMON_PLATFORM_H_
25#define BROTLI_COMMON_PLATFORM_H_
29#include <brotli/port.h>
30#include <brotli/types.h>
32#if defined(OS_LINUX) || defined(OS_CYGWIN) || defined(__EMSCRIPTEN__)
34#elif defined(OS_FREEBSD)
35#include <machine/endian.h>
36#elif defined(OS_MACOSX)
37#include <machine/endian.h>
39#define BROTLI_X_BYTE_ORDER BYTE_ORDER
40#define BROTLI_X_LITTLE_ENDIAN LITTLE_ENDIAN
41#define BROTLI_X_BIG_ENDIAN BIG_ENDIAN
44#if BROTLI_MSVC_VERSION_CHECK(18, 0, 0)
48#if defined(BROTLI_ENABLE_LOG) || defined(BROTLI_DEBUG)
76#if BROTLI_GNUC_HAS_BUILTIN(__builtin_expect, 3, 0, 0) || \
77 BROTLI_INTEL_VERSION_CHECK(16, 0, 0) || \
78 BROTLI_SUNPRO_VERSION_CHECK(5, 15, 0) || \
79 BROTLI_ARM_VERSION_CHECK(4, 1, 0) || \
80 BROTLI_IBM_VERSION_CHECK(10, 1, 0) || \
81 BROTLI_TI_VERSION_CHECK(7, 3, 0) || \
82 BROTLI_TINYC_VERSION_CHECK(0, 9, 27)
83#define BROTLI_PREDICT_TRUE(x) (__builtin_expect(!!(x), 1))
84#define BROTLI_PREDICT_FALSE(x) (__builtin_expect(x, 0))
86#define BROTLI_PREDICT_FALSE(x) (x)
87#define BROTLI_PREDICT_TRUE(x) (x)
90#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \
92#define BROTLI_RESTRICT restrict
93#elif BROTLI_GNUC_VERSION_CHECK(3, 1, 0) || \
94 BROTLI_MSVC_VERSION_CHECK(14, 0, 0) || \
95 BROTLI_INTEL_VERSION_CHECK(16, 0, 0) || \
96 BROTLI_ARM_VERSION_CHECK(4, 1, 0) || \
97 BROTLI_IBM_VERSION_CHECK(10, 1, 0) || \
98 BROTLI_PGI_VERSION_CHECK(17, 10, 0) || \
99 BROTLI_TI_VERSION_CHECK(8, 0, 0) || \
100 BROTLI_IAR_VERSION_CHECK(8, 0, 0) || \
101 (BROTLI_SUNPRO_VERSION_CHECK(5, 14, 0) && defined(__cplusplus))
102#define BROTLI_RESTRICT __restrict
103#elif BROTLI_SUNPRO_VERSION_CHECK(5, 3, 0) && !defined(__cplusplus)
104#define BROTLI_RESTRICT _Restrict
106#define BROTLI_RESTRICT
109#if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \
110 (defined(__cplusplus) && (__cplusplus >= 199711L))
111#define BROTLI_MAYBE_INLINE inline
112#elif defined(__GNUC_STDC_INLINE__) || defined(__GNUC_GNU_INLINE__) || \
113 BROTLI_ARM_VERSION_CHECK(6, 2, 0)
114#define BROTLI_MAYBE_INLINE __inline__
115#elif BROTLI_MSVC_VERSION_CHECK(12, 0, 0) || \
116 BROTLI_ARM_VERSION_CHECK(4, 1, 0) || BROTLI_TI_VERSION_CHECK(8, 0, 0)
117#define BROTLI_MAYBE_INLINE __inline
119#define BROTLI_MAYBE_INLINE
122#if BROTLI_GNUC_HAS_ATTRIBUTE(always_inline, 4, 0, 0) || \
123 BROTLI_INTEL_VERSION_CHECK(16, 0, 0) || \
124 BROTLI_SUNPRO_VERSION_CHECK(5, 11, 0) || \
125 BROTLI_ARM_VERSION_CHECK(4, 1, 0) || \
126 BROTLI_IBM_VERSION_CHECK(10, 1, 0) || \
127 BROTLI_TI_VERSION_CHECK(8, 0, 0) || \
128 (BROTLI_TI_VERSION_CHECK(7, 3, 0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__))
129#define BROTLI_INLINE BROTLI_MAYBE_INLINE __attribute__((__always_inline__))
130#elif BROTLI_MSVC_VERSION_CHECK(12, 0, 0)
131#define BROTLI_INLINE BROTLI_MAYBE_INLINE __forceinline
132#elif BROTLI_TI_VERSION_CHECK(7, 0, 0) && defined(__cplusplus)
133#define BROTLI_INLINE BROTLI_MAYBE_INLINE _Pragma("FUNC_ALWAYS_INLINE;")
134#elif BROTLI_IAR_VERSION_CHECK(8, 0, 0)
135#define BROTLI_INLINE BROTLI_MAYBE_INLINE _Pragma("inline=forced")
137#define BROTLI_INLINE BROTLI_MAYBE_INLINE
140#if BROTLI_GNUC_HAS_ATTRIBUTE(noinline, 4, 0, 0) || \
141 BROTLI_INTEL_VERSION_CHECK(16, 0, 0) || \
142 BROTLI_SUNPRO_VERSION_CHECK(5, 11, 0) || \
143 BROTLI_ARM_VERSION_CHECK(4, 1, 0) || \
144 BROTLI_IBM_VERSION_CHECK(10, 1, 0) || \
145 BROTLI_TI_VERSION_CHECK(8, 0, 0) || \
146 (BROTLI_TI_VERSION_CHECK(7, 3, 0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__))
147#define BROTLI_NOINLINE __attribute__((__noinline__))
148#elif BROTLI_MSVC_VERSION_CHECK(13, 10, 0)
149#define BROTLI_NOINLINE __declspec(noinline)
150#elif BROTLI_PGI_VERSION_CHECK(10, 2, 0)
151#define BROTLI_NOINLINE _Pragma("noinline")
152#elif BROTLI_TI_VERSION_CHECK(6, 0, 0) && defined(__cplusplus)
153#define BROTLI_NOINLINE _Pragma("FUNC_CANNOT_INLINE;")
154#elif BROTLI_IAR_VERSION_CHECK(8, 0, 0)
155#define BROTLI_NOINLINE _Pragma("inline=never")
157#define BROTLI_NOINLINE
162#if BROTLI_GNUC_HAS_ATTRIBUTE(unused, 2, 7, 0) || \
163 BROTLI_INTEL_VERSION_CHECK(16, 0, 0)
164#define BROTLI_UNUSED_FUNCTION static BROTLI_INLINE __attribute__ ((unused))
166#define BROTLI_UNUSED_FUNCTION static BROTLI_INLINE
169#if BROTLI_GNUC_HAS_ATTRIBUTE(aligned, 2, 7, 0)
170#define BROTLI_ALIGNED(N) __attribute__((aligned(N)))
172#define BROTLI_ALIGNED(N)
175#if (defined(__ARM_ARCH) && (__ARM_ARCH == 7)) || \
176 (defined(M_ARM) && (M_ARM == 7))
177#define BROTLI_TARGET_ARMV7
180#if (defined(__ARM_ARCH) && (__ARM_ARCH == 8)) || \
181 defined(__aarch64__) || defined(__ARM64_ARCH_8__)
182#define BROTLI_TARGET_ARMV8_ANY
184#if defined(__ARM_32BIT_STATE)
185#define BROTLI_TARGET_ARMV8_32
186#elif defined(__ARM_64BIT_STATE)
187#define BROTLI_TARGET_ARMV8_64
192#if defined(__ARM_NEON__) || defined(__ARM_NEON)
193#define BROTLI_TARGET_NEON
196#if defined(__i386) || defined(_M_IX86)
197#define BROTLI_TARGET_X86
200#if defined(__x86_64__) || defined(_M_X64)
201#define BROTLI_TARGET_X64
204#if defined(__PPC64__)
205#define BROTLI_TARGET_POWERPC64
208#if defined(__riscv) && defined(__riscv_xlen) && __riscv_xlen == 64
209#define BROTLI_TARGET_RISCV64
212#if defined(__loongarch_lp64)
213#define BROTLI_TARGET_LOONGARCH64
216#if defined(BROTLI_TARGET_X64) || defined(BROTLI_TARGET_ARMV8_64) || \
217 defined(BROTLI_TARGET_POWERPC64) || defined(BROTLI_TARGET_RISCV64) || \
218 defined(BROTLI_TARGET_LOONGARCH64)
219#define BROTLI_TARGET_64_BITS 1
221#define BROTLI_TARGET_64_BITS 0
224#if defined(BROTLI_BUILD_64_BIT)
225#define BROTLI_64_BITS 1
226#elif defined(BROTLI_BUILD_32_BIT)
227#define BROTLI_64_BITS 0
229#define BROTLI_64_BITS BROTLI_TARGET_64_BITS
233#define brotli_reg_t uint64_t
235#define brotli_reg_t uint32_t
238#if defined(BROTLI_BUILD_BIG_ENDIAN)
239#define BROTLI_BIG_ENDIAN 1
240#elif defined(BROTLI_BUILD_LITTLE_ENDIAN)
241#define BROTLI_LITTLE_ENDIAN 1
242#elif defined(BROTLI_BUILD_ENDIAN_NEUTRAL)
244#elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
245#define BROTLI_LITTLE_ENDIAN 1
246#elif defined(_WIN32) || defined(BROTLI_TARGET_X64)
248#define BROTLI_LITTLE_ENDIAN 1
249#elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
250#define BROTLI_BIG_ENDIAN 1
251#elif defined(BROTLI_X_BYTE_ORDER)
252#if BROTLI_X_BYTE_ORDER == BROTLI_X_LITTLE_ENDIAN
253#define BROTLI_LITTLE_ENDIAN 1
254#elif BROTLI_X_BYTE_ORDER == BROTLI_X_BIG_ENDIAN
255#define BROTLI_BIG_ENDIAN 1
259#if !defined(BROTLI_LITTLE_ENDIAN)
260#define BROTLI_LITTLE_ENDIAN 0
263#if !defined(BROTLI_BIG_ENDIAN)
264#define BROTLI_BIG_ENDIAN 0
267#if defined(BROTLI_X_BYTE_ORDER)
268#undef BROTLI_X_BYTE_ORDER
269#undef BROTLI_X_LITTLE_ENDIAN
270#undef BROTLI_X_BIG_ENDIAN
273#if defined(BROTLI_BUILD_NO_UNALIGNED_READ_FAST)
274#define BROTLI_UNALIGNED_READ_FAST (!!0)
275#elif defined(BROTLI_TARGET_X86) || defined(BROTLI_TARGET_X64) || \
276 defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_ANY) || \
277 defined(BROTLI_TARGET_RISCV64) || defined(BROTLI_TARGET_LOONGARCH64)
281#define BROTLI_UNALIGNED_READ_FAST (!!1)
283#define BROTLI_UNALIGNED_READ_FAST (!!0)
287static BROTLI_INLINE uint16_t BrotliUnalignedRead16(
const void* p) {
289 memcpy(&t, p,
sizeof t);
292static BROTLI_INLINE uint32_t BrotliUnalignedRead32(
const void* p) {
294 memcpy(&t, p,
sizeof t);
297static BROTLI_INLINE uint64_t BrotliUnalignedRead64(
const void* p) {
299 memcpy(&t, p,
sizeof t);
302static BROTLI_INLINE void BrotliUnalignedWrite64(
void* p, uint64_t v) {
303 memcpy(p, &v,
sizeof v);
306#if BROTLI_LITTLE_ENDIAN
308#define BROTLI_UNALIGNED_LOAD16LE BrotliUnalignedRead16
309#define BROTLI_UNALIGNED_LOAD32LE BrotliUnalignedRead32
310#define BROTLI_UNALIGNED_LOAD64LE BrotliUnalignedRead64
311#define BROTLI_UNALIGNED_STORE64LE BrotliUnalignedWrite64
312#elif BROTLI_BIG_ENDIAN
314#define BROTLI_BSWAP16_(V) ((uint16_t)( \
315 (((V) & 0xFFU) << 8) | \
316 (((V) >> 8) & 0xFFU)))
317static BROTLI_INLINE uint16_t BROTLI_UNALIGNED_LOAD16LE(
const void* p) {
318 uint16_t value = BrotliUnalignedRead16(p);
319 return BROTLI_BSWAP16_(value);
321#define BROTLI_BSWAP32_(V) ( \
322 (((V) & 0xFFU) << 24) | (((V) & 0xFF00U) << 8) | \
323 (((V) >> 8) & 0xFF00U) | (((V) >> 24) & 0xFFU))
324static BROTLI_INLINE uint32_t BROTLI_UNALIGNED_LOAD32LE(
const void* p) {
325 uint32_t value = BrotliUnalignedRead32(p);
326 return BROTLI_BSWAP32_(value);
328#define BROTLI_BSWAP64_(V) ( \
329 (((V) & 0xFFU) << 56) | (((V) & 0xFF00U) << 40) | \
330 (((V) & 0xFF0000U) << 24) | (((V) & 0xFF000000U) << 8) | \
331 (((V) >> 8) & 0xFF000000U) | (((V) >> 24) & 0xFF0000U) | \
332 (((V) >> 40) & 0xFF00U) | (((V) >> 56) & 0xFFU))
333static BROTLI_INLINE uint64_t BROTLI_UNALIGNED_LOAD64LE(
const void* p) {
334 uint64_t value = BrotliUnalignedRead64(p);
335 return BROTLI_BSWAP64_(value);
337static BROTLI_INLINE void BROTLI_UNALIGNED_STORE64LE(
void* p, uint64_t v) {
338 uint64_t value = BROTLI_BSWAP64_(v);
339 BrotliUnalignedWrite64(p, value);
343static BROTLI_INLINE uint16_t BROTLI_UNALIGNED_LOAD16LE(
const void* p) {
344 const uint8_t*
in = (
const uint8_t*)p;
345 return (uint16_t)(
in[0] | (
in[1] << 8));
347static BROTLI_INLINE uint32_t BROTLI_UNALIGNED_LOAD32LE(
const void* p) {
348 const uint8_t*
in = (
const uint8_t*)p;
349 uint32_t value = (uint32_t)(in[0]);
350 value |= (uint32_t)(in[1]) << 8;
351 value |= (uint32_t)(in[2]) << 16;
352 value |= (uint32_t)(in[3]) << 24;
355static BROTLI_INLINE uint64_t BROTLI_UNALIGNED_LOAD64LE(
const void* p) {
356 const uint8_t*
in = (
const uint8_t*)p;
357 uint64_t value = (uint64_t)(in[0]);
358 value |= (uint64_t)(in[1]) << 8;
359 value |= (uint64_t)(in[2]) << 16;
360 value |= (uint64_t)(in[3]) << 24;
361 value |= (uint64_t)(in[4]) << 32;
362 value |= (uint64_t)(in[5]) << 40;
363 value |= (uint64_t)(in[6]) << 48;
364 value |= (uint64_t)(in[7]) << 56;
367static BROTLI_INLINE void BROTLI_UNALIGNED_STORE64LE(
void* p, uint64_t v) {
368 uint8_t*
out = (uint8_t*)p;
370 out[1] = (uint8_t)(v >> 8);
371 out[2] = (uint8_t)(v >> 16);
372 out[3] = (uint8_t)(v >> 24);
373 out[4] = (uint8_t)(v >> 32);
374 out[5] = (uint8_t)(v >> 40);
375 out[6] = (uint8_t)(v >> 48);
376 out[7] = (uint8_t)(v >> 56);
380static BROTLI_INLINE void* BROTLI_UNALIGNED_LOAD_PTR(
const void* p) {
382 memcpy(&v, p,
sizeof(
void*));
386static BROTLI_INLINE void BROTLI_UNALIGNED_STORE_PTR(
void* p,
const void* v) {
387 memcpy(p, &v,
sizeof(
void*));
391#if BROTLI_GNUC_HAS_BUILTIN(__builtin_constant_p, 3, 0, 1) || \
392 BROTLI_INTEL_VERSION_CHECK(16, 0, 0)
393#define BROTLI_IS_CONSTANT(x) (!!__builtin_constant_p(x))
395#define BROTLI_IS_CONSTANT(x) (!!0)
398#if defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_ANY)
399#define BROTLI_HAS_UBFX (!!1)
401#define BROTLI_HAS_UBFX (!!0)
404#if defined(BROTLI_ENABLE_LOG)
405#define BROTLI_LOG(x) printf x
410#if defined(BROTLI_DEBUG) || defined(BROTLI_ENABLE_LOG)
411#define BROTLI_ENABLE_DUMP_DEFAULT 1
412#define BROTLI_DCHECK(x) assert(x)
414#define BROTLI_ENABLE_DUMP_DEFAULT 0
415#define BROTLI_DCHECK(x)
418#if !defined(BROTLI_ENABLE_DUMP)
419#define BROTLI_ENABLE_DUMP BROTLI_ENABLE_DUMP_DEFAULT
422#if BROTLI_ENABLE_DUMP
423static BROTLI_INLINE void BrotliDump(
const char*
f,
int l,
const char* fn) {
424 fprintf(stderr,
"%s:%d (%s)\n",
f, l, fn);
427#define BROTLI_DUMP() BrotliDump(__FILE__, __LINE__, __FUNCTION__)
429#define BROTLI_DUMP() (void)(0)
433#if (BROTLI_64_BITS == BROTLI_TARGET_64_BITS)
435#if (BROTLI_GNUC_VERSION_CHECK(3, 0, 0) || defined(__llvm__)) && \
436 !defined(BROTLI_BUILD_NO_RBIT)
437#if defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_ANY)
441 __asm__(
"rbit %0, %1\n" :
"=r"(
output) :
"r"(
input));
444#define BROTLI_RBIT(x) BrotliRBit(x)
448#if !defined(BROTLI_RBIT)
452#define BROTLI_REPEAT_4(X) {X; X; X; X;}
453#define BROTLI_REPEAT_5(X) {X; X; X; X; X;}
454#define BROTLI_REPEAT_6(X) {X; X; X; X; X; X;}
456#define BROTLI_UNUSED(X) (void)(X)
458#define BROTLI_MIN_MAX(T) \
459 static BROTLI_INLINE T brotli_min_ ## T (T a, T b) { return a < b ? a : b; } \
460 static BROTLI_INLINE T brotli_max_ ## T (T a, T b) { return a > b ? a : b; }
464#define BROTLI_MIN(T, A, B) (brotli_min_ ## T((A), (B)))
465#define BROTLI_MAX(T, A, B) (brotli_max_ ## T((A), (B)))
467#define BROTLI_SWAP(T, A, I, J) { \
468 T __brotli_swap_tmp = (A)[(I)]; \
469 (A)[(I)] = (A)[(J)]; \
470 (A)[(J)] = __brotli_swap_tmp; \
474#if BROTLI_GNUC_HAS_BUILTIN(__builtin_ctzll, 3, 4, 0) || \
475 BROTLI_INTEL_VERSION_CHECK(16, 0, 0)
476#define BROTLI_TZCNT64 __builtin_ctzll
477#elif BROTLI_MSVC_VERSION_CHECK(18, 0, 0)
478#if defined(BROTLI_TARGET_X64)
479#define BROTLI_TZCNT64 _tzcnt_u64
483 _BitScanForward64(&lsb, x);
486#define BROTLI_TZCNT64 BrotliBsf64Msvc
491#if BROTLI_GNUC_HAS_BUILTIN(__builtin_clz, 3, 4, 0) || \
492 BROTLI_INTEL_VERSION_CHECK(16, 0, 0)
493#define BROTLI_BSR32(x) (31u ^ (uint32_t)__builtin_clz(x))
494#elif BROTLI_MSVC_VERSION_CHECK(18, 0, 0)
497 _BitScanReverse(&msb, x);
498 return (uint32_t)msb;
500#define BROTLI_BSR32 BrotliBsr32Msvc
536#if BROTLI_ENABLE_DUMP
#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 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