MCPcopy Create free account
hub / github.com/CodingGay/BlackDex / BitFieldExtract

Function BitFieldExtract

Bcore/src/main/cpp/base/bit_utils.h:481–500  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

479// where S is the highest bit in 'bitfield'.
480template <typename T>
481inline static constexpr T BitFieldExtract(T value, size_t lsb, size_t width) {
482 DCHECK_GE(BitSizeOf(value), lsb + width) << "Bit field out of range for value";
483 const auto val = static_cast<std::make_unsigned_t<T>>(value);
484
485 const T bitfield_unsigned =
486 static_cast<T>((val >> lsb) & MaskLeastSignificant<T>(width));
487 if (std::is_signed<T>::value) {
488 // Perform sign extension
489 if (width == 0) { // Avoid underflow.
490 return static_cast<T>(0);
491 } else if (bitfield_unsigned & (1 << (width - 1))) { // Detect if sign bit was set.
492 // MSB <width> LSB
493 // 0b11111...100...000000
494 const auto ones_negmask = ~MaskLeastSignificant<T>(width);
495 return static_cast<T>(bitfield_unsigned | ones_negmask);
496 }
497 }
498 // Skip sign extension.
499 return bitfield_unsigned;
500}
501
502} // namespace art_lkchan
503

Callers

nothing calls this directly

Calls 1

BitSizeOfFunction · 0.85

Tested by

no test coverage detected