Skip to content

Commit c4b90f4

Browse files
committed
avoid signed conversion
1 parent 56a9e4d commit c4b90f4

File tree

3 files changed

+26
-22
lines changed

3 files changed

+26
-22
lines changed

program/src/oracle/oracle.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ static uint64_t upd_product( SolParameters *prm, SolAccountInfo *ka )
173173
sizeof( cmd_upd_product_t ) - sizeof( pc_prod_t ) ) {
174174
return ERROR_INVALID_ARGUMENT;
175175
}
176-
pptr->size_ = sizeof ( pc_prod_t ) + prm->data_len -
177-
sizeof( cmd_upd_product_t );
176+
pptr->size_ = ( uint32_t )( sizeof( pc_prod_t ) + prm->data_len -
177+
sizeof( cmd_upd_product_t ) );
178178
uint8_t *tgt = (uint8_t*)pptr + sizeof( pc_prod_t );
179179
const uint8_t *src = prm->data + sizeof( cmd_upd_product_t );
180180
const uint8_t *end = prm->data + prm->data_len;

program/src/oracle/test_oracle.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -236,14 +236,14 @@ Test(oracle, add_product) {
236236
mptr->magic_ = PC_MAGIC;
237237
mptr->ver_ = PC_VERSION;
238238
mptr->type_ = PC_ACCTYPE_MAPPING;
239-
for(int i =0;;++i) {
239+
for( unsigned i = 0;; ++i ) {
240240
sol_memset( sptr, 0, PC_PROD_ACC_SIZE );
241241
uint64_t rc = dispatch( &prm, acc );
242242
if ( rc != SUCCESS ) {
243-
cr_assert( i == (PC_MAP_TABLE_SIZE) );
243+
cr_assert( i == ( unsigned )(PC_MAP_TABLE_SIZE) );
244244
break;
245245
}
246-
cr_assert( mptr->num_ == i+1 );
246+
cr_assert( mptr->num_ == i + 1 );
247247
cr_assert( rc == SUCCESS );
248248
}
249249
}
@@ -310,14 +310,14 @@ Test( oracle, add_publisher ) {
310310
sptr->magic_ = PC_MAGIC;
311311

312312
// fill up price node
313-
for(int i =0;;++i) {
314-
idata.pub_.k8_[0] = 10+i;
313+
for( unsigned i = 0;; ++i ) {
314+
idata.pub_.k8_[0] = 10 + i;
315315
uint64_t rc = dispatch( &prm, acc );
316316
if ( rc != SUCCESS ) {
317-
cr_assert( i == (PC_COMP_SIZE) );
317+
cr_assert( i == ( unsigned )(PC_COMP_SIZE) );
318318
break;
319319
}
320-
cr_assert( sptr->num_ == i+1 );
320+
cr_assert( sptr->num_ == i + 1 );
321321
cr_assert( pc_pub_key_equal( &idata.pub_, &sptr->comp_[i].pub_ ) );
322322
cr_assert( rc == SUCCESS );
323323
}

program/src/oracle/upd_aggregate.h

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,15 @@ static void pd_scale( pd_t *n )
2323

2424
static void pd_adjust( pd_t *n, int e, const int64_t *p )
2525
{
26-
int neg = n->v_ < 0L;
27-
uint64_t v = neg?-n->v_:n->v_;
26+
int64_t v = n->v_;
2827
int d = n->e_ - e;
29-
if ( d>0 ) v *= p[d]; else if ( d<0 ) v /= p[-d];
30-
pd_new( n, neg ? -v : v, e );
28+
if ( d > 0 ) {
29+
v *= p[ d ];
30+
}
31+
else if ( d < 0 ) {
32+
v /= p[ -d ];
33+
}
34+
pd_new( n, v, e );
3135
}
3236

3337
static void pd_mul( pd_t *r, pd_t *n1, pd_t *n2 )
@@ -41,11 +45,11 @@ static void pd_div( pd_t *r, pd_t *n1, pd_t *n2 )
4145
{
4246
if ( n1->v_ == 0 ) { pd_set( r, n1 ); return; }
4347
int64_t v1 = n1->v_, v2 = n2->v_;
44-
int neg1 = v1<0L, neg2 = v2<0L, m=0;
48+
int neg1 = v1 < 0L, neg2 = v2 < 0L, m = 0;
4549
if ( neg1 ) v1 = -v1;
4650
if ( neg2 ) v2 = -v2;
47-
for( ;0L == (v1&0xfffffffff0000000); v1*= 10L, ++m );
48-
r->v_ = (v1 * PD_SCALE9) / v2;
51+
for( ; 0UL == ( ( uint64_t )v1 & 0xfffffffff0000000UL ); v1 *= 10L, ++m );
52+
r->v_ = ( v1 * PD_SCALE9 ) / v2;
4953
if ( neg1 ) r->v_ = -r->v_;
5054
if ( neg2 ) r->v_ = -r->v_;
5155
r->e_ = n1->e_ - n2->e_ - m - 9;
@@ -243,7 +247,7 @@ static inline void upd_twap(
243247
{
244248
pd_t px[1], conf[1];
245249
pd_new_scale( px, ptr->agg_.price_, ptr->expo_ );
246-
pd_new_scale( conf, ptr->agg_.conf_, ptr->expo_ );
250+
pd_new_scale( conf, ( int64_t )( ptr->agg_.conf_ ), ptr->expo_ );
247251
upd_ema( &ptr->twap_, px, conf, nslots, qs );
248252
upd_ema( &ptr->twac_, conf, conf, nslots, qs );
249253
}
@@ -279,7 +283,7 @@ static void upd_aggregate( pc_price_t *ptr, uint64_t slot )
279283
pc_qset_t *qs = qset_new( ptr->expo_ );
280284

281285
// get number of slots from last published valid price
282-
int64_t agg_diff = slot - ptr->last_slot_;
286+
int64_t agg_diff = ( int64_t )slot - ( int64_t )( ptr->last_slot_ );
283287

284288
// copy previous price
285289
ptr->prev_slot_ = ptr->valid_slot_;
@@ -298,7 +302,7 @@ static void upd_aggregate( pc_price_t *ptr, uint64_t slot )
298302
// copy contributing price to aggregate snapshot
299303
iptr->agg_ = iptr->latest_;
300304
// add quote to sorted permutation array if it is valid
301-
int64_t slot_diff = slot - iptr->agg_.pub_slot_;
305+
int64_t slot_diff = ( int64_t )slot - ( int64_t )( iptr->agg_.pub_slot_ );
302306
if ( iptr->agg_.status_ == PC_STATUS_TRADING &&
303307
iptr->agg_.conf_ != 0UL &&
304308
iptr->agg_.price_ != 0L &&
@@ -344,10 +348,10 @@ static void upd_aggregate( pc_price_t *ptr, uint64_t slot )
344348
for( uint32_t i=0;i != numa; ++i ) {
345349
pc_price_comp_t *iptr = &ptr->comp_[aidx[i]];
346350
// scale confidence interval by sqrt of slot age
347-
int64_t slot_diff = slot - iptr->agg_.pub_slot_;
351+
int64_t slot_diff = ( int64_t )slot - ( int64_t )( iptr->agg_.pub_slot_ );
348352
pd_t decay[1];
349353
pd_new( decay, qs->decay_[slot_diff], PC_EXP_DECAY );
350-
pd_new_scale( conf, iptr->agg_.conf_, ptr->expo_ );
354+
pd_new_scale( conf, ( int64_t )( iptr->agg_.conf_ ), ptr->expo_ );
351355
pd_mul( conf, conf, decay );
352356

353357
// assign price and upper/lower bounds of price
@@ -434,7 +438,7 @@ static void upd_aggregate( pc_price_t *ptr, uint64_t slot )
434438
// take confidence interval as larger
435439
pd_t *cptr = pd_gt( uprice, q3price, qs->fact_ ) ? uprice : q3price;
436440
pd_adjust( cptr, ptr->expo_, qs->fact_ );
437-
ptr->agg_.conf_ = cptr->v_;
441+
ptr->agg_.conf_ = ( uint64_t )( cptr->v_ );
438442
upd_twap( ptr, agg_diff, qs );
439443
}
440444

0 commit comments

Comments
 (0)