Skip to content

Commit b1e972a

Browse files
committed
use external rng
1 parent 240c165 commit b1e972a

File tree

3 files changed

+13
-70
lines changed

3 files changed

+13
-70
lines changed

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

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import de.tilman_neumann.jml.factor.tdiv.TDiv;
4747
import de.tilman_neumann.jml.gcd.Gcd63;
4848
import de.tilman_neumann.jml.primes.probable.BPSWTest;
49+
import de.tilman_neumann.jml.random.SpRand32;
4950
import de.tilman_neumann.util.Ensure;
5051

5152
/**
@@ -163,6 +164,8 @@ public ecm_work() {
163164

164165
private Gcd63 gcd63 = new Gcd63();
165166

167+
private SpRand32 spRand32 = new SpRand32();
168+
166169
public String getName() {
167170
return "TinyEcm64";
168171
}
@@ -227,28 +230,6 @@ long spMulMod(long u, long v, long m) {
227230
return Uint128.mul64Signed(u, v).spDivide(m)[1];
228231
}
229232

230-
int spRand(int lower, int upper) {
231-
if (DEBUG) LOG.debug("LCGSTATE=" + LCGSTATE);
232-
233-
// fix rng for negative upper values;
234-
long upperl = (long) upper;
235-
if (upperl<0) upperl += (1L<<32);
236-
long diff = upperl - lower;
237-
if (DEBUG) LOG.debug("lower=" + lower + ", upper=" + upperl + ", diff=" + diff);
238-
239-
// advance the state of the LCG and return the appropriate result
240-
LCGSTATE = 6364136223846793005L * LCGSTATE + 1442695040888963407L;
241-
long LCGSTATE_shifted = LCGSTATE >>> 32;
242-
if (DEBUG) LOG.debug("LCGSTATE=" + LCGSTATE + ", LCGSTATE_shifted=" + LCGSTATE_shifted);
243-
244-
double quot = (double)LCGSTATE_shifted / 4294967296.0; // dividend is 2^32
245-
double prod = diff * quot;
246-
int rand = (int)(0xFFFFFFFF & (long)prod); // (int)prod does not work for prod >= 2^31
247-
int result = lower + rand;
248-
if (DEBUG) LOG.debug("quot=" + quot + ", prod=" + prod + ", rand=" + rand + ", result=" + result);
249-
return result;
250-
}
251-
252233
void add(long rho, ecm_work work, ecm_pt P1, ecm_pt P2, ecm_pt Pin, ecm_pt Pout) {
253234
// compute:
254235
//x+ = z- * [(x1-z1)(x2+z2) + (x1+z1)(x2-z2)]^2
@@ -581,7 +562,7 @@ void build(ecm_pt P, long rho, ecm_work work, int sigma) {
581562

582563
if (sigma == 0)
583564
{
584-
work.sigma = spRand(7, (int)-1); // inlined is a tiny bit faster than using SpRand32
565+
work.sigma = spRand32.nextInt(7, (int)-1);
585566
if (DEBUG) LOG.debug("random sigma=" + work.sigma);
586567
}
587568
else

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

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import de.tilman_neumann.jml.factor.tdiv.TDiv;
4747
import de.tilman_neumann.jml.gcd.Gcd63;
4848
import de.tilman_neumann.jml.primes.probable.BPSWTest;
49+
import de.tilman_neumann.jml.random.SpRand32;
4950
import de.tilman_neumann.util.Ensure;
5051

5152
/**
@@ -165,7 +166,9 @@ public ecm_work() {
165166
private BPSWTest bpsw = new BPSWTest();
166167

167168
private Gcd63 gcd63 = new Gcd63();
168-
169+
170+
private SpRand32 spRand32 = new SpRand32();
171+
169172
public String getName() {
170173
return "TinyEcm64MH";
171174
}
@@ -231,28 +234,6 @@ long spMulMod(long u, long v, long m) {
231234
return Uint128.mul64SignedMH(u, v).spDivide_MH(m)[1];
232235
}
233236

234-
int spRand(int lower, int upper) {
235-
if (DEBUG) LOG.debug("LCGSTATE=" + LCGSTATE);
236-
237-
// fix rng for negative upper values;
238-
long upperl = (long) upper;
239-
if (upperl<0) upperl += (1L<<32);
240-
long diff = upperl - lower;
241-
if (DEBUG) LOG.debug("lower=" + lower + ", upper=" + upperl + ", diff=" + diff);
242-
243-
// advance the state of the LCG and return the appropriate result
244-
LCGSTATE = 6364136223846793005L * LCGSTATE + 1442695040888963407L;
245-
long LCGSTATE_shifted = LCGSTATE >>> 32;
246-
if (DEBUG) LOG.debug("LCGSTATE=" + LCGSTATE + ", LCGSTATE_shifted=" + LCGSTATE_shifted);
247-
248-
double quot = (double)LCGSTATE_shifted / 4294967296.0; // dividend is 2^32
249-
double prod = diff * quot;
250-
int rand = (int)(0xFFFFFFFF & (long)prod); // (int)prod does not work for prod >= 2^31
251-
int result = lower + rand;
252-
if (DEBUG) LOG.debug("quot=" + quot + ", prod=" + prod + ", rand=" + rand + ", result=" + result);
253-
return result;
254-
}
255-
256237
void add(long rho, ecm_work work, ecm_pt P1, ecm_pt P2, ecm_pt Pin, ecm_pt Pout) {
257238
// compute:
258239
//x+ = z- * [(x1-z1)(x2+z2) + (x1+z1)(x2-z2)]^2
@@ -585,7 +566,7 @@ void build(ecm_pt P, long rho, ecm_work work, int sigma) {
585566

586567
if (sigma == 0)
587568
{
588-
work.sigma = spRand(7, (int)-1); // inlined is a tiny bit faster than using SpRand32
569+
work.sigma = spRand32.nextInt(7, (int)-1);
589570
if (DEBUG) LOG.debug("random sigma=" + work.sigma);
590571
}
591572
else

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

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import de.tilman_neumann.jml.factor.tdiv.TDiv;
4747
import de.tilman_neumann.jml.gcd.Gcd63;
4848
import de.tilman_neumann.jml.primes.probable.BPSWTest;
49+
import de.tilman_neumann.jml.random.SpRand32;
4950
import de.tilman_neumann.util.Ensure;
5051

5152
/**
@@ -166,6 +167,8 @@ public ecm_work() {
166167

167168
private Gcd63 gcd63 = new Gcd63();
168169

170+
private SpRand32 spRand32 = new SpRand32();
171+
169172
public String getName() {
170173
return "TinyEcm64MHInlined";
171174
}
@@ -231,28 +234,6 @@ long spMulMod(long u, long v, long m) {
231234
return Uint128.spMul64_MH(u, v).spDivide_MH(m)[1];
232235
}
233236

234-
int spRand(int lower, int upper) {
235-
if (DEBUG) LOG.debug("LCGSTATE=" + LCGSTATE);
236-
237-
// fix rng for negative upper values;
238-
long upperl = (long) upper;
239-
if (upperl<0) upperl += (1L<<32);
240-
long diff = upperl - lower;
241-
if (DEBUG) LOG.debug("lower=" + lower + ", upper=" + upperl + ", diff=" + diff);
242-
243-
// advance the state of the LCG and return the appropriate result
244-
LCGSTATE = 6364136223846793005L * LCGSTATE + 1442695040888963407L;
245-
long LCGSTATE_shifted = LCGSTATE >>> 32;
246-
if (DEBUG) LOG.debug("LCGSTATE=" + LCGSTATE + ", LCGSTATE_shifted=" + LCGSTATE_shifted);
247-
248-
double quot = (double)LCGSTATE_shifted / 4294967296.0; // dividend is 2^32
249-
double prod = diff * quot;
250-
int rand = (int)(0xFFFFFFFF & (long)prod); // (int)prod does not work for prod >= 2^31
251-
int result = lower + rand;
252-
if (DEBUG) LOG.debug("quot=" + quot + ", prod=" + prod + ", rand=" + rand + ", result=" + result);
253-
return result;
254-
}
255-
256237
void add(long rho, ecm_work work, ecm_pt P1, ecm_pt P2, ecm_pt Pin, ecm_pt Pout) {
257238
// compute:
258239
//x+ = z- * [(x1-z1)(x2+z2) + (x1+z1)(x2-z2)]^2
@@ -585,7 +566,7 @@ void build(ecm_pt P, long rho, ecm_work work, int sigma) {
585566

586567
if (sigma == 0)
587568
{
588-
work.sigma = spRand(7, (int)-1); // inlined is a tiny bit faster than using SpRand32
569+
work.sigma = spRand32.nextInt(7, (int)-1);
589570
if (DEBUG) LOG.debug("random sigma=" + work.sigma);
590571
}
591572
else

0 commit comments

Comments
 (0)