summaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
authordan sinclair <dj2@everburning.com>2018-08-07 09:10:54 -0400
committerGitHub <noreply@github.com>2018-08-07 09:10:54 -0400
commit508df9a3870497b87d299802a2aee23e919ea697 (patch)
treefc9b5096e34012a788bf22c3279412eb39c1863a /source
parente3ea909ebe51ef8bd5229f6686bc19ea3c3e1c56 (diff)
Remove unused bit stream methods. (#1807)
This CL deletes methods from bit stream which are never used and moves several to the anonymous namespace in the bit_stream test file.
Diffstat (limited to 'source')
-rw-r--r--source/util/bit_stream.cpp90
-rw-r--r--source/util/bit_stream.h182
2 files changed, 5 insertions, 267 deletions
diff --git a/source/util/bit_stream.cpp b/source/util/bit_stream.cpp
index a275a63a..f40a2290 100644
--- a/source/util/bit_stream.cpp
+++ b/source/util/bit_stream.cpp
@@ -197,41 +197,6 @@ bool ReadVariableWidthSigned(BitReaderInterface* reader, T* val,
} // namespace
-size_t Log2U64(uint64_t val) {
- size_t res = 0;
-
- if (val & 0xFFFFFFFF00000000) {
- val >>= 32;
- res |= 32;
- }
-
- if (val & 0xFFFF0000) {
- val >>= 16;
- res |= 16;
- }
-
- if (val & 0xFF00) {
- val >>= 8;
- res |= 8;
- }
-
- if (val & 0xF0) {
- val >>= 4;
- res |= 4;
- }
-
- if (val & 0xC) {
- val >>= 2;
- res |= 2;
- }
-
- if (val & 0x2) {
- res |= 1;
- }
-
- return res;
-}
-
void BitWriterInterface::WriteVariableWidthU64(uint64_t val,
size_t chunk_length) {
WriteVariableWidthUnsigned(this, val, chunk_length);
@@ -247,41 +212,11 @@ void BitWriterInterface::WriteVariableWidthU16(uint16_t val,
WriteVariableWidthUnsigned(this, val, chunk_length);
}
-void BitWriterInterface::WriteVariableWidthU8(uint8_t val,
- size_t chunk_length) {
- WriteVariableWidthUnsigned(this, val, chunk_length);
-}
-
void BitWriterInterface::WriteVariableWidthS64(int64_t val, size_t chunk_length,
size_t zigzag_exponent) {
WriteVariableWidthSigned(this, val, chunk_length, zigzag_exponent);
}
-void BitWriterInterface::WriteVariableWidthS32(int32_t val, size_t chunk_length,
- size_t zigzag_exponent) {
- WriteVariableWidthSigned(this, val, chunk_length, zigzag_exponent);
-}
-
-void BitWriterInterface::WriteVariableWidthS16(int16_t val, size_t chunk_length,
- size_t zigzag_exponent) {
- WriteVariableWidthSigned(this, val, chunk_length, zigzag_exponent);
-}
-
-void BitWriterInterface::WriteVariableWidthS8(int8_t val, size_t chunk_length,
- size_t zigzag_exponent) {
- WriteVariableWidthSigned(this, val, chunk_length, zigzag_exponent);
-}
-
-void BitWriterInterface::WriteFixedWidth(uint64_t val, uint64_t max_val) {
- if (val > max_val) {
- assert(0 && "WriteFixedWidth: value too wide");
- return;
- }
-
- const size_t num_bits = 1 + Log2U64(max_val);
- WriteBits(val, num_bits);
-}
-
BitWriterWord64::BitWriterWord64(size_t reserve_bits) : end_(0) {
buffer_.reserve(NumBitsToNumWords<64>(reserve_bits));
}
@@ -340,36 +275,11 @@ bool BitReaderInterface::ReadVariableWidthU16(uint16_t* val,
return ReadVariableWidthUnsigned(this, val, chunk_length);
}
-bool BitReaderInterface::ReadVariableWidthU8(uint8_t* val,
- size_t chunk_length) {
- return ReadVariableWidthUnsigned(this, val, chunk_length);
-}
-
bool BitReaderInterface::ReadVariableWidthS64(int64_t* val, size_t chunk_length,
size_t zigzag_exponent) {
return ReadVariableWidthSigned(this, val, chunk_length, zigzag_exponent);
}
-bool BitReaderInterface::ReadVariableWidthS32(int32_t* val, size_t chunk_length,
- size_t zigzag_exponent) {
- return ReadVariableWidthSigned(this, val, chunk_length, zigzag_exponent);
-}
-
-bool BitReaderInterface::ReadVariableWidthS16(int16_t* val, size_t chunk_length,
- size_t zigzag_exponent) {
- return ReadVariableWidthSigned(this, val, chunk_length, zigzag_exponent);
-}
-
-bool BitReaderInterface::ReadVariableWidthS8(int8_t* val, size_t chunk_length,
- size_t zigzag_exponent) {
- return ReadVariableWidthSigned(this, val, chunk_length, zigzag_exponent);
-}
-
-bool BitReaderInterface::ReadFixedWidth(uint64_t* val, uint64_t max_val) {
- const size_t num_bits = 1 + Log2U64(max_val);
- return ReadBits(val, num_bits) == num_bits;
-}
-
BitReaderWord64::BitReaderWord64(std::vector<uint64_t>&& buffer)
: buffer_(std::move(buffer)), pos_(0) {}
diff --git a/source/util/bit_stream.h b/source/util/bit_stream.h
index 58165374..dd29e479 100644
--- a/source/util/bit_stream.h
+++ b/source/util/bit_stream.h
@@ -29,9 +29,6 @@
namespace spvtools {
namespace utils {
-// Returns rounded down log2(val). log2(0) is considered 0.
-size_t Log2U64(uint64_t val);
-
// Terminology:
// Bits - usually used for a uint64 word, first bit is the lowest.
// Stream - std::string of '0' and '1', read left-to-right,
@@ -54,33 +51,6 @@ inline T GetLowerBits(T in, size_t num_bits) {
return sizeof(T) * 8 == num_bits ? in : in & T((T(1) << num_bits) - T(1));
}
-// Encodes signed integer as unsigned in zigzag order:
-// 0 -> 0
-// -1 -> 1
-// 1 -> 2
-// -2 -> 3
-// 2 -> 4
-// Motivation: -1 is 0xFF...FF what doesn't work very well with
-// WriteVariableWidth which prefers to have as many 0 bits as possible.
-inline uint64_t EncodeZigZag(int64_t val) { return (val << 1) ^ (val >> 63); }
-
-// Decodes signed integer encoded with EncodeZigZag.
-inline int64_t DecodeZigZag(uint64_t val) {
- if (val & 1) {
- // Negative.
- // 1 -> -1
- // 3 -> -2
- // 5 -> -3
- return -1 - (val >> 1);
- } else {
- // Non-negative.
- // 0 -> 0
- // 2 -> 1
- // 4 -> 2
- return val >> 1;
- }
-}
-
// Encodes signed integer as unsigned. This is a generalized version of
// EncodeZigZag, designed to favor small positive numbers.
// Values are transformed in blocks of 2^|block_exponent|.
@@ -113,111 +83,24 @@ inline int64_t DecodeZigZag(uint64_t val, size_t block_exponent) {
}
}
-// Converts |buffer| to a stream of '0' and '1'.
-template <typename T>
-std::string BufferToStream(const std::vector<T>& buffer) {
- std::stringstream ss;
- for (auto it = buffer.begin(); it != buffer.end(); ++it) {
- std::string str = std::bitset<sizeof(T) * 8>(*it).to_string();
- // Strings generated by std::bitset::to_string are read right to left.
- // Reversing to left to right.
- std::reverse(str.begin(), str.end());
- ss << str;
- }
- return ss.str();
-}
-
-// Converts a left-to-right input string of '0' and '1' to a buffer of |T|
-// words.
-template <typename T>
-std::vector<T> StreamToBuffer(std::string str) {
- // The input string is left-to-right, the input argument of std::bitset needs
- // to right-to-left. Instead of reversing tokens, reverse the entire string
- // and iterate tokens from end to begin.
- std::reverse(str.begin(), str.end());
- const int word_size = static_cast<int>(sizeof(T) * 8);
- const int str_length = static_cast<int>(str.length());
- std::vector<T> buffer;
- buffer.reserve(NumBitsToNumWords<sizeof(T)>(str.length()));
- for (int index = str_length - word_size; index >= 0; index -= word_size) {
- buffer.push_back(static_cast<T>(
- std::bitset<sizeof(T) * 8>(str, index, word_size).to_ullong()));
- }
- const size_t suffix_length = str.length() % word_size;
- if (suffix_length != 0) {
- buffer.push_back(static_cast<T>(
- std::bitset<sizeof(T) * 8>(str, 0, suffix_length).to_ullong()));
- }
- return buffer;
-}
-
-// Adds '0' chars at the end of the string until the size is a multiple of N.
-template <size_t N>
-inline std::string PadToWord(std::string&& str) {
- const size_t tail_length = str.size() % N;
- if (tail_length != 0) str += std::string(N - tail_length, '0');
- return std::move(str);
-}
-
-// Adds '0' chars at the end of the string until the size is a multiple of N.
-template <size_t N>
-inline std::string PadToWord(const std::string& str) {
- return PadToWord<N>(std::string(str));
-}
-
-// Converts a left-to-right stream of bits to std::bitset.
-template <size_t N>
-inline std::bitset<N> StreamToBitset(std::string str) {
- std::reverse(str.begin(), str.end());
- return std::bitset<N>(str);
-}
-
-// Converts first |num_bits| of std::bitset to a left-to-right stream of bits.
-template <size_t N>
-inline std::string BitsetToStream(const std::bitset<N>& bits,
- size_t num_bits = N) {
- std::string str = bits.to_string().substr(N - num_bits);
- std::reverse(str.begin(), str.end());
- return str;
-}
-
-// Converts a left-to-right stream of bits to uint64.
-inline uint64_t StreamToBits(std::string str) {
- std::reverse(str.begin(), str.end());
- return std::bitset<64>(str).to_ullong();
-}
-
// Converts first |num_bits| stored in uint64 to a left-to-right stream of bits.
inline std::string BitsToStream(uint64_t bits, size_t num_bits = 64) {
std::bitset<64> bitset(bits);
- return BitsetToStream(bitset, num_bits);
+ std::string str = bitset.to_string().substr(64 - num_bits);
+ std::reverse(str.begin(), str.end());
+ return str;
}
// Base class for writing sequences of bits.
class BitWriterInterface {
public:
- BitWriterInterface() {}
- virtual ~BitWriterInterface() {}
+ BitWriterInterface() = default;
+ virtual ~BitWriterInterface() = default;
// Writes lower |num_bits| in |bits| to the stream.
// |num_bits| must be no greater than 64.
virtual void WriteBits(uint64_t bits, size_t num_bits) = 0;
- // Writes left-to-right string of '0' and '1' to stream.
- // String length must be no greater than 64.
- // Note: "01" will be writen as 0x2, not 0x1. The string doesn't represent
- // numbers but a stream of bits in the order they come from encoder.
- virtual void WriteStream(const std::string& bits) {
- WriteBits(StreamToBits(bits), bits.length());
- }
-
- // Writes lower |num_bits| in |bits| to the stream.
- // |num_bits| must be no greater than 64.
- template <size_t N>
- void WriteBitset(const std::bitset<N>& bits, size_t num_bits = N) {
- WriteBits(bits.to_ullong(), num_bits);
- }
-
// Writes bits from value of type |T| to the stream. No encoding is done.
// Always writes 8 * sizeof(T) bits.
template <typename T>
@@ -237,27 +120,8 @@ class BitWriterInterface {
void WriteVariableWidthU64(uint64_t val, size_t chunk_length);
void WriteVariableWidthU32(uint32_t val, size_t chunk_length);
void WriteVariableWidthU16(uint16_t val, size_t chunk_length);
- void WriteVariableWidthU8(uint8_t val, size_t chunk_length);
void WriteVariableWidthS64(int64_t val, size_t chunk_length,
size_t zigzag_exponent);
- void WriteVariableWidthS32(int32_t val, size_t chunk_length,
- size_t zigzag_exponent);
- void WriteVariableWidthS16(int16_t val, size_t chunk_length,
- size_t zigzag_exponent);
- void WriteVariableWidthS8(int8_t val, size_t chunk_length,
- size_t zigzag_exponent);
-
- // Writes |val| using fixed bit width. Bit width is determined by |max_val|:
- // max_val 0 -> bit width 1
- // max_val 1 -> bit width 1
- // max_val 2 -> bit width 2
- // max_val 3 -> bit width 2
- // max_val 4 -> bit width 3
- // max_val 5 -> bit width 3
- // max_val 8 -> bit width 4
- // max_val n -> bit width 1 + floor(log2(n))
- // |val| needs to be <= |max_val|.
- void WriteFixedWidth(uint64_t val, uint64_t max_val);
// Returns number of bits written.
virtual size_t GetNumBits() const = 0;
@@ -293,10 +157,6 @@ class BitWriterWord64 : public BitWriterInterface {
return std::vector<uint8_t>(GetData(), GetData() + GetDataSizeBytes());
}
- // Returns written stream as std::string, padded with zeroes so that the
- // length is a multiple of 64.
- std::string GetStreamPadded64() const { return BufferToStream(buffer_); }
-
// Sets callback to emit bit sequences after every write.
void SetCallback(std::function<void(const std::string&)> callback) {
callback_ = callback;
@@ -328,27 +188,6 @@ class BitReaderInterface {
// Returns number of read bits. |num_bits| must be no greater than 64.
virtual size_t ReadBits(uint64_t* bits, size_t num_bits) = 0;
- // Reads |num_bits| from the stream, stores them in |bits|.
- // Returns number of read bits. |num_bits| must be no greater than 64.
- template <size_t N>
- size_t ReadBitset(std::bitset<N>* bits, size_t num_bits = N) {
- uint64_t val = 0;
- size_t num_read = ReadBits(&val, num_bits);
- if (num_read) {
- *bits = std::bitset<N>(val);
- }
- return num_read;
- }
-
- // Reads |num_bits| from the stream, returns string in left-to-right order.
- // The length of the returned string may be less than |num_bits| if end was
- // reached.
- std::string ReadStream(size_t num_bits) {
- uint64_t bits = 0;
- size_t num_read = ReadBits(&bits, num_bits);
- return BitsToStream(bits, num_read);
- }
-
// Reads 8 * sizeof(T) bits and stores them in |val|.
template <typename T>
bool ReadUnencoded(T* val) {
@@ -383,19 +222,8 @@ class BitReaderInterface {
bool ReadVariableWidthU64(uint64_t* val, size_t chunk_length);
bool ReadVariableWidthU32(uint32_t* val, size_t chunk_length);
bool ReadVariableWidthU16(uint16_t* val, size_t chunk_length);
- bool ReadVariableWidthU8(uint8_t* val, size_t chunk_length);
bool ReadVariableWidthS64(int64_t* val, size_t chunk_length,
size_t zigzag_exponent);
- bool ReadVariableWidthS32(int32_t* val, size_t chunk_length,
- size_t zigzag_exponent);
- bool ReadVariableWidthS16(int16_t* val, size_t chunk_length,
- size_t zigzag_exponent);
- bool ReadVariableWidthS8(int8_t* val, size_t chunk_length,
- size_t zigzag_exponent);
-
- // Reads value written by WriteFixedWidth (|max_val| needs to be the same).
- // Returns true on success, false if the bit stream ends prematurely.
- bool ReadFixedWidth(uint64_t* val, uint64_t max_val);
BitReaderInterface(const BitReaderInterface&) = delete;
BitReaderInterface& operator=(const BitReaderInterface&) = delete;