Skip to content

Commit a2e30d6

Browse files
committed
generate using lane literals
1 parent 332bd67 commit a2e30d6

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

inc/zoo/swar/SWAR.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "zoo/meta/log.h"
66

77
#include <type_traits>
8+
#include <initializer_list>
89

910
#ifdef _MSC_VER
1011
#include <iso646.h>
@@ -70,6 +71,19 @@ struct SWAR {
7071

7172
constexpr T value() const noexcept { return m_v; }
7273

74+
constexpr static T baseFromLaneLiterals(std::initializer_list<T> args) noexcept {
75+
T result = 0;
76+
for (auto arg: args) {
77+
result = (result << NBits) | arg;
78+
}
79+
return result;
80+
}
81+
82+
constexpr static SWAR fromLaneLiterals(std::initializer_list<T> args) noexcept {
83+
return SWAR(baseFromLaneLiterals(args));
84+
}
85+
86+
7387
#define SWAR_UNARY_OPERATORS_X_LIST \
7488
X(SWAR, ~)
7589
//constexpr SWAR operator~() const noexcept { return SWAR{~m_v}; }

test/swar/BasicOperations.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@ static_assert(
4141
multiplication_OverflowUnsafe_SpecificBitCount<3>(Micand, Mplier).value()
4242
);
4343

44+
static_assert(0b00000010000000110000010100000110 == 0x02'03'05'06);
45+
4446
TEST_CASE("Jamie's totally working exponentiation :D") {
45-
constexpr auto base = SWAR<8, u32>{0x02'01'05'06}; // 2 | 1 | 5 | 6
46-
constexpr auto exponent = SWAR<8, u32>{0x07'00'02'03}; // 7 | 0 | 2 | 3
47-
constexpr auto expected = SWAR<8, u32>{0x80'01'19'D8}; // 128 | 1 | 25 | 216
47+
constexpr auto base = SWAR<8, u32>::fromLaneLiterals({2, 3, 5, 6}); // {(2 << 24) + (3 << 16) + (5 << 8) + (6)};
48+
constexpr auto exponent = SWAR<8, u32>::fromLaneLiterals({7, 0, 2, 3}); // 7 | 0 | 2 | 3
49+
constexpr auto expected = SWAR<8, u32>::fromLaneLiterals({128, 1, 25, 216}); // 128 | 1 | 25 | 216
4850
constexpr auto actual = exponentiation_OverflowUnsafe(base, exponent);
4951
static_assert(expected.value() == actual.value());
5052
CHECK(expected.value() == actual.value());

0 commit comments

Comments
 (0)