Skip to content

Commit

Permalink
Fix overflow on huge sieves
Browse files Browse the repository at this point in the history
  • Loading branch information
danaj committed Jan 31, 2018
1 parent d1e0575 commit f7354b2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
13 changes: 13 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
Revision history for Perl module Math::Prime::Util

0.71 2018-?

[FIXES]

- Monolithic sieves beyond 30*2^32 (~ 1.2 * 10^11) overflowed.

[OTHER]

- Added ability to use -DBENCH_SEG for benchmarking sieves using
ntheory::_segment_pi without table optimizations.



0.70 2017-12-02

[FIXES]
Expand Down
2 changes: 2 additions & 0 deletions prime_nth_count.c
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ UV segment_prime_count(UV low, UV high)

if (low > high) return count;

#if !defined(BENCH_SEGCOUNT)
if (low == 7 && high <= 30*NPRIME_SIEVE30) {
count += count_segment_ranged(prime_sieve30, NPRIME_SIEVE30, low, high);
return count;
Expand All @@ -285,6 +286,7 @@ UV segment_prime_count(UV low, UV high)
* to sieve all the rest. We should be using LMO or Lehmer much earlier. */
#ifdef APPLY_TABLES
APPLY_TABLES
#endif
#endif

low_d = low/30;
Expand Down
6 changes: 3 additions & 3 deletions sieve.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ static const unsigned char qinit30[30] =

typedef struct {
uint32_t prime;
uint32_t offset;
uint8_t index;
UV offset;
uint8_t index;
} wheel_t;

#define CROSS_INDEX(v, b0,b1,b2,b3,b4,b5,b6,b7, i0,i1,i2,i3,i4,i5,i6,i7, it) \
Expand Down Expand Up @@ -216,7 +216,7 @@ static wheel_t create_wheel(UV startp, uint32_t prime)
return w;
}

static void mark_primes(unsigned char* s, uint32_t bytes, wheel_t* w)
static void mark_primes(unsigned char* s, UV bytes, wheel_t* w)
{
if (w->offset >= bytes) {
w->offset -= bytes;
Expand Down

0 comments on commit f7354b2

Please sign in to comment.