MdeModulePkg BrotliLib: Fix the regression logic issue in loop
[mirror_edk2.git] / MdeModulePkg / Library / BrotliCustomDecompressLib / dec / port.h
1 /* Copyright 2015 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_MODERN_COMPILER forces to use modern compilers built-ins,
16 features and attributes
17 * BROTLI_BUILD_PORTABLE disables dangerous optimizations, like unaligned
18 read and overlapping memcpy; this reduces decompression speed by 5%
19 * BROTLI_DEBUG dumps file name and line number when decoder detects stream
20 or memory error
21 * BROTLI_ENABLE_LOG enables asserts and dumps various state information
22 */
23
24 #ifndef BROTLI_DEC_PORT_H_
25 #define BROTLI_DEC_PORT_H_
26
27 #if defined(BROTLI_ENABLE_LOG) || defined(BROTLI_DEBUG)
28 #include <assert.h>
29 #include <stdio.h>
30 #endif
31
32 #include "../common/port.h"
33
34 #if defined(__arm__) || defined(__thumb__) || \
35 defined(_M_ARM) || defined(_M_ARMT)
36 #define BROTLI_TARGET_ARM
37 #if (defined(__ARM_ARCH) && (__ARM_ARCH >= 7)) || \
38 (defined(M_ARM) && (M_ARM >= 7))
39 #define BROTLI_TARGET_ARMV7
40 #endif /* ARMv7 */
41 #if defined(__aarch64__)
42 #define BROTLI_TARGET_ARMV8
43 #endif /* ARMv8 */
44 #endif /* ARM */
45
46 #if defined(__i386) || defined(_M_IX86)
47 #define BROTLI_TARGET_X86
48 #endif
49
50 #if defined(__x86_64__) || defined(_M_X64)
51 #define BROTLI_TARGET_X64
52 #endif
53
54 #if defined(__PPC64__)
55 #define BROTLI_TARGET_POWERPC64
56 #endif
57
58 #ifdef BROTLI_BUILD_PORTABLE
59 #define BROTLI_ALIGNED_READ (!!1)
60 #elif defined(BROTLI_TARGET_X86) || defined(BROTLI_TARGET_X64) || \
61 defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8)
62 /* Allow unaligned read only for whitelisted CPUs. */
63 #define BROTLI_ALIGNED_READ (!!0)
64 #else
65 #define BROTLI_ALIGNED_READ (!!1)
66 #endif
67
68 /* IS_CONSTANT macros returns true for compile-time constant expressions. */
69 #if BROTLI_MODERN_COMPILER || __has_builtin(__builtin_constant_p)
70 #define IS_CONSTANT(x) (!!__builtin_constant_p(x))
71 #else
72 #define IS_CONSTANT(x) (!!0)
73 #endif
74
75 #ifdef BROTLI_ENABLE_LOG
76 #define BROTLI_DCHECK(x) assert(x)
77 #define BROTLI_LOG(x) printf x
78 #else
79 #define BROTLI_DCHECK(x)
80 #define BROTLI_LOG(x)
81 #endif
82
83 #if defined(BROTLI_DEBUG) || defined(BROTLI_ENABLE_LOG)
84 static BROTLI_INLINE void BrotliDump(const char* f, int l, const char* fn) {
85 fprintf(stderr, "%s:%d (%s)\n", f, l, fn);
86 fflush(stderr);
87 }
88 #define BROTLI_DUMP() BrotliDump(__FILE__, __LINE__, __FUNCTION__)
89 #else
90 #define BROTLI_DUMP() (void)(0)
91 #endif
92
93 #if defined(BROTLI_BUILD_64_BIT)
94 #define BROTLI_64_BITS 1
95 #elif defined(BROTLI_BUILD_32_BIT)
96 #define BROTLI_64_BITS 0
97 #elif defined(BROTLI_TARGET_X64) || defined(BROTLI_TARGET_ARMV8) || \
98 defined(BROTLI_TARGET_POWERPC64)
99 #define BROTLI_64_BITS 1
100 #else
101 #define BROTLI_64_BITS 0
102 #endif
103
104 #if defined(BROTLI_BUILD_BIG_ENDIAN)
105 #define BROTLI_LITTLE_ENDIAN 0
106 #define BROTLI_BIG_ENDIAN 1
107 #elif defined(BROTLI_BUILD_LITTLE_ENDIAN)
108 #define BROTLI_LITTLE_ENDIAN 1
109 #define BROTLI_BIG_ENDIAN 0
110 #elif defined(BROTLI_BUILD_ENDIAN_NEUTRAL)
111 #define BROTLI_LITTLE_ENDIAN 0
112 #define BROTLI_BIG_ENDIAN 0
113 #elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
114 #define BROTLI_LITTLE_ENDIAN 1
115 #define BROTLI_BIG_ENDIAN 0
116 #elif defined(_WIN32)
117 /* Win32 can currently always be assumed to be little endian */
118 #define BROTLI_LITTLE_ENDIAN 1
119 #define BROTLI_BIG_ENDIAN 0
120 #else
121 #if (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
122 #define BROTLI_BIG_ENDIAN 1
123 #else
124 #define BROTLI_BIG_ENDIAN 0
125 #endif
126 #define BROTLI_LITTLE_ENDIAN 0
127 #endif
128
129 #define BROTLI_REPEAT(N, X) { \
130 if ((N & 1) != 0) {X;} \
131 if ((N & 2) != 0) {X; X;} \
132 if ((N & 4) != 0) {X; X; X; X;} \
133 }
134
135 #if BROTLI_MODERN_COMPILER || defined(__llvm__)
136 #if defined(BROTLI_TARGET_ARMV7)
137 static BROTLI_INLINE unsigned BrotliRBit(unsigned input) {
138 unsigned output;
139 __asm__("rbit %0, %1\n" : "=r"(output) : "r"(input));
140 return output;
141 }
142 #define BROTLI_RBIT(x) BrotliRBit(x)
143 #endif /* armv7 */
144 #endif /* gcc || clang */
145
146 #if defined(BROTLI_TARGET_ARM)
147 #define BROTLI_HAS_UBFX (!!1)
148 #else
149 #define BROTLI_HAS_UBFX (!!0)
150 #endif
151
152 #define BROTLI_ALLOC(S, L) S->alloc_func(S->memory_manager_opaque, L)
153
154 #define BROTLI_FREE(S, X) { \
155 S->free_func(S->memory_manager_opaque, X); \
156 X = NULL; \
157 }
158
159 #endif /* BROTLI_DEC_PORT_H_ */