| 26 | namespace bit_util { |
| 27 | |
| 28 | void SetBitsTo(uint8_t* bits, int64_t start_offset, int64_t length, bool bits_are_set) { |
| 29 | if (length == 0) { |
| 30 | return; |
| 31 | } |
| 32 | |
| 33 | const int64_t i_begin = start_offset; |
| 34 | const int64_t i_end = start_offset + length; |
| 35 | const uint8_t fill_byte = static_cast<uint8_t>(-static_cast<uint8_t>(bits_are_set)); |
| 36 | |
| 37 | const int64_t bytes_begin = i_begin / 8; |
| 38 | const int64_t bytes_end = i_end / 8 + 1; |
| 39 | |
| 40 | const uint8_t first_byte_mask = kPrecedingBitmask[i_begin % 8]; |
| 41 | const uint8_t last_byte_mask = kTrailingBitmask[i_end % 8]; |
| 42 | |
| 43 | if (bytes_end == bytes_begin + 1) { |
| 44 | // set bits within a single byte |
| 45 | const uint8_t only_byte_mask = |
| 46 | i_end % 8 == 0 ? first_byte_mask |
| 47 | : static_cast<uint8_t>(first_byte_mask | last_byte_mask); |
| 48 | bits[bytes_begin] &= only_byte_mask; |
| 49 | bits[bytes_begin] |= static_cast<uint8_t>(fill_byte & ~only_byte_mask); |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | // set/clear trailing bits of first byte |
| 54 | bits[bytes_begin] &= first_byte_mask; |
| 55 | bits[bytes_begin] |= static_cast<uint8_t>(fill_byte & ~first_byte_mask); |
| 56 | |
| 57 | if (bytes_end - bytes_begin > 2) { |
| 58 | // set/clear whole bytes |
| 59 | std::memset(bits + bytes_begin + 1, fill_byte, |
| 60 | static_cast<size_t>(bytes_end - bytes_begin - 2)); |
| 61 | } |
| 62 | |
| 63 | if (i_end % 8 == 0) { |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | // set/clear leading bits of last byte |
| 68 | bits[bytes_end - 1] &= last_byte_mask; |
| 69 | bits[bytes_end - 1] |= static_cast<uint8_t>(fill_byte & ~last_byte_mask); |
| 70 | } |
| 71 | |
| 72 | template <bool value> |
| 73 | void SetBitmapImpl(uint8_t* data, int64_t offset, int64_t length) { |
nothing calls this directly
no outgoing calls
no test coverage detected