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 | */ |
22 | |
23 | #ifndef BROTLI_COMMON_PLATFORM_H_ |
24 | #define BROTLI_COMMON_PLATFORM_H_ |
25 | |
26 | #include <string.h> /* memcpy */ |
27 | |
28 | #include <brotli/port.h> |
29 | #include <brotli/types.h> |
30 | |
31 | #if defined(OS_LINUX) || defined(OS_CYGWIN) || defined(__EMSCRIPTEN__) |
32 | #include <endian.h> |
33 | #elif defined(OS_FREEBSD) |
34 | #include <machine/endian.h> |
35 | #elif defined(OS_MACOSX) |
36 | #include <machine/endian.h> |
37 | /* Let's try and follow the Linux convention */ |
38 | #define BROTLI_X_BYTE_ORDER BYTE_ORDER |
39 | #define BROTLI_X_LITTLE_ENDIAN LITTLE_ENDIAN |
40 | #define BROTLI_X_BIG_ENDIAN BIG_ENDIAN |
41 | #endif |
42 | |
43 | #if BROTLI_MSVC_VERSION_CHECK(18, 0, 0) |
44 | #include <intrin.h> |
45 | #endif |
46 | |
47 | #if defined(BROTLI_ENABLE_LOG) || defined(BROTLI_DEBUG) |
48 | #include <assert.h> |
49 | #include <stdio.h> |
50 | #endif |
51 | |
52 | /* The following macros were borrowed from https://github.com/nemequ/hedley |
53 | * with permission of original author - Evan Nemerson <evan@nemerson.com> */ |
54 | |
55 | /* >>> >>> >>> hedley macros */ |
56 | |
57 | /* Define "BROTLI_PREDICT_TRUE" and "BROTLI_PREDICT_FALSE" macros for capable |
58 | compilers. |
59 | |
60 | To apply compiler hint, enclose the branching condition into macros, like this: |
61 | |
62 | if (BROTLI_PREDICT_TRUE(zero == 0)) { |
63 | // main execution path |
64 | } else { |
65 | // compiler should place this code outside of main execution path |
66 | } |
67 | |
68 | OR: |
69 | |
70 | if (BROTLI_PREDICT_FALSE(something_rare_or_unexpected_happens)) { |
71 | // compiler should place this code outside of main execution path |
72 | } |
73 | |
74 | */ |
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)) |
84 | #else |
85 | #define BROTLI_PREDICT_FALSE(x) (x) |
86 | #define BROTLI_PREDICT_TRUE(x) (x) |
87 | #endif |
88 | |
89 | #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \ |
90 | !defined(__cplusplus) |
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 |
104 | #else |
105 | #define BROTLI_RESTRICT |
106 | #endif |
107 | |
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 |
117 | #else |
118 | #define BROTLI_MAYBE_INLINE |
119 | #endif |
120 | |
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") |
135 | #else |
136 | #define BROTLI_INLINE BROTLI_MAYBE_INLINE |
137 | #endif |
138 | |
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") |
155 | #else |
156 | #define BROTLI_NOINLINE |
157 | #endif |
158 | |
159 | /* <<< <<< <<< end of hedley macros. */ |
160 | |
161 | #if BROTLI_GNUC_HAS_ATTRIBUTE(unused, 2, 7, 0) || \ |
162 | BROTLI_INTEL_VERSION_CHECK(16, 0, 0) |
163 | #define BROTLI_UNUSED_FUNCTION static BROTLI_INLINE __attribute__ ((unused)) |
164 | #else |
165 | #define BROTLI_UNUSED_FUNCTION static BROTLI_INLINE |
166 | #endif |
167 | |
168 | #if BROTLI_GNUC_HAS_ATTRIBUTE(aligned, 2, 7, 0) |
169 | #define BROTLI_ALIGNED(N) __attribute__((aligned(N))) |
170 | #else |
171 | #define BROTLI_ALIGNED(N) |
172 | #endif |
173 | |
174 | #if (defined(__ARM_ARCH) && (__ARM_ARCH == 7)) || \ |
175 | (defined(M_ARM) && (M_ARM == 7)) |
176 | #define BROTLI_TARGET_ARMV7 |
177 | #endif /* ARMv7 */ |
178 | |
179 | #if (defined(__ARM_ARCH) && (__ARM_ARCH == 8)) || \ |
180 | defined(__aarch64__) || defined(__ARM64_ARCH_8__) |
181 | #define BROTLI_TARGET_ARMV8_ANY |
182 | |
183 | #if defined(__ARM_32BIT_STATE) |
184 | #define BROTLI_TARGET_ARMV8_32 |
185 | #elif defined(__ARM_64BIT_STATE) |
186 | #define BROTLI_TARGET_ARMV8_64 |
187 | #endif |
188 | |
189 | #endif /* ARMv8 */ |
190 | |
191 | #if defined(__ARM_NEON__) || defined(__ARM_NEON) |
192 | #define BROTLI_TARGET_NEON |
193 | #endif |
194 | |
195 | #if defined(__i386) || defined(_M_IX86) |
196 | #define BROTLI_TARGET_X86 |
197 | #endif |
198 | |
199 | #if defined(__x86_64__) || defined(_M_X64) |
200 | #define BROTLI_TARGET_X64 |
201 | #endif |
202 | |
203 | #if defined(__PPC64__) |
204 | #define BROTLI_TARGET_POWERPC64 |
205 | #endif |
206 | |
207 | #if defined(__riscv) && defined(__riscv_xlen) && __riscv_xlen == 64 |
208 | #define BROTLI_TARGET_RISCV64 |
209 | #endif |
210 | |
211 | #if defined(BROTLI_TARGET_X64) || defined(BROTLI_TARGET_ARMV8_64) || \ |
212 | defined(BROTLI_TARGET_POWERPC64) || defined(BROTLI_TARGET_RISCV64) |
213 | #define BROTLI_TARGET_64_BITS 1 |
214 | #else |
215 | #define BROTLI_TARGET_64_BITS 0 |
216 | #endif |
217 | |
218 | #if defined(BROTLI_BUILD_64_BIT) |
219 | #define BROTLI_64_BITS 1 |
220 | #elif defined(BROTLI_BUILD_32_BIT) |
221 | #define BROTLI_64_BITS 0 |
222 | #else |
223 | #define BROTLI_64_BITS BROTLI_TARGET_64_BITS |
224 | #endif |
225 | |
226 | #if (BROTLI_64_BITS) |
227 | #define brotli_reg_t uint64_t |
228 | #else |
229 | #define brotli_reg_t uint32_t |
230 | #endif |
231 | |
232 | #if defined(BROTLI_BUILD_BIG_ENDIAN) |
233 | #define BROTLI_BIG_ENDIAN 1 |
234 | #elif defined(BROTLI_BUILD_LITTLE_ENDIAN) |
235 | #define BROTLI_LITTLE_ENDIAN 1 |
236 | #elif defined(BROTLI_BUILD_ENDIAN_NEUTRAL) |
237 | /* Just break elif chain. */ |
238 | #elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) |
239 | #define BROTLI_LITTLE_ENDIAN 1 |
240 | #elif defined(_WIN32) || defined(BROTLI_TARGET_X64) |
241 | /* Win32 & x64 can currently always be assumed to be little endian */ |
242 | #define BROTLI_LITTLE_ENDIAN 1 |
243 | #elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) |
244 | #define BROTLI_BIG_ENDIAN 1 |
245 | #elif defined(BROTLI_X_BYTE_ORDER) |
246 | #if BROTLI_X_BYTE_ORDER == BROTLI_X_LITTLE_ENDIAN |
247 | #define BROTLI_LITTLE_ENDIAN 1 |
248 | #elif BROTLI_X_BYTE_ORDER == BROTLI_X_BIG_ENDIAN |
249 | #define BROTLI_BIG_ENDIAN 1 |
250 | #endif |
251 | #endif /* BROTLI_X_BYTE_ORDER */ |
252 | |
253 | #if !defined(BROTLI_LITTLE_ENDIAN) |
254 | #define BROTLI_LITTLE_ENDIAN 0 |
255 | #endif |
256 | |
257 | #if !defined(BROTLI_BIG_ENDIAN) |
258 | #define BROTLI_BIG_ENDIAN 0 |
259 | #endif |
260 | |
261 | #if defined(BROTLI_X_BYTE_ORDER) |
262 | #undef BROTLI_X_BYTE_ORDER |
263 | #undef BROTLI_X_LITTLE_ENDIAN |
264 | #undef BROTLI_X_BIG_ENDIAN |
265 | #endif |
266 | |
267 | #if defined(BROTLI_BUILD_NO_UNALIGNED_READ_FAST) |
268 | #define BROTLI_UNALIGNED_READ_FAST (!!0) |
269 | #elif defined(BROTLI_TARGET_X86) || defined(BROTLI_TARGET_X64) || \ |
270 | defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_ANY) || \ |
271 | defined(BROTLI_TARGET_RISCV64) |
272 | /* These targets are known to generate efficient code for unaligned reads |
273 | * (e.g. a single instruction, not multiple 1-byte loads, shifted and or'd |
274 | * together). */ |
275 | #define BROTLI_UNALIGNED_READ_FAST (!!1) |
276 | #else |
277 | #define BROTLI_UNALIGNED_READ_FAST (!!0) |
278 | #endif |
279 | |
280 | /* Portable unaligned memory access: read / write values via memcpy. */ |
281 | static BROTLI_INLINE uint16_t BrotliUnalignedRead16(const void* p) { |
282 | uint16_t t; |
283 | memcpy(&t, p, sizeof t); |
284 | return t; |
285 | } |
286 | static BROTLI_INLINE uint32_t BrotliUnalignedRead32(const void* p) { |
287 | uint32_t t; |
288 | memcpy(&t, p, sizeof t); |
289 | return t; |
290 | } |
291 | static BROTLI_INLINE uint64_t BrotliUnalignedRead64(const void* p) { |
292 | uint64_t t; |
293 | memcpy(&t, p, sizeof t); |
294 | return t; |
295 | } |
296 | static BROTLI_INLINE void BrotliUnalignedWrite64(void* p, uint64_t v) { |
297 | memcpy(p, &v, sizeof v); |
298 | } |
299 | |
300 | #if BROTLI_LITTLE_ENDIAN |
301 | /* Straight endianness. Just read / write values. */ |
302 | #define BROTLI_UNALIGNED_LOAD16LE BrotliUnalignedRead16 |
303 | #define BROTLI_UNALIGNED_LOAD32LE BrotliUnalignedRead32 |
304 | #define BROTLI_UNALIGNED_LOAD64LE BrotliUnalignedRead64 |
305 | #define BROTLI_UNALIGNED_STORE64LE BrotliUnalignedWrite64 |
306 | #elif BROTLI_BIG_ENDIAN /* BROTLI_LITTLE_ENDIAN */ |
307 | /* Explain compiler to byte-swap values. */ |
308 | #define BROTLI_BSWAP16_(V) ((uint16_t)( \ |
309 | (((V) & 0xFFU) << 8) | \ |
310 | (((V) >> 8) & 0xFFU))) |
311 | static BROTLI_INLINE uint16_t BROTLI_UNALIGNED_LOAD16LE(const void* p) { |
312 | uint16_t value = BrotliUnalignedRead16(p); |
313 | return BROTLI_BSWAP16_(value); |
314 | } |
315 | #define BROTLI_BSWAP32_(V) ( \ |
316 | (((V) & 0xFFU) << 24) | (((V) & 0xFF00U) << 8) | \ |
317 | (((V) >> 8) & 0xFF00U) | (((V) >> 24) & 0xFFU)) |
318 | static BROTLI_INLINE uint32_t BROTLI_UNALIGNED_LOAD32LE(const void* p) { |
319 | uint32_t value = BrotliUnalignedRead32(p); |
320 | return BROTLI_BSWAP32_(value); |
321 | } |
322 | #define BROTLI_BSWAP64_(V) ( \ |
323 | (((V) & 0xFFU) << 56) | (((V) & 0xFF00U) << 40) | \ |
324 | (((V) & 0xFF0000U) << 24) | (((V) & 0xFF000000U) << 8) | \ |
325 | (((V) >> 8) & 0xFF000000U) | (((V) >> 24) & 0xFF0000U) | \ |
326 | (((V) >> 40) & 0xFF00U) | (((V) >> 56) & 0xFFU)) |
327 | static BROTLI_INLINE uint64_t BROTLI_UNALIGNED_LOAD64LE(const void* p) { |
328 | uint64_t value = BrotliUnalignedRead64(p); |
329 | return BROTLI_BSWAP64_(value); |
330 | } |
331 | static BROTLI_INLINE void BROTLI_UNALIGNED_STORE64LE(void* p, uint64_t v) { |
332 | uint64_t value = BROTLI_BSWAP64_(v); |
333 | BrotliUnalignedWrite64(p, value); |
334 | } |
335 | #else /* BROTLI_LITTLE_ENDIAN */ |
336 | /* Read / store values byte-wise; hopefully compiler will understand. */ |
337 | static BROTLI_INLINE uint16_t BROTLI_UNALIGNED_LOAD16LE(const void* p) { |
338 | const uint8_t* in = (const uint8_t*)p; |
339 | return (uint16_t)(in[0] | (in[1] << 8)); |
340 | } |
341 | static BROTLI_INLINE uint32_t BROTLI_UNALIGNED_LOAD32LE(const void* p) { |
342 | const uint8_t* in = (const uint8_t*)p; |
343 | uint32_t value = (uint32_t)(in[0]); |
344 | value |= (uint32_t)(in[1]) << 8; |
345 | value |= (uint32_t)(in[2]) << 16; |
346 | value |= (uint32_t)(in[3]) << 24; |
347 | return value; |
348 | } |
349 | static BROTLI_INLINE uint64_t BROTLI_UNALIGNED_LOAD64LE(const void* p) { |
350 | const uint8_t* in = (const uint8_t*)p; |
351 | uint64_t value = (uint64_t)(in[0]); |
352 | value |= (uint64_t)(in[1]) << 8; |
353 | value |= (uint64_t)(in[2]) << 16; |
354 | value |= (uint64_t)(in[3]) << 24; |
355 | value |= (uint64_t)(in[4]) << 32; |
356 | value |= (uint64_t)(in[5]) << 40; |
357 | value |= (uint64_t)(in[6]) << 48; |
358 | value |= (uint64_t)(in[7]) << 56; |
359 | return value; |
360 | } |
361 | static BROTLI_INLINE void BROTLI_UNALIGNED_STORE64LE(void* p, uint64_t v) { |
362 | uint8_t* out = (uint8_t*)p; |
363 | out[0] = (uint8_t)v; |
364 | out[1] = (uint8_t)(v >> 8); |
365 | out[2] = (uint8_t)(v >> 16); |
366 | out[3] = (uint8_t)(v >> 24); |
367 | out[4] = (uint8_t)(v >> 32); |
368 | out[5] = (uint8_t)(v >> 40); |
369 | out[6] = (uint8_t)(v >> 48); |
370 | out[7] = (uint8_t)(v >> 56); |
371 | } |
372 | #endif /* BROTLI_LITTLE_ENDIAN */ |
373 | |
374 | static BROTLI_INLINE void* BROTLI_UNALIGNED_LOAD_PTR(const void* p) { |
375 | void* v; |
376 | memcpy(&v, p, sizeof(void*)); |
377 | return v; |
378 | } |
379 | |
380 | static BROTLI_INLINE void BROTLI_UNALIGNED_STORE_PTR(void* p, const void* v) { |
381 | memcpy(p, &v, sizeof(void*)); |
382 | } |
383 | |
384 | /* BROTLI_IS_CONSTANT macros returns true for compile-time constants. */ |
385 | #if BROTLI_GNUC_HAS_BUILTIN(__builtin_constant_p, 3, 0, 1) || \ |
386 | BROTLI_INTEL_VERSION_CHECK(16, 0, 0) |
387 | #define BROTLI_IS_CONSTANT(x) (!!__builtin_constant_p(x)) |
388 | #else |
389 | #define BROTLI_IS_CONSTANT(x) (!!0) |
390 | #endif |
391 | |
392 | #if defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_ANY) |
393 | #define BROTLI_HAS_UBFX (!!1) |
394 | #else |
395 | #define BROTLI_HAS_UBFX (!!0) |
396 | #endif |
397 | |
398 | #if defined(BROTLI_ENABLE_LOG) |
399 | #define BROTLI_LOG(x) printf x |
400 | #else |
401 | #define BROTLI_LOG(x) |
402 | #endif |
403 | |
404 | #if defined(BROTLI_DEBUG) || defined(BROTLI_ENABLE_LOG) |
405 | #define BROTLI_DCHECK(x) assert(x) |
406 | static BROTLI_INLINE void BrotliDump(const char* f, int l, const char* fn) { |
407 | fprintf(stderr, "%s:%d (%s)\n" , f, l, fn); |
408 | fflush(stderr); |
409 | } |
410 | #define BROTLI_DUMP() BrotliDump(__FILE__, __LINE__, __FUNCTION__) |
411 | #else |
412 | #define BROTLI_DCHECK(x) |
413 | #define BROTLI_DUMP() (void)(0) |
414 | #endif |
415 | |
416 | /* BrotliRBit assumes brotli_reg_t fits native CPU register type. */ |
417 | #if (BROTLI_64_BITS == BROTLI_TARGET_64_BITS) |
418 | /* TODO(eustas): add appropriate icc/sunpro/arm/ibm/ti checks. */ |
419 | #if (BROTLI_GNUC_VERSION_CHECK(3, 0, 0) || defined(__llvm__)) && \ |
420 | !defined(BROTLI_BUILD_NO_RBIT) |
421 | #if defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_ANY) |
422 | /* TODO(eustas): detect ARMv6T2 and enable this code for it. */ |
423 | static BROTLI_INLINE brotli_reg_t BrotliRBit(brotli_reg_t input) { |
424 | brotli_reg_t output; |
425 | __asm__("rbit %0, %1\n" : "=r" (output) : "r" (input)); |
426 | return output; |
427 | } |
428 | #define BROTLI_RBIT(x) BrotliRBit(x) |
429 | #endif /* armv7 / armv8 */ |
430 | #endif /* gcc || clang */ |
431 | #endif /* brotli_reg_t is native */ |
432 | #if !defined(BROTLI_RBIT) |
433 | static BROTLI_INLINE void BrotliRBit(void) { /* Should break build if used. */ } |
434 | #endif /* BROTLI_RBIT */ |
435 | |
436 | #define BROTLI_REPEAT_4(X) {X; X; X; X;} |
437 | #define BROTLI_REPEAT_5(X) {X; X; X; X; X;} |
438 | #define BROTLI_REPEAT_6(X) {X; X; X; X; X; X;} |
439 | |
440 | #define BROTLI_UNUSED(X) (void)(X) |
441 | |
442 | #define BROTLI_MIN_MAX(T) \ |
443 | static BROTLI_INLINE T brotli_min_ ## T (T a, T b) { return a < b ? a : b; } \ |
444 | static BROTLI_INLINE T brotli_max_ ## T (T a, T b) { return a > b ? a : b; } |
445 | BROTLI_MIN_MAX(double) BROTLI_MIN_MAX(float) BROTLI_MIN_MAX(int) |
446 | BROTLI_MIN_MAX(size_t) BROTLI_MIN_MAX(uint32_t) BROTLI_MIN_MAX(uint8_t) |
447 | #undef BROTLI_MIN_MAX |
448 | #define BROTLI_MIN(T, A, B) (brotli_min_ ## T((A), (B))) |
449 | #define BROTLI_MAX(T, A, B) (brotli_max_ ## T((A), (B))) |
450 | |
451 | #define BROTLI_SWAP(T, A, I, J) { \ |
452 | T __brotli_swap_tmp = (A)[(I)]; \ |
453 | (A)[(I)] = (A)[(J)]; \ |
454 | (A)[(J)] = __brotli_swap_tmp; \ |
455 | } |
456 | |
457 | #if BROTLI_64_BITS |
458 | #if BROTLI_GNUC_HAS_BUILTIN(__builtin_ctzll, 3, 4, 0) || \ |
459 | BROTLI_INTEL_VERSION_CHECK(16, 0, 0) |
460 | #define BROTLI_TZCNT64 __builtin_ctzll |
461 | #elif BROTLI_MSVC_VERSION_CHECK(18, 0, 0) |
462 | #if defined(BROTLI_TARGET_X64) |
463 | #define BROTLI_TZCNT64 _tzcnt_u64 |
464 | #else /* BROTLI_TARGET_X64 */ |
465 | static BROTLI_INLINE uint32_t BrotliBsf64Msvc(uint64_t x) { |
466 | uint32_t lsb; |
467 | _BitScanForward64(&lsb, x); |
468 | return lsb; |
469 | } |
470 | #define BROTLI_TZCNT64 BrotliBsf64Msvc |
471 | #endif /* BROTLI_TARGET_X64 */ |
472 | #endif /* __builtin_ctzll */ |
473 | #endif /* BROTLI_64_BITS */ |
474 | |
475 | #if BROTLI_GNUC_HAS_BUILTIN(__builtin_clz, 3, 4, 0) || \ |
476 | BROTLI_INTEL_VERSION_CHECK(16, 0, 0) |
477 | #define BROTLI_BSR32(x) (31u ^ (uint32_t)__builtin_clz(x)) |
478 | #elif BROTLI_MSVC_VERSION_CHECK(18, 0, 0) |
479 | static BROTLI_INLINE uint32_t BrotliBsr32Msvc(uint32_t x) { |
480 | unsigned long msb; |
481 | _BitScanReverse(&msb, x); |
482 | return (uint32_t)msb; |
483 | } |
484 | #define BROTLI_BSR32 BrotliBsr32Msvc |
485 | #endif /* __builtin_clz */ |
486 | |
487 | /* Default brotli_alloc_func */ |
488 | BROTLI_COMMON_API void* BrotliDefaultAllocFunc(void* opaque, size_t size); |
489 | |
490 | /* Default brotli_free_func */ |
491 | BROTLI_COMMON_API void BrotliDefaultFreeFunc(void* opaque, void* address); |
492 | |
493 | BROTLI_UNUSED_FUNCTION void BrotliSuppressUnusedFunctions(void) { |
494 | BROTLI_UNUSED(&BrotliSuppressUnusedFunctions); |
495 | BROTLI_UNUSED(&BrotliUnalignedRead16); |
496 | BROTLI_UNUSED(&BrotliUnalignedRead32); |
497 | BROTLI_UNUSED(&BrotliUnalignedRead64); |
498 | BROTLI_UNUSED(&BrotliUnalignedWrite64); |
499 | BROTLI_UNUSED(&BROTLI_UNALIGNED_LOAD16LE); |
500 | BROTLI_UNUSED(&BROTLI_UNALIGNED_LOAD32LE); |
501 | BROTLI_UNUSED(&BROTLI_UNALIGNED_LOAD64LE); |
502 | BROTLI_UNUSED(&BROTLI_UNALIGNED_STORE64LE); |
503 | BROTLI_UNUSED(&BROTLI_UNALIGNED_LOAD_PTR); |
504 | BROTLI_UNUSED(&BROTLI_UNALIGNED_STORE_PTR); |
505 | BROTLI_UNUSED(&BrotliRBit); |
506 | BROTLI_UNUSED(&brotli_min_double); |
507 | BROTLI_UNUSED(&brotli_max_double); |
508 | BROTLI_UNUSED(&brotli_min_float); |
509 | BROTLI_UNUSED(&brotli_max_float); |
510 | BROTLI_UNUSED(&brotli_min_int); |
511 | BROTLI_UNUSED(&brotli_max_int); |
512 | BROTLI_UNUSED(&brotli_min_size_t); |
513 | BROTLI_UNUSED(&brotli_max_size_t); |
514 | BROTLI_UNUSED(&brotli_min_uint32_t); |
515 | BROTLI_UNUSED(&brotli_max_uint32_t); |
516 | BROTLI_UNUSED(&brotli_min_uint8_t); |
517 | BROTLI_UNUSED(&brotli_max_uint8_t); |
518 | BROTLI_UNUSED(&BrotliDefaultAllocFunc); |
519 | BROTLI_UNUSED(&BrotliDefaultFreeFunc); |
520 | #if defined(BROTLI_DEBUG) || defined(BROTLI_ENABLE_LOG) |
521 | BROTLI_UNUSED(&BrotliDump); |
522 | #endif |
523 | } |
524 | |
525 | #endif /* BROTLI_COMMON_PLATFORM_H_ */ |
526 | |