Skip to content

Commit

Permalink
performance improvement for N>=32 bit
Browse files Browse the repository at this point in the history
  • Loading branch information
TilmanNeumann committed Jan 25, 2025
1 parent 975dac9 commit 0bdbf1a
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/main/java/de/tilman_neumann/jml/factor/tdiv/TDiv.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ public void searchFactors(FactorArguments args, FactorResult result) {
addToMap(p_i_big, exp*Nexp, primeFactors);
if (N.bitLength() < 63) {
// Check if we are done
long p_i_square = p_i *(long)p_i;
long p_i_square = ((long)p_i) * p_i;
if (p_i_square > N.longValue()) {
if (DEBUG) LOG.debug("N=" + N + " < p^2=" + p_i_square);
if (DEBUG) LOG.debug("N=" + N + " < p^2 = " + p_i_square);
// the remaining N is 1 or prime
if (N.compareTo(I_1)>0) addToMap(N, Nexp, primeFactors);
result.smallestPossibleFactor = p_i; // may be helpful in following factor algorithms
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void factor(BigInteger Nbig, SortedMultiset<BigInteger> primeFactors) {
} while (N%p == 0);
primeFactors.add(BigInteger.valueOf(p), exp);
}
if (p*(long)p > N) {
if (((long)p) * p > N) { // move p as long into registers makes a performance difference
if (N>1) primeFactors.add(BigInteger.valueOf(N));
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void factor(BigInteger Nbig, int Nexp, SortedMultiset<BigInteger> primeFa
primeFactors.add(BigInteger.valueOf(p), Nexp);
N = q;
}
if (p*(long)p > N) {
if (((long)p) * p > N) { // move p as long into registers makes a performance difference
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void factor(BigInteger Nbig, int Nexp, SortedMultiset<BigInteger> primeFa
primeFactors.add(BigInteger.valueOf(p), Nexp);
N = q; // avoiding a division here by storing q benefits the int version but not the long version
}
if (p*(long)p > N) {
if (((long)p) * p > N) { // move p as long into registers makes a performance difference
break;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/de/tilman_neumann/jml/factor/tdiv/TDiv63.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void factor(BigInteger Nbig, SortedMultiset<BigInteger> primeFactors) {
} while (N%p == 0);
primeFactors.add(BigInteger.valueOf(p), exp);
}
if (p*(long)p > N) {
if (((long)p) * p > N) { // move p as long into registers makes a performance difference
break;
}
}
Expand Down Expand Up @@ -110,7 +110,7 @@ public void searchFactors(FactorArguments args, FactorResult result) {
// At least one division has occurred, add the factor(s) to the result map
addToMap(BigInteger.valueOf(p_i), exp*Nexp, primeFactors);
// Check if we are done
if (p_i *(long)p_i > N) {
if (((long)p_i) * p_i > N) { // move p as long into registers makes a performance difference
// the remaining N is 1 or prime
if (N>1) addToMap(BigInteger.valueOf(N), Nexp, primeFactors);
result.smallestPossibleFactor = p_i; // may be helpful in following factor algorithms
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public void factor(BigInteger Nbig, SortedMultiset<BigInteger> primeFactors) {
if (exp>0) {
primeFactors.add(BigInteger.valueOf(p), exp);
}
if (p*(long)p > N) {
if (((long)p) * p > N) { // move p as long into registers makes a performance difference
break; // the remaining N is prime
}
}
Expand Down Expand Up @@ -176,7 +176,7 @@ public void searchFactors(FactorArguments args, FactorResult result) {
// At least one division has occurred, add the factor(s) to the result map
addToMap(BigInteger.valueOf(p_i), exp*Nexp, primeFactors);
// Check if we are done
if (p_i *(long)p_i > N) {
if (((long)p_i) * p_i > N) { // move p as long into registers makes a performance difference
// the remaining N is 1 or prime
if (N>1) addToMap(BigInteger.valueOf(N), Nexp, primeFactors);
result.smallestPossibleFactor = p_i; // may be helpful in following factor algorithms
Expand Down

0 comments on commit 0bdbf1a

Please sign in to comment.