]>
Commit | Line | Data |
---|---|---|
11b7501a SB |
1 | /* Copyright 2010 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 | /* Write bits into a byte array. */\r | |
8 | \r | |
9 | #ifndef BROTLI_ENC_WRITE_BITS_H_\r | |
10 | #define BROTLI_ENC_WRITE_BITS_H_\r | |
11 | \r | |
dd4f667e LG |
12 | #include "../common/platform.h"\r |
13 | #include <brotli/types.h>\r | |
11b7501a SB |
14 | \r |
15 | #if defined(__cplusplus) || defined(c_plusplus)\r | |
16 | extern "C" {\r | |
17 | #endif\r | |
18 | \r | |
19 | /*#define BIT_WRITER_DEBUG */\r | |
20 | \r | |
21 | /* This function writes bits into bytes in increasing addresses, and within\r | |
22 | a byte least-significant-bit first.\r | |
23 | \r | |
24 | The function can write up to 56 bits in one go with WriteBits\r | |
25 | Example: let's assume that 3 bits (Rs below) have been written already:\r | |
26 | \r | |
27 | BYTE-0 BYTE+1 BYTE+2\r | |
28 | \r | |
29 | 0000 0RRR 0000 0000 0000 0000\r | |
30 | \r | |
31 | Now, we could write 5 or less bits in MSB by just sifting by 3\r | |
32 | and OR'ing to BYTE-0.\r | |
33 | \r | |
34 | For n bits, we take the last 5 bits, OR that with high bits in BYTE-0,\r | |
35 | and locate the rest in BYTE+1, BYTE+2, etc. */\r | |
36 | static BROTLI_INLINE void BrotliWriteBits(size_t n_bits,\r | |
37 | uint64_t bits,\r | |
dd4f667e LG |
38 | size_t* BROTLI_RESTRICT pos,\r |
39 | uint8_t* BROTLI_RESTRICT array) {\r | |
40 | #if defined(BROTLI_LITTLE_ENDIAN)\r | |
11b7501a SB |
41 | /* This branch of the code can write up to 56 bits at a time,\r |
42 | 7 bits are lost by being perhaps already in *p and at least\r | |
43 | 1 bit is needed to initialize the bit-stream ahead (i.e. if 7\r | |
44 | bits are in *p and we write 57 bits, then the next write will\r | |
45 | access a byte that was never initialized). */\r | |
dd4f667e LG |
46 | uint8_t* p = &array[*pos >> 3];\r |
47 | uint64_t v = (uint64_t)(*p); /* Zero-extend 8 to 64 bits. */\r | |
48 | BROTLI_LOG(("WriteBits %2d 0x%08x%08x %10d\n", (int)n_bits,\r | |
49 | (uint32_t)(bits >> 32), (uint32_t)(bits & 0xFFFFFFFF),\r | |
50 | (int)*pos));\r | |
51 | BROTLI_DCHECK((bits >> n_bits) == 0);\r | |
52 | BROTLI_DCHECK(n_bits <= 56);\r | |
11b7501a | 53 | v |= bits << (*pos & 7);\r |
dd4f667e | 54 | BROTLI_UNALIGNED_STORE64LE(p, v); /* Set some bits. */\r |
11b7501a SB |
55 | *pos += n_bits;\r |
56 | #else\r | |
dd4f667e LG |
57 | /* implicit & 0xFF is assumed for uint8_t arithmetics */\r |
58 | uint8_t* array_pos = &array[*pos >> 3];\r | |
11b7501a SB |
59 | const size_t bits_reserved_in_first_byte = (*pos & 7);\r |
60 | size_t bits_left_to_write;\r | |
61 | bits <<= bits_reserved_in_first_byte;\r | |
62 | *array_pos++ |= (uint8_t)bits;\r | |
63 | for (bits_left_to_write = n_bits + bits_reserved_in_first_byte;\r | |
64 | bits_left_to_write >= 9;\r | |
65 | bits_left_to_write -= 8) {\r | |
66 | bits >>= 8;\r | |
67 | *array_pos++ = (uint8_t)bits;\r | |
68 | }\r | |
69 | *array_pos = 0;\r | |
70 | *pos += n_bits;\r | |
71 | #endif\r | |
72 | }\r | |
73 | \r | |
74 | static BROTLI_INLINE void BrotliWriteBitsPrepareStorage(\r | |
dd4f667e LG |
75 | size_t pos, uint8_t* array) {\r |
76 | BROTLI_LOG(("WriteBitsPrepareStorage %10d\n", (int)pos));\r | |
77 | BROTLI_DCHECK((pos & 7) == 0);\r | |
11b7501a SB |
78 | array[pos >> 3] = 0;\r |
79 | }\r | |
80 | \r | |
81 | #if defined(__cplusplus) || defined(c_plusplus)\r | |
82 | } /* extern "C" */\r | |
83 | #endif\r | |
84 | \r | |
85 | #endif /* BROTLI_ENC_WRITE_BITS_H_ */\r |