Skip to content

Commit 6ac5045

Browse files
committed
Switch to uint8_t, some optimizations
1 parent cf91378 commit 6ac5045

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

PrimeCPP/solution_2/PrimeCPP_PAR.cpp

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,56 +24,61 @@ using namespace std::chrono;
2424
const uint64_t DEFAULT_UPPER_LIMIT = 10'000'000LLU;
2525

2626
class BitArray {
27-
uint32_t *array;
27+
uint8_t *array;
2828
size_t arrSize;
2929

3030
inline static size_t arraySize(size_t size)
3131
{
32-
return (size >> 5) + ((size & 31) > 0);
32+
return (size >> 3) + ((size & 7) > 0);
3333
}
3434

3535
inline static size_t index(size_t n)
3636
{
37-
return (n >> 5);
37+
return (n >> 3);
3838
}
3939

40-
inline static uint32_t getSubindex(size_t n, uint32_t d)
40+
inline static uint8_t getSubindex(size_t n, uint8_t d)
4141
{
42-
return d & uint32_t(uint32_t(0x01) << (n % 32));
42+
return d & uint8_t(uint8_t(0x01) << (n % 8));
4343
}
4444

45-
inline void setFalseSubindex(size_t n, uint32_t &d)
45+
inline void setFalseSubindex(size_t n, uint8_t &d)
4646
{
47-
d &= ~uint32_t(uint32_t(0x01) << (n % (8*sizeof(uint32_t))));
47+
d &= ~uint8_t(uint8_t(0x01) << (n % 8));
4848
}
4949

5050
public:
5151
explicit BitArray(size_t size) : arrSize(size)
5252
{
53-
array = new uint32_t[arraySize(size)];
54-
std::memset(array, 0xFF, (size >> 3) + ((size & 7) > 0));
53+
array = new uint8_t[arraySize(size)];
54+
std::memset(array, 0xFF, arraySize(size));
5555
}
5656

57-
~BitArray() {delete [] array;}
57+
~BitArray() { delete[] array; }
5858

5959
bool get(size_t n) const
6060
{
61-
return getSubindex(n, array[index(n)]);
61+
return (array[index(n)] & (uint8_t(1) << (n % 8))) != 0;
6262
}
6363

64-
static constexpr uint32_t rol(uint32_t x, uint32_t n)
64+
static constexpr uint8_t rol(uint8_t x, uint8_t n)
6565
{
66-
return (x<<n) | (x>>(32-n));
66+
n %= 8;
67+
if (n == 0)
68+
return x;
69+
else
70+
return (x << n) | (x >> (8 - n));
6771
}
6872

6973
void setFlagsFalse(size_t n, size_t skip)
7074
{
71-
auto rolling_mask = ~uint32_t(1 << n % 32);
72-
auto roll_bits = skip % 32;
75+
auto rolling_mask = ~uint8_t(1 << n % 8);
76+
auto roll_bits = skip % 8;
7377
while (n < arrSize) {
7478
array[index(n)] &= rolling_mask;
7579
n += skip;
76-
rolling_mask = rol(rolling_mask, roll_bits);
80+
if (roll_bits != 0)
81+
rolling_mask = rol(rolling_mask, roll_bits);
7782
}
7883
}
7984

0 commit comments

Comments
 (0)