Skip to content

Commit bd62248

Browse files
committed
feat(math): add hard-code for arm pow
1 parent 7a20269 commit bd62248

4 files changed

Lines changed: 313 additions & 7 deletions

File tree

framework/src/test/java/org/tron/core/capsule/utils/ExchangeProcessorTest.java

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,63 @@ public void testWithdraw() {
138138
@Test
139139
public void testStrictMath() {
140140
long supply = 1_000_000_000_000_000_000L;
141-
ExchangeProcessor processor = new ExchangeProcessor(supply, false);
142-
long anotherTokenQuant = processor.exchange(4732214, 2202692725330L, 29218);
143-
processor = new ExchangeProcessor(supply, true);
144-
long result = processor.exchange(4732214, 2202692725330L, 29218);
145-
Assert.assertNotEquals(anotherTokenQuant, result);
141+
long[][] testData = {
142+
{4732214L, 2202692725330L, 29218L},
143+
{5618633L, 556559904655L, 1L},
144+
{9299554L, 1120271441185L, 7000L},
145+
{62433133L, 12013267997895L, 100000L},
146+
{64212664L, 725836766395L, 50000L},
147+
{64126212L, 2895100109660L, 5000L},
148+
{56459055L, 3288380567368L, 165000L},
149+
{21084707L, 1589204008960L, 50000L},
150+
{24120521L, 1243764649177L, 20000L},
151+
{836877L, 212532333234L, 5293L},
152+
{55879741L, 13424854054078L, 250000L},
153+
{66388882L, 11300012790454L, 300000L},
154+
{94470955L, 7941038150919L, 2000L},
155+
{13613746L, 5012660712983L, 122L},
156+
{71852829L, 5262251868618L, 396L},
157+
{3857658L, 446109245044L, 20637L},
158+
{35491863L, 3887393269796L, 100L},
159+
{295632118L, 1265298439004L, 500000L},
160+
{49320113L, 1692106302503L, 123267L},
161+
{10966984L, 6222910652894L, 2018L},
162+
{41634280L, 2004508994767L, 865L},
163+
{10087714L, 6765558834714L, 1009L},
164+
{42270078L, 210360843525L, 200000L},
165+
{571091915L, 655011397250L, 2032520L},
166+
{51026781L, 1635726339365L, 37L},
167+
{61594L, 312318864132L, 500L},
168+
{11616684L, 5875978057357L, 20L},
169+
{60584529L, 1377717821301L, 78132L},
170+
{29818073L, 3033545989651L, 182L},
171+
{3855280L, 834647482043L, 16L},
172+
{58310711L, 1431562205655L, 200000L},
173+
{60226263L, 1386036785882L, 178226L},
174+
{3537634L, 965771433992L, 225L},
175+
{3760534L, 908700758784L, 328L},
176+
{80913L, 301864126445L, 4L},
177+
{3789271L, 901842209723L, 1L},
178+
{4051904L, 843419481286L, 1005L},
179+
{89141L, 282107742510L, 100L},
180+
{90170L, 282854635378L, 26L},
181+
{4229852L, 787503315944L, 137L},
182+
{4259884L, 781975090197L, 295L},
183+
{3627657L, 918682223700L, 34L},
184+
{813519L, 457546358759L, 173L},
185+
{89626L, 327856173057L, 27L},
186+
{97368L, 306386489550L, 50L},
187+
{93712L, 305866015731L, 4L},
188+
{3281260L, 723656594544L, 40L},
189+
{3442652L, 689908773685L, 18L},
190+
};
191+
192+
for (long[] data : testData) {
193+
ExchangeProcessor processor = new ExchangeProcessor(supply, false);
194+
long anotherTokenQuant = processor.exchange(data[0], data[1], data[2]);
195+
processor = new ExchangeProcessor(supply, true);
196+
long result = processor.exchange(data[0], data[1], data[2]);
197+
Assert.assertNotEquals(anotherTokenQuant, result);
198+
}
146199
}
147-
148-
149200
}

framework/src/test/java/org/tron/core/db2/SnapshotManagerTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.google.common.primitives.Longs;
88
import com.google.protobuf.ByteString;
99
import java.io.IOException;
10+
import java.util.Arrays;
1011
import java.util.List;
1112
import java.util.Map;
1213
import java.util.stream.Collectors;
Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
package org.tron.common.math;
2+
3+
import java.util.Collections;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
import java.util.Objects;
7+
8+
/**
9+
* This class is deprecated and should not be used in new code,
10+
* for cross-platform consistency, please use {@link StrictMathWrapper} instead,
11+
* especially for floating-point calculations.
12+
*/
13+
@Deprecated
14+
public class MathWrapper {
15+
16+
private static final Map<PowData, Double> powData = Collections.synchronizedMap(new HashMap<>());
17+
private static final String EXPONENT = "3f40624dd2f1a9fc"; // 1/2000 = 0.0005
18+
19+
public static double pow(double a, double b) {
20+
double strictResult = StrictMath.pow(a, b);
21+
return powData.getOrDefault(new PowData(a, b), strictResult);
22+
}
23+
24+
/**
25+
* This static block is used to initialize the data map.
26+
*/
27+
static {
28+
// init main-net pow data start
29+
addPowData("3ff0192278704be3", EXPONENT, "3ff000033518c576"); // 4137160(block)
30+
addPowData("3ff000002fc6a33f", EXPONENT, "3ff0000000061d86"); // 4065476
31+
addPowData("3ff00314b1e73ecf", EXPONENT, "3ff0000064ea3ef8"); // 4071538
32+
addPowData("3ff0068cd52978ae", EXPONENT, "3ff00000d676966c"); // 4109544
33+
addPowData("3ff0032fda05447d", EXPONENT, "3ff0000068636fe0"); // 4123826
34+
addPowData("3ff00051c09cc796", EXPONENT, "3ff000000a76c20e"); // 4166806
35+
addPowData("3ff00bef8115b65d", EXPONENT, "3ff0000186893de0"); // 4225778
36+
addPowData("3ff009b0b2616930", EXPONENT, "3ff000013d27849e"); // 4251796
37+
addPowData("3ff00364ba163146", EXPONENT, "3ff000006f26a9dc"); // 4257157
38+
addPowData("3ff019be4095d6ae", EXPONENT, "3ff0000348e9f02a"); // 4260583
39+
addPowData("3ff0123e52985644", EXPONENT, "3ff0000254797fd0"); // 4367125
40+
addPowData("3ff0126d052860e2", EXPONENT, "3ff000025a6cde26"); // 4402197
41+
addPowData("3ff0001632cccf1b", EXPONENT, "3ff0000002d76406"); // 4405788
42+
addPowData("3ff0000965922b01", EXPONENT, "3ff000000133e966"); // 4490332
43+
addPowData("3ff00005c7692d61", EXPONENT, "3ff0000000bd5d34"); // 4499056
44+
addPowData("3ff015cba20ec276", EXPONENT, "3ff00002c84cef0e"); // 4518035
45+
addPowData("3ff00002f453d343", EXPONENT, "3ff000000060cf4e"); // 4533215
46+
addPowData("3ff006ea73f88946", EXPONENT, "3ff00000e26d4ea2"); // 4647814
47+
addPowData("3ff00a3632db72be", EXPONENT, "3ff000014e3382a6"); // 4766695
48+
addPowData("3ff000c0e8df0274", EXPONENT, "3ff0000018b0aeb2"); // 4771494
49+
addPowData("3ff00015c8f06afe", EXPONENT, "3ff0000002c9d73e"); // 4793587
50+
addPowData("3ff00068def18101", EXPONENT, "3ff000000d6c3cac"); // 4801947
51+
addPowData("3ff01349f3ac164b", EXPONENT, "3ff000027693328a"); // 4916843
52+
addPowData("3ff00e86a7859088", EXPONENT, "3ff00001db256a52"); // 4924111
53+
addPowData("3ff00000c2a51ab7", EXPONENT, "3ff000000018ea20"); // 5098864
54+
addPowData("3ff020fb74e9f170", EXPONENT, "3ff00004346fbfa2"); // 5133963
55+
addPowData("3ff00001ce277ce7", EXPONENT, "3ff00000003b27dc"); // 5139389
56+
addPowData("3ff005468a327822", EXPONENT, "3ff00000acc20750"); // 5151258
57+
addPowData("3ff00006666f30ff", EXPONENT, "3ff0000000d1b80e"); // 5185021
58+
addPowData("3ff000045a0b2035", EXPONENT, "3ff00000008e98e6"); // 5295829
59+
addPowData("3ff00e00380e10d7", EXPONENT, "3ff00001c9ff83c8"); // 5380897
60+
addPowData("3ff00c15de2b0d5e", EXPONENT, "3ff000018b6eaab6"); // 5400886
61+
addPowData("3ff00042afe6956a", EXPONENT, "3ff0000008892244"); // 5864127
62+
addPowData("3ff0005b7357c2d4", EXPONENT, "3ff000000bb48572"); // 6167339
63+
addPowData("3ff00033d5ab51c8", EXPONENT, "3ff0000006a279c8"); // 6240974
64+
addPowData("3ff0000046d74585", EXPONENT, "3ff0000000091150"); // 6279093
65+
addPowData("3ff0010403f34767", EXPONENT, "3ff0000021472146"); // 6428736
66+
addPowData("3ff00496fe59bc98", EXPONENT, "3ff000009650a4ca"); // 6432355,6493373
67+
addPowData("3ff0012e43815868", EXPONENT, "3ff0000026af266e"); // 6555029
68+
addPowData("3ff00021f6080e3c", EXPONENT, "3ff000000458d16a"); // 7092933
69+
addPowData("3ff000489c0f28bd", EXPONENT, "3ff00000094b3072"); // 7112412
70+
addPowData("3ff00009d3df2e9c", EXPONENT, "3ff00000014207b4"); // 7675535
71+
addPowData("3ff000def05fa9c8", EXPONENT, "3ff000001c887cdc"); // 7860324
72+
addPowData("3ff0013bca543227", EXPONENT, "3ff00000286a42d2"); // 8292427
73+
addPowData("3ff0021a2f14a0ee", EXPONENT, "3ff0000044deb040"); // 8517311
74+
addPowData("3ff0002cc166be3c", EXPONENT, "3ff0000005ba841e"); // 8763101
75+
addPowData("3ff0000cc84e613f", EXPONENT, "3ff0000001a2da46"); // 9269124
76+
addPowData("3ff000057b83c83f", EXPONENT, "3ff0000000b3a640"); // 9631452
77+
// init main-net pow data end
78+
// add pow data
79+
}
80+
81+
private static void addPowData(String a, String b, String ret) {
82+
powData.put(new PowData(hexToDouble(a), hexToDouble(b)), hexToDouble(ret));
83+
}
84+
85+
private static double hexToDouble(String input) {
86+
// Convert the hex string to a long
87+
long hexAsLong = Long.parseLong(input, 16);
88+
// and then convert the long to a double
89+
return Double.longBitsToDouble(hexAsLong);
90+
}
91+
92+
private static class PowData {
93+
final double a;
94+
final double b;
95+
96+
public PowData(double a, double b) {
97+
this.a = a;
98+
this.b = b;
99+
}
100+
101+
@Override
102+
public boolean equals(Object o) {
103+
if (this == o) {
104+
return true;
105+
}
106+
if (o == null || getClass() != o.getClass()) {
107+
return false;
108+
}
109+
PowData powData = (PowData) o;
110+
return Double.compare(powData.a, a) == 0 && Double.compare(powData.b, b) == 0;
111+
}
112+
113+
@Override
114+
public int hashCode() {
115+
return Objects.hash(a, b);
116+
}
117+
}
118+
119+
/**
120+
* *** methods are same as {@link java.lang.Math} methods, guaranteed by the call start ***
121+
*/
122+
123+
/**
124+
* finally calls {@link java.lang.Math#addExact(long, long)}
125+
*/
126+
127+
public static long addExact(long x, long y) {
128+
return StrictMath.addExact(x, y);
129+
}
130+
131+
/**
132+
* finally calls {@link java.lang.Math#addExact(int, int)}
133+
*/
134+
135+
public static int addExact(int x, int y) {
136+
return StrictMath.addExact(x, y);
137+
}
138+
139+
/**
140+
* finally calls {@link java.lang.Math#subtractExact(long, long)}
141+
*/
142+
143+
public static long subtractExact(long x, long y) {
144+
return StrictMath.subtractExact(x, y);
145+
}
146+
147+
/**
148+
* finally calls {@link java.lang.Math#floorMod(long, long)}
149+
*/
150+
public static long multiplyExact(long x, long y) {
151+
return StrictMath.multiplyExact(x, y);
152+
}
153+
154+
public static long multiplyExact(long x, int y) {
155+
return multiplyExact(x, (long) y);
156+
}
157+
158+
public static int multiplyExact(int x, int y) {
159+
return StrictMath.multiplyExact(x, y);
160+
}
161+
162+
/**
163+
* finally calls {@link java.lang.Math#floorDiv(long, long)}
164+
*/
165+
public static long floorDiv(long x, long y) {
166+
return StrictMath.floorDiv(x, y);
167+
}
168+
169+
public static long floorDiv(long x, int y) {
170+
return floorDiv(x, (long) y);
171+
}
172+
173+
/**
174+
* finally calls {@link java.lang.Math#min(int, int)}
175+
*/
176+
public static int min(int a, int b) {
177+
return StrictMath.min(a, b);
178+
}
179+
180+
/**
181+
* finally calls {@link java.lang.Math#min(long, long)}
182+
*/
183+
public static long min(long a, long b) {
184+
return StrictMath.min(a, b);
185+
}
186+
187+
/**
188+
* finally calls {@link java.lang.Math#max(int, int)}
189+
*/
190+
public static int max(int a, int b) {
191+
return StrictMath.max(a, b);
192+
}
193+
194+
/**
195+
* finally calls {@link java.lang.Math#max(long, long)}
196+
*/
197+
public static long max(long a, long b) {
198+
return StrictMath.max(a, b);
199+
}
200+
201+
/**
202+
* finally calls {@link java.lang.Math#round(float)}
203+
*/
204+
public static int round(float a) {
205+
return StrictMath.round(a);
206+
}
207+
208+
/**
209+
* finally calls {@link java.lang.Math#round(double)}
210+
*/
211+
public static long round(double a) {
212+
return StrictMath.round(a);
213+
}
214+
215+
/**
216+
* finally calls {@link java.lang.Math#signum(double)}
217+
*/
218+
public static double signum(double d) {
219+
return StrictMath.signum(d);
220+
}
221+
222+
/**
223+
* finally calls {@link java.lang.Math#signum(float)}
224+
*/
225+
public static long abs(long a) {
226+
return StrictMath.abs(a);
227+
}
228+
229+
/**
230+
* *** methods are same as {@link java.lang.Math} methods, guaranteed by the call end ***
231+
*/
232+
233+
/**
234+
* *** methods are same as {@link java.lang.Math} methods by mathematical algorithms***
235+
* /
236+
237+
238+
/**
239+
* mathematical integer: ceil(i) = floor(i) = i
240+
* @return the smallest (closest to negative infinity) double value that is greater
241+
* than or equal to the argument and is equal to a mathematical integer.
242+
*/
243+
public static double ceil(double a) {
244+
return StrictMath.ceil(a);
245+
}
246+
247+
/**
248+
* *** methods are no matters ***
249+
*/
250+
public static double random() {
251+
return StrictMath.random();
252+
}
253+
254+
}

common/src/main/java/org/tron/common/math/MathWrapper.java renamed to platform/src/main/java/x86/org/tron/common/math/MathWrapper.java

File renamed without changes.

0 commit comments

Comments
 (0)