Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Literals Utils #80

Merged
merged 16 commits into from
Jun 10, 2024
15 changes: 6 additions & 9 deletions inc/zoo/swar/SWAR.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,19 @@ struct SWAR {
LowerBits = MostSignificantBit - LeastSignificantBit,
MaxUnsignedLaneValue = LeastSignificantLaneMask;

template <typename U, typename ManipulationFn>
constexpr auto loadIntoLanes(const U (&values)[Lanes], const ManipulationFn&& manipulation) {
template <typename U>
constexpr auto loadIntoLanes(const U (&values)[Lanes]) const noexcept {
Copy link
Owner

@thecppzoo thecppzoo Apr 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rename to from_array. As the symmetrical operation to to_array.
Also, this is a factory, it ought to be static, a class-function, not instance.
Actually, we also need a constructor from std::array.

Apologies for all of the creep, I just think there is a lot of extra value by making SWAR a really ergonomic type as opposed to somewhat ergonomic.

I dislike that C++ forces you to do a lot of boilerplate, I've never figured out how to make tools to eliminate the work to do the boilerplate.

auto result = T{0};
for (auto value : values) {
auto laneValue = manipulation(value);
result = (result << NBits) | laneValue;
result = (result << NBits) | value;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blitElement?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I this does not quite blit the element, if it will move in an element, it shifts up what it got and then adds to the least significant lane

}
return result;
}

template <typename Arg, std::size_t N, typename = std::enable_if_t<N == Lanes, int>>
constexpr
SWAR(Literals_t<NBits, T>, const Arg (&values)[N])
: m_v{loadIntoLanes(values, [](auto x) { return x; })} {}
: m_v{loadIntoLanes(values)} {}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May you please indent to multiples of four spaces?


SWAR() = default;
constexpr explicit SWAR(T v): m_v(v) {}
Expand Down Expand Up @@ -261,10 +260,8 @@ struct BooleanSWAR: SWAR<NBits, T> {
using Base = SWAR<NBits, T>;

template <std::size_t N>
constexpr BooleanSWAR(Literals_t<NBits, T>, const bool (&values)[N]) : Base{0} {
constexpr auto msbOfFirstLane = T{1} << (NBits - 1);
this->m_v = Base::loadIntoLanes(values, [](auto x) { return x ? msbOfFirstLane : 0; });
}
constexpr BooleanSWAR(Literals_t<NBits, T>, const bool (&values)[N])
: Base(Literals<NBits, T>, values) { this->m_v <<= (NBits - 1); }

// Booleanness is stored in the MSBs
static constexpr auto MaskMSB =
Expand Down
2 changes: 0 additions & 2 deletions test/swar/BasicOperations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
#include <ios>
#include <iomanip>
#include <iostream>
#include <sys/wait.h>
#include <type_traits>


using namespace zoo;
using namespace zoo::swar;

Expand Down