Skip to content

Instantly share code, notes, and snippets.

@jweinst1
Created February 16, 2026 21:28
Show Gist options
  • Select an option

  • Save jweinst1/0313f19f73cd09cca542fc242c05bc2d to your computer and use it in GitHub Desktop.

Select an option

Save jweinst1/0313f19f73cd09cca542fc242c05bc2d to your computer and use it in GitHub Desktop.
calculates mean via pop count approximation
struct ConstexprBitset64 {
uint64_t block = 0;
constexpr void set(size_t idx) {
block |= uint64_t{1} << idx;
}
constexpr void clear(size_t idx) {
block &= ~(uint64_t{1} << idx);
}
constexpr bool test(size_t idx) const {
return (block >> idx) & 1u;
}
// mean approximate functions
constexpr size_t getMeanApprox64() const {
// Uses 64 bit approx
return __builtin_popcountll(block) >> 1;
}
constexpr size_t count() const {
return __builtin_popcountll(block);
}
};
struct BitSet256 {
ConstexprBitset64 bins[4];
constexpr size_t count() const {
return bins[0].count() +
bins[1].count() +
bins[2].count() +
bins[3].count();
}
constexpr size_t mean() const {
return bins[0].getMeanApprox64() +
bins[1].getMeanApprox64() +
bins[2].getMeanApprox64() +
bins[3].getMeanApprox64();
}
constexpr void set(size_t idx) {
const size_t block = idx >> 6;
const size_t offset = idx & 63;
bins[block].set(offset);
}
constexpr long blockBasedDeviation64(size_t point) const {
const size_t block = point >> 6;
const long myCount = bins[block].count();
return (myCount - (this->count())) + myCount;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment