Skip to content

Commit

Permalink
Branchfree bitmask calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
kimwalisch committed Jul 28, 2024
1 parent aacc5a0 commit 2d00ab3
Showing 1 changed file with 25 additions and 24 deletions.
49 changes: 25 additions & 24 deletions include/Sieve.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,32 +255,33 @@ class Sieve
uint64_t m2 = unset_larger[stop % 240];
const uint64_t* sieve64 = (const uint64_t*) sieve_.data();

if (start_idx == stop_idx)
return popcnt64(sieve64[start_idx] & m1 & m2);
else
{
uint64_t i = start_idx + 1;
uint64_t start_bits = sieve64[start_idx] & m1;
uint64_t stop_bits = sieve64[stop_idx] & m2;
uint64_t cnt = popcnt64(start_bits);
cnt += popcnt64(stop_bits);
svuint64_t vcnt = svdup_u64(0);

// Compute this for loop using ARM SVE.
// for (i = start_idx + 1; i < stop_idx; i++)
// cnt += popcnt64(sieve64[i]);
do {
svbool_t pg = svwhilelt_b64(i, stop_idx);
svuint64_t vec = svld1_u64(pg, &sieve64[i]);
vec = svcnt_u64_z(pg, vec);
vcnt = svadd_u64_x(svptrue_b64(), vcnt, vec);
i += svcntd();
}
while (i < stop_idx);
cnt += svaddv_u64(svptrue_b64(), vcnt);
// Branchfree bitmask calculation:
// m1 = (start_idx != stop_idx) ? m1 : m1 & m2;
m1 = (m1 * (start_idx != stop_idx)) | ((m1 & m2) * (start_idx == stop_idx));
// m2 = (start_idx != stop_idx) ? m2 : 0;
m2 *= (start_idx != stop_idx);

return cnt;
uint64_t i = start_idx + 1;
uint64_t start_bits = sieve64[start_idx] & m1;
uint64_t stop_bits = sieve64[stop_idx] & m2;
uint64_t cnt = popcnt64(start_bits);
cnt += popcnt64(stop_bits);
svuint64_t vcnt = svdup_u64(0);

// Compute this for loop using ARM SVE.
// for (i = start_idx + 1; i < stop_idx; i++)
// cnt += popcnt64(sieve64[i]);
do {
svbool_t pg = svwhilelt_b64(i, stop_idx);
svuint64_t vec = svld1_u64(pg, &sieve64[i]);
vec = svcnt_u64_z(pg, vec);
vcnt = svadd_u64_x(svptrue_b64(), vcnt, vec);
i += svcntd();
}
while (i < stop_idx);
cnt += svaddv_u64(svptrue_b64(), vcnt);

return cnt;
}

#endif
Expand Down

0 comments on commit 2d00ab3

Please sign in to comment.