Skip to content

Commit

Permalink
Update the bitextractor to deal with reads other than len=8
Browse files Browse the repository at this point in the history
He can still deal with READLEN=8 too -- it's easier to test this way.
  • Loading branch information
sz3 committed Jun 1, 2023
1 parent 1fd265d commit 731dfb1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
9 changes: 6 additions & 3 deletions src/lib/image_hash/bit_extractor.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@

#include <iostream>

template<typename C, size_t N>
template<typename C, size_t N, size_t READLEN=8>
class bit_extractor
{
protected:
static constexpr uint64_t BITMASK = (1 << READLEN) - 1; // e.g. 0xFF, 0x1F, 0xF

public:
bit_extractor(const C& bits)
: _bits(bits)
: _bits(bits)
{}

template<typename... T>
Expand All @@ -22,7 +25,7 @@ class bit_extractor
{
constexpr auto byte_offset = sizeof...(T);

uint64_t total = ((uint64_t)(_bits >> (N - bit_offset - 8)) & 0xFF) << (byte_offset << 3); // if byte_offset is 1, we want to shift 8. if 2, 16....
uint64_t total = ((uint64_t)(_bits >> (N - bit_offset - READLEN)) & BITMASK) << (byte_offset * READLEN); // if byte_offset is 1, we want to shift 5. if 2, 10....
return total | extract(t...);
}

Expand Down
20 changes: 17 additions & 3 deletions src/lib/image_hash/test/bitExtractorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,31 @@

using std::string;

TEST_CASE( "bitExtractorTest/testDefault", "[unit]" )
TEST_CASE( "bitExtractorTest/testDefault.4", "[unit]" )
{
intx::uint128 bits = 0x11d07e1f;
bit_extractor<intx::uint128, 32> be(bits);
uint64_t bits = 0x11d07e1f;
bit_extractor<uint64_t, 32, 4> be(bits);

// we're just reading 4 bits at a time.
assertEquals( 0x1d, be.extract(0, 8) ); // 0001 1101
assertEquals( 0x71, be.extract(16, 24) ); // 0x7 0x1
assertEquals( 0x1e, be.extract(0, 20) );
assertEquals( 0x0f, be.extract(12, 28) );
assertEquals( 0x1d, be.extract(24, 8) );
}

TEST_CASE( "bitExtractorTest/testDefault.8", "[unit]" )
{
uint64_t bits = 0x11d07e1f;
bit_extractor<uint64_t, 32, 8> be(bits);

assertEquals( 0x11d0, be.extract(0, 8) );
assertEquals( 0x7e1f, be.extract(16, 24) );
assertEquals( 0x111f, be.extract(0, 24) );
assertEquals( 0x1fd0, be.extract(24, 8) );
}


TEST_CASE( "bitExtractorTest/testLargerValue.1", "[unit]" )
{
intx::uint128 bits(0);
Expand Down

0 comments on commit 731dfb1

Please sign in to comment.