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

Walk prebuilt mask through memory for small steps #984

Merged
merged 1 commit into from
Nov 6, 2024
Merged
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
49 changes: 43 additions & 6 deletions PrimeCPP/solution_2/PrimeCPP_PAR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,51 @@ class BitArray {
return (x<<n) | (x>>(32-n));
}

static constexpr uint32_t buildSkipMask(size_t skip, size_t offset)
{
uint32_t mask = 0;
for (size_t i = offset; i < 32; i += skip) {
mask |= (1u << i);
}
return ~mask;
}

void setFlagsFalse(size_t n, size_t skip)
{
auto rolling_mask = ~uint32_t(1 << n % 32);
auto roll_bits = skip % 32;
while (n < arrSize) {
array[index(n)] &= rolling_mask;
n += skip;
rolling_mask = rol(rolling_mask, roll_bits);
if (skip <= 12) {
// For small skips, use pre-built mask approach
size_t word_idx = index(n);
size_t bit_pos = n % 32;
size_t curr_n = n;

while (curr_n < arrSize)
{
// Build mask for current word starting at bit_pos
uint32_t mask = buildSkipMask(skip, bit_pos);

// Apply mask to current word
array[word_idx] &= mask;

// Move to next word
size_t bits_remaining = 32 - bit_pos;
curr_n += ((bits_remaining + skip - 1) / skip) * skip;

if (curr_n >= arrSize) break;

word_idx = index(curr_n);
bit_pos = curr_n % 32;
}
}
else
{
// Original implementation for larger skips
auto rolling_mask = ~uint32_t(1 << (n % 32));
auto roll_bits = skip % 32;
while (n < arrSize) {
array[index(n)] &= rolling_mask;
n += skip;
rolling_mask = rol(rolling_mask, roll_bits);
}
}
}

Expand Down
Loading