Skip to content

perf(cpp): add nsieve 3.cpp (Wheel 8-of-30 Sieve) - #504

Open
bonnhatnguyen wants to merge 1 commit into
hanabi1224:mainfrom
bonnhatnguyen:cpp-nsieve-wheel-3
Open

bonnhatnguyen wants to merge 1 commit into
hanabi1224:mainfrom
bonnhatnguyen:cpp-nsieve-wheel-3

Conversation

@bonnhatnguyen

@bonnhatnguyen bonnhatnguyen commented Sep 28, 2026 •

Copy link
Copy Markdown

Description

Introduces a high-performance C++ implementation (3.cpp) for the nsieve benchmark utilizing a 1-bit Wheel 8-of-30 Sieve of Eratosthenes.

Key Optimizations

  • Wheel 8-of-30 Factorization: Skips all multiples of 2, 3, and 5 (8 candidate coprimes per cycle of 30: 1, 7, 11, 13, 17, 19, 23, 29). This eliminates ~73.3% of composite markings before sieving starts.
  • Bit-Packed Candidate Buffer: Uses 1 bit per odd integer (uint64_t words). For $M = 40,960,000$, total memory footprint is only ~2.44 MB, fitting entirely inside CPU L2/L3 cache and avoiding DRAM bandwidth bottlenecks.
  • Register-Unrolled Composite Marking: 8-stride pre-computed step cycle unrolled to maximize instruction-level parallelism (ILP) and branch prediction throughput.
  • Unrolled Branchless Prime Counting: Traverses remaining prime candidates across wheel residues with branchless bit extractions.

Verification & Benchmark

  • Verified against unit tests (4_out and 5_out), output matches byte-for-byte.
  • Benchmark comparison on input 12 ($M = 40,960,000$):
    • 1.cpp (baseline): ~0.37s user time
    • 3.cpp (this PR): ~0.04s user time (~9.2x faster)

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.

1 participant