Skip to content

Commit 0b65cd4

Browse files
committed
use some bits for storing exponent to avoid overflow
1 parent a819a9e commit 0b65cd4

File tree

7 files changed

+390
-357
lines changed

7 files changed

+390
-357
lines changed

program/src/oracle/pd.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ extern "C" {
1010
#define PD_EMA_DECAY (-117065) // 1e9*-log(2)/5921
1111
#define PC_FACTOR_SIZE 18
1212

13+
#define EXP_BITS 5
14+
#define EXP_MASK ( ( 1L << EXP_BITS ) - 1 )
15+
1316
#define pd_new( n,v,e ) {(n)->v_=v;(n)->e_=e;}
1417
#define pd_set( r,n ) (r)[0] = (n)[0]
1518
#define pd_new_scale(n,v,e) {(n)->v_=v;(n)->e_=e;pd_scale(n);}
@@ -29,6 +32,39 @@ static void pd_scale( pd_t *n )
2932
n->v_ = neg ? -v : v;
3033
}
3134

35+
static bool pd_store( int64_t *r, pd_t const *n )
36+
{
37+
int64_t v = n->v_;
38+
int32_t e = n->e_;
39+
while ( v < -( 1L << 58 ) ) {
40+
v /= 10;
41+
++e;
42+
}
43+
while ( v > ( 1L << 58 ) - 1 ) {
44+
v /= 10;
45+
++e;
46+
}
47+
while ( e < -( 1 << ( EXP_BITS - 1 ) ) ) {
48+
v /= 10;
49+
++e;
50+
}
51+
while ( e > ( 1 << ( EXP_BITS - 1 ) ) - 1 ) {
52+
v *= 10;
53+
if ( v < -( 1L << 58 ) || v > ( 1L << 58 ) - 1 ) {
54+
return false;
55+
}
56+
--e;
57+
}
58+
*r = ( v << EXP_BITS ) | ( e & EXP_MASK );
59+
return true;
60+
}
61+
62+
void pd_load( pd_t *r, int64_t const n )
63+
{
64+
pd_new( r, n >> EXP_BITS, ( ( n & EXP_MASK ) << 59 ) >> 59 );
65+
pd_scale( r );
66+
}
67+
3268
static void pd_adjust( pd_t *n, int e, const int64_t *p )
3369
{
3470
int64_t v = n->v_;

program/src/oracle/upd_aggregate.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
extern "C" {
1111
#endif
1212

13-
// quote info for aggregate price calc
1413
typedef struct pc_qset
1514
{
1615
pd_t iprice_[PC_COMP_SIZE];
@@ -103,8 +102,8 @@ static void upd_ema(
103102
pd_add( decay, decay, one, qs->fact_ );
104103

105104
// compute numer/denom and new value from decay factor
106-
pd_new_scale( numer, ptr->numer_, PD_EMA_EXPO );
107-
pd_new_scale( denom, ptr->denom_, PD_EMA_EXPO );
105+
pd_load( numer, ptr->numer_ );
106+
pd_load( denom, ptr->denom_ );
108107
pd_mul( numer, numer, decay );
109108
pd_mul( wval, val, cwgt );
110109
pd_add( numer, numer, wval, qs->fact_ );
@@ -115,11 +114,9 @@ static void upd_ema(
115114

116115
// adjust and store results
117116
pd_adjust( val, qs->expo_, qs->fact_ );
118-
pd_adjust( numer, PD_EMA_EXPO, qs->fact_ );
119-
pd_adjust( denom, PD_EMA_EXPO, qs->fact_ );
120117
ptr->val_ = val->v_;
121-
ptr->numer_ = numer->v_;
122-
ptr->denom_ = denom->v_;
118+
pd_store( &ptr->numer_, numer );
119+
pd_store( &ptr->denom_, denom );
123120
}
124121

125122
static inline void upd_twap(

pyth/tests/twap/audit_overflow.result

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,42 +17,42 @@ price,conf,expo,nslots,twap,twac
1717
5400000000,10,0,1,5399999900,10
1818
5400000000,10,0,1,5399999900,10
1919
5400000000,10,0,1,5399999900,10
20-
5400000000,10,0,1,-4317908700,10
21-
5400000000,10,0,1,-3831472600,10
22-
5400000000,10,0,1,-3391363800,10
23-
5400000000,10,0,1,-2991264900,10
24-
5400000000,10,0,1,-2625957220,10
25-
5400000000,10,0,1,-2291091870,10
26-
5400000000,10,0,1,-1983015760,10
27-
5400000000,10,0,1,-1698637830,10
28-
5400000000,10,0,1,-1435324970,10
29-
5400000000,10,0,1,-1190820180,10
30-
5400000000,10,0,1,-963177770,10
31-
5400000000,10,0,1,-750711560,10
32-
5400000000,10,0,1,-551952870,10
33-
5400000000,10,0,1,-365616590,10
34-
5400000000,10,0,1,-190573435,10
35-
5400000000,10,0,1,-25826945,10
36-
5400000000,10,0,1,129505450,10
37-
5400000000,10,0,1,276208260,10
38-
5400000000,10,0,1,414981170,10
39-
5400000000,10,0,1,546450240,10
40-
5400000000,10,0,1,671177310,10
41-
5400000000,10,0,1,789668000,10
42-
5400000000,10,0,1,902378650,10
43-
5400000000,10,0,1,1009722100,10
44-
5400000000,10,0,1,1112072850,10
45-
5400000000,10,0,1,1209771270,10
46-
5400000000,10,0,1,1303127550,10
47-
5400000000,10,0,1,1392424830,10
48-
5400000000,10,0,1,1477922240,10
49-
5400000000,10,0,1,1559857230,10
50-
5400000000,10,0,1,1638447910,10
51-
5400000000,10,0,1,1713894980,10
52-
5400000000,10,0,1,1786383320,10
53-
5400000000,10,0,1,1856083660,10
54-
5400000000,10,0,1,-1567561520,10
55-
5400000000,10,0,1,-1438131840,10
56-
5400000000,10,0,1,-1313408720,10
57-
5400000000,10,0,1,-1193139990,10
58-
5400000000,10,0,1,-1077091250,10
20+
5400000000,10,0,1,5399999900,10
21+
5400000000,10,0,1,5399999900,10
22+
5400000000,10,0,1,5399999900,10
23+
5400000000,10,0,1,5399999900,10
24+
5400000000,10,0,1,5399999900,10
25+
5400000000,10,0,1,5399999900,10
26+
5400000000,10,0,1,5399999900,10
27+
5400000000,10,0,1,5399999900,10
28+
5400000000,10,0,1,5399999900,10
29+
5400000000,10,0,1,5400000100,10
30+
5400000000,10,0,1,5400000100,10
31+
5400000000,10,0,1,5400000200,10
32+
5400000000,10,0,1,5400000300,10
33+
5400000000,10,0,1,5400000300,10
34+
5400000000,10,0,1,5400000300,10
35+
5400000000,10,0,1,5400000500,10
36+
5400000000,10,0,1,5400000500,10
37+
5400000000,10,0,1,5400000500,10
38+
5400000000,10,0,1,5400000500,10
39+
5400000000,10,0,1,5400000600,10
40+
5400000000,10,0,1,5400000700,10
41+
5400000000,10,0,1,5400000700,10
42+
5400000000,10,0,1,5400000800,10
43+
5400000000,10,0,1,5400000800,10
44+
5400000000,10,0,1,5400000900,10
45+
5400000000,10,0,1,5400000900,10
46+
5400000000,10,0,1,5400001000,10
47+
5400000000,10,0,1,5400001000,10
48+
5400000000,10,0,1,5400001100,10
49+
5400000000,10,0,1,5400001100,10
50+
5400000000,10,0,1,5400001100,10
51+
5400000000,10,0,1,5400001100,10
52+
5400000000,10,0,1,5400001100,10
53+
5400000000,10,0,1,5400001100,10
54+
5400000000,10,0,1,5400001000,10
55+
5400000000,10,0,1,5400000900,10
56+
5400000000,10,0,1,5400000800,10
57+
5400000000,10,0,1,5400000700,10
58+
5400000000,10,0,1,5400000600,10

pyth/tests/twap/price10_conf1.result

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,39 @@ price,conf,expo,nslots,twap,twac
77
1234567890,1,0,1,1234567820,1
88
1234567890,1,0,1,1234567830,1
99
1234567890,1,0,1,1234567830,1
10-
1234567890,1,0,1,-815790370,1
11-
1234567890,1,0,1,-610646500,1
12-
1234567890,1,0,1,-442801510,1
13-
1234567890,1,0,1,-302930690,1
14-
1234567890,1,0,1,-184578473,1
15-
1234567890,1,0,1,-83133713,1
16-
1234567890,1,0,1,4785076,1
17-
1234567890,1,0,1,81714014,1
18-
1234567890,1,0,1,149592486,1
19-
1234567890,1,0,1,209928901,1
20-
1234567890,1,0,1,263914106,1
21-
1234567890,1,0,1,312500780,1
22-
1234567890,1,0,1,356460160,1
23-
1234567890,1,0,1,396423230,1
24-
1234567890,1,0,1,432911250,1
25-
1234567890,1,0,1,-303200950,1
26-
1234567890,1,0,1,-241603736,1
27-
1234567890,1,0,1,-184744771,1
28-
1234567890,1,0,1,-132097585,1
29-
1234567890,1,0,1,-83210916,1
30-
1234567890,1,0,1,-37695744,1
31-
1234567890,1,0,1,4785080,1
32-
1234567890,1,0,1,44525205,1
33-
1234567890,1,0,1,81781570,1
34-
1234567890,1,0,1,116779971,1
35-
1234567890,1,0,1,149719638,1
36-
1234567890,1,0,1,180777037,1
37-
1234567890,1,0,1,210109023,1
38-
1234567890,1,0,1,237855493,1
39-
1234567890,1,0,1,264141621,1
40-
1234567890,1,0,1,-184911203,1
41-
1234567890,1,0,1,-149343142,1
42-
1234567890,1,0,1,-115510112,1
43-
1234567890,1,0,1,-83288179,1
44-
1234567890,1,0,1,-52564945,1
45-
1234567890,1,0,1,-23238224,1
10+
1234567890,1,0,1,1234567830,1
11+
1234567890,1,0,1,1234567830,1
12+
1234567890,1,0,1,1234567820,1
13+
1234567890,1,0,1,1234567820,1
14+
1234567890,1,0,1,1234567820,1
15+
1234567890,1,0,1,1234567820,1
16+
1234567890,1,0,1,1234567830,1
17+
1234567890,1,0,1,1234567830,1
18+
1234567890,1,0,1,1234567830,1
19+
1234567890,1,0,1,1234567830,1
20+
1234567890,1,0,1,1234567830,1
21+
1234567890,1,0,1,1234567830,1
22+
1234567890,1,0,1,1234567820,1
23+
1234567890,1,0,1,1234567800,1
24+
1234567890,1,0,1,1234567760,1
25+
1234567890,1,0,1,1234567730,1
26+
1234567890,1,0,1,1234567660,1
27+
1234567890,1,0,1,1234567610,1
28+
1234567890,1,0,1,1234567580,1
29+
1234567890,1,0,1,1234567570,1
30+
1234567890,1,0,1,1234567550,1
31+
1234567890,1,0,1,1234567530,1
32+
1234567890,1,0,1,1234567540,1
33+
1234567890,1,0,1,1234567510,1
34+
1234567890,1,0,1,1234567510,1
35+
1234567890,1,0,1,1234567510,1
36+
1234567890,1,0,1,1234567500,1
37+
1234567890,1,0,1,1234567490,1
38+
1234567890,1,0,1,1234567470,1
39+
1234567890,1,0,1,1234567460,1
40+
1234567890,1,0,1,1234567460,1
41+
1234567890,1,0,1,1234567440,1
42+
1234567890,1,0,1,1234567430,1
43+
1234567890,1,0,1,1234567430,1
44+
1234567890,1,0,1,1234567440,1
45+
1234567890,1,0,1,1234567430,1

pyth/tests/twap/price10_conf2.result

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -75,62 +75,62 @@ price,conf,expo,nslots,twap,twac
7575
1234567890,10,0,1,1234567320,10
7676
1234567890,10,0,1,1234567320,10
7777
1234567890,10,0,1,1234567330,10
78-
1234567890,10,0,1,-1171506650,10
79-
1234567890,10,0,1,-1140520280,10
80-
1234567890,10,0,1,-1110318390,10
81-
1234567890,10,0,1,-1080871540,10
82-
1234567890,10,0,1,-1052151780,10
83-
1234567890,10,0,1,-1024132510,10
84-
1234567890,10,0,1,-996788410,10
85-
1234567890,10,0,1,-970095360,10
86-
1234567890,10,0,1,-944030390,10
87-
1234567890,10,0,1,-918571600,10
88-
1234567890,10,0,1,-893698060,10
89-
1234567890,10,0,1,-869389830,10
90-
1234567890,10,0,1,-845627850,10
91-
1234567890,10,0,1,-822393920,10
92-
1234567890,10,0,1,-799670650,10
93-
1234567890,10,0,1,-777441350,10
94-
1234567890,10,0,1,-755690120,10
95-
1234567890,10,0,1,-734401680,10
96-
1234567890,10,0,1,-713561420,10
97-
1234567890,10,0,1,-693155340,10
98-
1234567890,10,0,1,-673169990,10
99-
1234567890,10,0,1,-653592530,10
100-
1234567890,10,0,1,-634410560,10
101-
1234567890,10,0,1,-615612250,10
102-
1234567890,10,0,1,-597186180,10
103-
1234567890,10,0,1,-579121420,10
104-
1234567890,10,0,1,-561407430,10
105-
1234567890,10,0,1,-544034090,10
106-
1234567890,10,0,1,-526991690,10
107-
1234567890,10,0,1,-510270840,10
108-
1234567890,10,0,1,-493862530,10
109-
1234567890,10,0,1,-477758090,10
110-
1234567890,10,0,1,-461949140,10
111-
1234567890,10,0,1,-446427640,10
112-
1234567890,10,0,1,-431185800,10
113-
1234567890,10,0,1,-416216140,10
114-
1234567890,10,0,1,-401511430,10
115-
1234567890,10,0,1,-387064710,10
116-
1234567890,10,0,1,-372869240,10
117-
1234567890,10,0,1,-358918520,10
118-
1234567890,10,0,1,-345206280,10
119-
1234567890,10,0,1,-331726450,10
120-
1234567890,10,0,1,-318473180,10
121-
1234567890,10,0,1,-305440800,10
122-
1234567890,10,0,1,-292623830,10
123-
1234567890,10,0,1,-280016970,10
124-
1234567890,10,0,1,-267615120,10
125-
1234567890,10,0,1,-255413296,10
126-
1234567890,10,0,1,-243406703,10
127-
1234567890,10,0,1,-231590696,10
128-
1234567890,10,0,1,-219960771,10
129-
1234567890,10,0,1,-208512568,10
130-
1234567890,10,0,1,-197241865,10
131-
1234567890,10,0,1,-186144561,10
132-
1234567890,10,0,1,-175216684,10
133-
1234567890,10,0,1,-164454384,10
134-
1234567890,10,0,1,-153853927,10
135-
1234567890,10,0,1,-143411688,10
136-
1234567890,10,0,1,-133124152,10
78+
1234567890,10,0,1,1234567320,10
79+
1234567890,10,0,1,1234567320,10
80+
1234567890,10,0,1,1234567320,10
81+
1234567890,10,0,1,1234567330,10
82+
1234567890,10,0,1,1234567320,10
83+
1234567890,10,0,1,1234567320,10
84+
1234567890,10,0,1,1234567320,10
85+
1234567890,10,0,1,1234567320,10
86+
1234567890,10,0,1,1234567330,10
87+
1234567890,10,0,1,1234567330,10
88+
1234567890,10,0,1,1234567330,10
89+
1234567890,10,0,1,1234567320,10
90+
1234567890,10,0,1,1234567310,10
91+
1234567890,10,0,1,1234567300,10
92+
1234567890,10,0,1,1234567290,10
93+
1234567890,10,0,1,1234567290,10
94+
1234567890,10,0,1,1234567280,10
95+
1234567890,10,0,1,1234567290,10
96+
1234567890,10,0,1,1234567280,10
97+
1234567890,10,0,1,1234567270,10
98+
1234567890,10,0,1,1234567260,10
99+
1234567890,10,0,1,1234567270,10
100+
1234567890,10,0,1,1234567270,10
101+
1234567890,10,0,1,1234567270,10
102+
1234567890,10,0,1,1234567270,10
103+
1234567890,10,0,1,1234567270,10
104+
1234567890,10,0,1,1234567260,10
105+
1234567890,10,0,1,1234567260,10
106+
1234567890,10,0,1,1234567260,10
107+
1234567890,10,0,1,1234567260,10
108+
1234567890,10,0,1,1234567260,10
109+
1234567890,10,0,1,1234567260,10
110+
1234567890,10,0,1,1234567260,10
111+
1234567890,10,0,1,1234567260,10
112+
1234567890,10,0,1,1234567260,10
113+
1234567890,10,0,1,1234567250,10
114+
1234567890,10,0,1,1234567260,10
115+
1234567890,10,0,1,1234567260,10
116+
1234567890,10,0,1,1234567260,10
117+
1234567890,10,0,1,1234567260,10
118+
1234567890,10,0,1,1234567260,10
119+
1234567890,10,0,1,1234567260,10
120+
1234567890,10,0,1,1234567260,10
121+
1234567890,10,0,1,1234567270,10
122+
1234567890,10,0,1,1234567260,10
123+
1234567890,10,0,1,1234567260,10
124+
1234567890,10,0,1,1234567260,10
125+
1234567890,10,0,1,1234567260,10
126+
1234567890,10,0,1,1234567260,10
127+
1234567890,10,0,1,1234567260,10
128+
1234567890,10,0,1,1234567260,10
129+
1234567890,10,0,1,1234567260,10
130+
1234567890,10,0,1,1234567260,10
131+
1234567890,10,0,1,1234567260,10
132+
1234567890,10,0,1,1234567260,10
133+
1234567890,10,0,1,1234567260,10
134+
1234567890,10,0,1,1234567260,10
135+
1234567890,10,0,1,1234567260,10
136+
1234567890,10,0,1,1234567250,10

0 commit comments

Comments
 (0)