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