Skip to content

jp/horizontal sum #100

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

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions inc/zoo/meta/popcount.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#define ZOO_HEADER_META_POPCOUNT_H

#include "zoo/meta/BitmaskMaker.h"
#include <cstdint>
#include <type_traits>

namespace zoo { namespace meta {

Expand Down Expand Up @@ -90,6 +92,30 @@ struct PopcountIntrinsic {
};
#endif

template<typename T>
constexpr auto NumBits() {
return sizeof(T) * 8;
}
static_assert(NumBits<uint16_t>() == 16);
static_assert(NumBits<uint64_t>() == 64);

template <typename T = std::uint64_t>
constexpr
std::enable_if_t<std::is_unsigned_v<T> && NumBits<T>() <= 64, T>
basic_popcount(T x) {
if constexpr (NumBits<T>() <= 32) {
return __builtin_popcountl(x);
} else {
return __builtin_popcountll(x);
}
}

static_assert(basic_popcount<uint64_t>(0b111) == 3);
static_assert(basic_popcount<uint64_t>(0xFF) == 8);
static_assert(basic_popcount<uint64_t>(0xFF'FF'FF'FF) == 32);
static_assert(basic_popcount<uint64_t>(0xFF'FF'FF'FF'FF'FF'FF'FF) == 64);
static_assert(basic_popcount<uint64_t>(0xFF'FF'FF'FF'FF'FF'FF'FF - 2 - 4 - 8) == 61);

}}

#endif
20 changes: 17 additions & 3 deletions inc/zoo/swar/SWAR.h
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ constexpr auto broadcast(SWAR<NBits, T> v) {

/// BooleanSWAR treats the MSB of each SWAR lane as the boolean associated with that lane.
template<int NBits, typename T>
struct BooleanSWAR: SWAR<NBits, T> {
struct BooleanSWAR : SWAR<NBits, T> {
using Base = SWAR<NBits, T>;

template<std::size_t N, typename = std::enable_if_t<Base::Lanes == N>>
Expand All @@ -308,8 +308,13 @@ struct BooleanSWAR: SWAR<NBits, T> {
{ this->m_v <<= (NBits - 1); }

// Booleanness is stored in the MSBs
static constexpr auto MaskMSB =
broadcast<NBits, T>(Base(T(1) << (NBits -1)));
static constexpr auto MaskMSB = []{
if constexpr (SWAR<NBits, T>::Lanes == 1) {
return Base(T{~0}); // all on, no lanes
}
return broadcast<NBits, T>(Base(T(1) << (NBits - 1)));
}();

static constexpr auto AllTrue = MaskMSB;
static constexpr auto MaskLSB =
broadcast<NBits, T>(Base(T(1)));
Expand Down Expand Up @@ -392,6 +397,11 @@ BooleanSWAR(
const bool (&values)[BooleanSWAR<NBits, T>::Lanes]
) -> BooleanSWAR<NBits, T>;

template <int NBits, typename T>
BooleanSWAR(
SWAR<NBits, T> arg
) -> BooleanSWAR<NBits, T>;

template<int NBits, typename T>
constexpr BooleanSWAR<NBits, T>
convertToBooleanSWAR(SWAR<NBits, T> arg) noexcept {
Expand Down Expand Up @@ -586,6 +596,10 @@ constexpr SWAR<NBits, T> logarithmFloor(SWAR<NBits, T> v) noexcept {
return SWAR<NBits, T>{popcounts - ones};
}





static_assert(
logarithmFloor(SWAR<8>{0x8040201008040201ull}).value() ==
0x0706050403020100ull
Expand Down
Loading
Loading