Skip to content

Commit

Permalink
Streamlines code in class FastIntegerMath.
Browse files Browse the repository at this point in the history
  • Loading branch information
wrandelshofer committed Jan 6, 2023
1 parent 3a436fa commit 1dac73b
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 48 deletions.
3 changes: 1 addition & 2 deletions fastdoubleparser-dev/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<argLine>--enable-preview</argLine>
<!--argLine>-Xmx16g - -enable-preview</argLine-->
<argLine>-Xmx24g --enable-preview</argLine>
</configuration>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ static BigInteger computePowerOfTen(NavigableMap<Integer, BigInteger> powersOfTe
/**
* Computes 10<sup>n&~15</sup>.
*/
static BigInteger computeTenRaisedByNFloor16Recursive(NavigableMap<Integer, BigInteger> powersOfTen, int n, boolean parallel) {
static BigInteger computeTenRaisedByNFloor16Recursive(NavigableMap<Integer, BigInteger> powersOfTen, int n) {
n = n & ~15;
Map.Entry<Integer, BigInteger> floorEntry = powersOfTen.floorEntry(n);
int floorPower = floorEntry.getKey();
Expand All @@ -78,7 +78,7 @@ static BigInteger computeTenRaisedByNFloor16Recursive(NavigableMap<Integer, BigI
int diff = n - floorPower;
BigInteger diffValue = powersOfTen.get(diff);
if (diffValue == null) {
diffValue = computeTenRaisedByNFloor16Recursive(powersOfTen, diff, parallel);
diffValue = computeTenRaisedByNFloor16Recursive(powersOfTen, diff);
powersOfTen.put(diff, diffValue);
}
return FftMultiplier.multiply(floorValue, diffValue, false);
Expand Down Expand Up @@ -109,7 +109,7 @@ public static long estimateNumBits(long numDecimalDigits) {
* @return the filled map
*/
static NavigableMap<Integer, BigInteger> fillPowersOf10Floor16(int from, int to, boolean parallel) {
// We fill the map with powers of 5
// Fill the map with powers of 5
NavigableMap<Integer, BigInteger> powers = new TreeMap<>();
powers.put(0, BigInteger.valueOf(5));
powers.put(16, FIVE_POW_16);
Expand All @@ -136,11 +136,10 @@ static void fillPowersOfNFloor16Recursive(NavigableMap<Integer, BigInteger> powe
if (!powersOfTen.containsKey(n)) {
fillPowersOfNFloor16Recursive(powersOfTen, from, mid, parallel);
fillPowersOfNFloor16Recursive(powersOfTen, mid, to, parallel);
powersOfTen.put(n, computeTenRaisedByNFloor16Recursive(powersOfTen, n, parallel));
powersOfTen.put(n, computeTenRaisedByNFloor16Recursive(powersOfTen, n));
}
}


/**
* Computes {@code uint128 product = (uint64)x * (uint64)y}.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import static org.junit.jupiter.api.DynamicTest.dynamicTest;

public class FftMultiplierTest {
private boolean longRunningTests = !"false".equals(System.getProperty("enableLongRunningTests"));
private boolean longRunningTests = !"false".equals(System.getProperty("xenableLongRunningTests"));

@TestFactory
public List<DynamicTest> dynamicTestsMultiply() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@
*
* After we integrated FftMultiplier:
*
* (digits) Mode Cnt Score Error Units
* seq 646391315 avgt 284_707376315.000 ns/op
*
* Before we integrated FftMultiplier:
* (digits) Mode Cnt Score Error Units
* seq 10000000 avgt 2_858113121.500 ns/op
* seq 646391315 avgt 284_707376315.000 ns/op
*
* (digits) Mode Cnt _ Score Error Units
* par 1 avgt _ 28.920 ns/op
Expand All @@ -61,6 +60,32 @@
* seq 646456993 avgt 232_468403178.000 ns/op
* seq 1292782621 avgt 222_404103626.000 ns/op
*
* Before we integrated FftMultiplier:
*
* (digits) Mode Cnt Score Error Units
* par 24 avgt 77.542 ns/op
* par 1 avgt 10.387 ns/op
* par 10 avgt 16.126 ns/op
* par 100 avgt 218.620 ns/op
* par 1000 avgt 4804.543 ns/op
* par 10000 avgt 117301.625 ns/op
* par 100000 avgt 3805758.892 ns/op
* par 1000000 avgt 55026280.527 ns/op
* par 10000000 avgt 1_455180955.000 ns/op
* par 100000000 avgt 33_793710446.000 ns/op
* par 646391315 avgt 393_551821905.000 ns/op
* seq 24 avgt 68.342 ns/op
* seq 1 avgt 7.347 ns/op
* seq 10 avgt 14.433 ns/op
* seq 100 avgt 217.995 ns/op
* seq 1000 avgt 4784.030 ns/op
* seq 10000 avgt 166472.648 ns/op
* seq 100000 avgt 6472800.381 ns/op
* seq 1000000 avgt 111609901.267 ns/op
* seq 10000000 avgt 2_418388865.000 ns/op
* seq 100000000 avgt 41_679193822.000 ns/op
* seq 646391315 avgt 400_608301899.000 ns/op
*
* recursive only (recursion threshold=0)
* (digits) Mode Cnt _ Score Error Units
* rec 24 avgt _ 122.948 ns/op
Expand Down Expand Up @@ -132,17 +157,17 @@
@State(Scope.Benchmark)
public class JmhJavaBigDecimalFromByteArrayScalability {
@Param({
"1"
, "10"
, "100"
, "1000"
, "10000"
, "100000"
, "1000000"
, "10000000"
, "100000000"
, "646456993"
, "1292782621"
// "1"
// , "10"
// , "100"
// , "1000"
// , "10000"
// , "100000"
// , "1000000"
"10000000"
// , "100000000"
// , "646456993"
// , "1292782621"

})
public int digits;
Expand All @@ -164,12 +189,12 @@ public void setUp() {
public BigDecimal seq() {
return JavaBigDecimalParser.parseBigDecimal(decLiteral);
}

/*
@Benchmark
public BigDecimal par() {
return JavaBigDecimalParser.parallelParseBigDecimal(decLiteral);
}

*/

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.NavigableMap;
import java.util.TreeMap;

public class FastIntegerMath {
class FastIntegerMath {
public static final BigInteger FIVE = BigInteger.valueOf(5);
final static BigInteger TEN_POW_16 = BigInteger.valueOf(10_000_000_000_000_000L);
final static BigInteger FIVE_POW_16 = BigInteger.valueOf(152_587_890_625L);
Expand All @@ -33,7 +33,6 @@ public class FastIntegerMath {
BigInteger.valueOf(1_000_000_000_000_000L)
};


/**
* Don't let anyone instantiate this class.
*/
Expand All @@ -59,7 +58,7 @@ static BigInteger computePowerOfTen(NavigableMap<Integer, BigInteger> powersOfTe
if (floorN == n) {
return floorEntry.getValue();
} else {
return parallelMultiply(floorEntry.getValue(), computePowerOfTen(powersOfTen, n - floorN, parallel), parallel);
return FftMultiplier.multiply(floorEntry.getValue(), computePowerOfTen(powersOfTen, n - floorN, parallel), parallel);
}
}
return FIVE.pow(n).shiftLeft(n);
Expand Down Expand Up @@ -101,6 +100,14 @@ public static long estimateNumBits(long numDecimalDigits) {
return (((numDecimalDigits * 3402L) >>> 10) + 1);
}

/**
* Fills a map with powers of 10 floor 16.
*
* @param from the start index of the character sequence that contains the digits
* @param to the end index of the character sequence that contains the digits
* @param parallel whether to fill the map in parallel
* @return the filled map
*/
static NavigableMap<Integer, BigInteger> fillPowersOf10Floor16(int from, int to, boolean parallel) {
// Fill the map with powers of 5
NavigableMap<Integer, BigInteger> powers = new TreeMap<>();
Expand All @@ -117,7 +124,6 @@ static NavigableMap<Integer, BigInteger> fillPowersOf10Floor16(int from, int to,
return powers;
}


static void fillPowersOfNFloor16Recursive(NavigableMap<Integer, BigInteger> powersOfTen, int from, int to, boolean parallel) {
int numDigits = to - from;
// base case:
Expand All @@ -132,9 +138,9 @@ static void fillPowersOfNFloor16Recursive(NavigableMap<Integer, BigInteger> powe
fillPowersOfNFloor16Recursive(powersOfTen, mid, to, parallel);
powersOfTen.put(n, computeTenRaisedByNFloor16Recursive(powersOfTen, n));
}
return;
}


/**
* Computes {@code uint128 product = (uint64)x * (uint64)y}.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.NavigableMap;
import java.util.TreeMap;

public class FastIntegerMath {
class FastIntegerMath {
public static final BigInteger FIVE = BigInteger.valueOf(5);
final static BigInteger TEN_POW_16 = BigInteger.valueOf(10_000_000_000_000_000L);
final static BigInteger FIVE_POW_16 = BigInteger.valueOf(152_587_890_625L);
Expand All @@ -33,7 +33,6 @@ public class FastIntegerMath {
BigInteger.valueOf(1_000_000_000_000_000L)
};


/**
* Don't let anyone instantiate this class.
*/
Expand All @@ -59,7 +58,7 @@ static BigInteger computePowerOfTen(NavigableMap<Integer, BigInteger> powersOfTe
if (floorN == n) {
return floorEntry.getValue();
} else {
return parallelMultiply(floorEntry.getValue(), computePowerOfTen(powersOfTen, n - floorN, parallel), parallel);
return FftMultiplier.multiply(floorEntry.getValue(), computePowerOfTen(powersOfTen, n - floorN, parallel), parallel);
}
}
return FIVE.pow(n).shiftLeft(n);
Expand Down Expand Up @@ -101,6 +100,14 @@ public static long estimateNumBits(long numDecimalDigits) {
return (((numDecimalDigits * 3402L) >>> 10) + 1);
}

/**
* Fills a map with powers of 10 floor 16.
*
* @param from the start index of the character sequence that contains the digits
* @param to the end index of the character sequence that contains the digits
* @param parallel whether to fill the map in parallel
* @return the filled map
*/
static NavigableMap<Integer, BigInteger> fillPowersOf10Floor16(int from, int to, boolean parallel) {
// Fill the map with powers of 5
NavigableMap<Integer, BigInteger> powers = new TreeMap<>();
Expand All @@ -117,7 +124,6 @@ static NavigableMap<Integer, BigInteger> fillPowersOf10Floor16(int from, int to,
return powers;
}


static void fillPowersOfNFloor16Recursive(NavigableMap<Integer, BigInteger> powersOfTen, int from, int to, boolean parallel) {
int numDigits = to - from;
// base case:
Expand All @@ -132,7 +138,6 @@ static void fillPowersOfNFloor16Recursive(NavigableMap<Integer, BigInteger> powe
fillPowersOfNFloor16Recursive(powersOfTen, mid, to, parallel);
powersOfTen.put(n, computeTenRaisedByNFloor16Recursive(powersOfTen, n));
}
return;
}

/**
Expand Down Expand Up @@ -185,12 +190,4 @@ private UInt128(long high, long low) {
this.low = low;
}
}

static BigInteger newBigInteger(int signum, int[] magnitude) {
byte[] bytes = new byte[magnitude.length << 2];
for (int i = 0; i < magnitude.length; i++) {
FastDoubleSwar.writeIntBE(bytes, i << 2, magnitude[i]);
}
return new BigInteger(signum, bytes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.NavigableMap;
import java.util.TreeMap;

public class FastIntegerMath {
class FastIntegerMath {
public static final BigInteger FIVE = BigInteger.valueOf(5);
final static BigInteger TEN_POW_16 = BigInteger.valueOf(10_000_000_000_000_000L);
final static BigInteger FIVE_POW_16 = BigInteger.valueOf(152_587_890_625L);
Expand All @@ -33,7 +33,6 @@ public class FastIntegerMath {
BigInteger.valueOf(1_000_000_000_000_000L)
};


/**
* Don't let anyone instantiate this class.
*/
Expand All @@ -59,7 +58,7 @@ static BigInteger computePowerOfTen(NavigableMap<Integer, BigInteger> powersOfTe
if (floorN == n) {
return floorEntry.getValue();
} else {
return parallelMultiply(floorEntry.getValue(), computePowerOfTen(powersOfTen, n - floorN, parallel), parallel);
return FftMultiplier.multiply(floorEntry.getValue(), computePowerOfTen(powersOfTen, n - floorN, parallel), parallel);
}
}
return FIVE.pow(n).shiftLeft(n);
Expand Down Expand Up @@ -101,8 +100,16 @@ public static long estimateNumBits(long numDecimalDigits) {
return (((numDecimalDigits * 3402L) >>> 10) + 1);
}

/**
* Fills a map with powers of 10 floor 16.
*
* @param from the start index of the character sequence that contains the digits
* @param to the end index of the character sequence that contains the digits
* @param parallel whether to fill the map in parallel
* @return the filled map
*/
static NavigableMap<Integer, BigInteger> fillPowersOf10Floor16(int from, int to, boolean parallel) {
// We fill the map with powers of 5
// Fill the map with powers of 5
NavigableMap<Integer, BigInteger> powers = new TreeMap<>();
powers.put(0, BigInteger.valueOf(5));
powers.put(16, FIVE_POW_16);
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
<systemPropertyVariables>
<enableLongRunningTests>false</enableLongRunningTests>
</systemPropertyVariables>
<argLine>-Xmx22g</argLine>
<argLine>-Xmx24g</argLine>
<parallel>classesAndMethods</parallel>
<forkCount>6</forkCount>
</configuration>
Expand Down

0 comments on commit 1dac73b

Please sign in to comment.