Skip to content

Commit

Permalink
log(math): add log for x87 ret.
Browse files Browse the repository at this point in the history
  • Loading branch information
halibobo1205 committed Jun 12, 2024
1 parent ce7a0c1 commit 28c110d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
8 changes: 7 additions & 1 deletion common/src/main/java/org/tron/common/math/MathWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ public static double pow(double a, double b) {
public static double powForExchange(double a, double b) {
Preconditions.checkArgument(b > -1 && b < 1, "b must be -1<b<1");
Preconditions.checkArgument(a > 1 && a < 2, "a must be 1<a<2");
return PowDataForJdk8.getData().getOrDefault(a, StrictMathWrapper.pow(a, b));
Double ret = PowDataForJdk8.get(a);
if (ret != null) {
logger.info("get from x87, a: {}, ret: {}", PowDataForJdk8.doubleToHex(a),
PowDataForJdk8.doubleToHex(ret));
return ret;
}
return pow(a, b);
}
}
13 changes: 11 additions & 2 deletions common/src/main/java/org/tron/common/math/PowDataForJdk8.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import lombok.Getter;

/**
* This class is used to provide the same pow data for jdk8.
Expand All @@ -12,7 +11,6 @@
*/
class PowDataForJdk8 {

@Getter
private static final Map<Double, Double> data = Collections.synchronizedMap(new HashMap<>());

/**
Expand Down Expand Up @@ -371,6 +369,17 @@ private static void addData(String a, String x87Ret) {
data.put(hexToDouble(a), hexToDouble(x87Ret));
}

public static Double get(double key) {
return data.get(key);
}

static String doubleToHex(double input) {
// Convert the double to a long
long inputAsLong = Double.doubleToLongBits(input);
// and then convert the long to a hex string
return Long.toHexString(inputAsLong);
}

static double hexToDouble(String input) {
// Convert the hex string to a long
long hexAsLong = Long.parseLong(input, 16);
Expand Down
3 changes: 1 addition & 2 deletions common/src/test/java/org/tron/common/math/MathTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ public void testPow() {
double ret3 = StrictMathWrapper.pow(a, 0.0005);
Assert.assertNotEquals(0, Double.compare(ret, ret2));
Assert.assertNotEquals(0, Double.compare(ret2, ret3));
Assert.assertEquals(0, Double.compare(
PowDataForJdk8.getData().get(a), ret2));
Assert.assertEquals(0, Double.compare(PowDataForJdk8.get(a), ret2));
Assert.assertEquals(0, Double.compare(ret, ret3));
}
}

0 comments on commit 28c110d

Please sign in to comment.