Skip to content

Commit

Permalink
Added binary functors for ~ & | ^
Browse files Browse the repository at this point in the history
  • Loading branch information
John Wellbelove committed Dec 18, 2023
1 parent 480363a commit d17f422
Show file tree
Hide file tree
Showing 2 changed files with 211 additions and 0 deletions.
105 changes: 105 additions & 0 deletions include/etl/binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -2276,6 +2276,111 @@ namespace etl
return msb_mask<T, NBits>::value;
}

//***************************************************************************
/// Bit 'not' a value
///\ingroup binary
//***************************************************************************
template <typename T>
struct binary_not : public etl::unary_function<T, T>
{
//***********************************
ETL_CONSTEXPR
ETL_NODISCARD
T operator ()(T value) ETL_NOEXCEPT
{
ETL_STATIC_ASSERT(etl::is_integral<T>::value, "Not an integral type");

return ~value;
}
};

//***************************************************************************
/// Bit 'and' a value with another
///\ingroup binary
//***************************************************************************
template <typename T>
struct binary_and : public etl::unary_function<T, T>
{
//***********************************
ETL_CONSTEXPR
explicit binary_and(T parameter_) ETL_NOEXCEPT
: parameter(parameter_)
{
}

//***********************************
ETL_CONSTEXPR
ETL_NODISCARD
T operator ()(T value) ETL_NOEXCEPT
{
ETL_STATIC_ASSERT(etl::is_integral<T>::value, "Not an integral type");

return value & parameter;
}

private:

T parameter;
};

//***************************************************************************
/// Bit 'or' a value with another
///\ingroup binary
//***************************************************************************
template <typename T>
struct binary_or : public etl::unary_function<T, T>
{
//***********************************
ETL_CONSTEXPR
explicit binary_or(T parameter_) ETL_NOEXCEPT
: parameter(parameter_)
{
}

//***********************************
ETL_CONSTEXPR
ETL_NODISCARD
T operator ()(T value) ETL_NOEXCEPT
{
ETL_STATIC_ASSERT(etl::is_integral<T>::value, "Not an integral type");

return value | parameter;
}

private:

T parameter;
};

//***************************************************************************
/// Bit 'exclusive-or' a value with another
///\ingroup binary
//***************************************************************************
template <typename T>
struct binary_xor : public etl::unary_function<T, T>
{
//***********************************
ETL_CONSTEXPR
explicit binary_xor(T parameter_) ETL_NOEXCEPT
: parameter(parameter_)
{
}

//***********************************
ETL_CONSTEXPR
ETL_NODISCARD
T operator ()(T value) ETL_NOEXCEPT
{
ETL_STATIC_ASSERT(etl::is_integral<T>::value, "Not an integral type");

return value ^ parameter;
}

private:

T parameter;
};

//***************************************************************************
/// 8 bit binary byte constants.
///\ingroup binary
Expand Down
106 changes: 106 additions & 0 deletions test/test_binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ SOFTWARE.
#include <cstdint>
#include <type_traits>
#include <limits>
#include <array>
#include <algorithm>

#include "etl/binary.h"
#include "etl/bitset.h"
Expand All @@ -42,6 +44,15 @@ SOFTWARE.

namespace
{
template <typename TIterator, typename T>
void generate_input(TIterator first, TIterator last, T value)
{
while (first != last)
{
*first++ = value++;
}
}

//***********************************
// Count bits the easy way.
template <typename T>
Expand Down Expand Up @@ -2894,6 +2905,101 @@ namespace
CHECK_EQUAL(int64_t(0xFFFFFFFFFFFFFF00), (etl::make_msb_mask<int64_t, 56>()));
CHECK_EQUAL(int64_t(0xFFFFFFFFFFFFFFFF), (etl::make_msb_mask<int64_t, 64>()));
}

//*************************************************************************
TEST(test_binary_not)
{
std::array<uint8_t, 256> input;
generate_input(input.begin(), input.end(), 0);

std::array<uint8_t, 256> expected;
for (size_t i = 0; i < input.size(); ++i)
{
expected[i] = ~input[i];
}

std::array<uint8_t, 256> output;

std::transform(input.begin(),
input.end(),
output.begin(),
etl::binary_not<uint8_t>());

CHECK_ARRAY_EQUAL(expected.data(), output.data(), expected.size());
}

//*************************************************************************
TEST(test_binary_and)
{
std::array<uint8_t, 256> input;
generate_input(input.begin(), input.end(), 0);

const uint8_t value = etl::b01101001;

std::array<uint8_t, 256> expected;
for (size_t i = 0; i < input.size(); ++i)
{
expected[i] = input[i] & value;
}

std::array<uint8_t, 256> output;

std::transform(input.begin(),
input.end(),
output.begin(),
etl::binary_and<uint8_t>(value));

CHECK_ARRAY_EQUAL(expected.data(), output.data(), expected.size());
}

//*************************************************************************
TEST(test_binary_or)
{
std::array<uint8_t, 256> input;
generate_input(input.begin(), input.end(), 0);

const uint8_t value = etl::b01101001;

std::array<uint8_t, 256> expected;
for (size_t i = 0; i < input.size(); ++i)
{
expected[i] = input[i] | value;
}

std::array<uint8_t, 256> output;

std::transform(input.begin(),
input.end(),
output.begin(),
etl::binary_or<uint8_t>(value));

CHECK_ARRAY_EQUAL(expected.data(), output.data(), expected.size());
}

//*************************************************************************
TEST(test_binary_xor)
{
std::array<uint8_t, 256> input;
generate_input(input.begin(), input.end(), 0);

const uint8_t value = etl::b01101001;

std::array<uint8_t, 256> expected;

for (size_t i = 0; i < input.size(); ++i)
{
expected[i] = input[i] ^ value;
}

std::array<uint8_t, 256> output;

std::transform(input.begin(),
input.end(),
output.begin(),
etl::binary_xor<uint8_t>(value));

CHECK_ARRAY_EQUAL(expected.data(), output.data(), expected.size());
}
};
}

Expand Down

0 comments on commit d17f422

Please sign in to comment.