MCPcopy Create free account
hub / github.com/cwida/ALP / InnerRead

Method InnerRead

publication/source_code/include/chimp/bit_reader.hpp:83–108  ·  view source on GitHub ↗

index: 4 size: 7 input: [12345678][12345678] result: [-AAAA BBB] Result contains 4 bits from the first byte (making up the most significant bits) And 3 bits from the second byte (the least significant bits)

Source from the content-addressed store, hash-verified

81 //! Result contains 4 bits from the first byte (making up the most significant bits)
82 //! And 3 bits from the second byte (the least significant bits)
83 inline uint8_t InnerRead(const uint8_t& size, const uint8_t& offset) {
84 const uint8_t right_shift = 8 - size;
85 const uint8_t bit_remainder = (8 - ((size + BitIndex()) - 8)) & 7;
86 // The least significant bits are positioned at the far right of the byte
87
88 // Create a mask given the size and index
89 // Take the first byte
90 // Left-shift it by index, to line up the bits we're interested in with the mask
91 // Get the mask for the given size
92 // Bit-wise AND the byte and the mask together
93 // Right-shift this result (the most significant bits)
94
95 // Sometimes we will need to read from the second byte
96 // But to make this branchless, we will perform what is basically a no-op if this condition is not true
97 // SPILL = (index + size >= 8)
98 //
99 // If SPILL is true:
100 // The REMAINDER_MASKS gives us the mask for the bits we're interested in
101 // We bit-wise AND these together (no need to shift anything because the index is essentially zero for this new
102 // byte) And we then right-shift these bits in place (to the right of the previous bits)
103 const bool spill_to_next_byte = (size + BitIndex() >= 8);
104 uint8_t result =
105 ((input[ByteIndex() + offset] << BitIndex()) & MASKS[size]) >> right_shift |
106 ((input[ByteIndex() + offset + spill_to_next_byte] & REMAINDER_MASKS[size + BitIndex()]) >> bit_remainder);
107 return result;
108 }
109
110 template <class T, uint8_t BYTES>
111 inline T ReadBytes(const uint8_t& remainder) {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected