write_bits.h 2.8 KB

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