write_bits.h 2.8 KB

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