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