AntoinePrv commented on code in PR #50217:
URL: https://github.com/apache/arrow/pull/50217#discussion_r3434826842
##########
cpp/src/arrow/util/bit_util.h:
##########
@@ -176,6 +179,54 @@ static constexpr bool GetBitFromByte(uint8_t byte, uint8_t
i) {
return byte & kBitmask[i];
}
+/// Read 32 bits starting at bit `offset` into a uint32.
+///
+/// The return value in in-memory byte layout matches the source bit-stream
+/// identically on little- and big-endian platforms.
+///
+/// The caller must guarantee that many bytes are readable.
+static inline uint32_t Get32Bits(const uint8_t* bits, uint64_t offset) {
+ uint64_t buffer = {};
+ std::memcpy(&buffer, bits + (offset / 8), BytesForBits(offset % 8 + 32));
+ // Interpret the loaded bytes as little-endian, drop the low `offset % 8`
bits
+ // to align LSB-first, then store back in little-endian byte order so the
+ // result's memory representation matches the bit-stream.
+ buffer = FromLittleEndian(buffer);
+ return ToLittleEndian(static_cast<uint32_t>(buffer >> (offset % 8)));
+}
+
+template <typename Uint>
+struct CopyBitsParams {
+ Uint src = {};
+ Uint dst = {};
+ Uint start = {};
+ Uint end = {};
+};
+
+/// Copy a contiguous span of bits from src into dst.
+///
+/// Copy bits [start, end[ from src into the position [start, end[ in dst
+/// and return the result (inputs are unmodified).
+/// Setting ``kAllowFullCopy`` to false is an optimization when the caller can
+/// guarantee that the range of bits to copy does not cover the whole range.
+template <typename Uint, bool kAllowFullCopy = true>
+[[nodiscard]] constexpr Uint CopyBits(const CopyBitsParams<Uint>& params) {
+ constexpr auto kUintSizeBits = static_cast<Uint>(sizeof(Uint) * 8);
+ assert(params.start <= params.end);
+ assert(params.start < kUintSizeBits);
+ assert(params.end <= kUintSizeBits);
+
+ const auto length = static_cast<Uint>(params.end - params.start);
+ if constexpr (kAllowFullCopy) {
+ if (length == kUintSizeBits) {
+ return params.src;
+ }
+ }
+ assert(length < kUintSizeBits);
+ const Uint mask = LeastSignificantBitMask<Uint, false>(length) <<
params.start;
+ return (~mask & params.dst) | (mask & params.src);
+}
Review Comment:
We explicitly do not want to handle this for performance reasons.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]