tint/utils: Add Bitset.

A compact, dynamically sized, vector of bits.

Bug: tint:1613
Change-Id: Ida10d1bdedad48dd4c2ab7908732f328b4b96055
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/96544
Reviewed-by: dan sinclair <dsinclair@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
This commit is contained in:
Ben Clayton 2022-07-21 23:32:24 +00:00 committed by Dawn LUCI CQ
parent 6863be0487
commit e43034bef2
4 changed files with 225 additions and 0 deletions

View File

@ -550,6 +550,7 @@ libtint_source_set("libtint_core_all_src") {
"transform/zero_init_workgroup_memory.cc",
"transform/zero_init_workgroup_memory.h",
"utils/bitcast.h",
"utils/bitset.h",
"utils/block_allocator.h",
"utils/compiler_macros.h",
"utils/concat.h",
@ -1213,6 +1214,7 @@ if (tint_build_unittests) {
tint_unittests_source_set("tint_unittests_utils_src") {
sources = [
"utils/bitcast_test.cc",
"utils/bitset_test.cc",
"utils/crc32_test.cc",
"utils/defer_test.cc",
"utils/enum_set_test.cc",

View File

@ -462,6 +462,7 @@ set(TINT_LIB_SRCS
transform/zero_init_workgroup_memory.cc
transform/zero_init_workgroup_memory.h
utils/bitcast.h
utils/bitset.h
utils/block_allocator.h
utils/compiler_macros.h
utils/concat.h
@ -842,6 +843,7 @@ if(TINT_BUILD_TESTS)
traits_test.cc
transform/transform_test.cc
utils/bitcast_test.cc
utils/bitset_test.cc
utils/block_allocator_test.cc
utils/crc32_test.cc
utils/defer_test.cc

102
src/tint/utils/bitset.h Normal file
View File

@ -0,0 +1,102 @@
// Copyright 2022 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef SRC_TINT_UTILS_BITSET_H_
#define SRC_TINT_UTILS_BITSET_H_
#include <stdint.h>
#include "src/tint/utils/vector.h"
namespace tint::utils {
/// Bitset is a dynamically sized, vector of bits, packed into integer words.
/// Bits can be individually read and written using the index operator.
///
/// Bitset will fit at least `N` bits internally before spilling to heap allocations.
template <size_t N = 0>
class Bitset {
/// The integer word type used to hold the bits
using Word = size_t;
/// Number of bits per word
static constexpr size_t kWordBits = sizeof(Word) * 8;
/// Number of words required to hold the number of bits
static constexpr size_t NumWords(size_t num_bits) {
return ((num_bits + kWordBits - 1) / kWordBits);
}
public:
/// Constructor
Bitset() = default;
/// Destructor
~Bitset() = default;
/// Accessor for a single bit
struct Bit {
/// The word that contains the bit
Word& word;
/// A word with a single bit set, which masks the targetted bit
Word const mask;
/// Assignment operator
/// @param value the new value for the bit
/// @returns this Bit so calls can be chained
const Bit& operator=(bool value) const {
if (value) {
word = word | mask;
} else {
word = word & ~mask;
}
return *this;
}
/// Conversion operator
/// @returns the bit value
operator bool() const { return (word & mask) != 0; }
};
/// @param new_len the new size of the bitmap, in bits.
void Resize(size_t new_len) {
vec_.Resize(NumWords(new_len));
// Clear any potentially set bits that are in the top part of the word
if (size_t high_bit = new_len % kWordBits; high_bit > 0) {
vec_.Back() &= (static_cast<Word>(1) << high_bit) - 1;
}
len_ = new_len;
}
/// @return the number of bits in the bitset.
size_t Length() const { return len_; }
/// Index operator
/// @param index the index of the bit to access
/// @return the accessor for the indexed bit
Bit operator[](size_t index) {
auto& word = vec_[index / kWordBits];
auto mask = static_cast<Word>(1) << (index % kWordBits);
return Bit{word, mask};
}
private:
Vector<size_t, NumWords(N)> vec_;
size_t len_ = 0;
};
} // namespace tint::utils
#endif // SRC_TINT_UTILS_BITSET_H_

View File

@ -0,0 +1,119 @@
// Copyright 2022 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "src/tint/utils/bitset.h"
#include "gtest/gtest.h"
namespace tint::utils {
namespace {
TEST(Bitset, Length) {
Bitset<8> bits;
EXPECT_EQ(bits.Length(), 0u);
bits.Resize(100u);
EXPECT_EQ(bits.Length(), 100u);
}
TEST(Bitset, InitCleared_NoSpill) {
Bitset<256> bits;
bits.Resize(256);
for (size_t i = 0; i < 256; i++) {
EXPECT_FALSE(bits[i]);
}
}
TEST(Bitset, InitCleared_Spill) {
Bitset<64> bits;
bits.Resize(256);
for (size_t i = 0; i < 256; i++) {
EXPECT_FALSE(bits[i]);
}
}
TEST(Bitset, ReadWrite_NoSpill) {
Bitset<256> bits;
bits.Resize(256);
for (size_t i = 0; i < 256; i++) {
bits[i] = (i & 0x2) == 0;
}
for (size_t i = 0; i < 256; i++) {
EXPECT_EQ(bits[i], (i & 0x2) == 0);
}
}
TEST(Bitset, ReadWrite_Spill) {
Bitset<64> bits;
bits.Resize(256);
for (size_t i = 0; i < 256; i++) {
bits[i] = (i & 0x2) == 0;
}
for (size_t i = 0; i < 256; i++) {
EXPECT_EQ(bits[i], (i & 0x2) == 0);
}
}
TEST(Bitset, ShinkGrowAlignedClears_NoSpill) {
Bitset<256> bits;
bits.Resize(256);
for (size_t i = 0; i < 256; i++) {
bits[i] = true;
}
bits.Resize(64);
bits.Resize(256);
for (size_t i = 0; i < 256; i++) {
EXPECT_EQ(bits[i], i < 64u);
}
}
TEST(Bitset, ShinkGrowAlignedClears_Spill) {
Bitset<64> bits;
bits.Resize(256);
for (size_t i = 0; i < 256; i++) {
bits[i] = true;
}
bits.Resize(64);
bits.Resize(256);
for (size_t i = 0; i < 256; i++) {
EXPECT_EQ(bits[i], i < 64u);
}
}
TEST(Bitset, ShinkGrowMisalignedClears_NoSpill) {
Bitset<256> bits;
bits.Resize(256);
for (size_t i = 0; i < 256; i++) {
bits[i] = true;
}
bits.Resize(42);
bits.Resize(256);
for (size_t i = 0; i < 256; i++) {
EXPECT_EQ(bits[i], i < 42u);
}
}
TEST(Bitset, ShinkGrowMisalignedClears_Spill) {
Bitset<64> bits;
bits.Resize(256);
for (size_t i = 0; i < 256; i++) {
bits[i] = true;
}
bits.Resize(42);
bits.Resize(256);
for (size_t i = 0; i < 256; i++) {
EXPECT_EQ(bits[i], i < 42u);
}
}
} // namespace
} // namespace tint::utils