Skip to content

Commit fcbb144

Browse files
committed
test both mod computation branches
1 parent 512b51c commit fcbb144

File tree

4 files changed

+60
-40
lines changed

4 files changed

+60
-40
lines changed

src/main/java/de/tilman_neumann/jml/factor/siqs/sieve/Sieve03h.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -510,16 +510,15 @@ private SmoothCandidate tdivUnsievedPrimeBaseElements(BigInteger A, BigInteger Q
510510
xModP = (int) ( ((long)x) - q * p);
511511
if (xModP<0) xModP += p;
512512
else if (xModP>=p) xModP -= p;
513-
if (DEBUG) {
514-
// 0 <= xModP < p
515-
Ensure.ensureSmallerEquals(0, xModP);
516-
Ensure.ensureSmaller(xModP, p);
517-
518-
int xModP2 = x % p;
519-
if (xModP2<0) xModP2 += p;
520-
if (xModP != xModP2) LOG.debug("x=" + x + ", p=" + p + ": xModP=" + xModP + ", but xModP2=" + xModP2);
521-
Ensure.ensureEquals(xModP2, xModP);
522-
}
513+
}
514+
if (DEBUG) {
515+
// 0 <= xModP < p
516+
Ensure.ensureSmallerEquals(0, xModP);
517+
Ensure.ensureSmaller(xModP, p);
518+
// compare with correct but slower mod computation
519+
int correctMod = correctMod(x, p);
520+
if (xModP != correctMod) LOG.debug("x=" + x + ", p=" + p + ": xModP=" + xModP + ", but correctMod=" + correctMod);
521+
Ensure.ensureEquals(correctMod, xModP);
523522
}
524523
if (xModP==x1Array[pIndex] || xModP==x2Array[pIndex]) {
525524
pass2Primes[pass2Count] = primes[pIndex];
@@ -574,6 +573,12 @@ private SmoothCandidate tdivUnsievedPrimeBaseElements(BigInteger A, BigInteger Q
574573
return smoothCandidate;
575574
}
576575

576+
private static final int correctMod(int x, int p) {
577+
int mod = x % p;
578+
// x < 0 then mod < 0, fix that
579+
return mod < 0 ? mod + p : mod;
580+
}
581+
577582
@Override
578583
public SieveReport getReport() {
579584
return new SieveReport(sieveHitCount, initDuration, sieveDuration, collectDuration);

src/main/java/de/tilman_neumann/jml/factor/siqs/sieve/Sieve03hU.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -621,16 +621,15 @@ private SmoothCandidate tdivUnsievedPrimeBaseElements(BigInteger A, BigInteger Q
621621
xModP = (int) ( ((long)x) - q * p);
622622
if (xModP<0) xModP += p;
623623
else if (xModP>=p) xModP -= p;
624-
if (DEBUG) {
625-
// 0 <= xModP < p
626-
Ensure.ensureSmallerEquals(0, xModP);
627-
Ensure.ensureSmaller(xModP, p);
628-
629-
int xModP2 = x % p;
630-
if (xModP2<0) xModP2 += p;
631-
if (xModP != xModP2) LOG.debug("x=" + x + ", p=" + p + ": xModP=" + xModP + ", but xModP2=" + xModP2);
632-
Ensure.ensureEquals(xModP2, xModP);
633-
}
624+
}
625+
if (DEBUG) {
626+
// 0 <= xModP < p
627+
Ensure.ensureSmallerEquals(0, xModP);
628+
Ensure.ensureSmaller(xModP, p);
629+
// compare with correct but slower mod computation
630+
int correctMod = correctMod(x, p);
631+
if (xModP != correctMod) LOG.debug("x=" + x + ", p=" + p + ": xModP=" + xModP + ", but correctMod=" + correctMod);
632+
Ensure.ensureEquals(correctMod, xModP);
634633
}
635634
if (xModP==x1Array[pIndex] || xModP==x2Array[pIndex]) {
636635
pass2Primes[pass2Count] = primes[pIndex];
@@ -685,6 +684,12 @@ private SmoothCandidate tdivUnsievedPrimeBaseElements(BigInteger A, BigInteger Q
685684
return smoothCandidate;
686685
}
687686

687+
private static final int correctMod(int x, int p) {
688+
int mod = x % p;
689+
// x < 0 then mod < 0, fix that
690+
return mod < 0 ? mod + p : mod;
691+
}
692+
688693
@Override
689694
public SieveReport getReport() {
690695
return new SieveReport(sieveHitCount, initDuration, sieveDuration, collectDuration);

src/main/java/de/tilman_neumann/jml/factor/siqs/sieve/SingleBlockSieve.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -511,16 +511,15 @@ private SmoothCandidate tdivUnsievedPrimeBaseElements(BigInteger A, BigInteger Q
511511
xModP = (int) ( ((long)x) - q * p);
512512
if (xModP<0) xModP += p;
513513
else if (xModP>=p) xModP -= p;
514-
if (DEBUG) {
515-
// 0 <= xModP < p
516-
Ensure.ensureSmallerEquals(0, xModP);
517-
Ensure.ensureSmaller(xModP, p);
518-
519-
int xModP2 = x % p;
520-
if (xModP2<0) xModP2 += p;
521-
if (xModP != xModP2) LOG.debug("x=" + x + ", p=" + p + ": xModP=" + xModP + ", but xModP2=" + xModP2);
522-
Ensure.ensureEquals(xModP2, xModP);
523-
}
514+
}
515+
if (DEBUG) {
516+
// 0 <= xModP < p
517+
Ensure.ensureSmallerEquals(0, xModP);
518+
Ensure.ensureSmaller(xModP, p);
519+
// compare with correct but slower mod computation
520+
int correctMod = correctMod(x, p);
521+
if (xModP != correctMod) LOG.debug("x=" + x + ", p=" + p + ": xModP=" + xModP + ", but correctMod=" + correctMod);
522+
Ensure.ensureEquals(correctMod, xModP);
524523
}
525524
if (xModP==x1Array[pIndex] || xModP==x2Array[pIndex]) {
526525
pass2Primes[pass2Count] = primes[pIndex];
@@ -574,6 +573,12 @@ private SmoothCandidate tdivUnsievedPrimeBaseElements(BigInteger A, BigInteger Q
574573
smoothCandidate.QRest = Q_rest_UBI.toBigInteger();
575574
return smoothCandidate;
576575
}
576+
577+
private static final int correctMod(int x, int p) {
578+
int mod = x % p;
579+
// x < 0 then mod < 0, fix that
580+
return mod < 0 ? mod + p : mod;
581+
}
577582

578583
@Override
579584
public SieveReport getReport() {

src/main/java/de/tilman_neumann/jml/factor/siqs/sieve/SingleBlockSieveU.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -621,16 +621,15 @@ private SmoothCandidate tdivUnsievedPrimeBaseElements(BigInteger A, BigInteger Q
621621
xModP = (int) ( ((long)x) - q * p);
622622
if (xModP<0) xModP += p;
623623
else if (xModP>=p) xModP -= p;
624-
if (DEBUG) {
625-
// 0 <= xModP < p
626-
Ensure.ensureSmallerEquals(0, xModP);
627-
Ensure.ensureSmaller(xModP, p);
628-
629-
int xModP2 = x % p;
630-
if (xModP2<0) xModP2 += p;
631-
if (xModP != xModP2) LOG.debug("x=" + x + ", p=" + p + ": xModP=" + xModP + ", but xModP2=" + xModP2);
632-
Ensure.ensureEquals(xModP2, xModP);
633-
}
624+
}
625+
if (DEBUG) {
626+
// 0 <= xModP < p
627+
Ensure.ensureSmallerEquals(0, xModP);
628+
Ensure.ensureSmaller(xModP, p);
629+
// compare with correct but slower mod computation
630+
int correctMod = correctMod(x, p);
631+
if (xModP != correctMod) LOG.debug("x=" + x + ", p=" + p + ": xModP=" + xModP + ", but correctMod=" + correctMod);
632+
Ensure.ensureEquals(correctMod, xModP);
634633
}
635634
if (xModP==x1Array[pIndex] || xModP==x2Array[pIndex]) {
636635
pass2Primes[pass2Count] = primes[pIndex];
@@ -684,6 +683,12 @@ private SmoothCandidate tdivUnsievedPrimeBaseElements(BigInteger A, BigInteger Q
684683
smoothCandidate.QRest = Q_rest_UBI.toBigInteger();
685684
return smoothCandidate;
686685
}
686+
687+
private static final int correctMod(int x, int p) {
688+
int mod = x % p;
689+
// x < 0 then mod < 0, fix that
690+
return mod < 0 ? mod + p : mod;
691+
}
687692

688693
@Override
689694
public SieveReport getReport() {

0 commit comments

Comments
 (0)