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
2 changes: 1 addition & 1 deletion cpp/src/arrow/array-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2893,7 +2893,7 @@ TEST_F(TestListArray, TestFromArrays) {
test::AssertArraysEqual(expected3, *list3);

// Check that the last offset bit is zero
ASSERT_TRUE(BitUtil::BitNotSet(list3->null_bitmap()->data(), length + 1));
ASSERT_FALSE(BitUtil::GetBit(list3->null_bitmap()->data(), length + 1));

ListArray expected4(list_type, length, offsets2->data()->buffers[1], values,
offsets4->data()->buffers[0], 1);
Expand Down
2 changes: 1 addition & 1 deletion cpp/src/arrow/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ class ARROW_EXPORT Array {
/// \brief Return true if value at index is null. Does not boundscheck
bool IsNull(int64_t i) const {
return null_bitmap_data_ != NULLPTR &&
BitUtil::BitNotSet(null_bitmap_data_, i + data_->offset);
!BitUtil::GetBit(null_bitmap_data_, i + data_->offset);
}

/// \brief Return true if value at index is valid (not null). Does not
Expand Down
15 changes: 15 additions & 0 deletions cpp/src/arrow/buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,19 @@ Status AllocateResizableBuffer(const int64_t size,
return AllocateResizableBuffer(default_memory_pool(), size, out);
}

Status AllocateEmptyBitmap(MemoryPool* pool, int64_t length,
std::shared_ptr<Buffer>* out) {
RETURN_NOT_OK(AllocateBuffer(pool, BitUtil::BytesForBits(length), out));
memset((*out)->mutable_data(), 0, static_cast<size_t>((*out)->size()));
return Status::OK();
}

Status AllocateEmptyBitmap(int64_t length, std::shared_ptr<Buffer>* out) {
return AllocateEmptyBitmap(default_memory_pool(), length, out);
}

Status GetEmptyBitmap(MemoryPool* pool, int64_t length, std::shared_ptr<Buffer>* out) {
return AllocateEmptyBitmap(pool, length, out);
}

} // namespace arrow
28 changes: 28 additions & 0 deletions cpp/src/arrow/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,34 @@ Status AllocateResizableBuffer(MemoryPool* pool, const int64_t size,
ARROW_EXPORT
Status AllocateResizableBuffer(const int64_t size, std::shared_ptr<ResizableBuffer>* out);

/// \brief Allocate a zero-initialized bitmap buffer from a memory pool
///
/// \param[in] pool memory pool to allocate memory from
/// \param[in] length size in bits of bitmap to allocate
/// \param[out] out the resulting buffer
///
/// \return Status message
ARROW_EXPORT
Status AllocateEmptyBitmap(MemoryPool* pool, int64_t length,
std::shared_ptr<Buffer>* out);

/// \brief Allocate a zero-initialized bitmap buffer from the default memory pool
///
/// \param[in] length size in bits of bitmap to allocate
/// \param[out] out the resulting buffer
///
/// \return Status message
ARROW_EXPORT
Status AllocateEmptyBitmap(int64_t length, std::shared_ptr<Buffer>* out);

/// \deprecated Use AllocateEmptyBitmap instead.
ARROW_DEPRECATED("Use AllocateEmptyBitmap instead")
ARROW_EXPORT
Status GetEmptyBitmap(MemoryPool* pool, int64_t length, std::shared_ptr<Buffer>* out);

// ----------------------------------------------------------------------
// Buffer builder classes

/// \class BufferBuilder
/// \brief A class for incrementally building a contiguous chunk of in-memory data
class ARROW_EXPORT BufferBuilder {
Expand Down
2 changes: 1 addition & 1 deletion cpp/src/arrow/ipc/json-internal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,7 @@ class ArrayReader {
int length = static_cast<int>(is_valid.size());

std::shared_ptr<Buffer> out_buffer;
RETURN_NOT_OK(GetEmptyBitmap(pool_, length, &out_buffer));
RETURN_NOT_OK(AllocateEmptyBitmap(pool_, length, &out_buffer));
uint8_t* bitmap = out_buffer->mutable_data();

*null_count = 0;
Expand Down
2 changes: 1 addition & 1 deletion cpp/src/arrow/test-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ static inline Status GetBitmapFromVector(const std::vector<T>& is_valid,
size_t length = is_valid.size();

std::shared_ptr<Buffer> buffer;
RETURN_NOT_OK(GetEmptyBitmap(default_memory_pool(), length, &buffer));
RETURN_NOT_OK(AllocateEmptyBitmap(length, &buffer));

uint8_t* bitmap = buffer->mutable_data();
for (size_t i = 0; i < static_cast<size_t>(length); ++i) {
Expand Down
9 changes: 5 additions & 4 deletions cpp/src/arrow/util/bit-stream-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class BitWriter {
/// The number of current bytes written, including the current byte (i.e. may include a
/// fraction of a byte). Includes buffered values.
int bytes_written() const {
return byte_offset_ + static_cast<int>(BitUtil::Ceil(bit_offset_, 8));
return byte_offset_ + static_cast<int>(BitUtil::BytesForBits(bit_offset_));
}
uint8_t* buffer() const { return buffer_; }
int buffer_len() const { return max_bytes_; }
Expand Down Expand Up @@ -148,7 +148,8 @@ class BitReader {
/// Returns the number of bytes left in the stream, not including the current
/// byte (i.e., there may be an additional fraction of a byte).
int bytes_left() {
return max_bytes_ - (byte_offset_ + static_cast<int>(BitUtil::Ceil(bit_offset_, 8)));
return max_bytes_ -
(byte_offset_ + static_cast<int>(BitUtil::BytesForBits(bit_offset_)));
}

/// Maximum byte length of a vlq encoded int
Expand Down Expand Up @@ -190,7 +191,7 @@ inline bool BitWriter::PutValue(uint64_t v, int num_bits) {
}

inline void BitWriter::Flush(bool align) {
int num_bytes = static_cast<int>(BitUtil::Ceil(bit_offset_, 8));
int num_bytes = static_cast<int>(BitUtil::BytesForBits(bit_offset_));
DCHECK_LE(byte_offset_ + num_bytes, max_bytes_);
memcpy(buffer_ + byte_offset_, &buffered_values_, num_bytes);

Expand Down Expand Up @@ -355,7 +356,7 @@ inline int BitReader::GetBatch(int num_bits, T* v, int batch_size) {
template <typename T>
inline bool BitReader::GetAligned(int num_bytes, T* v) {
DCHECK_LE(num_bytes, static_cast<int>(sizeof(T)));
int bytes_read = static_cast<int>(BitUtil::Ceil(bit_offset_, 8));
int bytes_read = static_cast<int>(BitUtil::BytesForBits(bit_offset_));
if (ARROW_PREDICT_FALSE(byte_offset_ + bytes_read + num_bytes > max_bytes_))
return false;

Expand Down
141 changes: 67 additions & 74 deletions cpp/src/arrow/util/bit-util-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,6 @@

namespace arrow {

static void EnsureCpuInfoInitialized() {
if (!CpuInfo::initialized()) {
CpuInfo::Init();
}
}

template <class BitmapWriter>
void WriteVectorToWriter(BitmapWriter& writer, const std::vector<int> values) {
for (const auto& value : values) {
Expand All @@ -61,7 +55,7 @@ void BitmapFromVector(const std::vector<int>& values, int64_t bit_offset,
std::shared_ptr<Buffer>* out_buffer, int64_t* out_length) {
const int64_t length = values.size();
*out_length = length;
ASSERT_OK(GetEmptyBitmap(default_memory_pool(), length + bit_offset, out_buffer));
ASSERT_OK(AllocateEmptyBitmap(length + bit_offset, out_buffer));
auto writer = internal::BitmapWriter((*out_buffer)->mutable_data(), bit_offset, length);
WriteVectorToWriter(writer, values);
}
Expand Down Expand Up @@ -456,18 +450,18 @@ TEST(BitUtilTests, TestCopyBitmap) {
}
}

TEST(BitUtil, Ceil) {
EXPECT_EQ(BitUtil::Ceil(0, 1), 0);
EXPECT_EQ(BitUtil::Ceil(1, 1), 1);
EXPECT_EQ(BitUtil::Ceil(1, 2), 1);
EXPECT_EQ(BitUtil::Ceil(1, 8), 1);
EXPECT_EQ(BitUtil::Ceil(7, 8), 1);
EXPECT_EQ(BitUtil::Ceil(8, 8), 1);
EXPECT_EQ(BitUtil::Ceil(9, 8), 2);
EXPECT_EQ(BitUtil::Ceil(9, 9), 1);
EXPECT_EQ(BitUtil::Ceil(10000000000, 10), 1000000000);
EXPECT_EQ(BitUtil::Ceil(10, 10000000000), 1);
EXPECT_EQ(BitUtil::Ceil(100000000000, 10000000000), 10);
TEST(BitUtil, CeilDiv) {
EXPECT_EQ(BitUtil::CeilDiv(0, 1), 0);
EXPECT_EQ(BitUtil::CeilDiv(1, 1), 1);
EXPECT_EQ(BitUtil::CeilDiv(1, 2), 1);
EXPECT_EQ(BitUtil::CeilDiv(1, 8), 1);
EXPECT_EQ(BitUtil::CeilDiv(7, 8), 1);
EXPECT_EQ(BitUtil::CeilDiv(8, 8), 1);
EXPECT_EQ(BitUtil::CeilDiv(9, 8), 2);
EXPECT_EQ(BitUtil::CeilDiv(9, 9), 1);
EXPECT_EQ(BitUtil::CeilDiv(10000000000, 10), 1000000000);
EXPECT_EQ(BitUtil::CeilDiv(10, 10000000000), 1);
EXPECT_EQ(BitUtil::CeilDiv(100000000000, 10000000000), 10);
}

TEST(BitUtil, RoundUp) {
Expand All @@ -482,31 +476,6 @@ TEST(BitUtil, RoundUp) {
EXPECT_EQ(BitUtil::RoundUp(100000000000, 10000000000), 100000000000);
}

TEST(BitUtil, RoundDown) {
EXPECT_EQ(BitUtil::RoundDown(0, 1), 0);
EXPECT_EQ(BitUtil::RoundDown(1, 1), 1);
EXPECT_EQ(BitUtil::RoundDown(1, 2), 0);
EXPECT_EQ(BitUtil::RoundDown(6, 2), 6);
EXPECT_EQ(BitUtil::RoundDown(7, 3), 6);
EXPECT_EQ(BitUtil::RoundDown(9, 9), 9);
EXPECT_EQ(BitUtil::RoundDown(10000000001, 10), 10000000000);
EXPECT_EQ(BitUtil::RoundDown(10, 10000000000), 0);
EXPECT_EQ(BitUtil::RoundDown(100000000000, 10000000000), 100000000000);
}

TEST(BitUtil, Popcount) {
EnsureCpuInfoInitialized();

EXPECT_EQ(BitUtil::Popcount(BOOST_BINARY(0 1 0 1 0 1 0 1)), 4);
EXPECT_EQ(BitUtil::PopcountNoHw(BOOST_BINARY(0 1 0 1 0 1 0 1)), 4);
EXPECT_EQ(BitUtil::Popcount(BOOST_BINARY(1 1 1 1 0 1 0 1)), 6);
EXPECT_EQ(BitUtil::PopcountNoHw(BOOST_BINARY(1 1 1 1 0 1 0 1)), 6);
EXPECT_EQ(BitUtil::Popcount(BOOST_BINARY(1 1 1 1 1 1 1 1)), 8);
EXPECT_EQ(BitUtil::PopcountNoHw(BOOST_BINARY(1 1 1 1 1 1 1 1)), 8);
EXPECT_EQ(BitUtil::Popcount(0), 0);
EXPECT_EQ(BitUtil::PopcountNoHw(0), 0);
}

TEST(BitUtil, TrailingBits) {
EXPECT_EQ(BitUtil::TrailingBits(BOOST_BINARY(1 1 1 1 1 1 1 1), 0), 0);
EXPECT_EQ(BitUtil::TrailingBits(BOOST_BINARY(1 1 1 1 1 1 1 1), 1), 1);
Expand Down Expand Up @@ -549,47 +518,71 @@ TEST(BitUtil, Log2) {
EXPECT_EQ(BitUtil::Log2(3), 2);
EXPECT_EQ(BitUtil::Log2(4), 2);
EXPECT_EQ(BitUtil::Log2(5), 3);
EXPECT_EQ(BitUtil::Log2(8), 3);
EXPECT_EQ(BitUtil::Log2(9), 4);
EXPECT_EQ(BitUtil::Log2(INT_MAX), 31);
EXPECT_EQ(BitUtil::Log2(UINT_MAX), 32);
EXPECT_EQ(BitUtil::Log2(ULLONG_MAX), 64);
}

TEST(BitUtil, NumRequiredBits) {
EXPECT_EQ(BitUtil::NumRequiredBits(0), 0);
EXPECT_EQ(BitUtil::NumRequiredBits(1), 1);
EXPECT_EQ(BitUtil::NumRequiredBits(2), 2);
EXPECT_EQ(BitUtil::NumRequiredBits(3), 2);
EXPECT_EQ(BitUtil::NumRequiredBits(4), 3);
EXPECT_EQ(BitUtil::NumRequiredBits(5), 3);
EXPECT_EQ(BitUtil::NumRequiredBits(7), 3);
EXPECT_EQ(BitUtil::NumRequiredBits(8), 4);
EXPECT_EQ(BitUtil::NumRequiredBits(9), 4);
EXPECT_EQ(BitUtil::NumRequiredBits(UINT_MAX - 1), 32);
EXPECT_EQ(BitUtil::NumRequiredBits(UINT_MAX), 32);
EXPECT_EQ(BitUtil::NumRequiredBits(static_cast<uint64_t>(UINT_MAX) + 1), 33);
EXPECT_EQ(BitUtil::NumRequiredBits(ULLONG_MAX / 2), 63);
EXPECT_EQ(BitUtil::NumRequiredBits(ULLONG_MAX / 2 + 1), 64);
EXPECT_EQ(BitUtil::NumRequiredBits(ULLONG_MAX - 1), 64);
EXPECT_EQ(BitUtil::NumRequiredBits(ULLONG_MAX), 64);
}

#define U32(x) static_cast<uint32_t>(x)
#define U64(x) static_cast<uint64_t>(x)

TEST(BitUtil, CountLeadingZeros) {
EXPECT_EQ(BitUtil::CountLeadingZeros(U32(0)), 32);
EXPECT_EQ(BitUtil::CountLeadingZeros(U32(1)), 31);
EXPECT_EQ(BitUtil::CountLeadingZeros(U32(2)), 30);
EXPECT_EQ(BitUtil::CountLeadingZeros(U32(3)), 30);
EXPECT_EQ(BitUtil::CountLeadingZeros(U32(4)), 29);
EXPECT_EQ(BitUtil::CountLeadingZeros(U32(7)), 29);
EXPECT_EQ(BitUtil::CountLeadingZeros(U32(8)), 28);
EXPECT_EQ(BitUtil::CountLeadingZeros(U32(UINT_MAX / 2)), 1);
EXPECT_EQ(BitUtil::CountLeadingZeros(U32(UINT_MAX / 2 + 1)), 0);
EXPECT_EQ(BitUtil::CountLeadingZeros(U32(UINT_MAX)), 0);

EXPECT_EQ(BitUtil::CountLeadingZeros(U64(0)), 64);
EXPECT_EQ(BitUtil::CountLeadingZeros(U64(1)), 63);
EXPECT_EQ(BitUtil::CountLeadingZeros(U64(2)), 62);
EXPECT_EQ(BitUtil::CountLeadingZeros(U64(3)), 62);
EXPECT_EQ(BitUtil::CountLeadingZeros(U64(4)), 61);
EXPECT_EQ(BitUtil::CountLeadingZeros(U64(7)), 61);
EXPECT_EQ(BitUtil::CountLeadingZeros(U64(8)), 60);
EXPECT_EQ(BitUtil::CountLeadingZeros(U64(UINT_MAX)), 32);
EXPECT_EQ(BitUtil::CountLeadingZeros(U64(UINT_MAX) + 1), 31);
EXPECT_EQ(BitUtil::CountLeadingZeros(U64(ULLONG_MAX / 2)), 1);
EXPECT_EQ(BitUtil::CountLeadingZeros(U64(ULLONG_MAX / 2 + 1)), 0);
EXPECT_EQ(BitUtil::CountLeadingZeros(U64(ULLONG_MAX)), 0);
}

#undef U32
#undef U64

TEST(BitUtil, RoundUpToPowerOf2) {
EXPECT_EQ(BitUtil::RoundUpToPowerOf2(7, 8), 8);
EXPECT_EQ(BitUtil::RoundUpToPowerOf2(8, 8), 8);
EXPECT_EQ(BitUtil::RoundUpToPowerOf2(9, 8), 16);
}

TEST(BitUtil, RoundDownToPowerOf2) {
EXPECT_EQ(BitUtil::RoundDownToPowerOf2(7, 8), 0);
EXPECT_EQ(BitUtil::RoundDownToPowerOf2(8, 8), 8);
EXPECT_EQ(BitUtil::RoundDownToPowerOf2(9, 8), 8);
}

TEST(BitUtil, RoundUpDown) {
EXPECT_EQ(BitUtil::RoundUpNumBytes(7), 1);
EXPECT_EQ(BitUtil::RoundUpNumBytes(8), 1);
EXPECT_EQ(BitUtil::RoundUpNumBytes(9), 2);
EXPECT_EQ(BitUtil::RoundDownNumBytes(7), 0);
EXPECT_EQ(BitUtil::RoundDownNumBytes(8), 1);
EXPECT_EQ(BitUtil::RoundDownNumBytes(9), 1);

EXPECT_EQ(BitUtil::RoundUpNumi32(31), 1);
EXPECT_EQ(BitUtil::RoundUpNumi32(32), 1);
EXPECT_EQ(BitUtil::RoundUpNumi32(33), 2);
EXPECT_EQ(BitUtil::RoundDownNumi32(31), 0);
EXPECT_EQ(BitUtil::RoundDownNumi32(32), 1);
EXPECT_EQ(BitUtil::RoundDownNumi32(33), 1);

EXPECT_EQ(BitUtil::RoundUpNumi64(63), 1);
EXPECT_EQ(BitUtil::RoundUpNumi64(64), 1);
EXPECT_EQ(BitUtil::RoundUpNumi64(65), 2);
EXPECT_EQ(BitUtil::RoundDownNumi64(63), 0);
EXPECT_EQ(BitUtil::RoundDownNumi64(64), 1);
EXPECT_EQ(BitUtil::RoundDownNumi64(65), 1);
}

void TestZigZag(int32_t v) {
static void TestZigZag(int32_t v) {
uint8_t buffer[BitReader::MAX_VLQ_BYTE_LEN];
BitWriter writer(buffer, sizeof(buffer));
BitReader reader(buffer, sizeof(buffer));
Expand Down
30 changes: 16 additions & 14 deletions cpp/src/arrow/util/bit-util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,23 @@

namespace arrow {

void BitUtil::FillBitsFromBytes(const std::vector<uint8_t>& bytes, uint8_t* bits) {
namespace BitUtil {

namespace {

void FillBitsFromBytes(const std::vector<uint8_t>& bytes, uint8_t* bits) {
for (size_t i = 0; i < bytes.size(); ++i) {
if (bytes[i] > 0) {
SetBit(bits, i);
}
}
}

Status BitUtil::BytesToBits(const std::vector<uint8_t>& bytes, MemoryPool* pool,
std::shared_ptr<Buffer>* out) {
int64_t bit_length = BitUtil::BytesForBits(bytes.size());
} // namespace

Status BytesToBits(const std::vector<uint8_t>& bytes, MemoryPool* pool,
std::shared_ptr<Buffer>* out) {
int64_t bit_length = BytesForBits(bytes.size());

std::shared_ptr<Buffer> buffer;
RETURN_NOT_OK(AllocateBuffer(pool, bit_length, &buffer));
Expand All @@ -57,6 +63,8 @@ Status BitUtil::BytesToBits(const std::vector<uint8_t>& bytes, MemoryPool* pool,
return Status::OK();
}

} // namespace BitUtil

int64_t CountSetBits(const uint8_t* data, int64_t bit_offset, int64_t length) {
constexpr int64_t pop_len = sizeof(uint64_t) * 8;

Expand Down Expand Up @@ -98,16 +106,10 @@ int64_t CountSetBits(const uint8_t* data, int64_t bit_offset, int64_t length) {
return count;
}

Status GetEmptyBitmap(MemoryPool* pool, int64_t length, std::shared_ptr<Buffer>* result) {
RETURN_NOT_OK(AllocateBuffer(pool, BitUtil::BytesForBits(length), result));
memset((*result)->mutable_data(), 0, static_cast<size_t>((*result)->size()));
return Status::OK();
}

Status CopyBitmap(MemoryPool* pool, const uint8_t* data, int64_t offset, int64_t length,
std::shared_ptr<Buffer>* out) {
std::shared_ptr<Buffer> buffer;
RETURN_NOT_OK(GetEmptyBitmap(pool, length, &buffer));
RETURN_NOT_OK(AllocateEmptyBitmap(pool, length, &buffer));
uint8_t* dest = buffer->mutable_data();

int64_t byte_offset = offset / 8;
Expand Down Expand Up @@ -137,7 +139,7 @@ Status CopyBitmap(MemoryPool* pool, const uint8_t* data, int64_t offset, int64_t

for (int64_t i = length; i < length + bits_to_zero; ++i) {
// Both branches may copy extra bits - unsetting to match specification.
BitUtil::SetBitTo(dest, i, false);
BitUtil::ClearBit(dest, i);
}

*out = buffer;
Expand Down Expand Up @@ -214,12 +216,12 @@ Status BitmapAnd(MemoryPool* pool, const uint8_t* left, int64_t left_offset,
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(GetEmptyBitmap(pool, phys_bits, out_buffer));
RETURN_NOT_OK(AllocateEmptyBitmap(pool, phys_bits, out_buffer));
AlignedBitmapAnd(left, left_offset, right, right_offset,
(*out_buffer)->mutable_data(), out_offset, length);
} else {
// Unaligned
RETURN_NOT_OK(GetEmptyBitmap(pool, length + out_offset, out_buffer));
RETURN_NOT_OK(AllocateEmptyBitmap(pool, length + out_offset, out_buffer));
UnalignedBitmapAnd(left, left_offset, right, right_offset,
(*out_buffer)->mutable_data(), out_offset, length);
}
Expand Down
Loading