Skip to content

Commit

Permalink
pd.h: Change static to extern inline when not building for BPF.
Browse files Browse the repository at this point in the history
- Allows including `pd.h` in test sources without adding `-Wno-unused-function`. See pyth-network#91
  • Loading branch information
Tony Ricciardi authored and tony-ricciardi committed Oct 18, 2021
1 parent 418edba commit 580cd37
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions program/src/oracle/pd.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@
extern "C" {
#endif

// TODO Remove PD_INLINE and move definitions to a pd.c file.
#ifdef __bpf__
#define PD_INLINE static
#else
// Avoid `-Wunused-function` in test sources, etc.
#define PD_INLINE extern inline
#endif

#define PD_SCALE9 (1000000000L)
#define PD_EMA_MAX_DIFF 4145 // maximum slots before reset
#define PD_EMA_EXPO -9 // exponent of temporary storage
#define PD_EMA_EXPO (-9) // exponent of temporary storage
#define PD_EMA_DECAY (-117065) // 1e9*-log(2)/5921
#define PC_FACTOR_SIZE 18

Expand All @@ -24,15 +32,15 @@ typedef struct pd
int64_t v_;
} pd_t;

static void pd_scale( pd_t *n )
PD_INLINE void pd_scale( pd_t *n )
{
int const neg = n->v_ < 0L;
int64_t v = neg ? -n->v_ : n->v_; // make v positive for loop condition
for( ; v >= ( 1L << 28 ); v /= 10L, ++n->e_ );
n->v_ = neg ? -v : v;
}

static bool pd_store( int64_t *r, pd_t const *n )
PD_INLINE bool pd_store( int64_t *r, pd_t const *n )
{
int64_t v = n->v_;
int32_t e = n->e_;
Expand Down Expand Up @@ -65,7 +73,7 @@ void pd_load( pd_t *r, int64_t const n )
pd_scale( r );
}

static void pd_adjust( pd_t *n, int e, const int64_t *p )
PD_INLINE void pd_adjust( pd_t *n, int e, const int64_t *p )
{
int64_t v = n->v_;
int d = n->e_ - e;
Expand All @@ -78,14 +86,14 @@ static void pd_adjust( pd_t *n, int e, const int64_t *p )
pd_new( n, v, e );
}

static void pd_mul( pd_t *r, const pd_t *n1, const pd_t *n2 )
PD_INLINE void pd_mul( pd_t *r, const pd_t *n1, const pd_t *n2 )
{
r->v_ = n1->v_ * n2->v_;
r->e_ = n1->e_ + n2->e_;
pd_scale( r );
}

static void pd_div( pd_t *r, pd_t *n1, pd_t *n2 )
PD_INLINE void pd_div( pd_t *r, pd_t *n1, pd_t *n2 )
{
if ( n1->v_ == 0 ) { pd_set( r, n1 ); return; }
int64_t v1 = n1->v_, v2 = n2->v_;
Expand All @@ -100,7 +108,7 @@ static void pd_div( pd_t *r, pd_t *n1, pd_t *n2 )
pd_scale( r );
}

static void pd_add( pd_t *r, const pd_t *n1, const pd_t *n2, const int64_t *p )
PD_INLINE void pd_add( pd_t *r, const pd_t *n1, const pd_t *n2, const int64_t *p )
{
int d = n1->e_ - n2->e_;
if ( d==0 ) {
Expand All @@ -126,7 +134,7 @@ static void pd_add( pd_t *r, const pd_t *n1, const pd_t *n2, const int64_t *p )
pd_scale( r );
}

static void pd_sub( pd_t *r, const pd_t *n1, const pd_t *n2, const int64_t *p )
PD_INLINE void pd_sub( pd_t *r, const pd_t *n1, const pd_t *n2, const int64_t *p )
{
int d = n1->e_ - n2->e_;
if ( d==0 ) {
Expand All @@ -152,21 +160,21 @@ static void pd_sub( pd_t *r, const pd_t *n1, const pd_t *n2, const int64_t *p )
pd_scale( r );
}

static int pd_lt( const pd_t *n1, const pd_t *n2, const int64_t *p )
PD_INLINE int pd_lt( const pd_t *n1, const pd_t *n2, const int64_t *p )
{
pd_t r[1];
pd_sub( r, n1, n2, p );
return r->v_ < 0L;
}

static int pd_gt( const pd_t *n1, const pd_t *n2, const int64_t *p )
PD_INLINE int pd_gt( const pd_t *n1, const pd_t *n2, const int64_t *p )
{
pd_t r[1];
pd_sub( r, n1, n2, p );
return r->v_ > 0L;
}

static void pd_sqrt( pd_t *r, pd_t *val, const int64_t *f )
PD_INLINE void pd_sqrt( pd_t *r, pd_t *val, const int64_t *f )
{
pd_t t[1], x[1], hlf[1];
pd_set( t, val );
Expand Down

0 comments on commit 580cd37

Please sign in to comment.