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