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