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

Side by Side Diff: third_party/brotli/enc/write_bits.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/utf8_util.cc ('k') | third_party/brotli/include/brotli/decode.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 2010 Google Inc. All Rights Reserved. 1 /* Copyright 2010 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 // Write bits into a byte array. 7 /* Write bits into a byte array. */
8 8
9 #ifndef BROTLI_ENC_WRITE_BITS_H_ 9 #ifndef BROTLI_ENC_WRITE_BITS_H_
10 #define BROTLI_ENC_WRITE_BITS_H_ 10 #define BROTLI_ENC_WRITE_BITS_H_
11 11
12 #include <assert.h> 12 #include <assert.h>
13 #include <stdio.h> 13 #include <stdio.h> /* printf */
14 14
15 #include <brotli/types.h>
15 #include "./port.h" 16 #include "./port.h"
16 #include "./types.h"
17 17
18 namespace brotli { 18 #if defined(__cplusplus) || defined(c_plusplus)
19 extern "C" {
20 #endif
19 21
20 //#define BIT_WRITER_DEBUG 22 /*#define BIT_WRITER_DEBUG */
21 23
22 // This function writes bits into bytes in increasing addresses, and within 24 /* This function writes bits into bytes in increasing addresses, and within
23 // a byte least-significant-bit first. 25 a byte least-significant-bit first.
24 // 26
25 // The function can write up to 56 bits in one go with WriteBits 27 The function can write up to 56 bits in one go with WriteBits
26 // Example: let's assume that 3 bits (Rs below) have been written already: 28 Example: let's assume that 3 bits (Rs below) have been written already:
27 // 29
28 // BYTE-0 BYTE+1 BYTE+2 30 BYTE-0 BYTE+1 BYTE+2
29 // 31
30 // 0000 0RRR 0000 0000 0000 0000 32 0000 0RRR 0000 0000 0000 0000
31 // 33
32 // Now, we could write 5 or less bits in MSB by just sifting by 3 34 Now, we could write 5 or less bits in MSB by just sifting by 3
33 // and OR'ing to BYTE-0. 35 and OR'ing to BYTE-0.
34 // 36
35 // For n bits, we take the last 5 bits, OR that with high bits in BYTE-0, 37 For n bits, we take the last 5 bits, OR that with high bits in BYTE-0,
36 // and locate the rest in BYTE+1, BYTE+2, etc. 38 and locate the rest in BYTE+1, BYTE+2, etc. */
37 inline void WriteBits(size_t n_bits, 39 static BROTLI_INLINE void BrotliWriteBits(size_t n_bits,
38 uint64_t bits, 40 uint64_t bits,
39 size_t * __restrict pos, 41 size_t * BROTLI_RESTRICT pos,
40 uint8_t * __restrict array) { 42 uint8_t * BROTLI_RESTRICT array) {
43 #ifdef IS_LITTLE_ENDIAN
44 /* This branch of the code can write up to 56 bits at a time,
45 7 bits are lost by being perhaps already in *p and at least
46 1 bit is needed to initialize the bit-stream ahead (i.e. if 7
47 bits are in *p and we write 57 bits, then the next write will
48 access a byte that was never initialized). */
49 uint8_t *p = &array[*pos >> 3];
50 uint64_t v = *p;
41 #ifdef BIT_WRITER_DEBUG 51 #ifdef BIT_WRITER_DEBUG
42 printf("WriteBits %2d 0x%016llx %10d\n", n_bits, bits, *pos); 52 printf("WriteBits %2d 0x%016llx %10d\n", n_bits, bits, *pos);
43 #endif 53 #endif
44 assert((bits >> n_bits) == 0); 54 assert((bits >> n_bits) == 0);
45 assert(n_bits <= 56); 55 assert(n_bits <= 56);
46 #ifdef IS_LITTLE_ENDIAN
47 // This branch of the code can write up to 56 bits at a time,
48 // 7 bits are lost by being perhaps already in *p and at least
49 // 1 bit is needed to initialize the bit-stream ahead (i.e. if 7
50 // bits are in *p and we write 57 bits, then the next write will
51 // access a byte that was never initialized).
52 uint8_t *p = &array[*pos >> 3];
53 uint64_t v = *p;
54 v |= bits << (*pos & 7); 56 v |= bits << (*pos & 7);
55 BROTLI_UNALIGNED_STORE64(p, v); // Set some bits. 57 BROTLI_UNALIGNED_STORE64(p, v); /* Set some bits. */
56 *pos += n_bits; 58 *pos += n_bits;
57 #else 59 #else
58 // implicit & 0xff is assumed for uint8_t arithmetics 60 /* implicit & 0xff is assumed for uint8_t arithmetics */
59 uint8_t *array_pos = &array[*pos >> 3]; 61 uint8_t *array_pos = &array[*pos >> 3];
60 const size_t bits_reserved_in_first_byte = (*pos & 7); 62 const size_t bits_reserved_in_first_byte = (*pos & 7);
63 size_t bits_left_to_write;
61 bits <<= bits_reserved_in_first_byte; 64 bits <<= bits_reserved_in_first_byte;
62 *array_pos++ |= static_cast<uint8_t>(bits); 65 *array_pos++ |= (uint8_t)bits;
63 for (size_t bits_left_to_write = n_bits + bits_reserved_in_first_byte; 66 for (bits_left_to_write = n_bits + bits_reserved_in_first_byte;
64 bits_left_to_write >= 9; 67 bits_left_to_write >= 9;
65 bits_left_to_write -= 8) { 68 bits_left_to_write -= 8) {
66 bits >>= 8; 69 bits >>= 8;
67 *array_pos++ = static_cast<uint8_t>(bits); 70 *array_pos++ = (uint8_t)bits;
68 } 71 }
69 *array_pos = 0; 72 *array_pos = 0;
70 *pos += n_bits; 73 *pos += n_bits;
71 #endif 74 #endif
72 } 75 }
73 76
74 inline void WriteBitsPrepareStorage(size_t pos, uint8_t *array) { 77 static BROTLI_INLINE void BrotliWriteBitsPrepareStorage(
78 size_t pos, uint8_t *array) {
75 #ifdef BIT_WRITER_DEBUG 79 #ifdef BIT_WRITER_DEBUG
76 printf("WriteBitsPrepareStorage %10d\n", pos); 80 printf("WriteBitsPrepareStorage %10d\n", pos);
77 #endif 81 #endif
78 assert((pos & 7) == 0); 82 assert((pos & 7) == 0);
79 array[pos >> 3] = 0; 83 array[pos >> 3] = 0;
80 } 84 }
81 85
82 } // namespace brotli 86 #if defined(__cplusplus) || defined(c_plusplus)
87 } /* extern "C" */
88 #endif
83 89
84 #endif // BROTLI_ENC_WRITE_BITS_H_ 90 #endif /* BROTLI_ENC_WRITE_BITS_H_ */
OLDNEW
« no previous file with comments | « third_party/brotli/enc/utf8_util.cc ('k') | third_party/brotli/include/brotli/decode.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698