Skip to content

Commit ef1832b

Browse files
committed
small performance improvements
1 parent ad1bc88 commit ef1832b

File tree

3 files changed

+7
-9
lines changed

3 files changed

+7
-9
lines changed

src/main/java/de/tilman_neumann/jml/factor/ecm/TinyEcm64MH.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public ecm_work() {
103103

104104
private static final boolean DEBUG = false;
105105

106-
private static final int MAX_BITS_SUPPORTED = 62;
106+
private static final int MAX_BITS_SUPPORTED = 62; // seems to work for 63 bit numbers now, but very slow - so not completely fixed for that
107107

108108
// The reducer R is 2^64, but the only constant still required is the half of it.
109109
private static final long R_HALF = 1L << 63;
@@ -227,7 +227,7 @@ long u64div(long c, long n) {
227227
* @return u*v mod m
228228
*/
229229
long spMulMod(long u, long v, long m) {
230-
return Uint128.spMul64_MH(u, v).spDivide_MH(m)[1];
230+
return Uint128.mul64SignedMH(u, v).spDivide_MH(m)[1];
231231
}
232232

233233
long spGCD(long x, long y) {
@@ -1094,15 +1094,15 @@ private long setUpMontgomeryMult_v2(long N) {
10941094
*/
10951095
public static long montMul64(long a, long b, long N, long Nhat) {
10961096
// Step 1: Compute a*b
1097-
Uint128 ab = Uint128.spMul64_MH(a, b);
1097+
Uint128 ab = Uint128.mul64SignedMH(a, b);
10981098
// Step 2: Compute t = ab * (-1/N) mod R
10991099
// Since R=2^64, "x mod R" just means to get the low part of x.
11001100
// That would give t = Uint128.mul64(ab.getLow(), minusNInvModR).getLow();
11011101
// but even better, the long product just gives the low part -> we can get rid of one expensive mul64().
11021102
long t = ab.getLow() * Nhat;
11031103
// Step 3: Compute r = (a*b + t*N) / R
11041104
// Since R=2^64, "x / R" just means to get the high part of x.
1105-
long r = ab.add_getHigh(Uint128.spMul64_MH(t, N));
1105+
long r = ab.add_getHigh(Uint128.mul64SignedMH(t, N));
11061106
// If the correct result is c, then now r==c or r==c+N.
11071107
r = r<N ? r : r-N; // required at ecm
11081108

src/main/java/de/tilman_neumann/jml/factor/ecm/TinyEcm64MHInlined.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1104,7 +1104,7 @@ public static long montMul64(long a, long b, long N, long Nhat) {
11041104
long t = abLow * Nhat;
11051105
final long tNLow = t*N;
11061106
long tNHigh = Math.multiplyHigh(t, N);
1107-
if (t<0) tNHigh += N;
1107+
//if (t<0) tNHigh += N; // bad for performance
11081108

11091109
// Step 3: Compute r = (a*b + t*N) / R
11101110
// Since R=2^64, "x / R" just means to get the high part of x.

src/main/java/de/tilman_neumann/jml/factor/pollardRho/PollardRhoBrentMontgomery64MHInlined.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import org.apache.logging.log4j.Logger;
1919
import org.apache.logging.log4j.LogManager;
2020

21-
import de.tilman_neumann.jml.base.Uint128;
2221
import de.tilman_neumann.jml.factor.FactorAlgorithm;
2322
import de.tilman_neumann.jml.gcd.Gcd63;
2423
import de.tilman_neumann.jml.random.Rng;
@@ -180,12 +179,11 @@ public static long montMul64(long a, long b, long N, long Nhat) {
180179
final long tNLow = t*N;
181180
long tNHigh = Math.multiplyHigh(t, N);
182181
//if (t<0) tNHigh += N; // bad for performance
183-
Uint128 tN = new Uint128(tNHigh, tNLow); // for some reason, removing this object seems to degrade performance
184182

185183
// Step 3: Compute r = (a*b + t*N) / R
186184
// Since R=2^64, "x / R" just means to get the high part of x.
187-
long low = abLow + tN.getLow();
188-
long high = abHigh + tN.getHigh();
185+
long low = abLow + tNLow;
186+
long high = abHigh + tNHigh;
189187
long r = (low+Long.MIN_VALUE < abLow+Long.MIN_VALUE) ? high + 1 : high;
190188
// If the correct result is c, then now r==c or r==c+N.
191189
// This is fine for this factoring algorithm, because r will

0 commit comments

Comments
 (0)