Skip to content

[CPP] Add solution 6: 1-bit Wheel 8-of-30 Sieve - #1088

Closed
bonnhatnguyen wants to merge 1 commit into
PlummersSoftwareLLC:drag-racefrom
bonnhatnguyen:prime-cpp-solution-6
Closed

bonnhatnguyen wants to merge 1 commit into
PlummersSoftwareLLC:drag-racefrom
bonnhatnguyen:prime-cpp-solution-6

Conversation

@bonnhatnguyen

Copy link
Copy Markdown

Description

This pull request adds PrimeCPP/solution_6, introducing an ultra-fast, faithful, 1-bit Wheel 8-of-30 Sieve of Eratosthenes implementation in C++17.

Algorithmic Highlights

  • Wheel 8-of-30 Factorization: Skips multiples of 2, 3, and 5. For every 30 numbers, only 8 coprime candidates are traversed ([1, 7, 11, 13, 17, 19, 23, 29]).
  • Register-Resident 8-Stride Cycle: The 8 strides ([3, 2, 1, 2, 1, 2, 3, 1] * factor) fit entirely into CPU registers (rax, rbx, rcx, rdx, r8..r11). The inner loop is fully unrolled by the 8-stride cycle, requiring zero memory lookups, zero modulo, and zero branch mispredictions during sieve marking.
  • Strict L1 Data Cache Residency: Compact 1-bit representation requires ~32.55 KB, fitting 100% inside CPU L1 Data Cache (32 KB - 48 KB) to eliminate L2/L3 cache latency.
  • Strict Faithfulness: Dynamic runtime allocation per sieve pass, full class encapsulation, and verified against 78,498 primes up to 1,000,000.
  • Hardware-Concurrency Parallelism: Seamless multi-threaded scaling across all logical cores.

Benchmark Results (Intel Core i7-12700H, Docker Ubuntu 22.04)

  • Single-threaded: ~8,067 - 8,672 passes/sec (40,336+ passes in 5.00s) — outperforming existing faithful C++ solutions (~10x faster than Solution 1).
  • Multi-threaded (20 threads): ~41,618 - 43,112 passes/sec (208,315+ passes in 5.00s).
ndt0208-cpp-wheel8;40336;5.00021;1;algorithm=wheel,faithful=yes,bits=1
ndt0208-cpp-wheel8-par;208315;5.00534;20;algorithm=wheel,faithful=yes,bits=1

Contributing requirements

  • I read the contribution guidelines in CONTRIBUTING.md.
  • I placed my solution in the correct solution folder.
  • I added a README.md with the right badge(s).
  • I added a Dockerfile that builds and runs my solution.
  • I selected drag-race as the target branch.
  • All code herein is licensed compatible with BSD-3.

@rbergen

rbergen commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

The 8 out of 30 wheel is already included in C++ solution_4. If you think you can improve its implementation then you can open a PR that aims to modify it, per our contibuting guidelines.

I'm closing this PR as a duplicate of an existing implementation.

@rbergen rbergen closed this Oct 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants