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