| 26 | namespace impala { |
| 27 | |
| 28 | inline bool BitWriter::PutValue(uint64_t v, int num_bits) { |
| 29 | DCHECK_LE(num_bits, MAX_BITWIDTH); |
| 30 | DCHECK(num_bits == MAX_BITWIDTH || v >> num_bits == 0) |
| 31 | << "v = " << v << ", num_bits = " << num_bits; |
| 32 | |
| 33 | if (UNLIKELY(byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8)) return false; |
| 34 | |
| 35 | buffered_values_ |= v << bit_offset_; |
| 36 | bit_offset_ += num_bits; |
| 37 | |
| 38 | if (UNLIKELY(bit_offset_ >= 64)) { |
| 39 | // Flush buffered_values_ and write out bits of v that did not fit |
| 40 | memcpy(buffer_ + byte_offset_, &buffered_values_, 8); |
| 41 | byte_offset_ += 8; |
| 42 | bit_offset_ -= 64; |
| 43 | |
| 44 | // Shifting with the same or greater amount than the number of bits in the number is |
| 45 | // undefined behaviour. |
| 46 | int shift = num_bits - bit_offset_; |
| 47 | |
| 48 | if (LIKELY(shift < 64)) { |
| 49 | buffered_values_ = v >> shift; |
| 50 | } else { |
| 51 | buffered_values_ = 0; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | DCHECK_LT(bit_offset_, 64); |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | inline void BitWriter::Flush(bool align) { |
| 60 | int num_bytes = BitUtil::Ceil(bit_offset_, 8); |
no outgoing calls