Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(308)

Side by Side Diff: third_party/brotli/enc/command.h

Issue 2537133002: Update brotli to v1.0.0-snapshot. (Closed)
Patch Set: Fixed typo Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/brotli/enc/cluster_inc.h ('k') | third_party/brotli/enc/compress_fragment.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* Copyright 2013 Google Inc. All Rights Reserved. 1 /* Copyright 2013 Google Inc. All Rights Reserved.
2 2
3 Distributed under MIT license. 3 Distributed under MIT license.
4 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT 4 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
5 */ 5 */
6 6
7 // This class models a sequence of literals and a backward reference copy. 7 /* This class models a sequence of literals and a backward reference copy. */
8 8
9 #ifndef BROTLI_ENC_COMMAND_H_ 9 #ifndef BROTLI_ENC_COMMAND_H_
10 #define BROTLI_ENC_COMMAND_H_ 10 #define BROTLI_ENC_COMMAND_H_
11 11
12 #include "../common/constants.h"
13 #include <brotli/port.h>
14 #include <brotli/types.h>
12 #include "./fast_log.h" 15 #include "./fast_log.h"
13 #include "./prefix.h" 16 #include "./prefix.h"
14 #include "./types.h"
15 17
16 namespace brotli { 18 #if defined(__cplusplus) || defined(c_plusplus)
19 extern "C" {
20 #endif
17 21
18 static uint32_t kInsBase[] = { 0, 1, 2, 3, 4, 5, 6, 8, 10, 14, 18, 26, 34, 50, 22 static uint32_t kInsBase[] = { 0, 1, 2, 3, 4, 5, 6, 8, 10, 14, 18, 26, 34, 50,
19 66, 98, 130, 194, 322, 578, 1090, 2114, 6210, 22594 }; 23 66, 98, 130, 194, 322, 578, 1090, 2114, 6210, 22594 };
20 static uint32_t kInsExtra[] = { 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 24 static uint32_t kInsExtra[] = { 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4,
21 5, 5, 6, 7, 8, 9, 10, 12, 14, 24 }; 25 5, 5, 6, 7, 8, 9, 10, 12, 14, 24 };
22 static uint32_t kCopyBase[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 18, 22, 30, 26 static uint32_t kCopyBase[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 18, 22, 30,
23 38, 54, 70, 102, 134, 198, 326, 582, 1094, 2118 }; 27 38, 54, 70, 102, 134, 198, 326, 582, 1094, 2118 };
24 static uint32_t kCopyExtra[] = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 28 static uint32_t kCopyExtra[] = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 3,
25 4, 4, 5, 5, 6, 7, 8, 9, 10, 24 }; 29 4, 4, 5, 5, 6, 7, 8, 9, 10, 24 };
26 30
27 static inline uint16_t GetInsertLengthCode(size_t insertlen) { 31 static BROTLI_INLINE uint16_t GetInsertLengthCode(size_t insertlen) {
28 if (insertlen < 6) { 32 if (insertlen < 6) {
29 return static_cast<uint16_t>(insertlen); 33 return (uint16_t)insertlen;
30 } else if (insertlen < 130) { 34 } else if (insertlen < 130) {
31 insertlen -= 2; 35 uint32_t nbits = Log2FloorNonZero(insertlen - 2) - 1u;
32 uint32_t nbits = Log2FloorNonZero(insertlen) - 1u; 36 return (uint16_t)((nbits << 1) + ((insertlen - 2) >> nbits) + 2);
33 return static_cast<uint16_t>((nbits << 1) + (insertlen >> nbits) + 2);
34 } else if (insertlen < 2114) { 37 } else if (insertlen < 2114) {
35 return static_cast<uint16_t>(Log2FloorNonZero(insertlen - 66) + 10); 38 return (uint16_t)(Log2FloorNonZero(insertlen - 66) + 10);
36 } else if (insertlen < 6210) { 39 } else if (insertlen < 6210) {
37 return 21u; 40 return 21u;
38 } else if (insertlen < 22594) { 41 } else if (insertlen < 22594) {
39 return 22u; 42 return 22u;
40 } else { 43 } else {
41 return 23u; 44 return 23u;
42 } 45 }
43 } 46 }
44 47
45 static inline uint16_t GetCopyLengthCode(size_t copylen) { 48 static BROTLI_INLINE uint16_t GetCopyLengthCode(size_t copylen) {
46 if (copylen < 10) { 49 if (copylen < 10) {
47 return static_cast<uint16_t>(copylen - 2); 50 return (uint16_t)(copylen - 2);
48 } else if (copylen < 134) { 51 } else if (copylen < 134) {
49 copylen -= 6; 52 uint32_t nbits = Log2FloorNonZero(copylen - 6) - 1u;
50 uint32_t nbits = Log2FloorNonZero(copylen) - 1u; 53 return (uint16_t)((nbits << 1) + ((copylen - 6) >> nbits) + 4);
51 return static_cast<uint16_t>((nbits << 1) + (copylen >> nbits) + 4);
52 } else if (copylen < 2118) { 54 } else if (copylen < 2118) {
53 return static_cast<uint16_t>(Log2FloorNonZero(copylen - 70) + 12); 55 return (uint16_t)(Log2FloorNonZero(copylen - 70) + 12);
54 } else { 56 } else {
55 return 23u; 57 return 23u;
56 } 58 }
57 } 59 }
58 60
59 static inline uint16_t CombineLengthCodes( 61 static BROTLI_INLINE uint16_t CombineLengthCodes(
60 uint16_t inscode, uint16_t copycode, bool use_last_distance) { 62 uint16_t inscode, uint16_t copycode, BROTLI_BOOL use_last_distance) {
61 uint16_t bits64 = 63 uint16_t bits64 =
62 static_cast<uint16_t>((copycode & 0x7u) | ((inscode & 0x7u) << 3)); 64 (uint16_t)((copycode & 0x7u) | ((inscode & 0x7u) << 3));
63 if (use_last_distance && inscode < 8 && copycode < 16) { 65 if (use_last_distance && inscode < 8 && copycode < 16) {
64 return (copycode < 8) ? bits64 : (bits64 | 64); 66 return (copycode < 8) ? bits64 : (bits64 | 64);
65 } else { 67 } else {
66 // "To convert an insert-and-copy length code to an insert length code and 68 /* "To convert an insert-and-copy length code to an insert length code and
67 // a copy length code, the following table can be used" 69 a copy length code, the following table can be used" */
68 static const uint16_t cells[9] = { 128u, 192u, 384u, 256u, 320u, 512u, 70 static const uint16_t cells[9] = { 128u, 192u, 384u, 256u, 320u, 512u,
69 448u, 576u, 640u }; 71 448u, 576u, 640u };
70 return cells[(copycode >> 3) + 3 * (inscode >> 3)] | bits64; 72 return cells[(copycode >> 3) + 3 * (inscode >> 3)] | bits64;
71 } 73 }
72 } 74 }
73 75
74 static inline void GetLengthCode(size_t insertlen, size_t copylen, 76 static BROTLI_INLINE void GetLengthCode(size_t insertlen, size_t copylen,
75 bool use_last_distance, 77 BROTLI_BOOL use_last_distance,
76 uint16_t* code) { 78 uint16_t* code) {
77 uint16_t inscode = GetInsertLengthCode(insertlen); 79 uint16_t inscode = GetInsertLengthCode(insertlen);
78 uint16_t copycode = GetCopyLengthCode(copylen); 80 uint16_t copycode = GetCopyLengthCode(copylen);
79 *code = CombineLengthCodes(inscode, copycode, use_last_distance); 81 *code = CombineLengthCodes(inscode, copycode, use_last_distance);
80 } 82 }
81 83
82 static inline uint32_t GetInsertBase(uint16_t inscode) { 84 static BROTLI_INLINE uint32_t GetInsertBase(uint16_t inscode) {
83 return kInsBase[inscode]; 85 return kInsBase[inscode];
84 } 86 }
85 87
86 static inline uint32_t GetInsertExtra(uint16_t inscode) { 88 static BROTLI_INLINE uint32_t GetInsertExtra(uint16_t inscode) {
87 return kInsExtra[inscode]; 89 return kInsExtra[inscode];
88 } 90 }
89 91
90 static inline uint32_t GetCopyBase(uint16_t copycode) { 92 static BROTLI_INLINE uint32_t GetCopyBase(uint16_t copycode) {
91 return kCopyBase[copycode]; 93 return kCopyBase[copycode];
92 } 94 }
93 95
94 static inline uint32_t GetCopyExtra(uint16_t copycode) { 96 static BROTLI_INLINE uint32_t GetCopyExtra(uint16_t copycode) {
95 return kCopyExtra[copycode]; 97 return kCopyExtra[copycode];
96 } 98 }
97 99
98 struct Command { 100 typedef struct Command {
99 // distance_code is e.g. 0 for same-as-last short code, or 16 for offset 1.
100 Command(size_t insertlen, size_t copylen, size_t copylen_code,
101 size_t distance_code)
102 : insert_len_(static_cast<uint32_t>(insertlen)) {
103 copy_len_ = static_cast<uint32_t>(
104 copylen | ((copylen_code ^ copylen) << 24));
105 // The distance prefix and extra bits are stored in this Command as if
106 // npostfix and ndirect were 0, they are only recomputed later after the
107 // clustering if needed.
108 PrefixEncodeCopyDistance(distance_code, 0, 0, &dist_prefix_, &dist_extra_);
109 GetLengthCode(insertlen, copylen_code, dist_prefix_ == 0,
110 &cmd_prefix_);
111 }
112
113 explicit Command(size_t insertlen)
114 : insert_len_(static_cast<uint32_t>(insertlen))
115 , copy_len_(4 << 24), dist_extra_(0), dist_prefix_(16) {
116 GetLengthCode(insertlen, 4, dist_prefix_ == 0, &cmd_prefix_);
117 }
118
119 uint32_t DistanceCode(void) const {
120 if (dist_prefix_ < 16) {
121 return dist_prefix_;
122 }
123 uint32_t nbits = dist_extra_ >> 24;
124 uint32_t extra = dist_extra_ & 0xffffff;
125 uint32_t prefix = dist_prefix_ - 12 - 2 * nbits;
126 return (prefix << nbits) + extra + 12;
127 }
128
129 uint32_t DistanceContext(void) const {
130 uint32_t r = cmd_prefix_ >> 6;
131 uint32_t c = cmd_prefix_ & 7;
132 if ((r == 0 || r == 2 || r == 4 || r == 7) && (c <= 2)) {
133 return c;
134 }
135 return 3;
136 }
137
138 inline uint32_t copy_len(void) const {
139 return copy_len_ & 0xFFFFFF;
140 }
141
142 inline uint32_t copy_len_code(void) const {
143 return (copy_len_ & 0xFFFFFF) ^ (copy_len_ >> 24);
144 }
145
146 uint32_t insert_len_; 101 uint32_t insert_len_;
147 /* Stores copy_len in low 24 bits and copy_len XOR copy_code in high 8 bit. */ 102 /* Stores copy_len in low 24 bits and copy_len XOR copy_code in high 8 bit. */
148 uint32_t copy_len_; 103 uint32_t copy_len_;
149 uint32_t dist_extra_; 104 uint32_t dist_extra_;
150 uint16_t cmd_prefix_; 105 uint16_t cmd_prefix_;
151 uint16_t dist_prefix_; 106 uint16_t dist_prefix_;
152 }; 107 } Command;
153 108
154 } // namespace brotli 109 /* distance_code is e.g. 0 for same-as-last short code, or 16 for offset 1. */
110 static BROTLI_INLINE void InitCommand(Command* self, size_t insertlen,
111 size_t copylen, size_t copylen_code, size_t distance_code) {
112 self->insert_len_ = (uint32_t)insertlen;
113 self->copy_len_ = (uint32_t)(copylen | ((copylen_code ^ copylen) << 24));
114 /* The distance prefix and extra bits are stored in this Command as if
115 npostfix and ndirect were 0, they are only recomputed later after the
116 clustering if needed. */
117 PrefixEncodeCopyDistance(
118 distance_code, 0, 0, &self->dist_prefix_, &self->dist_extra_);
119 GetLengthCode(
120 insertlen, copylen_code, TO_BROTLI_BOOL(self->dist_prefix_ == 0),
121 &self->cmd_prefix_);
122 }
155 123
156 #endif // BROTLI_ENC_COMMAND_H_ 124 static BROTLI_INLINE void InitInsertCommand(Command* self, size_t insertlen) {
125 self->insert_len_ = (uint32_t)insertlen;
126 self->copy_len_ = 4 << 24;
127 self->dist_extra_ = 0;
128 self->dist_prefix_ = BROTLI_NUM_DISTANCE_SHORT_CODES;
129 GetLengthCode(insertlen, 4, BROTLI_FALSE, &self->cmd_prefix_);
130 }
131
132 static BROTLI_INLINE uint32_t CommandRestoreDistanceCode(const Command* self) {
133 if (self->dist_prefix_ < BROTLI_NUM_DISTANCE_SHORT_CODES) {
134 return self->dist_prefix_;
135 } else {
136 uint32_t nbits = self->dist_extra_ >> 24;
137 uint32_t extra = self->dist_extra_ & 0xffffff;
138 /* It is assumed that the distance was first encoded with NPOSTFIX = 0 and
139 NDIRECT = 0, so the code itself is of this form:
140 BROTLI_NUM_DISTANCE_SHORT_CODES + 2 * (nbits - 1) + prefix_bit
141 Therefore, the following expression results in (2 + prefix_bit). */
142 uint32_t prefix =
143 self->dist_prefix_ + 4u - BROTLI_NUM_DISTANCE_SHORT_CODES - 2u * nbits;
144 /* Subtract 4 for offset (Chapter 4.) and
145 increase by BROTLI_NUM_DISTANCE_SHORT_CODES - 1 */
146 return (prefix << nbits) + extra + BROTLI_NUM_DISTANCE_SHORT_CODES - 4u;
147 }
148 }
149
150 static BROTLI_INLINE uint32_t CommandDistanceContext(const Command* self) {
151 uint32_t r = self->cmd_prefix_ >> 6;
152 uint32_t c = self->cmd_prefix_ & 7;
153 if ((r == 0 || r == 2 || r == 4 || r == 7) && (c <= 2)) {
154 return c;
155 }
156 return 3;
157 }
158
159 static BROTLI_INLINE uint32_t CommandCopyLen(const Command* self) {
160 return self->copy_len_ & 0xFFFFFF;
161 }
162
163 static BROTLI_INLINE uint32_t CommandCopyLenCode(const Command* self) {
164 return (self->copy_len_ & 0xFFFFFF) ^ (self->copy_len_ >> 24);
165 }
166
167 #if defined(__cplusplus) || defined(c_plusplus)
168 } /* extern "C" */
169 #endif
170
171 #endif /* BROTLI_ENC_COMMAND_H_ */
OLDNEW
« no previous file with comments | « third_party/brotli/enc/cluster_inc.h ('k') | third_party/brotli/enc/compress_fragment.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698