Skip to content

Commit 45525a7

Browse files
kbowers-jumpReisen
authored andcommitted
Solana uses its own custom replacement for stdint
1 parent 29fab29 commit 45525a7

File tree

5 files changed

+208
-3
lines changed

5 files changed

+208
-3
lines changed

program/src/oracle/model/model.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef _pyth_oracle_model_model_h_
22
#define _pyth_oracle_model_model_h_
33

4-
#include <stdint.h>
4+
#include "../util/compat_stdint.h"
55
#include <stdalign.h>
66

77
#ifdef __cplusplus

program/src/oracle/sort/tmpl/sort_stable.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@
5757
Other defines exist to change the sort criteria / direction, linkage
5858
and so forth. See below for details. */
5959

60-
#include <stdint.h> /* For uint64_t */
61-
#include "../../util/align.h" /* For ALIGN_UP */
60+
#include "../../util/compat_stdint.h" /* For uint64_t */
61+
#include "../../util/align.h" /* For ALIGN_UP */
6262

6363
#ifndef SORT_NAME
6464
#error "Define SORT_NAME"

program/src/oracle/util/align.h

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#ifndef _pyth_oracle_util_align_h_
2+
#define _pyth_oracle_util_align_h_
3+
4+
#include "compat_stdint.h" /* For uintptr_t */
5+
#include <stdalign.h> /* For alignof */
6+
7+
#ifdef __cplusplus
8+
extern "C" {
9+
#endif
10+
11+
/* align_ispow2 returns non-zero if a is a non-negative integral power
12+
of 2 and zero otherwise. */
13+
14+
static inline int
15+
align_ispow2( uintptr_t a ) {
16+
uintptr_t mask = a - (uintptr_t)1;
17+
return (a>(uintptr_t)0) & !(a & mask);
18+
}
19+
20+
/* align_isaligned returns non-zero if p has byte alignment a. Assumes
21+
align_ispow2(a) is true. */
22+
23+
static inline int
24+
align_isaligned( void * p,
25+
uintptr_t a ) {
26+
uintptr_t mask = a - (uintptr_t)1;
27+
return !(((uintptr_t)p) & mask);
28+
}
29+
30+
/* align_dn/align_up aligns p to the first byte at or before / after p
31+
with alignment a. Assumes align_ispow2(a) is true. */
32+
33+
static inline void *
34+
align_dn( void * p,
35+
uintptr_t a ) {
36+
uintptr_t mask = a - (uintptr_t)1;
37+
return (void *)(((uintptr_t)p) & (~mask));
38+
}
39+
40+
static inline void *
41+
align_up( void * p,
42+
uintptr_t a ) {
43+
uintptr_t mask = a - (uintptr_t)1;
44+
return (void *)((((uintptr_t)p) + mask) & (~mask));
45+
}
46+
47+
/* Helper macros for aligning pointers up or down to compatible
48+
alignments for type T. FIXME: ADD UNIT TEST COVERAGE */
49+
50+
#define ALIGN_DN( p, T ) ((T *)align_dn( p, (uintptr_t)alignof(T) ))
51+
#define ALIGN_UP( p, T ) ((T *)align_up( p, (uintptr_t)alignof(T) ))
52+
53+
#ifdef __cplusplus
54+
}
55+
#endif
56+
57+
#endif /* _pyth_oracle_util_align_h_ */

program/src/oracle/util/hash.h

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#ifndef _pyth_oracle_util_hash_h_
2+
#define _pyth_oracle_util_hash_h_
3+
4+
/* Useful hashing utilities */
5+
6+
#include "compat_stdint.h"
7+
8+
#ifdef __cplusplus
9+
extern "C" {
10+
#endif
11+
12+
/* High quality (full avalanche) high speed integer to integer hashing.
13+
hash_uint32 has the properties that [0,2^32) hashes to a random
14+
looking permutation of [0,2^32) and hash(0)==0. Similarly for
15+
hash_uint64. Based on Google's Murmur3 hash finalizer (public
16+
domain). Not cryptographically secure but passes various strict
17+
tests of randomness when used as a PRNG (see prng.h). */
18+
19+
static inline uint32_t
20+
hash_uint32( uint32_t x ) {
21+
x ^= x >> 16;
22+
x *= UINT32_C( 0x85ebca6b );
23+
x ^= x >> 13;
24+
x *= UINT32_C( 0xc2b2ae35 );
25+
x ^= x >> 16;
26+
return x;
27+
}
28+
29+
static inline uint64_t
30+
hash_uint64( uint64_t x ) {
31+
x ^= x >> 33;
32+
x *= UINT64_C( 0xff51afd7ed558ccd );
33+
x ^= x >> 33;
34+
x *= UINT64_C( 0xc4ceb9fe1a85ec53 );
35+
x ^= x >> 33;
36+
return x;
37+
}
38+
39+
/* Inverses of the above. E.g.:
40+
hash_inverse_uint32( hash_uint32( (uint32_t)x ) )==(uint32_t)x
41+
and:
42+
hash_uint32( hash_inverse_uint32( (uint32_t)x ) )==(uint32_t)x
43+
Similarly for hash_inverse_uint64. These by themselves are similar
44+
quality hashes to the above and having the inverses of the above can
45+
be useful. The fact these have (nearly) identical operations /
46+
operation counts concretely demonstrates that none of these are
47+
standalone cryptographically secure. */
48+
49+
static inline uint32_t
50+
hash_inverse_uint32( uint32_t x ) {
51+
x ^= x >> 16;
52+
x *= UINT32_C( 0x7ed1b41d );
53+
x ^= (x >> 13) ^ (x >> 26);
54+
x *= UINT32_C( 0xa5cb9243 );
55+
x ^= x >> 16;
56+
return x;
57+
}
58+
59+
static inline uint64_t
60+
hash_inverse_uint64( uint64_t x ) {
61+
x ^= x >> 33;
62+
x *= UINT64_C( 0x9cb4b2f8129337db );
63+
x ^= x >> 33;
64+
x *= UINT64_C( 0x4f74430c22a54005 );
65+
x ^= x >> 33;
66+
return x;
67+
}
68+
69+
#ifdef __cplusplus
70+
}
71+
#endif
72+
73+
#endif /* _pyth_oracle_util_hash_h_ */
74+

program/src/oracle/util/sar.h

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#ifndef _pyth_oracle_util_sar_h_
2+
#define _pyth_oracle_util_sar_h_
3+
4+
/* Portable arithmetic shift right. */
5+
6+
#include "compat_stdint.h"
7+
8+
/* Define PYTH_ORACLE_UTIL_SAR_USE_PLATFORM to non-zero if the platform
9+
does arithmetic right shift signed integer and zero if the platform
10+
does not or is not known to. PYTH_ORACLE_UTIL_SAR_USE_PLATFORM
11+
defaults to zero. */
12+
13+
#ifndef PYTH_ORACLE_UTIL_SAR_USE_PLATFORM
14+
#define PYTH_ORACLE_UTIL_SAR_USE_PLATFORM 0
15+
#endif
16+
17+
#ifdef __cplusplus
18+
extern "C" {
19+
#endif
20+
21+
static inline int8_t
22+
sar_int8( int8_t x,
23+
int n ) { /* Assumed in [0,7] */
24+
# if PYTH_ORACLE_UTIL_SAR_USE_PLATFORM
25+
return x >> n;
26+
# else
27+
uint8_t ux = (uint8_t)x;
28+
uint8_t s = (uint8_t)-(ux >> 7); /* get the sign, ((int8_t)s)==-1 if x<0 and 0 otherwise */
29+
return (int8_t)(((ux ^ s) >> n) ^ s);
30+
# endif
31+
}
32+
33+
static inline int16_t
34+
sar_int16( int16_t x,
35+
int n ) { /* Assumed in [0,15] */
36+
# if PYTH_ORACLE_UTIL_SAR_USE_PLATFORM
37+
return x >> n;
38+
# else
39+
uint16_t ux = (uint16_t)x;
40+
uint16_t s = (uint16_t)-(ux >> 15); /* get the sign, ((int16_t)s)==-1 if x<0 and 0 otherwise */
41+
return (int16_t)(((ux ^ s) >> n) ^ s);
42+
# endif
43+
}
44+
45+
static inline int32_t
46+
sar_int32( int32_t x,
47+
int n ) { /* Assumed in [0,31] */
48+
# if PYTH_ORACLE_UTIL_SAR_USE_PLATFORM
49+
return x >> n;
50+
# else
51+
uint32_t ux = (uint32_t)x;
52+
uint32_t s = -(ux >> 31); /* get the sign, ((int32_t)s)==-1 if x<0 and 0 otherwise */
53+
return (int32_t)(((ux ^ s) >> n) ^ s);
54+
# endif
55+
}
56+
57+
static inline int64_t
58+
sar_int64( int64_t x,
59+
int n ) { /* Assumed in [0,63] */
60+
# if PYTH_ORACLE_UTIL_SAR_USE_PLATFORM
61+
return x >> n;
62+
# else
63+
uint64_t ux = (uint64_t)x;
64+
uint64_t s = -(ux >> 63); /* get the sign, ((int64_t)s)==-1 if x<0 and 0 otherwise */
65+
return (int64_t)(((ux ^ s) >> n) ^ s);
66+
# endif
67+
}
68+
69+
#ifdef __cplusplus
70+
}
71+
#endif
72+
73+
#endif /* _pyth_oracle_util_sar_h_ */
74+

0 commit comments

Comments
 (0)