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