• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_NO_RBIT disables "rbit" optimization for ARM CPUs
16     * BROTLI_BUILD_NO_UNALIGNED_READ_FAST forces off the fast-unaligned-read
17       optimizations (mainly for testing purposes)
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     * BROTLI_ENABLE_DUMP overrides default "dump" behaviour
22 */
23 
24 #ifndef BROTLI_COMMON_PLATFORM_H_
25 #define BROTLI_COMMON_PLATFORM_H_
26 
27 #include <string.h>  /* memcpy */
28 
29 #include <brotli/port.h>
30 #include <brotli/types.h>
31 
32 #if defined(OS_LINUX) || defined(OS_CYGWIN) || defined(__EMSCRIPTEN__)
33 #include <endian.h>
34 #elif defined(OS_FREEBSD)
35 #include <machine/endian.h>
36 #elif defined(OS_MACOSX)
37 #include <machine/endian.h>
38 /* Let's try and follow the Linux convention */
39 #define BROTLI_X_BYTE_ORDER BYTE_ORDER
40 #define BROTLI_X_LITTLE_ENDIAN LITTLE_ENDIAN
41 #define BROTLI_X_BIG_ENDIAN BIG_ENDIAN
42 #endif
43 
44 #if BROTLI_MSVC_VERSION_CHECK(18, 0, 0)
45 #include <intrin.h>
46 #endif
47 
48 #if defined(BROTLI_ENABLE_LOG) || defined(BROTLI_DEBUG)
49 #include <assert.h>
50 #include <stdio.h>
51 #endif
52 
53 /* The following macros were borrowed from https://github.com/nemequ/hedley
54  * with permission of original author - Evan Nemerson <evan@nemerson.com> */
55 
56 /* >>> >>> >>> hedley macros */
57 
58 /* Define "BROTLI_PREDICT_TRUE" and "BROTLI_PREDICT_FALSE" macros for capable
59    compilers.
60 
61 To apply compiler hint, enclose the branching condition into macros, like this:
62 
63   if (BROTLI_PREDICT_TRUE(zero == 0)) {
64     // main execution path
65   } else {
66     // compiler should place this code outside of main execution path
67   }
68 
69 OR:
70 
71   if (BROTLI_PREDICT_FALSE(something_rare_or_unexpected_happens)) {
72     // compiler should place this code outside of main execution path
73   }
74 
75 */
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))
85 #else
86 #define BROTLI_PREDICT_FALSE(x) (x)
87 #define BROTLI_PREDICT_TRUE(x) (x)
88 #endif
89 
90 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \
91     !defined(__cplusplus)
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
105 #else
106 #define BROTLI_RESTRICT
107 #endif
108 
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
118 #else
119 #define BROTLI_MAYBE_INLINE
120 #endif
121 
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")
136 #else
137 #define BROTLI_INLINE BROTLI_MAYBE_INLINE
138 #endif
139 
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")
156 #else
157 #define BROTLI_NOINLINE
158 #endif
159 
160 /* <<< <<< <<< end of hedley macros. */
161 
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))
165 #else
166 #define BROTLI_UNUSED_FUNCTION static BROTLI_INLINE
167 #endif
168 
169 #if BROTLI_GNUC_HAS_ATTRIBUTE(aligned, 2, 7, 0)
170 #define BROTLI_ALIGNED(N) __attribute__((aligned(N)))
171 #else
172 #define BROTLI_ALIGNED(N)
173 #endif
174 
175 #if (defined(__ARM_ARCH) && (__ARM_ARCH == 7)) || \
176     (defined(M_ARM) && (M_ARM == 7))
177 #define BROTLI_TARGET_ARMV7
178 #endif  /* ARMv7 */
179 
180 #if (defined(__ARM_ARCH) && (__ARM_ARCH == 8)) || \
181     defined(__aarch64__) || defined(__ARM64_ARCH_8__)
182 #define BROTLI_TARGET_ARMV8_ANY
183 
184 #if defined(__ARM_32BIT_STATE)
185 #define BROTLI_TARGET_ARMV8_32
186 #elif defined(__ARM_64BIT_STATE)
187 #define BROTLI_TARGET_ARMV8_64
188 #endif
189 
190 #endif  /* ARMv8 */
191 
192 #if defined(__ARM_NEON__) || defined(__ARM_NEON)
193 #define BROTLI_TARGET_NEON
194 #endif
195 
196 #if defined(__i386) || defined(_M_IX86)
197 #define BROTLI_TARGET_X86
198 #endif
199 
200 #if defined(__x86_64__) || defined(_M_X64)
201 #define BROTLI_TARGET_X64
202 #endif
203 
204 #if defined(__PPC64__)
205 #define BROTLI_TARGET_POWERPC64
206 #endif
207 
208 #if defined(__riscv) && defined(__riscv_xlen) && __riscv_xlen == 64
209 #define BROTLI_TARGET_RISCV64
210 #endif
211 
212 #if defined(__loongarch_lp64)
213 #define BROTLI_TARGET_LOONGARCH64
214 #endif
215 
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
220 #else
221 #define BROTLI_TARGET_64_BITS 0
222 #endif
223 
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
228 #else
229 #define BROTLI_64_BITS BROTLI_TARGET_64_BITS
230 #endif
231 
232 #if (BROTLI_64_BITS)
233 #define brotli_reg_t uint64_t
234 #else
235 #define brotli_reg_t uint32_t
236 #endif
237 
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)
243 /* Just break elif chain. */
244 #elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
245 #define BROTLI_LITTLE_ENDIAN 1
246 #elif defined(_WIN32) || defined(BROTLI_TARGET_X64)
247 /* Win32 & x64 can currently always be assumed to be little endian */
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
256 #endif
257 #endif  /* BROTLI_X_BYTE_ORDER */
258 
259 #if !defined(BROTLI_LITTLE_ENDIAN)
260 #define BROTLI_LITTLE_ENDIAN 0
261 #endif
262 
263 #if !defined(BROTLI_BIG_ENDIAN)
264 #define BROTLI_BIG_ENDIAN 0
265 #endif
266 
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
271 #endif
272 
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)
278 /* These targets are known to generate efficient code for unaligned reads
279  * (e.g. a single instruction, not multiple 1-byte loads, shifted and or'd
280  * together). */
281 #define BROTLI_UNALIGNED_READ_FAST (!!1)
282 #else
283 #define BROTLI_UNALIGNED_READ_FAST (!!0)
284 #endif
285 
286 /* Portable unaligned memory access: read / write values via memcpy. */
BrotliUnalignedRead16(const void * p)287 static BROTLI_INLINE uint16_t BrotliUnalignedRead16(const void* p) {
288   uint16_t t;
289   memcpy(&t, p, sizeof t);
290   return t;
291 }
BrotliUnalignedRead32(const void * p)292 static BROTLI_INLINE uint32_t BrotliUnalignedRead32(const void* p) {
293   uint32_t t;
294   memcpy(&t, p, sizeof t);
295   return t;
296 }
BrotliUnalignedRead64(const void * p)297 static BROTLI_INLINE uint64_t BrotliUnalignedRead64(const void* p) {
298   uint64_t t;
299   memcpy(&t, p, sizeof t);
300   return t;
301 }
BrotliUnalignedWrite64(void * p,uint64_t v)302 static BROTLI_INLINE void BrotliUnalignedWrite64(void* p, uint64_t v) {
303   memcpy(p, &v, sizeof v);
304 }
305 
306 #if BROTLI_LITTLE_ENDIAN
307 /* Straight endianness. Just read / write values. */
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  /* BROTLI_LITTLE_ENDIAN */
313 /* Explain compiler to byte-swap values. */
314 #define BROTLI_BSWAP16_(V) ((uint16_t)( \
315   (((V) & 0xFFU) << 8) | \
316   (((V) >> 8) & 0xFFU)))
BROTLI_UNALIGNED_LOAD16LE(const void * p)317 static BROTLI_INLINE uint16_t BROTLI_UNALIGNED_LOAD16LE(const void* p) {
318   uint16_t value = BrotliUnalignedRead16(p);
319   return BROTLI_BSWAP16_(value);
320 }
321 #define BROTLI_BSWAP32_(V) ( \
322   (((V) & 0xFFU) << 24) | (((V) & 0xFF00U) << 8) | \
323   (((V) >> 8) & 0xFF00U) | (((V) >> 24) & 0xFFU))
BROTLI_UNALIGNED_LOAD32LE(const void * p)324 static BROTLI_INLINE uint32_t BROTLI_UNALIGNED_LOAD32LE(const void* p) {
325   uint32_t value = BrotliUnalignedRead32(p);
326   return BROTLI_BSWAP32_(value);
327 }
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))
BROTLI_UNALIGNED_LOAD64LE(const void * p)333 static BROTLI_INLINE uint64_t BROTLI_UNALIGNED_LOAD64LE(const void* p) {
334   uint64_t value = BrotliUnalignedRead64(p);
335   return BROTLI_BSWAP64_(value);
336 }
BROTLI_UNALIGNED_STORE64LE(void * p,uint64_t v)337 static BROTLI_INLINE void BROTLI_UNALIGNED_STORE64LE(void* p, uint64_t v) {
338   uint64_t value = BROTLI_BSWAP64_(v);
339   BrotliUnalignedWrite64(p, value);
340 }
341 #else  /* BROTLI_LITTLE_ENDIAN */
342 /* Read / store values byte-wise; hopefully compiler will understand. */
BROTLI_UNALIGNED_LOAD16LE(const void * p)343 static 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));
346 }
BROTLI_UNALIGNED_LOAD32LE(const void * p)347 static 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;
353   return value;
354 }
BROTLI_UNALIGNED_LOAD64LE(const void * p)355 static 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;
365   return value;
366 }
BROTLI_UNALIGNED_STORE64LE(void * p,uint64_t v)367 static BROTLI_INLINE void BROTLI_UNALIGNED_STORE64LE(void* p, uint64_t v) {
368   uint8_t* out = (uint8_t*)p;
369   out[0] = (uint8_t)v;
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);
377 }
378 #endif  /* BROTLI_LITTLE_ENDIAN */
379 
BROTLI_UNALIGNED_LOAD_PTR(const void * p)380 static BROTLI_INLINE void* BROTLI_UNALIGNED_LOAD_PTR(const void* p) {
381   void* v;
382   memcpy(&v, p, sizeof(void*));
383   return v;
384 }
385 
BROTLI_UNALIGNED_STORE_PTR(void * p,const void * v)386 static BROTLI_INLINE void BROTLI_UNALIGNED_STORE_PTR(void* p, const void* v) {
387   memcpy(p, &v, sizeof(void*));
388 }
389 
390 /* BROTLI_IS_CONSTANT macros returns true for compile-time constants. */
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))
394 #else
395 #define BROTLI_IS_CONSTANT(x) (!!0)
396 #endif
397 
398 #if defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_ANY)
399 #define BROTLI_HAS_UBFX (!!1)
400 #else
401 #define BROTLI_HAS_UBFX (!!0)
402 #endif
403 
404 #if defined(BROTLI_ENABLE_LOG)
405 #define BROTLI_LOG(x) printf x
406 #else
407 #define BROTLI_LOG(x)
408 #endif
409 
410 #if defined(BROTLI_DEBUG) || defined(BROTLI_ENABLE_LOG)
411 #define BROTLI_ENABLE_DUMP_DEFAULT 1
412 #define BROTLI_DCHECK(x) assert(x)
413 #else
414 #define BROTLI_ENABLE_DUMP_DEFAULT 0
415 #define BROTLI_DCHECK(x)
416 #endif
417 
418 #if !defined(BROTLI_ENABLE_DUMP)
419 #define BROTLI_ENABLE_DUMP BROTLI_ENABLE_DUMP_DEFAULT
420 #endif
421 
422 #if BROTLI_ENABLE_DUMP
BrotliDump(const char * f,int l,const char * fn)423 static BROTLI_INLINE void BrotliDump(const char* f, int l, const char* fn) {
424   fprintf(stderr, "%s:%d (%s)\n", f, l, fn);
425   fflush(stderr);
426 }
427 #define BROTLI_DUMP() BrotliDump(__FILE__, __LINE__, __FUNCTION__)
428 #else
429 #define BROTLI_DUMP() (void)(0)
430 #endif
431 
432 /* BrotliRBit assumes brotli_reg_t fits native CPU register type. */
433 #if (BROTLI_64_BITS == BROTLI_TARGET_64_BITS)
434 /* TODO(eustas): add appropriate icc/sunpro/arm/ibm/ti checks. */
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)
438 /* TODO(eustas): detect ARMv6T2 and enable this code for it. */
BrotliRBit(brotli_reg_t input)439 static BROTLI_INLINE brotli_reg_t BrotliRBit(brotli_reg_t input) {
440   brotli_reg_t output;
441   __asm__("rbit %0, %1\n" : "=r"(output) : "r"(input));
442   return output;
443 }
444 #define BROTLI_RBIT(x) BrotliRBit(x)
445 #endif  /* armv7 / armv8 */
446 #endif  /* gcc || clang */
447 #endif  /* brotli_reg_t is native */
448 #if !defined(BROTLI_RBIT)
BrotliRBit(void)449 static BROTLI_INLINE void BrotliRBit(void) { /* Should break build if used. */ }
450 #endif  /* BROTLI_RBIT */
451 
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;}
455 
456 #define BROTLI_UNUSED(X) (void)(X)
457 
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; }
461 BROTLI_MIN_MAX(double) BROTLI_MIN_MAX(float) BROTLI_MIN_MAX(int)
462 BROTLI_MIN_MAX(size_t) BROTLI_MIN_MAX(uint32_t) BROTLI_MIN_MAX(uint8_t)
463 #undef BROTLI_MIN_MAX
464 #define BROTLI_MIN(T, A, B) (brotli_min_ ## T((A), (B)))
465 #define BROTLI_MAX(T, A, B) (brotli_max_ ## T((A), (B)))
466 
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;   \
471 }
472 
473 #if BROTLI_64_BITS
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
480 #else /* BROTLI_TARGET_X64 */
481 static BROTLI_INLINE uint32_t BrotliBsf64Msvc(uint64_t x) {
482   uint32_t lsb;
483   _BitScanForward64(&lsb, x);
484   return lsb;
485 }
486 #define BROTLI_TZCNT64 BrotliBsf64Msvc
487 #endif /* BROTLI_TARGET_X64 */
488 #endif /* __builtin_ctzll */
489 #endif /* BROTLI_64_BITS */
490 
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)
495 static BROTLI_INLINE uint32_t BrotliBsr32Msvc(uint32_t x) {
496   unsigned long msb;
497   _BitScanReverse(&msb, x);
498   return (uint32_t)msb;
499 }
500 #define BROTLI_BSR32 BrotliBsr32Msvc
501 #endif /* __builtin_clz */
502 
503 /* Default brotli_alloc_func */
504 BROTLI_COMMON_API void* BrotliDefaultAllocFunc(void* opaque, size_t size);
505 
506 /* Default brotli_free_func */
507 BROTLI_COMMON_API void BrotliDefaultFreeFunc(void* opaque, void* address);
508 
BrotliSuppressUnusedFunctions(void)509 BROTLI_UNUSED_FUNCTION void BrotliSuppressUnusedFunctions(void) {
510   BROTLI_UNUSED(&BrotliSuppressUnusedFunctions);
511   BROTLI_UNUSED(&BrotliUnalignedRead16);
512   BROTLI_UNUSED(&BrotliUnalignedRead32);
513   BROTLI_UNUSED(&BrotliUnalignedRead64);
514   BROTLI_UNUSED(&BrotliUnalignedWrite64);
515   BROTLI_UNUSED(&BROTLI_UNALIGNED_LOAD16LE);
516   BROTLI_UNUSED(&BROTLI_UNALIGNED_LOAD32LE);
517   BROTLI_UNUSED(&BROTLI_UNALIGNED_LOAD64LE);
518   BROTLI_UNUSED(&BROTLI_UNALIGNED_STORE64LE);
519   BROTLI_UNUSED(&BROTLI_UNALIGNED_LOAD_PTR);
520   BROTLI_UNUSED(&BROTLI_UNALIGNED_STORE_PTR);
521   BROTLI_UNUSED(&BrotliRBit);
522   BROTLI_UNUSED(&brotli_min_double);
523   BROTLI_UNUSED(&brotli_max_double);
524   BROTLI_UNUSED(&brotli_min_float);
525   BROTLI_UNUSED(&brotli_max_float);
526   BROTLI_UNUSED(&brotli_min_int);
527   BROTLI_UNUSED(&brotli_max_int);
528   BROTLI_UNUSED(&brotli_min_size_t);
529   BROTLI_UNUSED(&brotli_max_size_t);
530   BROTLI_UNUSED(&brotli_min_uint32_t);
531   BROTLI_UNUSED(&brotli_max_uint32_t);
532   BROTLI_UNUSED(&brotli_min_uint8_t);
533   BROTLI_UNUSED(&brotli_max_uint8_t);
534   BROTLI_UNUSED(&BrotliDefaultAllocFunc);
535   BROTLI_UNUSED(&BrotliDefaultFreeFunc);
536 #if BROTLI_ENABLE_DUMP
537   BROTLI_UNUSED(&BrotliDump);
538 #endif
539 }
540 
541 #endif  /* BROTLI_COMMON_PLATFORM_H_ */
542