Skip to content

Commit ff18ac1

Browse files
committed
optimize long multiplications
1 parent 631b536 commit ff18ac1

File tree

4 files changed

+14
-14
lines changed

4 files changed

+14
-14
lines changed

src/main/java/de/tilman_neumann/jml/factor/squfof/SquFoF31.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public long findSingleFactor(long N) {
8686
// The cast may be wrong for some bigger kN, but fixing the cast would mean a small performance
8787
// penalty, so we ignore it. Return immediately if kN is square.
8888
floor_sqrt_kN = (int) Math.sqrt(kN);
89-
int diff = (int) (kN - floor_sqrt_kN*(long)floor_sqrt_kN);
89+
int diff = (int) (kN - ((long)floor_sqrt_kN) * floor_sqrt_kN);
9090
if (diff==0) return gcdEngine.gcd(N, floor_sqrt_kN);
9191

9292
// search square Q_i
@@ -140,7 +140,7 @@ private Long reverseIteration(int found_P, int found_Q_sqrt) {
140140
int b_i = (floor_sqrt_kN-found_P)/found_Q_sqrt; // floor(rational result)
141141
int P_i = b_i*found_Q_sqrt + found_P;
142142
int Q_i = found_Q_sqrt;
143-
int Q_ip1 = (int) ((kN - P_i*(long)P_i)/found_Q_sqrt);
143+
int Q_ip1 = (int) ((kN - ((long)P_i) * P_i) / found_Q_sqrt);
144144

145145
// second iteration step
146146
int P_im1, Q_im1;

src/main/java/de/tilman_neumann/jml/factor/squfof/SquFoF31Preload.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public long findSingleFactor(long N) {
109109
// The cast may be wrong for some bigger kN, but fixing the cast would mean a small performance
110110
// penalty, so we ignore it. Return immediately if kN is square.
111111
floor_sqrt_kN = (int) Math.sqrt(kN);
112-
int diff = (int) (kN - floor_sqrt_kN*(long)floor_sqrt_kN);
112+
int diff = (int) (kN - ((long)floor_sqrt_kN) * floor_sqrt_kN);
113113
if (diff==0) return gcdEngine.gcd(N, floor_sqrt_kN);
114114

115115
// search square Q_i
@@ -163,7 +163,7 @@ private Long reverseIteration(int found_P, int found_Q_sqrt) {
163163
int b_i = (floor_sqrt_kN-found_P)/found_Q_sqrt; // floor(rational result)
164164
int P_i = b_i*found_Q_sqrt + found_P;
165165
int Q_i = found_Q_sqrt;
166-
int Q_ip1 = (int) ((kN - P_i*(long)P_i)/found_Q_sqrt);
166+
int Q_ip1 = (int) ((kN - ((long)P_i) * P_i) / found_Q_sqrt);
167167

168168
// second iteration step
169169
int P_im1, Q_im1;

src/main/java/de/tilman_neumann/jml/modular/ModularSqrt.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ private int Tonelli_Shanks(BigInteger n, int p) {
102102
Ensure.ensureEquals(p-1, mpe.modPow(c, 1<<(M-1), p)); // -1 == c^(2^(M-1)) (mod p)
103103
Ensure.ensureEquals(1, mpe.modPow(t, 1<<(M-1), p)); // 1 == t^(2^(M-1)) (mod p)
104104
long nModP = n.mod(BigInteger.valueOf(p)).longValue();
105-
Ensure.ensureEquals((R*(long)R) % p, (t*nModP) % p); // R^2 == t*n (mod p)
105+
Ensure.ensureEquals((((long)R) * R) % p, (t*nModP) % p); // R^2 == t*n (mod p)
106106
}
107107
boolean foundI = false;
108108
int i;
@@ -115,9 +115,9 @@ private int Tonelli_Shanks(BigInteger n, int p) {
115115
if (foundI==false) throw new IllegalStateException("Tonelli-Shanks did not find an 'i' < M=" + M);
116116

117117
int b = mpe.modPow(c, 1<<(M-i-1), p); // c^(2^(M-i-1))
118-
R = (int) ((R*(long)b) % p);
119-
c = (int) ((b*(long)b) % p);
120-
t = (int) ((t*(long)c) % p);
118+
R = (int) ((((long)R) * b) % p);
119+
c = (int) ((((long)b) * b) % p);
120+
t = (int) ((((long)t) * c) % p);
121121
M = i;
122122
}
123123
if (DEBUG) Ensure.ensureEquals(BigInteger.valueOf(R).pow(2).mod(BigInteger.valueOf(p)), n.mod(BigInteger.valueOf(p)));
@@ -148,10 +148,10 @@ private int case5Mod8(BigInteger n, int p) {
148148
int k = p>>3; // for p == 5 (mod 8) we need k = (p-5)/8 = p>>3
149149
BigInteger n2 = n.shiftLeft(1);
150150
int g = mpe.modPow(n2, k, p);
151-
BigInteger gSquare = BigInteger.valueOf(g*(long)g);
151+
BigInteger gSquare = BigInteger.valueOf(((long)g) * g);
152152
BigInteger p_big = BigInteger.valueOf(p);
153153
int i = n2.multiply(gSquare).mod(p_big).intValue();
154-
int t = n.multiply(BigInteger.valueOf(g*(long)(i-1))).mod(p_big).intValue();
154+
int t = n.multiply(BigInteger.valueOf(((long)g) * (i-1))).mod(p_big).intValue();
155155
if (DEBUG) Ensure.ensureEquals(BigInteger.valueOf(t).pow(2).mod(p_big), n.mod(p_big));
156156
// return the smaller sqrt
157157
return t <= (p>>1) ? t : p-t;

src/main/java/de/tilman_neumann/jml/modular/ModularSqrt31.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ int Tonelli_Shanks(int n, int p) { // not private because used in tests
102102
// test invariants from <link>https://en.wikipedia.org/wiki/Tonelli%E2%80%93Shanks_algorithm#Proof</link>:
103103
Ensure.ensureEquals(p-1, mpe.modPow(c, 1<<(M-1), p)); // -1 == c^(2^(M-1)) (mod p)
104104
Ensure.ensureEquals(1, mpe.modPow(t, 1<<(M-1), p)); // 1 == t^(2^(M-1)) (mod p)
105-
Ensure.ensureEquals((R*(long)R) % p, (t*(long)n) % p); // R^2 == t*n (mod p)
105+
Ensure.ensureEquals((((long)R) * R) % p, (((long)t) * n) % p); // R^2 == t*n (mod p)
106106
}
107107
boolean foundI = false;
108108
int i;
@@ -115,9 +115,9 @@ int Tonelli_Shanks(int n, int p) { // not private because used in tests
115115
if (foundI==false) throw new IllegalStateException("Tonelli-Shanks did not find an 'i' < M=" + M);
116116

117117
int b = mpe.modPow(c, 1<<(M-i-1), p); // c^(2^(M-i-1))
118-
R = (int) ((R*(long)b) % p);
119-
c = (int) ((b*(long)b) % p);
120-
t = (int) ((t*(long)c) % p);
118+
R = (int) ((((long)R) * b) % p);
119+
c = (int) ((((long)b) * b) % p);
120+
t = (int) ((((long)t) * c) % p);
121121
M = i;
122122
}
123123
if (DEBUG) Ensure.ensureEquals(BigInteger.valueOf(R).pow(2).mod(BigInteger.valueOf(p)), BigInteger.valueOf(n%p));

0 commit comments

Comments
 (0)