Added findNSB functions and tests

This commit is contained in:
Christophe 2019-07-12 21:50:30 +02:00
parent 72f776b280
commit 4074dfcf07
7 changed files with 268 additions and 14 deletions

View File

@ -119,5 +119,44 @@ namespace detail
return vec<4, T, Q>(Func(a.x, b), Func(a.y, b), Func(a.z, b), Func(a.w, b));
}
};
template<length_t L, typename T, qualifier Q>
struct functor2_vec_int {};
template<typename T, qualifier Q>
struct functor2_vec_int<1, T, Q>
{
GLM_FUNC_QUALIFIER static vec<1, int, Q> call(int (*Func) (T x, int y), vec<1, T, Q> const& a, vec<1, int, Q> const& b)
{
return vec<1, int, Q>(Func(a.x, b.x));
}
};
template<typename T, qualifier Q>
struct functor2_vec_int<2, T, Q>
{
GLM_FUNC_QUALIFIER static vec<2, int, Q> call(int (*Func) (T x, int y), vec<2, T, Q> const& a, vec<2, int, Q> const& b)
{
return vec<2, int, Q>(Func(a.x, b.x), Func(a.y, b.y));
}
};
template<typename T, qualifier Q>
struct functor2_vec_int<3, T, Q>
{
GLM_FUNC_QUALIFIER static vec<3, int, Q> call(int (*Func) (T x, int y), vec<3, T, Q> const& a, vec<3, int, Q> const& b)
{
return vec<3, int, Q>(Func(a.x, b.x), Func(a.y, b.y), Func(a.z, b.z));
}
};
template<typename T, qualifier Q>
struct functor2_vec_int<4, T, Q>
{
GLM_FUNC_QUALIFIER static vec<4, int, Q> call(int (*Func) (T x, int y), vec<4, T, Q> const& a, vec<4, int, Q> const& b)
{
return vec<4, int, Q>(Func(a.x, b.x), Func(a.y, b.y), Func(a.z, b.z), Func(a.w, b.w));
}
};
}//namespace detail
}//namespace glm

View File

@ -30,24 +30,28 @@ namespace glm
/// @{
/// Return true if the value is a power of two number.
///
/// @see ext_scalar_integer
template<typename genIUType>
GLM_FUNC_DECL bool isPowerOfTwo(genIUType v);
/// Return the power of two number which value is just higher the input value,
/// round up to a power of two.
///
/// @see ext_scalar_integer
template<typename genIUType>
GLM_FUNC_DECL genIUType nextPowerOfTwo(genIUType v);
/// Return the power of two number which value is just lower the input value,
/// round down to a power of two.
///
/// @see gtc_round
/// @see ext_scalar_integer
template<typename genIUType>
GLM_FUNC_DECL genIUType prevPowerOfTwo(genIUType v);
/// Return true if the 'Value' is a multiple of 'Multiple'.
///
/// @see gtc_round
/// @see ext_scalar_integer
template<typename genIUType>
GLM_FUNC_DECL bool isMultiple(genIUType v, genIUType Multiple);
@ -58,7 +62,7 @@ namespace glm
/// @param v Source value to which is applied the function
/// @param Multiple Must be a null or positive value
///
/// @see gtc_round
/// @see ext_scalar_integer
template<typename genIUType>
GLM_FUNC_DECL genIUType nextMultiple(genIUType v, genIUType Multiple);
@ -69,10 +73,20 @@ namespace glm
/// @param v Source value to which is applied the function
/// @param Multiple Must be a null or positive value
///
/// @see gtc_round
/// @see ext_scalar_integer
template<typename genIUType>
GLM_FUNC_DECL genIUType prevMultiple(genIUType v, genIUType Multiple);
/// Returns the bit number of the Nth significant bit set to
/// 1 in the binary representation of value.
/// If value bitcount is less than the Nth significant bit, -1 will be returned.
///
/// @tparam genIUType Signed or unsigned integer scalar types.
///
/// @see ext_scalar_integer
template<typename genIUType>
GLM_FUNC_DECL int findNSB(genIUType x, int significantBitCount);
/// @}
} //namespace glm

View File

@ -204,4 +204,40 @@ namespace detail
return detail::compute_floorMultiple<std::numeric_limits<genIUType>::is_iec559, std::numeric_limits<genIUType>::is_signed>::call(Source, Multiple);
}
template<typename genIUType>
GLM_FUNC_QUALIFIER int findNSB(genIUType x, int significantBitCount)
{
GLM_STATIC_ASSERT(std::numeric_limits<genIUType>::is_integer, "'findNSB' only accept integer inputs");
if(bitCount(x) < significantBitCount)
return -1;
genIUType const One = static_cast<genIUType>(1);
int bitPos = 0;
genIUType key = x;
int nBitCount = significantBitCount;
int Step = sizeof(x) * 8 / 2;
while (key > One)
{
genIUType Mask = static_cast<genIUType>((One << Step) - One);
genIUType currentKey = key & Mask;
int currentBitCount = bitCount(currentKey);
if (nBitCount > currentBitCount)
{
nBitCount -= currentBitCount;
bitPos += Step;
key >>= static_cast<genIUType>(Step);
}
else
{
key = key & Mask;
}
Step >>= 1;
}
return static_cast<int>(bitPos);
}
}//namespace glm

View File

@ -31,8 +31,10 @@ namespace glm
/// Return true if the value is a power of two number.
///
/// @tparam L Integer between 1 and 4 included that qualify the dimension of the vector
/// @tparam T Floating-point or integer scalar types
/// @tparam T Signed or unsigned integer scalar types.
/// @tparam Q Value from qualifier enum
///
/// @see ext_vector_integer
template<length_t L, typename T, qualifier Q>
GLM_FUNC_DECL vec<L, bool, Q> isPowerOfTwo(vec<L, T, Q> const& v);
@ -40,8 +42,10 @@ namespace glm
/// round up to a power of two.
///
/// @tparam L Integer between 1 and 4 included that qualify the dimension of the vector
/// @tparam T Floating-point or integer scalar types
/// @tparam T Signed or unsigned integer scalar types.
/// @tparam Q Value from qualifier enum
///
/// @see ext_vector_integer
template<length_t L, typename T, qualifier Q>
GLM_FUNC_DECL vec<L, T, Q> nextPowerOfTwo(vec<L, T, Q> const& v);
@ -49,71 +53,96 @@ namespace glm
/// round down to a power of two.
///
/// @tparam L Integer between 1 and 4 included that qualify the dimension of the vector
/// @tparam T Floating-point or integer scalar types
/// @tparam T Signed or unsigned integer scalar types.
/// @tparam Q Value from qualifier enum
///
/// @see ext_vector_integer
template<length_t L, typename T, qualifier Q>
GLM_FUNC_DECL vec<L, T, Q> prevPowerOfTwo(vec<L, T, Q> const& v);
/// Return true if the 'Value' is a multiple of 'Multiple'.
///
/// @tparam L Integer between 1 and 4 included that qualify the dimension of the vector
/// @tparam T Floating-point or integer scalar types
/// @tparam T Signed or unsigned integer scalar types.
/// @tparam Q Value from qualifier enum
///
/// @see ext_vector_integer
template<length_t L, typename T, qualifier Q>
GLM_FUNC_DECL vec<L, bool, Q> isMultiple(vec<L, T, Q> const& v, T Multiple);
/// Return true if the 'Value' is a multiple of 'Multiple'.
///
/// @tparam L Integer between 1 and 4 included that qualify the dimension of the vector
/// @tparam T Floating-point or integer scalar types
/// @tparam T Signed or unsigned integer scalar types.
/// @tparam Q Value from qualifier enum
///
/// @see ext_vector_integer
template<length_t L, typename T, qualifier Q>
GLM_FUNC_DECL vec<L, bool, Q> isMultiple(vec<L, T, Q> const& v, vec<L, T, Q> const& Multiple);
/// Higher multiple number of Source.
///
/// @tparam L Integer between 1 and 4 included that qualify the dimension of the vector
/// @tparam T Floating-point or integer scalar types
/// @tparam T Signed or unsigned integer scalar types.
/// @tparam Q Value from qualifier enum
///
/// @param v Source values to which is applied the function
/// @param Multiple Must be a null or positive value
///
/// @see ext_vector_integer
template<length_t L, typename T, qualifier Q>
GLM_FUNC_DECL vec<L, T, Q> nextMultiple(vec<L, T, Q> const& v, T Multiple);
/// Higher multiple number of Source.
///
/// @tparam L Integer between 1 and 4 included that qualify the dimension of the vector
/// @tparam T Floating-point or integer scalar types
/// @tparam T Signed or unsigned integer scalar types.
/// @tparam Q Value from qualifier enum
///
/// @param v Source values to which is applied the function
/// @param Multiple Must be a null or positive value
///
/// @see ext_vector_integer
template<length_t L, typename T, qualifier Q>
GLM_FUNC_DECL vec<L, T, Q> nextMultiple(vec<L, T, Q> const& v, vec<L, T, Q> const& Multiple);
/// Lower multiple number of Source.
///
/// @tparam L Integer between 1 and 4 included that qualify the dimension of the vector
/// @tparam T Floating-point or integer scalar types
/// @tparam T Signed or unsigned integer scalar types.
/// @tparam Q Value from qualifier enum
///
/// @param v Source values to which is applied the function
/// @param Multiple Must be a null or positive value
///
/// @see ext_vector_integer
template<length_t L, typename T, qualifier Q>
GLM_FUNC_DECL vec<L, T, Q> prevMultiple(vec<L, T, Q> const& v, T Multiple);
/// Lower multiple number of Source.
///
/// @tparam L Integer between 1 and 4 included that qualify the dimension of the vector
/// @tparam T Floating-point or integer scalar types
/// @tparam T Signed or unsigned integer scalar types.
/// @tparam Q Value from qualifier enum
///
/// @param v Source values to which is applied the function
/// @param Multiple Must be a null or positive value
///
/// @see ext_vector_integer
template<length_t L, typename T, qualifier Q>
GLM_FUNC_DECL vec<L, T, Q> prevMultiple(vec<L, T, Q> const& v, vec<L, T, Q> const& Multiple);
/// Returns the bit number of the Nth significant bit set to
/// 1 in the binary representation of value.
/// If value bitcount is less than the Nth significant bit, -1 will be returned.
///
/// @tparam L An integer between 1 and 4 included that qualify the dimension of the vector.
/// @tparam T Signed or unsigned integer scalar types.
///
/// @see ext_vector_integer
template<length_t L, typename T, qualifier Q>
GLM_FUNC_DECL vec<L, int, Q> findNSB(vec<L, T, Q> const& Source, vec<L, int, Q> SignificantBitCount);
/// @}
} //namespace glm

View File

@ -74,4 +74,12 @@ namespace glm
return detail::functor2<vec, L, T, Q>::call(prevMultiple, Source, Multiple);
}
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, int, Q> findNSB(vec<L, T, Q> const& Source, vec<L, int, Q> SignificantBitCount)
{
GLM_STATIC_ASSERT(std::numeric_limits<T>::is_integer, "'findNSB' only accept integer inputs");
return detail::functor2_vec_int<L, T, Q>::call(findNSB, Source, SignificantBitCount);
}
}//namespace glm

View File

@ -600,9 +600,68 @@ namespace nextMultiple
}
}//namespace nextMultiple
namespace findNSB
{
template<typename T>
struct type
{
T Source;
int SignificantBitCount;
int Return;
};
template <typename T>
int run()
{
type<T> const Data[] =
{
{ 0x00, 1,-1 },
{ 0x01, 2,-1 },
{ 0x02, 2,-1 },
{ 0x06, 3,-1 },
{ 0x01, 1, 0 },
{ 0x03, 1, 0 },
{ 0x03, 2, 1 },
{ 0x07, 2, 1 },
{ 0x05, 2, 2 },
{ 0x0D, 2, 2 }
};
int Error = 0;
for (std::size_t i = 0, n = sizeof(Data) / sizeof(type<T>); i < n; ++i)
{
int const Result0 = glm::findNSB(Data[i].Source, Data[i].SignificantBitCount);
Error += Data[i].Return == Result0 ? 0 : 1;
assert(!Error);
}
return Error;
}
int test()
{
int Error = 0;
Error += run<glm::uint8>();
Error += run<glm::uint16>();
Error += run<glm::uint32>();
Error += run<glm::uint64>();
/*
Error += run<glm::int8>();
Error += run<glm::int16>();
Error += run<glm::int32>();
Error += run<glm::int64>();
*/
return Error;
}
}//namespace findNSB
int main()
{
int Error(0);
int Error = 0;
Error += findNSB::test();
Error += isPowerOfTwo::test();
Error += prevPowerOfTwo::test();

View File

@ -444,6 +444,74 @@ namespace nextMultiple
}
}//namespace nextMultiple
namespace findNSB
{
template<typename T>
struct type
{
T Source;
int SignificantBitCount;
int Return;
};
template <glm::length_t L, typename T>
int run()
{
type<T> const Data[] =
{
{ 0x00, 1,-1 },
{ 0x01, 2,-1 },
{ 0x02, 2,-1 },
{ 0x06, 3,-1 },
{ 0x01, 1, 0 },
{ 0x03, 1, 0 },
{ 0x03, 2, 1 },
{ 0x07, 2, 1 },
{ 0x05, 2, 2 },
{ 0x0D, 2, 2 }
};
int Error = 0;
for (std::size_t i = 0, n = sizeof(Data) / sizeof(type<T>); i < n; ++i)
{
glm::vec<L, int> const Result0 = glm::findNSB<L, T, glm::defaultp>(glm::vec<L, T>(Data[i].Source), glm::vec<L, int>(Data[i].SignificantBitCount));
Error += glm::vec<L, int>(Data[i].Return) == Result0 ? 0 : 1;
assert(!Error);
}
return Error;
}
int test()
{
int Error = 0;
/*
Error += run<1, glm::uint8>();
Error += run<2, glm::uint8>();
Error += run<3, glm::uint8>();
Error += run<4, glm::uint8>();
Error += run<1, glm::uint16>();
Error += run<2, glm::uint16>();
Error += run<3, glm::uint16>();
Error += run<4, glm::uint16>();
Error += run<1, glm::uint32>();
Error += run<2, glm::uint32>();
Error += run<3, glm::uint32>();
*/
Error += run<4, glm::uint32>();
/*
Error += run<1, glm::uint64>();
Error += run<2, glm::uint64>();
Error += run<3, glm::uint64>();
Error += run<4, glm::uint64>();
*/
return Error;
}
}//namespace findNSB
int main()
{
int Error = 0;
@ -453,6 +521,7 @@ int main()
Error += nextPowerOfTwo::test();
Error += prevMultiple::test();
Error += nextMultiple::test();
Error += findNSB::test();
return Error;
}