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