-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GH-41687: [C++] bit_util: Trying to remove pre-compute table #41690
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -281,10 +281,22 @@ static inline int Log2(uint64_t x) { | |
// | ||
|
||
// Bitmask selecting the k-th bit in a byte | ||
static constexpr uint8_t kBitmask[] = {1, 2, 4, 8, 16, 32, 64, 128}; | ||
// static constexpr uint8_t kBitmask[] = {1, 2, 4, 8, 16, 32, 64, 128}; | ||
template <typename T> | ||
static constexpr uint8_t GetBitMask(T index) { | ||
// DCHECK(index >= 0 && index <= 7); | ||
ARROW_COMPILER_ASSUME(index >= 0 && index <= 7); | ||
return static_cast<uint8_t>(1) << index; | ||
} | ||
|
||
// the bitwise complement version of kBitmask | ||
static constexpr uint8_t kFlippedBitmask[] = {254, 253, 251, 247, 239, 223, 191, 127}; | ||
// static constexpr uint8_t kFlippedBitmask[] = {254, 253, 251, 247, 239, 223, 191, 127}; | ||
template <typename T> | ||
static constexpr uint8_t GetFlippedBitMask(T index) { | ||
// DCHECK(index >= 0 && index <= 7); | ||
ARROW_COMPILER_ASSUME(index >= 0 && index <= 7); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Putting the assumption here doesn't have the same effect. Because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not fully understand this, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or from the godbolt link, do you mean change There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right, tried in godbolt, I'll add this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aha, I use |
||
return ~(static_cast<uint8_t>(1) << index); | ||
} | ||
|
||
// Bitmask selecting the (k - 1) preceding bits in a byte | ||
static constexpr uint8_t kPrecedingBitmask[] = {0, 1, 3, 7, 15, 31, 63, 127}; | ||
|
@@ -299,22 +311,22 @@ static constexpr bool GetBit(const uint8_t* bits, uint64_t i) { | |
|
||
// Gets the i-th bit from a byte. Should only be used with i <= 7. | ||
static constexpr bool GetBitFromByte(uint8_t byte, uint8_t i) { | ||
return byte & kBitmask[i]; | ||
return byte & GetBitMask(i); | ||
} | ||
|
||
static inline void ClearBit(uint8_t* bits, int64_t i) { | ||
bits[i / 8] &= kFlippedBitmask[i % 8]; | ||
bits[i / 8] &= GetFlippedBitMask(i % 8); | ||
} | ||
|
||
static inline void SetBit(uint8_t* bits, int64_t i) { bits[i / 8] |= kBitmask[i % 8]; } | ||
static inline void SetBit(uint8_t* bits, int64_t i) { bits[i / 8] |= GetBitMask(i % 8); } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One advantage that the We can bring that UB back (yes, UB is a Good Thing™️) by using SetBit2(unsigned char*, long): # @SetBit2(unsigned char*, long)
mov rcx, rsi
lea rax, [rsi + 7]
test rsi, rsi
cmovns rax, rsi
mov edx, eax
and edx, 248
sub ecx, edx
mov edx, 1
shl edx, cl
sar rax, 3
or byte ptr [rdi + rax], dl
ret
SetBit2NNeg(unsigned char*, long): # @SetBit2NNeg(unsigned char*, long)
mov ecx, esi
and cl, 7
mov al, 1
shl al, cl
shr rsi, 3
or byte ptr [rdi + rsi], al
ret All the experiments on Godbolt -> https://godbolt.org/z/Ez974vE3d There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is OK because whatever is defined as the expected behavior for negative |
||
|
||
static inline void SetBitTo(uint8_t* bits, int64_t i, bool bit_is_set) { | ||
// https://graphics.stanford.edu/~seander/bithacks.html | ||
// "Conditionally set or clear bits without branching" | ||
// NOTE: this seems to confuse Valgrind as it reads from potentially | ||
// uninitialized memory | ||
bits[i / 8] ^= static_cast<uint8_t>(-static_cast<uint8_t>(bit_is_set) ^ bits[i / 8]) & | ||
kBitmask[i % 8]; | ||
GetBitMask(i % 8); | ||
} | ||
|
||
/// \brief set or clear a range of bits quickly | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The compiler will generate code that performs the
<<
on a 32-bit integer. A more honest implementation (in the sense that it gives more freedom to the compiler [1]):And since indices in arrow are rarely
uint8_t
, I would keep the index type unconstrained like this:[1] might matter more for
rustc
thanclang
because C/C++ compilers already have a lot of freedom even when your code contains many type constraints