2015-11-27 10:27:11 +00:00
|
|
|
/* Copyright 2010 Google Inc. All Rights Reserved.
|
|
|
|
|
2015-12-11 10:11:51 +00:00
|
|
|
Distributed under MIT license.
|
2015-11-27 10:27:11 +00:00
|
|
|
See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
|
|
|
|
*/
|
|
|
|
|
2016-06-03 09:19:23 +00:00
|
|
|
/* Write bits into a byte array. */
|
2013-10-23 11:06:13 +00:00
|
|
|
|
|
|
|
#ifndef BROTLI_ENC_WRITE_BITS_H_
|
|
|
|
#define BROTLI_ENC_WRITE_BITS_H_
|
|
|
|
|
2017-12-12 13:33:12 +00:00
|
|
|
#include "../common/platform.h"
|
2016-08-23 12:40:33 +00:00
|
|
|
#include <brotli/types.h>
|
2013-10-23 11:06:13 +00:00
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
2013-10-23 11:06:13 +00:00
|
|
|
|
2016-06-03 09:19:23 +00:00
|
|
|
/*#define BIT_WRITER_DEBUG */
|
|
|
|
|
|
|
|
/* This function writes bits into bytes in increasing addresses, and within
|
|
|
|
a byte least-significant-bit first.
|
|
|
|
|
|
|
|
The function can write up to 56 bits in one go with WriteBits
|
|
|
|
Example: let's assume that 3 bits (Rs below) have been written already:
|
|
|
|
|
|
|
|
BYTE-0 BYTE+1 BYTE+2
|
|
|
|
|
|
|
|
0000 0RRR 0000 0000 0000 0000
|
|
|
|
|
|
|
|
Now, we could write 5 or less bits in MSB by just sifting by 3
|
|
|
|
and OR'ing to BYTE-0.
|
|
|
|
|
|
|
|
For n bits, we take the last 5 bits, OR that with high bits in BYTE-0,
|
|
|
|
and locate the rest in BYTE+1, BYTE+2, etc. */
|
2016-06-13 09:01:04 +00:00
|
|
|
static BROTLI_INLINE void BrotliWriteBits(size_t n_bits,
|
|
|
|
uint64_t bits,
|
2018-02-26 14:04:36 +00:00
|
|
|
size_t* BROTLI_RESTRICT pos,
|
|
|
|
uint8_t* BROTLI_RESTRICT array) {
|
2017-08-04 08:02:56 +00:00
|
|
|
#ifdef BROTLI_LITTLE_ENDIAN
|
2016-06-03 09:19:23 +00:00
|
|
|
/* This branch of the code can write up to 56 bits at a time,
|
|
|
|
7 bits are lost by being perhaps already in *p and at least
|
|
|
|
1 bit is needed to initialize the bit-stream ahead (i.e. if 7
|
|
|
|
bits are in *p and we write 57 bits, then the next write will
|
|
|
|
access a byte that was never initialized). */
|
2018-02-26 14:04:36 +00:00
|
|
|
uint8_t* p = &array[*pos >> 3];
|
2013-10-23 11:06:13 +00:00
|
|
|
uint64_t v = *p;
|
2018-02-26 14:04:36 +00:00
|
|
|
BROTLI_LOG(("WriteBits %2d 0x%08x%08x %10d\n", (int)n_bits,
|
|
|
|
(uint32_t)(bits >> 32), (uint32_t)(bits & 0xFFFFFFFF),
|
|
|
|
(int)*pos));
|
2017-12-12 13:33:12 +00:00
|
|
|
BROTLI_DCHECK((bits >> n_bits) == 0);
|
|
|
|
BROTLI_DCHECK(n_bits <= 56);
|
2013-10-23 11:06:13 +00:00
|
|
|
v |= bits << (*pos & 7);
|
2017-08-04 08:02:56 +00:00
|
|
|
BROTLI_UNALIGNED_STORE64LE(p, v); /* Set some bits. */
|
2013-10-23 11:06:13 +00:00
|
|
|
*pos += n_bits;
|
|
|
|
#else
|
2018-02-26 14:04:36 +00:00
|
|
|
/* implicit & 0xFF is assumed for uint8_t arithmetics */
|
|
|
|
uint8_t* array_pos = &array[*pos >> 3];
|
2016-01-07 15:27:49 +00:00
|
|
|
const size_t bits_reserved_in_first_byte = (*pos & 7);
|
2016-06-14 11:54:11 +00:00
|
|
|
size_t bits_left_to_write;
|
2013-10-23 11:06:13 +00:00
|
|
|
bits <<= bits_reserved_in_first_byte;
|
2016-06-13 09:01:04 +00:00
|
|
|
*array_pos++ |= (uint8_t)bits;
|
2016-06-14 11:54:11 +00:00
|
|
|
for (bits_left_to_write = n_bits + bits_reserved_in_first_byte;
|
2016-01-07 15:27:49 +00:00
|
|
|
bits_left_to_write >= 9;
|
2013-10-23 11:06:13 +00:00
|
|
|
bits_left_to_write -= 8) {
|
|
|
|
bits >>= 8;
|
2016-06-13 09:01:04 +00:00
|
|
|
*array_pos++ = (uint8_t)bits;
|
2013-10-23 11:06:13 +00:00
|
|
|
}
|
|
|
|
*array_pos = 0;
|
|
|
|
*pos += n_bits;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
static BROTLI_INLINE void BrotliWriteBitsPrepareStorage(
|
2018-02-26 14:04:36 +00:00
|
|
|
size_t pos, uint8_t* array) {
|
|
|
|
BROTLI_LOG(("WriteBitsPrepareStorage %10d\n", (int)pos));
|
2017-12-12 13:33:12 +00:00
|
|
|
BROTLI_DCHECK((pos & 7) == 0);
|
2013-10-23 11:06:13 +00:00
|
|
|
array[pos >> 3] = 0;
|
|
|
|
}
|
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
} /* extern "C" */
|
|
|
|
#endif
|
2013-10-23 11:06:13 +00:00
|
|
|
|
2016-06-03 09:19:23 +00:00
|
|
|
#endif /* BROTLI_ENC_WRITE_BITS_H_ */
|