File tree Expand file tree Collapse file tree 1 file changed +6
-2
lines changed
tensorflow_compression/cc/lib Expand file tree Collapse file tree 1 file changed +6
-2
lines changed Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ limitations under the License.
18
18
#include < cassert>
19
19
#include < cstddef>
20
20
#include < cstdint>
21
+ #include < limits>
21
22
22
23
#include " absl/base/internal/endian.h"
23
24
#include " absl/status/status.h"
@@ -26,13 +27,16 @@ namespace tensorflow_compression {
26
27
27
28
BitWriter::BitWriter (size_t maximum_bit_size)
28
29
// We write 8 bytes at a time, so we might over-write by 8 bytes.
29
- : data_(std::make_unique<char []>(maximum_bit_size / 8 + 8 )),
30
+ // +1 byte since we need to round bits *up* to bytes.
31
+ : data_(std::make_unique<char []>(maximum_bit_size / 8 + 9 )),
30
32
next_byte_ (data_.get()),
31
33
bits_in_buffer_(0 ),
32
34
buffer_(0 ) {}
33
35
34
36
void BitWriter::WriteBits (uint32_t count, uint64_t bits) {
35
- assert (count <= kMaxBitsPerCall ); // Max remaining room in buffer.
37
+ assert (count <= kMaxBitsPerCall );
38
+ // This implementation relies on the unused MSBs in `bits` to be zero.
39
+ assert (!(bits & (std::numeric_limits<uint64_t >::max () << count)));
36
40
buffer_ |= bits << bits_in_buffer_;
37
41
bits_in_buffer_ += count;
38
42
absl::little_endian::Store64 (next_byte_, buffer_);
You can’t perform that action at this time.
0 commit comments