mirror of
https://github.com/ETLCPP/etl.git
synced 2026-11-09 14:49:53 +08:00
Added more power templates
Added power_of_2_round_up power_of_2_round_down is_power_of_2
This commit is contained in:
parent
254ad1e1da
commit
4cec2c88ff
118
power.h
118
power.h
@ -30,6 +30,7 @@ SOFTWARE.
|
||||
#define __ETL_POW__
|
||||
|
||||
#include <stddef.h>
|
||||
#include "log.h"
|
||||
|
||||
///\defgroup power power
|
||||
/// power<N, POWER> : Calculates N to the power POWER.
|
||||
@ -51,7 +52,8 @@ namespace etl
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
// Specialisation for POWER == 0
|
||||
/// Calculates powers.
|
||||
/// Specialisation for POWER == 0.
|
||||
//***************************************************************************
|
||||
template <const size_t N>
|
||||
struct power<N, 0>
|
||||
@ -61,6 +63,120 @@ namespace etl
|
||||
value = 1
|
||||
};
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
///\ingroup power
|
||||
/// Calculates the rounded up power of 2.
|
||||
//***************************************************************************
|
||||
template <const size_t N>
|
||||
struct power_of_2_round_up
|
||||
{
|
||||
enum value_type
|
||||
{
|
||||
value = 1 << (etl::log2<N - 1>::value + 1)
|
||||
};
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
///\ingroup power
|
||||
/// Calculates the rounded up power of 2.
|
||||
/// Specialisation for 0.
|
||||
//***************************************************************************
|
||||
template <>
|
||||
struct power_of_2_round_up<0>
|
||||
{
|
||||
enum value_type
|
||||
{
|
||||
value = 2
|
||||
};
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
///\ingroup power
|
||||
/// Calculates the rounded down power of 2.
|
||||
//***************************************************************************
|
||||
template <const size_t N>
|
||||
struct power_of_2_round_down
|
||||
{
|
||||
enum value_type
|
||||
{
|
||||
value = 1 << (etl::log2<N - 1>::value)
|
||||
};
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
///\ingroup power
|
||||
/// Calculates the rounded down power of 2.
|
||||
/// Specialisation for 0.
|
||||
//***************************************************************************
|
||||
template <>
|
||||
struct power_of_2_round_down<0>
|
||||
{
|
||||
enum value_type
|
||||
{
|
||||
value = 2
|
||||
};
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
///\ingroup power
|
||||
/// Calculates the rounded down power of 2.
|
||||
/// Specialisation for 1.
|
||||
//***************************************************************************
|
||||
template <>
|
||||
struct power_of_2_round_down<1>
|
||||
{
|
||||
enum value_type
|
||||
{
|
||||
value = 2
|
||||
};
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
///\ingroup power
|
||||
/// Calculates the rounded down power of 2.
|
||||
/// Specialisation for 2.
|
||||
//***************************************************************************
|
||||
template <>
|
||||
struct power_of_2_round_down<2>
|
||||
{
|
||||
enum value_type
|
||||
{
|
||||
value = 2
|
||||
};
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
///\ingroup power
|
||||
/// Checks if N is a power of 2.
|
||||
//***************************************************************************
|
||||
template <const size_t N>
|
||||
struct is_power_of_2
|
||||
{
|
||||
static const bool value = (N & (N - 1)) == 0;
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
///\ingroup power
|
||||
/// Checks if N is a power of 2.
|
||||
/// Specialisation for 0.
|
||||
//***************************************************************************
|
||||
template <>
|
||||
struct is_power_of_2<0>
|
||||
{
|
||||
static const bool value = false;
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
///\ingroup power
|
||||
/// Checks if N is a power of 2.
|
||||
/// Specialisation for 1.
|
||||
//***************************************************************************
|
||||
template <>
|
||||
struct is_power_of_2<1>
|
||||
{
|
||||
static const bool value = false;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@ -164,6 +164,106 @@ namespace
|
||||
CHECK_EQUAL(1000000000, actual);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_power_of_2_round_up)
|
||||
{
|
||||
int actual;
|
||||
|
||||
//
|
||||
actual = etl::power_of_2_round_up<0>::value;
|
||||
CHECK_EQUAL(2, actual);
|
||||
|
||||
actual = etl::power_of_2_round_up<1>::value;
|
||||
CHECK_EQUAL(2, actual);
|
||||
|
||||
actual = etl::power_of_2_round_up<2>::value;
|
||||
CHECK_EQUAL(2, actual);
|
||||
|
||||
actual = etl::power_of_2_round_up<3>::value;
|
||||
CHECK_EQUAL(4, actual);
|
||||
|
||||
actual = etl::power_of_2_round_up<4>::value;
|
||||
CHECK_EQUAL(4, actual);
|
||||
|
||||
actual = etl::power_of_2_round_up<5>::value;
|
||||
CHECK_EQUAL(8, actual);
|
||||
|
||||
actual = etl::power_of_2_round_up<127>::value;
|
||||
CHECK_EQUAL(128, actual);
|
||||
|
||||
actual = etl::power_of_2_round_up<128>::value;
|
||||
CHECK_EQUAL(128, actual);
|
||||
|
||||
actual = etl::power_of_2_round_up<129>::value;
|
||||
CHECK_EQUAL(256, actual);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_power_of_2_round_down)
|
||||
{
|
||||
int actual;
|
||||
|
||||
actual = etl::power_of_2_round_down<0>::value;
|
||||
CHECK_EQUAL(2, actual);
|
||||
|
||||
actual = etl::power_of_2_round_down<1>::value;
|
||||
CHECK_EQUAL(2, actual);
|
||||
|
||||
actual = etl::power_of_2_round_down<2>::value;
|
||||
CHECK_EQUAL(2, actual);
|
||||
|
||||
actual = etl::power_of_2_round_down<3>::value;
|
||||
CHECK_EQUAL(2, actual);
|
||||
|
||||
actual = etl::power_of_2_round_down<4>::value;
|
||||
CHECK_EQUAL(2, actual);
|
||||
|
||||
actual = etl::power_of_2_round_down<5>::value;
|
||||
CHECK_EQUAL(4, actual);
|
||||
|
||||
actual = etl::power_of_2_round_down<127>::value;
|
||||
CHECK_EQUAL(64, actual);
|
||||
|
||||
actual = etl::power_of_2_round_down<128>::value;
|
||||
CHECK_EQUAL(64, actual);
|
||||
|
||||
actual = etl::power_of_2_round_down<129>::value;
|
||||
CHECK_EQUAL(128, actual);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_is_power_of_2)
|
||||
{
|
||||
bool actual;
|
||||
|
||||
actual = etl::is_power_of_2<0>::value;
|
||||
CHECK_EQUAL(false, actual);
|
||||
|
||||
actual = etl::is_power_of_2<1>::value;
|
||||
CHECK_EQUAL(false, actual);
|
||||
|
||||
actual = etl::is_power_of_2<2>::value;
|
||||
CHECK_EQUAL(true, actual);
|
||||
|
||||
actual = etl::is_power_of_2<3>::value;
|
||||
CHECK_EQUAL(false, actual);
|
||||
|
||||
actual = etl::is_power_of_2<4>::value;
|
||||
CHECK_EQUAL(true, actual);
|
||||
|
||||
actual = etl::is_power_of_2<5>::value;
|
||||
CHECK_EQUAL(false, actual);
|
||||
|
||||
actual = etl::is_power_of_2<127>::value;
|
||||
CHECK_EQUAL(false, actual);
|
||||
|
||||
actual = etl::is_power_of_2<128>::value;
|
||||
CHECK_EQUAL(true, actual);
|
||||
|
||||
actual = etl::is_power_of_2<129>::value;
|
||||
CHECK_EQUAL(false, actual);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_fibbonacci)
|
||||
{
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user