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