Debian Codegraph @

c/common/platform.h

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
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> /* IWYU pragma: export memcmp, memcpy, memset */
28 #include <stdlib.h> /* IWYU pragma: export exit, free, malloc */
29 #include <sys/types.h> /* should include endian.h for us */
30
31 #include <brotli/port.h> /* IWYU pragma: export */
32 #include <brotli/types.h> /* IWYU pragma: export */
33
34 #if BROTLI_MSVC_VERSION_CHECK(18, 0, 0)
35 #include <intrin.h>
36 #endif
37
38 #if defined(BROTLI_ENABLE_LOG) || defined(BROTLI_DEBUG)
39 #include <assert.h>
40 #include <stdio.h>
41 #endif
42
43 /* The following macros were borrowed from https://github.com/nemequ/hedley
44 * with permission of original author - Evan Nemerson <[email protected]> */
45
46 /* >>> >>> >>> hedley macros */
47
48 /* Define "BROTLI_PREDICT_TRUE" and "BROTLI_PREDICT_FALSE" macros for capable
49 compilers.
50
51 To apply compiler hint, enclose the branching condition into macros, like this:
52
53 if (BROTLI_PREDICT_TRUE(zero == 0)) {
54 // main execution path
55 } else {
56 // compiler should place this code outside of main execution path
57 }
58
59 OR:
60
61 if (BROTLI_PREDICT_FALSE(something_rare_or_unexpected_happens)) {
62 // compiler should place this code outside of main execution path
63 }
64
65 */
66 #if BROTLI_GNUC_HAS_BUILTIN(__builtin_expect, 3, 0, 0) || \
67 BROTLI_INTEL_VERSION_CHECK(16, 0, 0) || \
68 BROTLI_SUNPRO_VERSION_CHECK(5, 15, 0) || \
69 BROTLI_ARM_VERSION_CHECK(4, 1, 0) || \
70 BROTLI_IBM_VERSION_CHECK(10, 1, 0) || \
71 BROTLI_TI_VERSION_CHECK(7, 3, 0) || \
72 BROTLI_TINYC_VERSION_CHECK(0, 9, 27)
73 #define BROTLI_PREDICT_TRUE(x) (__builtin_expect(!!(x), 1))
74 #define BROTLI_PREDICT_FALSE(x) (__builtin_expect(x, 0))
75 #else
76 #define BROTLI_PREDICT_FALSE(x) (x)
77 #define BROTLI_PREDICT_TRUE(x) (x)
78 #endif
79
80 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \
81 !defined(__cplusplus)
82 #define BROTLI_RESTRICT restrict
83 #elif BROTLI_GNUC_VERSION_CHECK(3, 1, 0) || \
84 BROTLI_MSVC_VERSION_CHECK(14, 0, 0) || \
85 BROTLI_INTEL_VERSION_CHECK(16, 0, 0) || \
86 BROTLI_ARM_VERSION_CHECK(4, 1, 0) || \
87 BROTLI_IBM_VERSION_CHECK(10, 1, 0) || \
88 BROTLI_PGI_VERSION_CHECK(17, 10, 0) || \
89 BROTLI_TI_VERSION_CHECK(8, 0, 0) || \
90 BROTLI_IAR_VERSION_CHECK(8, 0, 0) || \
91 (BROTLI_SUNPRO_VERSION_CHECK(5, 14, 0) && defined(__cplusplus))
92 #define BROTLI_RESTRICT __restrict
93 #elif BROTLI_SUNPRO_VERSION_CHECK(5, 3, 0) && !defined(__cplusplus)
94 #define BROTLI_RESTRICT _Restrict
95 #else
96 #define BROTLI_RESTRICT
97 #endif
98
99 #if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \
100 (defined(__cplusplus) && (__cplusplus >= 199711L))
101 #define BROTLI_MAYBE_INLINE inline
102 #elif defined(__GNUC_STDC_INLINE__) || defined(__GNUC_GNU_INLINE__) || \
103 BROTLI_ARM_VERSION_CHECK(6, 2, 0)
104 #define BROTLI_MAYBE_INLINE __inline__
105 #elif BROTLI_MSVC_VERSION_CHECK(12, 0, 0) || \
106 BROTLI_ARM_VERSION_CHECK(4, 1, 0) || BROTLI_TI_VERSION_CHECK(8, 0, 0)
107 #define BROTLI_MAYBE_INLINE __inline
108 #else
109 #define BROTLI_MAYBE_INLINE
110 #endif
111
112 #if BROTLI_GNUC_HAS_ATTRIBUTE(always_inline, 4, 0, 0) || \
113 BROTLI_INTEL_VERSION_CHECK(16, 0, 0) || \
114 BROTLI_SUNPRO_VERSION_CHECK(5, 11, 0) || \
115 BROTLI_ARM_VERSION_CHECK(4, 1, 0) || \
116 BROTLI_IBM_VERSION_CHECK(10, 1, 0) || \
117 BROTLI_TI_VERSION_CHECK(8, 0, 0) || \
118 (BROTLI_TI_VERSION_CHECK(7, 3, 0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__))
119 #define BROTLI_INLINE BROTLI_MAYBE_INLINE __attribute__((__always_inline__))
120 #elif BROTLI_MSVC_VERSION_CHECK(12, 0, 0)
121 #define BROTLI_INLINE BROTLI_MAYBE_INLINE __forceinline
122 #elif BROTLI_TI_VERSION_CHECK(7, 0, 0) && defined(__cplusplus)
123 #define BROTLI_INLINE BROTLI_MAYBE_INLINE _Pragma("FUNC_ALWAYS_INLINE;")
124 #elif BROTLI_IAR_VERSION_CHECK(8, 0, 0)
125 #define BROTLI_INLINE BROTLI_MAYBE_INLINE _Pragma("inline=forced")
126 #else
127 #define BROTLI_INLINE BROTLI_MAYBE_INLINE
128 #endif
129
130 #if BROTLI_GNUC_HAS_ATTRIBUTE(noinline, 4, 0, 0) || \
131 BROTLI_INTEL_VERSION_CHECK(16, 0, 0) || \
132 BROTLI_SUNPRO_VERSION_CHECK(5, 11, 0) || \
133 BROTLI_ARM_VERSION_CHECK(4, 1, 0) || \
134 BROTLI_IBM_VERSION_CHECK(10, 1, 0) || \
135 BROTLI_TI_VERSION_CHECK(8, 0, 0) || \
136 (BROTLI_TI_VERSION_CHECK(7, 3, 0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__))
137 #define BROTLI_NOINLINE __attribute__((__noinline__))
138 #elif BROTLI_MSVC_VERSION_CHECK(13, 10, 0)
139 #define BROTLI_NOINLINE __declspec(noinline)
140 #elif BROTLI_PGI_VERSION_CHECK(10, 2, 0)
141 #define BROTLI_NOINLINE _Pragma("noinline")
142 #elif BROTLI_TI_VERSION_CHECK(6, 0, 0) && defined(__cplusplus)
143 #define BROTLI_NOINLINE _Pragma("FUNC_CANNOT_INLINE;")
144 #elif BROTLI_IAR_VERSION_CHECK(8, 0, 0)
145 #define BROTLI_NOINLINE _Pragma("inline=never")
146 #else
147 #define BROTLI_NOINLINE
148 #endif
149
150 /* <<< <<< <<< end of hedley macros. */
151
152 #if BROTLI_GNUC_HAS_ATTRIBUTE(unused, 2, 7, 0) || \
153 BROTLI_INTEL_VERSION_CHECK(16, 0, 0)
154 #define BROTLI_UNUSED_FUNCTION static BROTLI_INLINE __attribute__ ((unused))
155 #else
156 #define BROTLI_UNUSED_FUNCTION static BROTLI_INLINE
157 #endif
158
159 #if BROTLI_GNUC_HAS_ATTRIBUTE(aligned, 2, 7, 0)
160 #define BROTLI_ALIGNED(N) __attribute__((aligned(N)))
161 #else
162 #define BROTLI_ALIGNED(N)
163 #endif
164
165 #if (defined(__ARM_ARCH) && (__ARM_ARCH == 7)) || \
166 (defined(M_ARM) && (M_ARM == 7))
167 #define BROTLI_TARGET_ARMV7
168 #endif /* ARMv7 */
169
170 #if (defined(__ARM_ARCH) && (__ARM_ARCH == 8)) || \
171 defined(__aarch64__) || defined(__ARM64_ARCH_8__)
172 #define BROTLI_TARGET_ARMV8_ANY
173
174 #if defined(__ARM_32BIT_STATE)
175 #define BROTLI_TARGET_ARMV8_32
176 #elif defined(__ARM_64BIT_STATE)
177 #define BROTLI_TARGET_ARMV8_64
178 #endif
179
180 #endif /* ARMv8 */
181
182 #if defined(__ARM_NEON__) || defined(__ARM_NEON)
183 #define BROTLI_TARGET_NEON
184 #endif
185
186 #if defined(__i386) || defined(_M_IX86)
187 #define BROTLI_TARGET_X86
188 #endif
189
190 #if defined(__x86_64__) || defined(_M_X64)
191 #define BROTLI_TARGET_X64
192 #endif
193
194 #if defined(__PPC64__)
195 #define BROTLI_TARGET_POWERPC64
196 #endif
197
198 #if defined(__riscv) && defined(__riscv_xlen) && __riscv_xlen == 64
199 #define BROTLI_TARGET_RISCV64
200 #endif
201
202 #if defined(__loongarch_lp64)
203 #define BROTLI_TARGET_LOONGARCH64
204 #endif
205
206 /* This does not seem to be an indicator of z/Architecture (64-bit); neither
207 that allows to use unaligned loads. */
208 #if defined(__s390x__)
209 #define BROTLI_TARGET_S390X
210 #endif
211
212 #if defined(__mips64)
213 #define BROTLI_TARGET_MIPS64
214 #endif
215
•216 #if defined(__ia64__) || defined(_M_IA64)
217 #define BROTLI_TARGET_IA64
218 #endif
219
220 #if defined(BROTLI_TARGET_X64) || defined(BROTLI_TARGET_ARMV8_64) || \
221 defined(BROTLI_TARGET_POWERPC64) || defined(BROTLI_TARGET_RISCV64) || \
222 defined(BROTLI_TARGET_LOONGARCH64) || defined(BROTLI_TARGET_MIPS64)
223 #define BROTLI_TARGET_64_BITS 1
224 #else
225 #define BROTLI_TARGET_64_BITS 0
226 #endif
227
228 #if defined(BROTLI_BUILD_64_BIT)
229 #define BROTLI_64_BITS 1
230 #elif defined(BROTLI_BUILD_32_BIT)
231 #define BROTLI_64_BITS 0
232 #else
233 #define BROTLI_64_BITS BROTLI_TARGET_64_BITS
234 #endif
235
236 #if (BROTLI_64_BITS)
237 #define brotli_reg_t uint64_t
238 #else
239 #define brotli_reg_t uint32_t
240 #endif
241
242 #if defined(BROTLI_BUILD_BIG_ENDIAN)
243 #define BROTLI_BIG_ENDIAN 1
244 #elif defined(BROTLI_BUILD_LITTLE_ENDIAN)
245 #define BROTLI_LITTLE_ENDIAN 1
246 #elif defined(BROTLI_BUILD_ENDIAN_NEUTRAL)
247 /* Just break elif chain. */
248 #elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
249 #define BROTLI_LITTLE_ENDIAN 1
250 #elif defined(_WIN32) || defined(BROTLI_TARGET_X64)
251 /* Win32 & x64 can currently always be assumed to be little endian */
252 #define BROTLI_LITTLE_ENDIAN 1
253 #elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
254 #define BROTLI_BIG_ENDIAN 1
255 /* Likely target platform is iOS / OSX. */
256 #elif defined(BYTE_ORDER) && (BYTE_ORDER == LITTLE_ENDIAN)
257 #define BROTLI_LITTLE_ENDIAN 1
258 #elif defined(BYTE_ORDER) && (BYTE_ORDER == BIG_ENDIAN)
259 #define BROTLI_BIG_ENDIAN 1
260 #endif
261
262 #if !defined(BROTLI_LITTLE_ENDIAN)
263 #define BROTLI_LITTLE_ENDIAN 0
264 #endif
265
266 #if !defined(BROTLI_BIG_ENDIAN)
267 #define BROTLI_BIG_ENDIAN 0
268 #endif
269
270 #if defined(BROTLI_BUILD_NO_UNALIGNED_READ_FAST)
271 #define BROTLI_UNALIGNED_READ_FAST (!!0)
272 #elif defined(BROTLI_TARGET_X86) || defined(BROTLI_TARGET_X64) || \
273 defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_ANY) || \
274 defined(BROTLI_TARGET_RISCV64) || defined(BROTLI_TARGET_LOONGARCH64)
275 /* These targets are known to generate efficient code for unaligned reads
276 * (e.g. a single instruction, not multiple 1-byte loads, shifted and or'd
277 * together). */
278 #define BROTLI_UNALIGNED_READ_FAST (!!1)
279 #else
280 #define BROTLI_UNALIGNED_READ_FAST (!!0)
281 #endif
282
283 /* Portable unaligned memory access: read / write values via memcpy. */
284 #if !defined(BROTLI_USE_PACKED_FOR_UNALIGNED)
285 #if defined(__mips__) && (!defined(__mips_isa_rev) || __mips_isa_rev < 6)
286 #define BROTLI_USE_PACKED_FOR_UNALIGNED 1
287 #else
288 #define BROTLI_USE_PACKED_FOR_UNALIGNED 0
289 #endif
290 #endif /* defined(BROTLI_USE_PACKED_FOR_UNALIGNED) */
291
292 #if BROTLI_USE_PACKED_FOR_UNALIGNED
293
294 typedef union BrotliPackedValue {
295 uint16_t u16;
296 uint32_t u32;
297 uint64_t u64;
298 size_t szt;
299 } __attribute__ ((packed)) BrotliPackedValue;
300
301 static BROTLI_INLINE uint16_t BrotliUnalignedRead16(const void* p) {
302 const BrotliPackedValue* address = (const BrotliPackedValue*)p;
303 return address->u16;
304 }
305 static BROTLI_INLINE uint32_t BrotliUnalignedRead32(const void* p) {
306 const BrotliPackedValue* address = (const BrotliPackedValue*)p;
307 return address->u32;
308 }
309 static BROTLI_INLINE uint64_t BrotliUnalignedRead64(const void* p) {
310 const BrotliPackedValue* address = (const BrotliPackedValue*)p;
311 return address->u64;
312 }
313 static BROTLI_INLINE size_t BrotliUnalignedReadSizeT(const void* p) {
314 const BrotliPackedValue* address = (const BrotliPackedValue*)p;
315 return address->szt;
316 }
317 static BROTLI_INLINE void BrotliUnalignedWrite64(void* p, uint64_t v) {
318 BrotliPackedValue* address = (BrotliPackedValue*)p;
319 address->u64 = v;
320 }
321
322 #else /* not BROTLI_USE_PACKED_FOR_UNALIGNED */
323
324 static BROTLI_INLINE uint16_t BrotliUnalignedRead16(const void* p) {
325 uint16_t t;
326 memcpy(&t, p, sizeof t);
327 return t;
328 }
329 static BROTLI_INLINE uint32_t BrotliUnalignedRead32(const void* p) {
330 uint32_t t;
331 memcpy(&t, p, sizeof t);
332 return t;
333 }
334 static BROTLI_INLINE uint64_t BrotliUnalignedRead64(const void* p) {
335 uint64_t t;
336 memcpy(&t, p, sizeof t);
337 return t;
338 }
339 static BROTLI_INLINE size_t BrotliUnalignedReadSizeT(const void* p) {
340 size_t t;
341 memcpy(&t, p, sizeof t);
342 return t;
343 }
344 static BROTLI_INLINE void BrotliUnalignedWrite64(void* p, uint64_t v) {
345 memcpy(p, &v, sizeof v);
346 }
347
348 #endif /* BROTLI_USE_PACKED_FOR_UNALIGNED */
349
350 #if BROTLI_GNUC_HAS_BUILTIN(__builtin_bswap16, 4, 3, 0)
351 #define BROTLI_BSWAP16(V) ((uint16_t)__builtin_bswap16(V))
352 #else
353 #define BROTLI_BSWAP16(V) ((uint16_t)( \
354 (((V) & 0xFFU) << 8) | \
355 (((V) >> 8) & 0xFFU)))
356 #endif
357
358 #if BROTLI_GNUC_HAS_BUILTIN(__builtin_bswap32, 4, 3, 0)
359 #define BROTLI_BSWAP32(V) ((uint32_t)__builtin_bswap32(V))
360 #else
361 #define BROTLI_BSWAP32(V) ((uint32_t)( \
362 (((V) & 0xFFU) << 24) | (((V) & 0xFF00U) << 8) | \
363 (((V) >> 8) & 0xFF00U) | (((V) >> 24) & 0xFFU)))
364 #endif
365
366 #if BROTLI_GNUC_HAS_BUILTIN(__builtin_bswap64, 4, 3, 0)
367 #define BROTLI_BSWAP64(V) ((uint64_t)__builtin_bswap64(V))
368 #else
369 #define BROTLI_BSWAP64(V) ((uint64_t)( \
370 (((V) & 0xFFU) << 56) | (((V) & 0xFF00U) << 40) | \
371 (((V) & 0xFF0000U) << 24) | (((V) & 0xFF000000U) << 8) | \
372 (((V) >> 8) & 0xFF000000U) | (((V) >> 24) & 0xFF0000U) | \
373 (((V) >> 40) & 0xFF00U) | (((V) >> 56) & 0xFFU)))
374 #endif
375
376 #if BROTLI_LITTLE_ENDIAN
377 /* Straight endianness. Just read / write values. */
378 #define BROTLI_UNALIGNED_LOAD16LE BrotliUnalignedRead16
379 #define BROTLI_UNALIGNED_LOAD32LE BrotliUnalignedRead32
380 #define BROTLI_UNALIGNED_LOAD64LE BrotliUnalignedRead64
381 #define BROTLI_UNALIGNED_STORE64LE BrotliUnalignedWrite64
382 #elif BROTLI_BIG_ENDIAN /* BROTLI_LITTLE_ENDIAN */
383 static BROTLI_INLINE uint16_t BROTLI_UNALIGNED_LOAD16LE(const void* p) {
384 uint16_t value = BrotliUnalignedRead16(p);
385 return BROTLI_BSWAP16(value);
386 }
387 static BROTLI_INLINE uint32_t BROTLI_UNALIGNED_LOAD32LE(const void* p) {
388 uint32_t value = BrotliUnalignedRead32(p);
389 return BROTLI_BSWAP32(value);
390 }
391 static BROTLI_INLINE uint64_t BROTLI_UNALIGNED_LOAD64LE(const void* p) {
392 uint64_t value = BrotliUnalignedRead64(p);
393 return BROTLI_BSWAP64(value);
394 }
395 static BROTLI_INLINE void BROTLI_UNALIGNED_STORE64LE(void* p, uint64_t v) {
396 uint64_t value = BROTLI_BSWAP64(v);
397 BrotliUnalignedWrite64(p, value);
398 }
399 #else /* BROTLI_LITTLE_ENDIAN */
400 /* Read / store values byte-wise; hopefully compiler will understand. */
401 static BROTLI_INLINE uint16_t BROTLI_UNALIGNED_LOAD16LE(const void* p) {
402 const uint8_t* in = (const uint8_t*)p;
403 return (uint16_t)(in[0] | (in[1] << 8));
404 }
405 static BROTLI_INLINE uint32_t BROTLI_UNALIGNED_LOAD32LE(const void* p) {
406 const uint8_t* in = (const uint8_t*)p;
407 uint32_t value = (uint32_t)(in[0]);
408 value |= (uint32_t)(in[1]) << 8;
409 value |= (uint32_t)(in[2]) << 16;
410 value |= (uint32_t)(in[3]) << 24;
411 return value;
412 }
413 static BROTLI_INLINE uint64_t BROTLI_UNALIGNED_LOAD64LE(const void* p) {
414 const uint8_t* in = (const uint8_t*)p;
415 uint64_t value = (uint64_t)(in[0]);
416 value |= (uint64_t)(in[1]) << 8;
417 value |= (uint64_t)(in[2]) << 16;
418 value |= (uint64_t)(in[3]) << 24;
419 value |= (uint64_t)(in[4]) << 32;
420 value |= (uint64_t)(in[5]) << 40;
421 value |= (uint64_t)(in[6]) << 48;
422 value |= (uint64_t)(in[7]) << 56;
423 return value;
424 }
425 static BROTLI_INLINE void BROTLI_UNALIGNED_STORE64LE(void* p, uint64_t v) {
426 uint8_t* out = (uint8_t*)p;
427 out[0] = (uint8_t)v;
428 out[1] = (uint8_t)(v >> 8);
429 out[2] = (uint8_t)(v >> 16);
430 out[3] = (uint8_t)(v >> 24);
431 out[4] = (uint8_t)(v >> 32);
432 out[5] = (uint8_t)(v >> 40);
433 out[6] = (uint8_t)(v >> 48);
434 out[7] = (uint8_t)(v >> 56);
435 }
436 #endif /* BROTLI_LITTLE_ENDIAN */
437
438 static BROTLI_INLINE void* BROTLI_UNALIGNED_LOAD_PTR(const void* p) {
439 void* v;
440 memcpy(&v, p, sizeof(void*));
441 return v;
442 }
443
444 static BROTLI_INLINE void BROTLI_UNALIGNED_STORE_PTR(void* p, const void* v) {
445 memcpy(p, &v, sizeof(void*));
446 }
447
448 /* BROTLI_IS_CONSTANT macros returns true for compile-time constants. */
449 #if BROTLI_GNUC_HAS_BUILTIN(__builtin_constant_p, 3, 0, 1) || \
450 BROTLI_INTEL_VERSION_CHECK(16, 0, 0)
451 #define BROTLI_IS_CONSTANT(x) (!!__builtin_constant_p(x))
452 #else
453 #define BROTLI_IS_CONSTANT(x) (!!0)
454 #endif
455
456 #if defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_ANY)
457 #define BROTLI_HAS_UBFX (!!1)
458 #else
459 #define BROTLI_HAS_UBFX (!!0)
460 #endif
461
462 #if defined(BROTLI_ENABLE_LOG)
463 #define BROTLI_LOG(x) printf x
464 #else
465 #define BROTLI_LOG(x)
466 #endif
467
468 #if defined(BROTLI_DEBUG) || defined(BROTLI_ENABLE_LOG)
469 #define BROTLI_ENABLE_DUMP_DEFAULT 1
470 #define BROTLI_DCHECK(x) assert(x)
471 #else
472 #define BROTLI_ENABLE_DUMP_DEFAULT 0
473 #define BROTLI_DCHECK(x)
474 #endif
475
476 #if !defined(BROTLI_ENABLE_DUMP)
477 #define BROTLI_ENABLE_DUMP BROTLI_ENABLE_DUMP_DEFAULT
478 #endif
479
480 #if BROTLI_ENABLE_DUMP
481 static BROTLI_INLINE void BrotliDump(const char* f, int l, const char* fn) {
482 fprintf(stderr, "%s:%d (%s)\n", f, l, fn);
483 fflush(stderr);
484 }
485 #define BROTLI_DUMP() BrotliDump(__FILE__, __LINE__, __FUNCTION__)
486 #else
487 #define BROTLI_DUMP() (void)(0)
488 #endif
489
490 /* BrotliRBit assumes brotli_reg_t fits native CPU register type. */
491 #if (BROTLI_64_BITS == BROTLI_TARGET_64_BITS)
492 /* TODO(eustas): add appropriate icc/sunpro/arm/ibm/ti checks. */
493 #if (BROTLI_GNUC_VERSION_CHECK(3, 0, 0) || defined(__llvm__)) && \
494 !defined(BROTLI_BUILD_NO_RBIT)
495 #if defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_ANY)
496 /* TODO(eustas): detect ARMv6T2 and enable this code for it. */
497 static BROTLI_INLINE brotli_reg_t BrotliRBit(brotli_reg_t input) {
498 brotli_reg_t output;
499 __asm__("rbit %0, %1\n" : "=r"(output) : "r"(input));
500 return output;
501 }
502 #define BROTLI_RBIT(x) BrotliRBit(x)
503 #endif /* armv7 / armv8 */
504 #endif /* gcc || clang */
505 #endif /* brotli_reg_t is native */
506 #if !defined(BROTLI_RBIT)
507 static BROTLI_INLINE void BrotliRBit(void) { /* Should break build if used. */ }
508 #endif /* BROTLI_RBIT */
509
510 #define BROTLI_REPEAT_4(X) {X; X; X; X;}
511 #define BROTLI_REPEAT_5(X) {X; X; X; X; X;}
512 #define BROTLI_REPEAT_6(X) {X; X; X; X; X; X;}
513
514 #define BROTLI_UNUSED(X) (void)(X)
515
516 #define BROTLI_MIN_MAX(T) \
517 static BROTLI_INLINE T brotli_min_ ## T (T a, T b) { return a < b ? a : b; } \
518 static BROTLI_INLINE T brotli_max_ ## T (T a, T b) { return a > b ? a : b; }
519 BROTLI_MIN_MAX(double) BROTLI_MIN_MAX(float) BROTLI_MIN_MAX(int)
520 BROTLI_MIN_MAX(size_t) BROTLI_MIN_MAX(uint32_t) BROTLI_MIN_MAX(uint8_t)
521 #undef BROTLI_MIN_MAX
522 #define BROTLI_MIN(T, A, B) (brotli_min_ ## T((A), (B)))
523 #define BROTLI_MAX(T, A, B) (brotli_max_ ## T((A), (B)))
524
525 #define BROTLI_SWAP(T, A, I, J) { \
526 T __brotli_swap_tmp = (A)[(I)]; \
527 (A)[(I)] = (A)[(J)]; \
528 (A)[(J)] = __brotli_swap_tmp; \
529 }
530
531 #if BROTLI_64_BITS
532 #if BROTLI_GNUC_HAS_BUILTIN(__builtin_ctzll, 3, 4, 0) || \
533 BROTLI_INTEL_VERSION_CHECK(16, 0, 0)
534 #define BROTLI_TZCNT64 __builtin_ctzll
535 #elif BROTLI_MSVC_VERSION_CHECK(18, 0, 0)
536 #if defined(BROTLI_TARGET_X64) && !defined(_M_ARM64EC)
537 #define BROTLI_TZCNT64 _tzcnt_u64
538 #else /* BROTLI_TARGET_X64 */
539 static BROTLI_INLINE uint32_t BrotliBsf64Msvc(uint64_t x) {
540 uint32_t lsb;
541 _BitScanForward64(&lsb, x);
542 return lsb;
543 }
544 #define BROTLI_TZCNT64 BrotliBsf64Msvc
545 #endif /* BROTLI_TARGET_X64 */
546 #endif /* __builtin_ctzll */
547 #endif /* BROTLI_64_BITS */
548
549 #if BROTLI_GNUC_HAS_BUILTIN(__builtin_clz, 3, 4, 0) || \
550 BROTLI_INTEL_VERSION_CHECK(16, 0, 0)
551 #define BROTLI_BSR32(x) (31u ^ (uint32_t)__builtin_clz(x))
552 #elif BROTLI_MSVC_VERSION_CHECK(18, 0, 0)
553 static BROTLI_INLINE uint32_t BrotliBsr32Msvc(uint32_t x) {
554 unsigned long msb;
555 _BitScanReverse(&msb, x);
556 return (uint32_t)msb;
557 }
558 #define BROTLI_BSR32 BrotliBsr32Msvc
559 #endif /* __builtin_clz */
560
561 /* Default brotli_alloc_func */
562 BROTLI_COMMON_API void* BrotliDefaultAllocFunc(void* opaque, size_t size);
563
564 /* Default brotli_free_func */
565 BROTLI_COMMON_API void BrotliDefaultFreeFunc(void* opaque, void* address);
566
567 /* Circular logical rotates. */
568 static BROTLI_INLINE uint16_t BrotliRotateRight16(uint16_t const value,
569 size_t count) {
570 count &= 0x0F; /* for fickle pattern recognition */
571 return (value >> count) | (uint16_t)(value << ((0U - count) & 0x0F));
572 }
573 static BROTLI_INLINE uint32_t BrotliRotateRight32(uint32_t const value,
574 size_t count) {
575 count &= 0x1F; /* for fickle pattern recognition */
576 return (value >> count) | (uint32_t)(value << ((0U - count) & 0x1F));
577 }
578 static BROTLI_INLINE uint64_t BrotliRotateRight64(uint64_t const value,
579 size_t count) {
580 count &= 0x3F; /* for fickle pattern recognition */
581 return (value >> count) | (uint64_t)(value << ((0U - count) & 0x3F));
582 }
583
584 BROTLI_UNUSED_FUNCTION void BrotliSuppressUnusedFunctions(void) {
585 BROTLI_UNUSED(&BrotliSuppressUnusedFunctions);
586 BROTLI_UNUSED(&BrotliUnalignedRead16);
587 BROTLI_UNUSED(&BrotliUnalignedRead32);
588 BROTLI_UNUSED(&BrotliUnalignedRead64);
589 BROTLI_UNUSED(&BrotliUnalignedReadSizeT);
590 BROTLI_UNUSED(&BrotliUnalignedWrite64);
591 BROTLI_UNUSED(&BROTLI_UNALIGNED_LOAD16LE);
592 BROTLI_UNUSED(&BROTLI_UNALIGNED_LOAD32LE);
593 BROTLI_UNUSED(&BROTLI_UNALIGNED_LOAD64LE);
594 BROTLI_UNUSED(&BROTLI_UNALIGNED_STORE64LE);
595 BROTLI_UNUSED(&BROTLI_UNALIGNED_LOAD_PTR);
596 BROTLI_UNUSED(&BROTLI_UNALIGNED_STORE_PTR);
597 BROTLI_UNUSED(&BrotliRBit);
598 BROTLI_UNUSED(&brotli_min_double);
599 BROTLI_UNUSED(&brotli_max_double);
600 BROTLI_UNUSED(&brotli_min_float);
601 BROTLI_UNUSED(&brotli_max_float);
602 BROTLI_UNUSED(&brotli_min_int);
603 BROTLI_UNUSED(&brotli_max_int);
604 BROTLI_UNUSED(&brotli_min_size_t);
605 BROTLI_UNUSED(&brotli_max_size_t);
606 BROTLI_UNUSED(&brotli_min_uint32_t);
607 BROTLI_UNUSED(&brotli_max_uint32_t);
608 BROTLI_UNUSED(&brotli_min_uint8_t);
609 BROTLI_UNUSED(&brotli_max_uint8_t);
610 BROTLI_UNUSED(&BrotliDefaultAllocFunc);
611 BROTLI_UNUSED(&BrotliDefaultFreeFunc);
612 BROTLI_UNUSED(&BrotliRotateRight16);
613 BROTLI_UNUSED(&BrotliRotateRight32);
614 BROTLI_UNUSED(&BrotliRotateRight64);
615 #if BROTLI_ENABLE_DUMP
616 BROTLI_UNUSED(&BrotliDump);
617 #endif
618
619 #if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_I86)) && \
620 !defined(_M_ARM64EC)
621 /* _mm_prefetch() is not defined outside of x86/x64 */
622 /* https://msdn.microsoft.com/fr-fr/library/84szxsww(v=vs.90).aspx */
623 #include <mmintrin.h>
624 #define PREFETCH_L1(ptr) _mm_prefetch((const char*)(ptr), _MM_HINT_T0)
625 #define PREFETCH_L2(ptr) _mm_prefetch((const char*)(ptr), _MM_HINT_T1)
626 #elif BROTLI_GNUC_HAS_BUILTIN(__builtin_prefetch, 3, 1, 0)
627 #define PREFETCH_L1(ptr) \
628 __builtin_prefetch((ptr), 0 /* rw==read */, 3 /* locality */)
629 #define PREFETCH_L2(ptr) \
630 __builtin_prefetch((ptr), 0 /* rw==read */, 2 /* locality */)
631 #elif defined(__aarch64__)
632 #define PREFETCH_L1(ptr) \
633 do { \
634 __asm__ __volatile__("prfm pldl1keep, %0" ::"Q"(*(ptr))); \
635 } while (0)
636 #define PREFETCH_L2(ptr) \
637 do { \
638 __asm__ __volatile__("prfm pldl2keep, %0" ::"Q"(*(ptr))); \
639 } while (0)
640 #else
641 #define PREFETCH_L1(ptr) \
642 do { \
643 (void)(ptr); \
644 } while (0) /* disabled */
645 #define PREFETCH_L2(ptr) \
646 do { \
647 (void)(ptr); \
648 } while (0) /* disabled */
649 #endif
650
651 /* The SIMD matchers are only faster at certain quality levels. */
652 #if defined(_M_X64) && defined(BROTLI_TZCNT64)
653 #define BROTLI_MAX_SIMD_QUALITY 7
654 #elif defined(BROTLI_TZCNT64)
655 #define BROTLI_MAX_SIMD_QUALITY 6
656 #endif
657 }
658
659 #if defined(_MSC_VER)
660 #define BROTLI_CRASH() __debugbreak(), (void)abort()
661 #elif BROTLI_GNUC_HAS_BUILTIN(__builtin_trap, 3, 0, 0)
662 #define BROTLI_CRASH() (void)__builtin_trap()
663 #else
664 #define BROTLI_CRASH() (void)abort()
665 #endif
666
667 /* Make BROTLI_TEST=0 act same as undefined. */
668 #if defined(BROTLI_TEST) && ((1-BROTLI_TEST-1) == 0)
669 #undef BROTLI_TEST
670 #endif
671
•672 #if !defined(BROTLI_MODEL) && BROTLI_GNUC_HAS_ATTRIBUTE(model, 3, 0, 3) && \
673 !defined(BROTLI_TARGET_IA64) && !defined(BROTLI_TARGET_LOONGARCH64)
674 #define BROTLI_MODEL(M) __attribute__((model(M)))
675 #else
676 #define BROTLI_MODEL(M) /* M */
677 #endif
678
•679 #if !defined(BROTLI_COLD) && BROTLI_GNUC_HAS_ATTRIBUTE(cold, 4, 3, 0)
680 #define BROTLI_COLD __attribute__((cold))
681 #else
682 #define BROTLI_COLD /* cold */
683 #endif
684
685 #endif /* BROTLI_COMMON_PLATFORM_H_ */