Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions cpp/src/arrow/util/bit-util-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,10 @@ struct BitmapOperation {
const uint8_t* right, int64_t right_offset, int64_t length,
int64_t out_offset, std::shared_ptr<Buffer>* out_buffer) const = 0;

virtual Status Call(const uint8_t* left, int64_t left_offset, const uint8_t* right,
int64_t right_offset, int64_t length, int64_t out_offset,
uint8_t* out_buffer) const = 0;

virtual ~BitmapOperation() = default;
};

Expand All @@ -389,6 +393,13 @@ struct BitmapAndOp : public BitmapOperation {
return BitmapAnd(pool, left, left_offset, right, right_offset, length, out_offset,
out_buffer);
}

Status Call(const uint8_t* left, int64_t left_offset, const uint8_t* right,
int64_t right_offset, int64_t length, int64_t out_offset,
uint8_t* out_buffer) const override {
BitmapAnd(left, left_offset, right, right_offset, length, out_offset, out_buffer);
return Status::OK();
}
};

struct BitmapOrOp : public BitmapOperation {
Expand All @@ -398,6 +409,13 @@ struct BitmapOrOp : public BitmapOperation {
return BitmapOr(pool, left, left_offset, right, right_offset, length, out_offset,
out_buffer);
}

Status Call(const uint8_t* left, int64_t left_offset, const uint8_t* right,
int64_t right_offset, int64_t length, int64_t out_offset,
uint8_t* out_buffer) const override {
BitmapOr(left, left_offset, right, right_offset, length, out_offset, out_buffer);
return Status::OK();
}
};

struct BitmapXorOp : public BitmapOperation {
Expand All @@ -407,6 +425,13 @@ struct BitmapXorOp : public BitmapOperation {
return BitmapXor(pool, left, left_offset, right, right_offset, length, out_offset,
out_buffer);
}

Status Call(const uint8_t* left, int64_t left_offset, const uint8_t* right,
int64_t right_offset, int64_t length, int64_t out_offset,
uint8_t* out_buffer) const override {
BitmapXor(left, left_offset, right, right_offset, length, out_offset, out_buffer);
return Status::OK();
}
};

class BitmapOp : public TestBase {
Expand All @@ -427,6 +452,13 @@ class BitmapOp : public TestBase {
&out));
auto reader = internal::BitmapReader(out->mutable_data(), out_offset, length);
ASSERT_READER_VALUES(reader, result_bits);

// Clear out buffer and try non-allocating version
std::memset(out->mutable_data(), 0, out->size());
ASSERT_OK(op.Call(left->mutable_data(), left_offset, right->mutable_data(),
right_offset, length, out_offset, out->mutable_data()));
reader = internal::BitmapReader(out->mutable_data(), out_offset, length);
ASSERT_READER_VALUES(reader, result_bits);
}
}
}
Expand All @@ -451,6 +483,13 @@ class BitmapOp : public TestBase {
&out));
auto reader = internal::BitmapReader(out->mutable_data(), out_offset, length);
ASSERT_READER_VALUES(reader, result_bits);

// Clear out buffer and try non-allocating version
std::memset(out->mutable_data(), 0, out->size());
ASSERT_OK(op.Call(left->mutable_data(), left_offset, right->mutable_data(),
right_offset, length, out_offset, out->mutable_data()));
reader = internal::BitmapReader(out->mutable_data(), out_offset, length);
ASSERT_READER_VALUES(reader, result_bits);
}
}
}
Expand Down
45 changes: 35 additions & 10 deletions cpp/src/arrow/util/bit-util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -296,21 +296,28 @@ void UnalignedBitmapOp(const uint8_t* left, int64_t left_offset, const uint8_t*
}

template <typename BitOp, typename LogicalOp>
Status BitmapOp(MemoryPool* pool, const uint8_t* left, int64_t left_offset,
const uint8_t* right, int64_t right_offset, int64_t length,
int64_t out_offset, std::shared_ptr<Buffer>* out_buffer) {
void BitmapOp(const uint8_t* left, int64_t left_offset, const uint8_t* right,
int64_t right_offset, int64_t length, int64_t out_offset, uint8_t* dest) {
if ((out_offset % 8 == left_offset % 8) && (out_offset % 8 == right_offset % 8)) {
// Fast case: can use bytewise AND
const int64_t phys_bits = length + out_offset;
RETURN_NOT_OK(AllocateEmptyBitmap(pool, phys_bits, out_buffer));
AlignedBitmapOp<BitOp>(left, left_offset, right, right_offset,
(*out_buffer)->mutable_data(), out_offset, length);
AlignedBitmapOp<BitOp>(left, left_offset, right, right_offset, dest, out_offset,
length);
} else {
// Unaligned
RETURN_NOT_OK(AllocateEmptyBitmap(pool, length + out_offset, out_buffer));
UnalignedBitmapOp<LogicalOp>(left, left_offset, right, right_offset,
(*out_buffer)->mutable_data(), out_offset, length);
UnalignedBitmapOp<LogicalOp>(left, left_offset, right, right_offset, dest, out_offset,
length);
}
}

template <typename BitOp, typename LogicalOp>
Status BitmapOp(MemoryPool* pool, const uint8_t* left, int64_t left_offset,
const uint8_t* right, int64_t right_offset, int64_t length,
int64_t out_offset, std::shared_ptr<Buffer>* out_buffer) {
const int64_t phys_bits = length + out_offset;
RETURN_NOT_OK(AllocateEmptyBitmap(pool, phys_bits, out_buffer));
uint8_t* out = (*out_buffer)->mutable_data();
BitmapOp<BitOp, LogicalOp>(left, left_offset, right, right_offset, length, out_offset,
out);
return Status::OK();
}

Expand All @@ -323,19 +330,37 @@ Status BitmapAnd(MemoryPool* pool, const uint8_t* left, int64_t left_offset,
pool, left, left_offset, right, right_offset, length, out_offset, out_buffer);
}

void BitmapAnd(const uint8_t* left, int64_t left_offset, const uint8_t* right,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lots of duplication (2 functions for each operation), bonus point for a macro:

#define BITMAP_BINARY_IMPL ...

BITMAP_BINARY_IMPL(And, std::bit_and<uint8_t>, std::logical_and<bool>);
BITMAP_BINARY_IMPL(Or, std::bit_or<uint8_t>, std::logical_or<bool>);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't mind I'm going to leave as is MACROs are discouraged in the style guide and I'm on the fence if this is enough boiler to justify using them.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

int64_t right_offset, int64_t length, int64_t out_offset, uint8_t* out) {
BitmapOp<std::bit_and<uint8_t>, std::logical_and<bool>>(
left, left_offset, right, right_offset, length, out_offset, out);
}

Status BitmapOr(MemoryPool* pool, const uint8_t* left, int64_t left_offset,
const uint8_t* right, int64_t right_offset, int64_t length,
int64_t out_offset, std::shared_ptr<Buffer>* out_buffer) {
return BitmapOp<std::bit_or<uint8_t>, std::logical_or<bool>>(
pool, left, left_offset, right, right_offset, length, out_offset, out_buffer);
}

void BitmapOr(const uint8_t* left, int64_t left_offset, const uint8_t* right,
int64_t right_offset, int64_t length, int64_t out_offset, uint8_t* out) {
BitmapOp<std::bit_or<uint8_t>, std::logical_or<bool>>(
left, left_offset, right, right_offset, length, out_offset, out);
}

Status BitmapXor(MemoryPool* pool, const uint8_t* left, int64_t left_offset,
const uint8_t* right, int64_t right_offset, int64_t length,
int64_t out_offset, std::shared_ptr<Buffer>* out_buffer) {
return BitmapOp<std::bit_xor<uint8_t>, std::bit_xor<bool>>(
pool, left, left_offset, right, right_offset, length, out_offset, out_buffer);
}

void BitmapXor(const uint8_t* left, int64_t left_offset, const uint8_t* right,
int64_t right_offset, int64_t length, int64_t out_offset, uint8_t* out) {
BitmapOp<std::bit_xor<uint8_t>, std::bit_xor<bool>>(
left, left_offset, right, right_offset, length, out_offset, out);
}

} // namespace internal
} // namespace arrow
39 changes: 39 additions & 0 deletions cpp/src/arrow/util/bit-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -738,21 +738,60 @@ ARROW_EXPORT
bool BitmapEquals(const uint8_t* left, int64_t left_offset, const uint8_t* right,
int64_t right_offset, int64_t bit_length);

/// \brief Do a "bitmap and" on right and left buffers starting at
/// their respective bit-offsets for the given bit-length and put
/// the results in out_buffer starting at the given bit-offset.
///
/// out_buffer will be allocated and initialized to zeros using pool before
/// the operation.
ARROW_EXPORT
Status BitmapAnd(MemoryPool* pool, const uint8_t* left, int64_t left_offset,
const uint8_t* right, int64_t right_offset, int64_t length,
int64_t out_offset, std::shared_ptr<Buffer>* out_buffer);

/// \brief Do a "bitmap and" on right and left buffers starting at
/// their respective bit-offsets for the given bit-length and put
/// the results in out starting at the given bit-offset.
ARROW_EXPORT
void BitmapAnd(const uint8_t* left, int64_t left_offset, const uint8_t* right,
int64_t right_offset, int64_t length, int64_t out_offset, uint8_t* out);

/// \brief Do a "bitmap or" for the given bit length on right and left buffers
/// starting at their respective bit-offsets and put the results in out_buffer
/// starting at the given bit-offset.
///
/// out_buffer will be allocated and initialized to zeros using pool before
/// the operation.
ARROW_EXPORT
Status BitmapOr(MemoryPool* pool, const uint8_t* left, int64_t left_offset,
const uint8_t* right, int64_t right_offset, int64_t length,
int64_t out_offset, std::shared_ptr<Buffer>* out_buffer);

/// \brief Do a "bitmap or" for the given bit length on right and left buffers
/// starting at their respective bit-offsets and put the results in out
/// starting at the given bit-offset.
ARROW_EXPORT
void BitmapOr(const uint8_t* left, int64_t left_offset, const uint8_t* right,
int64_t right_offset, int64_t length, int64_t out_offset, uint8_t* out);

/// \brief Do a "bitmap xor" for the given bit-length on right and left
/// buffers starting at their respective bit-offsets and put the results in
/// out_buffer starting at the given bit offset.
///
/// out_buffer will be allocated and initialized to zeros using pool before
/// the operation.
ARROW_EXPORT
Status BitmapXor(MemoryPool* pool, const uint8_t* left, int64_t left_offset,
const uint8_t* right, int64_t right_offset, int64_t length,
int64_t out_offset, std::shared_ptr<Buffer>* out_buffer);

/// \brief Do a "bitmap xor" for the given bit-length on right and left
/// buffers starting at their respective bit-offsets and put the results in
/// out starting at the given bit offset.
ARROW_EXPORT
void BitmapXor(const uint8_t* left, int64_t left_offset, const uint8_t* right,
int64_t right_offset, int64_t length, int64_t out_offset, uint8_t* out);

} // namespace internal
} // namespace arrow

Expand Down