TGViewer
C++ - Reddit C++ - Reddit @r_cpp · 229 subscribers
Post #25681 6
pilot_v1 sin10 | 38.28 | 14.30\* | 11.18\* | 9.18\* | 7.75\* | 6.73\* | 5.94\* | 5.31\* | 4.78 | 4.36 | 4.02 |
| pilot_v2 sin10 | 76.60\* | 14.16 | 11.09 | 9.11 | 7.73 | 6.70 | 5.92 | 5.31\* | 4.82 | 4.84 | 4.84 |
| BSL sin10 | 4.85 | 4.85 | 4.84 | 4.85 | 4.84 | 4.84 | 4.85 | 4.85 | 4.85\* | 4.85\* | 4.85\* |

At thd = 0, Skip gives a huge win. For high thd, bsl is selected correctly.
But now on sparse masks, expand for empty registers is wasted.

### pilot v3

New detour version: during compress, we'll store only the indices of non-empty registers (into the `idx` buffer).
And expand will iterate over them:

template <bool Skip>
size_t detour_compact(float* dst, const size_t n, const auto w, auto f, auto mask) {
float* ptr = tmp.data();
size_t k = 0;
for (size_t i = 0; i < n; i += 16) {
// ... same as detour

if constexpr (Skip)
if (vmaxvq_u32(vaddq_u32(vaddq_u32(m[0], m[1]), vaddq_u32(m[2], m[3]))) == 0) continue;

std::array<uint32_t, 4> sk;
for (size_t j = 0; j < 4; ++j) sk[j] = vaddvq_u32(vandq_u32(m[j], w));
for (size_t j = 0; j < 4; ++j) {
s[k] = sk[j];
idx[k] = i + 4 * j;
k += bool(sk[j]);
}
// ... same as detour
}
// ... same as detour

for (size_t j = 0; j < 3; ++j) s[k + j] = 0, idx[k + j] = 0;
// ... same as detour
k = (k + 3) & ~size_t(3);
for (size_t i = 0; i < k; i += 4) {
std::array<uint32_t, 4> sk;
for (size_t j = 0; j < 4; ++j) sk[j] = s[i + j];
std::array<float32x4_t, 4> a;
for (size_t j = 0; j < 4; ++j) a[j] = vld1q_f32(dst + idx[i + j]);
// ... same as detour
for (size_t j = 0; j < 4; ++j)
vst1q_f32(dst + idx[i + j], vreinterpretq_f32_u8(vqtbl2q_u8(tbl[j], index[j])));
ptr += off[3] + (sk[3] >> 4);
}

return size;
}

k (number of non-empty registers) is rounded up to a multiple of 4 before expand,
so the unrolled loop has no tail left.

No branches in the hot loops: they'd kill speed, so compress runs on all four registers.
Instead of branches, the position of the current element is advanced by `bool(sk)`.

detour_compact wins when at least 50% of all registers are empty. The density is approximately 0.16 (`(1 - x) ^ 4 = 0.5`).

void pilot_v3(float* dst, const size_t n, auto f, auto mask) {
// ... same as v2
constexpr size_t lo = 0.16 * probe;
for (size_t i = 2 * tile; i < n; i += tile) {
const long long cnt = density(dst, probe, mask);
if (cnt > hi) {
if (cnt < xlo)
bsl<true>(dst, tile, f, mask);
else
bsl<false>(dst, tile, f, mask);
} else if (cnt < xlo)
detour_compact<true>(dst, tile, w, f, mask);
else if (cnt < lo)
detour_compact<false>(dst, tile, w, f, mask);
else
detour<false>(dst, tile, w, f, mask);
dst += tile;
}
}

And v3 is noticeably faster at low density:

| thd | 0 | 0.02 | 0.04 | 0.06 | 0.08 | 0.1 | 0.12 | 0.14 | 0.16 | 0.18 | 0.2 |
|:---------------|--------:|--------:|--------:|--------:|--------:|--------:|--------:|--------:|--------:|--------:|--------:|
| pilot_v2 sin35 | 77.29\* | 18.67 | 18.40 | 17.70 | 17.18 | 16.66 | 16.15 | 15.68 | 15.24\* | 14.82\* | 14.43\* |
| pilot_v3 sin35 | 77.26 | 23.71\* | 22.19\* | 20.48\* | 19.03\* | 17.82\* | 16.73\* | 15.76\* | 15.16 | 14.74 | 14.38 |
| pilot_v2 pow10 | 72.88 | 13.93 | 11.32 | 9.38 | 8.00 | 6.97 | 6.18 | 5.54 | 5.02\* | 4.59\* | 4.24\* |
| pilot_v3 pow10 | 73.71\* | 16.54\* | 12.63\* | 10.07\* | 8.39\* | 7.17\* | 6.26\* | 5.55\* | 5.02\* | 4.59\* | 4.24\* |

Now the algo is fast,
More from @r_cpp
  1. Sep 26, 2026Token Sequence Injection & Modern Macros: The Most Game-Changing Compile-Time Feature in C…
  2. Sep 25, 2026myStringStream.str("") Considered Harmful Under C++20 I was recently looking at some (rath…
  3. Sep 20, 2026A clever branch free optimization I'm the developer of memlz which is an extremely fast co…
  4. Sep 15, 2026Inside Boost.PolyCollection https://bannalia.blogspot.com/2026/09/inside-boostpolycollecti…
  5. Sep 11, 2026C++26: Standard Library Hardening Experiments https://www.cppstories.com/2026/hardening-ex…
  6. Sep 11, 2026MSVC C++23: constexpr cmath with LLVM Libc https://devblogs.microsoft.com/cppblog/msvc-c23…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →