]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/C/BrotliCompress/enc/command.h
MdeModulePkg/BrotliCustomDecompressLib: Make brotli a submodule
[mirror_edk2.git] / BaseTools / Source / C / BrotliCompress / enc / command.h
CommitLineData
11b7501a
SB
1/* Copyright 2013 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/* This class models a sequence of literals and a backward reference copy. */\r
8\r
9#ifndef BROTLI_ENC_COMMAND_H_\r
10#define BROTLI_ENC_COMMAND_H_\r
11\r
dd4f667e
LG
12#include "../common/constants.h"\r
13#include "../common/platform.h"\r
14#include <brotli/types.h>\r
11b7501a 15#include "./fast_log.h"\r
dd4f667e 16#include "./params.h"\r
11b7501a
SB
17#include "./prefix.h"\r
18\r
19#if defined(__cplusplus) || defined(c_plusplus)\r
20extern "C" {\r
21#endif\r
22\r
23static uint32_t kInsBase[] = { 0, 1, 2, 3, 4, 5, 6, 8, 10, 14, 18, 26, 34, 50,\r
24 66, 98, 130, 194, 322, 578, 1090, 2114, 6210, 22594 };\r
25static uint32_t kInsExtra[] = { 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4,\r
26 5, 5, 6, 7, 8, 9, 10, 12, 14, 24 };\r
27static uint32_t kCopyBase[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 18, 22, 30,\r
28 38, 54, 70, 102, 134, 198, 326, 582, 1094, 2118 };\r
29static uint32_t kCopyExtra[] = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 3,\r
30 4, 4, 5, 5, 6, 7, 8, 9, 10, 24 };\r
31\r
32static BROTLI_INLINE uint16_t GetInsertLengthCode(size_t insertlen) {\r
33 if (insertlen < 6) {\r
34 return (uint16_t)insertlen;\r
35 } else if (insertlen < 130) {\r
36 uint32_t nbits = Log2FloorNonZero(insertlen - 2) - 1u;\r
37 return (uint16_t)((nbits << 1) + ((insertlen - 2) >> nbits) + 2);\r
38 } else if (insertlen < 2114) {\r
39 return (uint16_t)(Log2FloorNonZero(insertlen - 66) + 10);\r
40 } else if (insertlen < 6210) {\r
41 return 21u;\r
42 } else if (insertlen < 22594) {\r
43 return 22u;\r
44 } else {\r
45 return 23u;\r
46 }\r
47}\r
48\r
49static BROTLI_INLINE uint16_t GetCopyLengthCode(size_t copylen) {\r
50 if (copylen < 10) {\r
51 return (uint16_t)(copylen - 2);\r
52 } else if (copylen < 134) {\r
53 uint32_t nbits = Log2FloorNonZero(copylen - 6) - 1u;\r
54 return (uint16_t)((nbits << 1) + ((copylen - 6) >> nbits) + 4);\r
55 } else if (copylen < 2118) {\r
56 return (uint16_t)(Log2FloorNonZero(copylen - 70) + 12);\r
57 } else {\r
58 return 23u;\r
59 }\r
60}\r
61\r
62static BROTLI_INLINE uint16_t CombineLengthCodes(\r
63 uint16_t inscode, uint16_t copycode, BROTLI_BOOL use_last_distance) {\r
64 uint16_t bits64 =\r
dd4f667e
LG
65 (uint16_t)((copycode & 0x7u) | ((inscode & 0x7u) << 3u));\r
66 if (use_last_distance && inscode < 8u && copycode < 16u) {\r
67 return (copycode < 8u) ? bits64 : (bits64 | 64u);\r
11b7501a 68 } else {\r
dd4f667e
LG
69 /* Specification: 5 Encoding of ... (last table) */\r
70 /* offset = 2 * index, where index is in range [0..8] */\r
71 uint32_t offset = 2u * ((copycode >> 3u) + 3u * (inscode >> 3u));\r
72 /* All values in specification are K * 64,\r
73 where K = [2, 3, 6, 4, 5, 8, 7, 9, 10],\r
74 i + 1 = [1, 2, 3, 4, 5, 6, 7, 8, 9],\r
75 K - i - 1 = [1, 1, 3, 0, 0, 2, 0, 1, 2] = D.\r
76 All values in D require only 2 bits to encode.\r
77 Magic constant is shifted 6 bits left, to avoid final multiplication. */\r
78 offset = (offset << 5u) + 0x40u + ((0x520D40u >> offset) & 0xC0u);\r
79 return (uint16_t)(offset | bits64);\r
11b7501a
SB
80 }\r
81}\r
82\r
83static BROTLI_INLINE void GetLengthCode(size_t insertlen, size_t copylen,\r
84 BROTLI_BOOL use_last_distance,\r
85 uint16_t* code) {\r
86 uint16_t inscode = GetInsertLengthCode(insertlen);\r
87 uint16_t copycode = GetCopyLengthCode(copylen);\r
88 *code = CombineLengthCodes(inscode, copycode, use_last_distance);\r
89}\r
90\r
91static BROTLI_INLINE uint32_t GetInsertBase(uint16_t inscode) {\r
92 return kInsBase[inscode];\r
93}\r
94\r
95static BROTLI_INLINE uint32_t GetInsertExtra(uint16_t inscode) {\r
96 return kInsExtra[inscode];\r
97}\r
98\r
99static BROTLI_INLINE uint32_t GetCopyBase(uint16_t copycode) {\r
100 return kCopyBase[copycode];\r
101}\r
102\r
103static BROTLI_INLINE uint32_t GetCopyExtra(uint16_t copycode) {\r
104 return kCopyExtra[copycode];\r
105}\r
106\r
107typedef struct Command {\r
108 uint32_t insert_len_;\r
dd4f667e 109 /* Stores copy_len in low 25 bits and copy_code - copy_len in high 7 bit. */\r
11b7501a 110 uint32_t copy_len_;\r
dd4f667e 111 /* Stores distance extra bits. */\r
11b7501a
SB
112 uint32_t dist_extra_;\r
113 uint16_t cmd_prefix_;\r
dd4f667e
LG
114 /* Stores distance code in low 10 bits\r
115 and number of extra bits in high 6 bits. */\r
11b7501a
SB
116 uint16_t dist_prefix_;\r
117} Command;\r
118\r
119/* distance_code is e.g. 0 for same-as-last short code, or 16 for offset 1. */\r
dd4f667e
LG
120static BROTLI_INLINE void InitCommand(Command* self,\r
121 const BrotliDistanceParams* dist, size_t insertlen,\r
122 size_t copylen, int copylen_code_delta, size_t distance_code) {\r
123 /* Don't rely on signed int representation, use honest casts. */\r
124 uint32_t delta = (uint8_t)((int8_t)copylen_code_delta);\r
11b7501a 125 self->insert_len_ = (uint32_t)insertlen;\r
dd4f667e 126 self->copy_len_ = (uint32_t)(copylen | (delta << 25));\r
11b7501a
SB
127 /* The distance prefix and extra bits are stored in this Command as if\r
128 npostfix and ndirect were 0, they are only recomputed later after the\r
129 clustering if needed. */\r
130 PrefixEncodeCopyDistance(\r
dd4f667e
LG
131 distance_code, dist->num_direct_distance_codes,\r
132 dist->distance_postfix_bits, &self->dist_prefix_, &self->dist_extra_);\r
11b7501a 133 GetLengthCode(\r
dd4f667e
LG
134 insertlen, (size_t)((int)copylen + copylen_code_delta),\r
135 TO_BROTLI_BOOL((self->dist_prefix_ & 0x3FF) == 0), &self->cmd_prefix_);\r
11b7501a
SB
136}\r
137\r
138static BROTLI_INLINE void InitInsertCommand(Command* self, size_t insertlen) {\r
139 self->insert_len_ = (uint32_t)insertlen;\r
dd4f667e 140 self->copy_len_ = 4 << 25;\r
11b7501a 141 self->dist_extra_ = 0;\r
dd4f667e 142 self->dist_prefix_ = BROTLI_NUM_DISTANCE_SHORT_CODES;\r
11b7501a
SB
143 GetLengthCode(insertlen, 4, BROTLI_FALSE, &self->cmd_prefix_);\r
144}\r
145\r
dd4f667e
LG
146static BROTLI_INLINE uint32_t CommandRestoreDistanceCode(\r
147 const Command* self, const BrotliDistanceParams* dist) {\r
148 if ((self->dist_prefix_ & 0x3FFu) <\r
149 BROTLI_NUM_DISTANCE_SHORT_CODES + dist->num_direct_distance_codes) {\r
150 return self->dist_prefix_ & 0x3FFu;\r
11b7501a 151 } else {\r
dd4f667e
LG
152 uint32_t dcode = self->dist_prefix_ & 0x3FFu;\r
153 uint32_t nbits = self->dist_prefix_ >> 10;\r
154 uint32_t extra = self->dist_extra_;\r
155 uint32_t postfix_mask = (1U << dist->distance_postfix_bits) - 1U;\r
156 uint32_t hcode = (dcode - dist->num_direct_distance_codes -\r
157 BROTLI_NUM_DISTANCE_SHORT_CODES) >>\r
158 dist->distance_postfix_bits;\r
159 uint32_t lcode = (dcode - dist->num_direct_distance_codes -\r
160 BROTLI_NUM_DISTANCE_SHORT_CODES) & postfix_mask;\r
161 uint32_t offset = ((2U + (hcode & 1U)) << nbits) - 4U;\r
162 return ((offset + extra) << dist->distance_postfix_bits) + lcode +\r
163 dist->num_direct_distance_codes + BROTLI_NUM_DISTANCE_SHORT_CODES;\r
11b7501a
SB
164 }\r
165}\r
166\r
167static BROTLI_INLINE uint32_t CommandDistanceContext(const Command* self) {\r
168 uint32_t r = self->cmd_prefix_ >> 6;\r
169 uint32_t c = self->cmd_prefix_ & 7;\r
170 if ((r == 0 || r == 2 || r == 4 || r == 7) && (c <= 2)) {\r
171 return c;\r
172 }\r
173 return 3;\r
174}\r
175\r
176static BROTLI_INLINE uint32_t CommandCopyLen(const Command* self) {\r
dd4f667e 177 return self->copy_len_ & 0x1FFFFFF;\r
11b7501a
SB
178}\r
179\r
180static BROTLI_INLINE uint32_t CommandCopyLenCode(const Command* self) {\r
dd4f667e
LG
181 uint32_t modifier = self->copy_len_ >> 25;\r
182 int32_t delta = (int8_t)((uint8_t)(modifier | ((modifier & 0x40) << 1)));\r
183 return (uint32_t)((int32_t)(self->copy_len_ & 0x1FFFFFF) + delta);\r
11b7501a
SB
184}\r
185\r
186#if defined(__cplusplus) || defined(c_plusplus)\r
187} /* extern "C" */\r
188#endif\r
189\r
190#endif /* BROTLI_ENC_COMMAND_H_ */\r