write_bits.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 <assert.h>
  9. #include <stdio.h> /* printf */
  10. #include "../common/types.h"
  11. #include "./port.h"
  12. #if defined(__cplusplus) || defined(c_plusplus)
  13. extern "C" {
  14. #endif
  15. /*#define BIT_WRITER_DEBUG */
  16. /* This function writes bits into bytes in increasing addresses, and within
  17. a byte least-significant-bit first.
  18. The function can write up to 56 bits in one go with WriteBits
  19. Example: let's assume that 3 bits (Rs below) have been written already:
  20. BYTE-0 BYTE+1 BYTE+2
  21. 0000 0RRR 0000 0000 0000 0000
  22. Now, we could write 5 or less bits in MSB by just sifting by 3
  23. and OR'ing to BYTE-0.
  24. For n bits, we take the last 5 bits, OR that with high bits in BYTE-0,
  25. and locate the rest in BYTE+1, BYTE+2, etc. */
  26. static BROTLI_INLINE void BrotliWriteBits(size_t n_bits,
  27. uint64_t bits,
  28. size_t * BROTLI_RESTRICT pos,
  29. uint8_t * BROTLI_RESTRICT array) {
  30. #ifdef IS_LITTLE_ENDIAN
  31. /* This branch of the code can write up to 56 bits at a time,
  32. 7 bits are lost by being perhaps already in *p and at least
  33. 1 bit is needed to initialize the bit-stream ahead (i.e. if 7
  34. bits are in *p and we write 57 bits, then the next write will
  35. access a byte that was never initialized). */
  36. uint8_t *p = &array[*pos >> 3];
  37. uint64_t v = *p;
  38. #ifdef BIT_WRITER_DEBUG
  39. printf("WriteBits %2d 0x%016llx %10d\n", n_bits, bits, *pos);
  40. #endif
  41. assert((bits >> n_bits) == 0);
  42. assert(n_bits <= 56);
  43. v |= bits << (*pos & 7);
  44. BROTLI_UNALIGNED_STORE64(p, v); /* Set some bits. */
  45. *pos += n_bits;
  46. #else
  47. /* implicit & 0xff is assumed for uint8_t arithmetics */
  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. #endif
  62. }
  63. static BROTLI_INLINE void BrotliWriteBitsPrepareStorage(
  64. size_t pos, uint8_t *array) {
  65. #ifdef BIT_WRITER_DEBUG
  66. printf("WriteBitsPrepareStorage %10d\n", pos);
  67. #endif
  68. assert((pos & 7) == 0);
  69. array[pos >> 3] = 0;
  70. }
  71. #if defined(__cplusplus) || defined(c_plusplus)
  72. } /* extern "C" */
  73. #endif
  74. #endif /* BROTLI_ENC_WRITE_BITS_H_ */