Skip to content

Commit f25d650

Browse files
committed
feat(math): add hard-code for arm pow
1 parent 909cd2b commit f25d650

File tree

4 files changed

+311
-7
lines changed

4 files changed

+311
-7
lines changed

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

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)