Skip to content

Commit 9ace2e6

Browse files
committed
Merge mask functions
1 parent 95271fe commit 9ace2e6

File tree

6 files changed

+16
-18
lines changed

6 files changed

+16
-18
lines changed

cpp/src/arrow/util/bit_run_reader.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ BitRunReader::BitRunReader(const uint8_t* bitmap, int64_t start_offset, int64_t
4545

4646
// Prepare for inversion in NextRun.
4747
// Clear out any preceding bits.
48-
word_ = word_ & ~bit_util::LeastSignificantBitMask(position_);
48+
word_ = word_ & ~bit_util::LeastSignificantBitMask<uint64_t>(position_);
4949
}
5050

5151
#endif

cpp/src/arrow/util/bit_run_reader.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class ARROW_EXPORT BitRunReader {
106106
int64_t start_bit_offset = start_position & 63;
107107
// Invert the word for proper use of CountTrailingZeros and
108108
// clear bits so CountTrailingZeros can do it magic.
109-
word_ = ~word_ & ~bit_util::LeastSignificantBitMask(start_bit_offset);
109+
word_ = ~word_ & ~bit_util::LeastSignificantBitMask<uint64_t>(start_bit_offset);
110110

111111
// Go forward until the next change from unset to set.
112112
int64_t new_bits = bit_util::CountTrailingZeros(word_) - start_bit_offset;
@@ -311,12 +311,12 @@ class BaseSetBitRunReader {
311311
memcpy(reinterpret_cast<char*>(&word) + 8 - num_bytes, bitmap_, num_bytes);
312312
// XXX MostSignificantBitmask
313313
return (bit_util::ToLittleEndian(word) << bit_offset) &
314-
~bit_util::LeastSignificantBitMask(64 - num_bits);
314+
~bit_util::LeastSignificantBitMask<uint64_t>(64 - num_bits);
315315
} else {
316316
memcpy(&word, bitmap_, num_bytes);
317317
bitmap_ += num_bytes;
318318
return (bit_util::ToLittleEndian(word) >> bit_offset) &
319-
bit_util::LeastSignificantBitMask(num_bits);
319+
bit_util::LeastSignificantBitMask<uint64_t>(num_bits);
320320
}
321321
}
322322

cpp/src/arrow/util/bit_util.h

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,14 @@ constexpr bool IsMultipleOf64(int64_t n) { return (n & 63) == 0; }
113113
constexpr bool IsMultipleOf8(int64_t n) { return (n & 7) == 0; }
114114

115115
// Returns a mask for the bit_index lower order bits.
116-
// Only valid for bit_index in the range [0, 64).
117-
constexpr uint64_t LeastSignificantBitMask(int64_t bit_index) {
118-
return (static_cast<uint64_t>(1) << bit_index) - 1;
119-
}
120-
121-
// Returns a mask for the bit_index lower order bits.
122-
// Only valid for bit_index in the range [0, sizeof(Uint)].
123-
template <typename Uint>
124-
constexpr auto LeastSignificantBitMaskInc(Uint bit_index) {
125-
if (bit_index == 8 * sizeof(Uint)) {
126-
return ~Uint{0};
116+
// Valid in the range `[0, 8*sizof(Uint)]` if `kAllowUpperBound`
117+
// otherwise `[0, 8*sizof(Uint)[`
118+
template <typename Uint, bool kAllowUpperBound = false>
119+
constexpr auto LeastSignificantBitMask(Uint bit_index) {
120+
if constexpr (kAllowUpperBound) {
121+
if (bit_index == 8 * sizeof(Uint)) {
122+
return ~Uint{0};
123+
}
127124
}
128125
return (Uint{1} << bit_index) - Uint{1};
129126
}

cpp/src/arrow/util/bitmap_reader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ class BitmapUInt64Reader {
136136
memcpy(&word, bitmap_, num_bytes);
137137
bitmap_ += num_bytes;
138138
return (bit_util::ToLittleEndian(word) >> bit_offset) &
139-
bit_util::LeastSignificantBitMask(num_bits);
139+
bit_util::LeastSignificantBitMask<uint64_t>(num_bits);
140140
}
141141

142142
const uint8_t* bitmap_;

cpp/src/arrow/util/bpacking_dispatch_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ int unpack_exact(const uint8_t* in, Uint* out, int batch_size, int bit_offset) {
109109
// aligned bytes.
110110
constexpr bool kOversized = kBufferSize < kMaxSpreadBytes;
111111
constexpr buffer_uint kLowMask =
112-
bit_util::LeastSignificantBitMaskInc<buffer_uint>(kPackedBitWidth);
112+
bit_util::LeastSignificantBitMask<buffer_uint, true>(kPackedBitWidth);
113113

114114
ARROW_DCHECK_GE(bit_offset, 0);
115115
ARROW_DCHECK_LE(bit_offset, 8);

cpp/src/arrow/util/decimal.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,8 @@ static void AppendLittleEndianArrayToString(const std::array<uint64_t, n>& array
610610
// *elem = dividend / 1e9;
611611
// remainder = dividend % 1e9.
612612
uint32_t hi = static_cast<uint32_t>(*elem >> 32);
613-
uint32_t lo = static_cast<uint32_t>(*elem & bit_util::LeastSignificantBitMask(32));
613+
uint32_t lo =
614+
static_cast<uint32_t>(*elem & bit_util::LeastSignificantBitMask<uint64_t>(32));
614615
uint64_t dividend_hi = (static_cast<uint64_t>(remainder) << 32) | hi;
615616
uint64_t quotient_hi = dividend_hi / k1e9;
616617
remainder = static_cast<uint32_t>(dividend_hi % k1e9);

0 commit comments

Comments
 (0)