Bit shifting functions

This commit is contained in:
Charles Schlosser
2024-05-03 18:55:02 +00:00
committed by Rasmus Munk Larsen
parent 9700fc847a
commit 8e47971789
6 changed files with 113 additions and 48 deletions

View File

@@ -292,6 +292,27 @@ void check_signbit() {
check_signbit_impl<T>::run();
}
template <typename T>
void check_shift() {
using SignedT = typename numext::get_integer_by_size<sizeof(T)>::signed_type;
using UnsignedT = typename numext::get_integer_by_size<sizeof(T)>::unsigned_type;
constexpr int kNumBits = CHAR_BIT * sizeof(T);
for (int i = 0; i < 1000; ++i) {
const T a = internal::random<T>();
for (int s = 1; s < kNumBits; s++) {
T a_bsll = numext::logical_shift_left(a, s);
T a_bsll_ref = a << s;
VERIFY_IS_EQUAL(a_bsll, a_bsll_ref);
T a_bsrl = numext::logical_shift_right(a, s);
T a_bsrl_ref = numext::bit_cast<T, UnsignedT>(numext::bit_cast<UnsignedT, T>(a) >> s);
VERIFY_IS_EQUAL(a_bsrl, a_bsrl_ref);
T a_bsra = numext::arithmetic_shift_right(a, s);
T a_bsra_ref = numext::bit_cast<T, SignedT>(numext::bit_cast<SignedT, T>(a) >> s);
VERIFY_IS_EQUAL(a_bsra, a_bsra_ref);
}
}
}
EIGEN_DECLARE_TEST(numext) {
for (int k = 0; k < g_repeat; ++k) {
CALL_SUBTEST(check_negate<signed char>());
@@ -354,5 +375,15 @@ EIGEN_DECLARE_TEST(numext) {
CALL_SUBTEST(check_signbit<int16_t>());
CALL_SUBTEST(check_signbit<int32_t>());
CALL_SUBTEST(check_signbit<int64_t>());
CALL_SUBTEST(check_shift<int8_t>());
CALL_SUBTEST(check_shift<int16_t>());
CALL_SUBTEST(check_shift<int32_t>());
CALL_SUBTEST(check_shift<int64_t>());
CALL_SUBTEST(check_shift<uint8_t>());
CALL_SUBTEST(check_shift<uint16_t>());
CALL_SUBTEST(check_shift<uint32_t>());
CALL_SUBTEST(check_shift<uint64_t>());
}
}