Skip to content

Commit

Permalink
test both mod computation branches
Browse files Browse the repository at this point in the history
  • Loading branch information
TilmanNeumann committed Feb 3, 2025
1 parent 512b51c commit fcbb144
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 40 deletions.
25 changes: 15 additions & 10 deletions src/main/java/de/tilman_neumann/jml/factor/siqs/sieve/Sieve03h.java
Original file line number Diff line number Diff line change
Expand Up @@ -510,16 +510,15 @@ private SmoothCandidate tdivUnsievedPrimeBaseElements(BigInteger A, BigInteger Q
xModP = (int) ( ((long)x) - q * p);
if (xModP<0) xModP += p;
else if (xModP>=p) xModP -= p;
if (DEBUG) {
// 0 <= xModP < p
Ensure.ensureSmallerEquals(0, xModP);
Ensure.ensureSmaller(xModP, p);

int xModP2 = x % p;
if (xModP2<0) xModP2 += p;
if (xModP != xModP2) LOG.debug("x=" + x + ", p=" + p + ": xModP=" + xModP + ", but xModP2=" + xModP2);
Ensure.ensureEquals(xModP2, xModP);
}
}
if (DEBUG) {
// 0 <= xModP < p
Ensure.ensureSmallerEquals(0, xModP);
Ensure.ensureSmaller(xModP, p);
// compare with correct but slower mod computation
int correctMod = correctMod(x, p);
if (xModP != correctMod) LOG.debug("x=" + x + ", p=" + p + ": xModP=" + xModP + ", but correctMod=" + correctMod);
Ensure.ensureEquals(correctMod, xModP);
}
if (xModP==x1Array[pIndex] || xModP==x2Array[pIndex]) {
pass2Primes[pass2Count] = primes[pIndex];
Expand Down Expand Up @@ -574,6 +573,12 @@ private SmoothCandidate tdivUnsievedPrimeBaseElements(BigInteger A, BigInteger Q
return smoothCandidate;
}

private static final int correctMod(int x, int p) {
int mod = x % p;
// x < 0 then mod < 0, fix that
return mod < 0 ? mod + p : mod;
}

@Override
public SieveReport getReport() {
return new SieveReport(sieveHitCount, initDuration, sieveDuration, collectDuration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -621,16 +621,15 @@ private SmoothCandidate tdivUnsievedPrimeBaseElements(BigInteger A, BigInteger Q
xModP = (int) ( ((long)x) - q * p);
if (xModP<0) xModP += p;
else if (xModP>=p) xModP -= p;
if (DEBUG) {
// 0 <= xModP < p
Ensure.ensureSmallerEquals(0, xModP);
Ensure.ensureSmaller(xModP, p);

int xModP2 = x % p;
if (xModP2<0) xModP2 += p;
if (xModP != xModP2) LOG.debug("x=" + x + ", p=" + p + ": xModP=" + xModP + ", but xModP2=" + xModP2);
Ensure.ensureEquals(xModP2, xModP);
}
}
if (DEBUG) {
// 0 <= xModP < p
Ensure.ensureSmallerEquals(0, xModP);
Ensure.ensureSmaller(xModP, p);
// compare with correct but slower mod computation
int correctMod = correctMod(x, p);
if (xModP != correctMod) LOG.debug("x=" + x + ", p=" + p + ": xModP=" + xModP + ", but correctMod=" + correctMod);
Ensure.ensureEquals(correctMod, xModP);
}
if (xModP==x1Array[pIndex] || xModP==x2Array[pIndex]) {
pass2Primes[pass2Count] = primes[pIndex];
Expand Down Expand Up @@ -685,6 +684,12 @@ private SmoothCandidate tdivUnsievedPrimeBaseElements(BigInteger A, BigInteger Q
return smoothCandidate;
}

private static final int correctMod(int x, int p) {
int mod = x % p;
// x < 0 then mod < 0, fix that
return mod < 0 ? mod + p : mod;
}

@Override
public SieveReport getReport() {
return new SieveReport(sieveHitCount, initDuration, sieveDuration, collectDuration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -511,16 +511,15 @@ private SmoothCandidate tdivUnsievedPrimeBaseElements(BigInteger A, BigInteger Q
xModP = (int) ( ((long)x) - q * p);
if (xModP<0) xModP += p;
else if (xModP>=p) xModP -= p;
if (DEBUG) {
// 0 <= xModP < p
Ensure.ensureSmallerEquals(0, xModP);
Ensure.ensureSmaller(xModP, p);

int xModP2 = x % p;
if (xModP2<0) xModP2 += p;
if (xModP != xModP2) LOG.debug("x=" + x + ", p=" + p + ": xModP=" + xModP + ", but xModP2=" + xModP2);
Ensure.ensureEquals(xModP2, xModP);
}
}
if (DEBUG) {
// 0 <= xModP < p
Ensure.ensureSmallerEquals(0, xModP);
Ensure.ensureSmaller(xModP, p);
// compare with correct but slower mod computation
int correctMod = correctMod(x, p);
if (xModP != correctMod) LOG.debug("x=" + x + ", p=" + p + ": xModP=" + xModP + ", but correctMod=" + correctMod);
Ensure.ensureEquals(correctMod, xModP);
}
if (xModP==x1Array[pIndex] || xModP==x2Array[pIndex]) {
pass2Primes[pass2Count] = primes[pIndex];
Expand Down Expand Up @@ -574,6 +573,12 @@ private SmoothCandidate tdivUnsievedPrimeBaseElements(BigInteger A, BigInteger Q
smoothCandidate.QRest = Q_rest_UBI.toBigInteger();
return smoothCandidate;
}

private static final int correctMod(int x, int p) {
int mod = x % p;
// x < 0 then mod < 0, fix that
return mod < 0 ? mod + p : mod;
}

@Override
public SieveReport getReport() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -621,16 +621,15 @@ private SmoothCandidate tdivUnsievedPrimeBaseElements(BigInteger A, BigInteger Q
xModP = (int) ( ((long)x) - q * p);
if (xModP<0) xModP += p;
else if (xModP>=p) xModP -= p;
if (DEBUG) {
// 0 <= xModP < p
Ensure.ensureSmallerEquals(0, xModP);
Ensure.ensureSmaller(xModP, p);

int xModP2 = x % p;
if (xModP2<0) xModP2 += p;
if (xModP != xModP2) LOG.debug("x=" + x + ", p=" + p + ": xModP=" + xModP + ", but xModP2=" + xModP2);
Ensure.ensureEquals(xModP2, xModP);
}
}
if (DEBUG) {
// 0 <= xModP < p
Ensure.ensureSmallerEquals(0, xModP);
Ensure.ensureSmaller(xModP, p);
// compare with correct but slower mod computation
int correctMod = correctMod(x, p);
if (xModP != correctMod) LOG.debug("x=" + x + ", p=" + p + ": xModP=" + xModP + ", but correctMod=" + correctMod);
Ensure.ensureEquals(correctMod, xModP);
}
if (xModP==x1Array[pIndex] || xModP==x2Array[pIndex]) {
pass2Primes[pass2Count] = primes[pIndex];
Expand Down Expand Up @@ -684,6 +683,12 @@ private SmoothCandidate tdivUnsievedPrimeBaseElements(BigInteger A, BigInteger Q
smoothCandidate.QRest = Q_rest_UBI.toBigInteger();
return smoothCandidate;
}

private static final int correctMod(int x, int p) {
int mod = x % p;
// x < 0 then mod < 0, fix that
return mod < 0 ? mod + p : mod;
}

@Override
public SieveReport getReport() {
Expand Down

0 comments on commit fcbb144

Please sign in to comment.