PandA-2024.02
support.h
Go to the documentation of this file.
1 #include <stdlib.h>
2 #include <inttypes.h>
3 
5 char *readfile(int fd);
6 char *find_section_start(char *s, int n);
7 
9 #define SECTION_TERMINATED -1
10 int parse_string(char *s, char *arr, int n); // n==-1 : %%-terminated
11 int parse_uint8_t_array(char *s, uint8_t *arr, int n);
12 int parse_uint16_t_array(char *s, uint16_t *arr, int n);
13 int parse_uint32_t_array(char *s, uint32_t *arr, int n);
14 int parse_uint64_t_array(char *s, uint64_t *arr, int n);
15 int parse_int8_t_array(char *s, int8_t *arr, int n);
16 int parse_int16_t_array(char *s, int16_t *arr, int n);
17 int parse_int32_t_array(char *s, int32_t *arr, int n);
18 int parse_int64_t_array(char *s, int64_t *arr, int n);
19 int parse_float_array(char *s, float *arr, int n);
20 int parse_double_array(char *s, double *arr, int n);
21 
23 int write_string(int fd, char *arr, int n);
24 int write_uint8_t_array(int fd, uint8_t *arr, int n);
25 int write_uint16_t_array(int fd, uint16_t *arr, int n);
26 int write_uint32_t_array(int fd, uint32_t *arr, int n);
27 int write_uint64_t_array(int fd, uint64_t *arr, int n);
28 int write_int8_t_array(int fd, int8_t *arr, int n);
29 int write_int16_t_array(int fd, int16_t *arr, int n);
30 int write_int32_t_array(int fd, int32_t *arr, int n);
31 int write_int64_t_array(int fd, int64_t *arr, int n);
32 int write_float_array(int fd, float *arr, int n);
33 int write_double_array(int fd, double *arr, int n);
34 
35 int write_section_header(int fd);
36 
38 void run_benchmark( void *vargs );
39 void input_to_data(int fd, void *vdata);
40 void data_to_input(int fd, void *vdata);
41 void output_to_data(int fd, void *vdata);
42 void data_to_output(int fd, void *vdata);
43 int check_data(void *vdata, void *vref);
44 
45 extern int INPUT_SIZE;
46 
48 // Macro trick to automatically expand TYPE into the appropriate function
49 // (S)et (T)ype (A)nd (C)oncatenate
50 #define __STAC_EXPANDED(f_pfx,t,f_sfx) f_pfx##t##f_sfx
51 #define STAC(f_pfx,t,f_sfx) __STAC_EXPANDED(f_pfx,t,f_sfx)
52 // Invoke like this:
53 // #define TYPE int32_t
54 // STAC(write_,TYPE,_array)(fd, array, n);
55 // where array is of type (TYPE *)
56 // This translates to:
57 // write_int32_t_array(fd, array, n);
58 
59 
60 /**** PRNG library. Available at https://github.com/rdadolf/prng. *****/
61 #ifndef __PRNG_H__
62 #define __PRNG_H__
63 
64 #include <stdlib.h>
65 #include <stdio.h>
66 #include <inttypes.h>
67 #include <stdint.h>
68 
69 #define LAG1 (UINT16_C(24))
70 #define LAG2 (UINT16_C(55))
71 #define RAND_SSIZE ((UINT16_C(1))<<6)
72 #define RAND_SMASK (RAND_SSIZE-1)
73 #define RAND_EXHAUST_LIMIT LAG2
74 // 10x is a heuristic, it just needs to be large enough to remove correlation
75 #define RAND_REFILL_COUNT ((LAG2*10)-RAND_EXHAUST_LIMIT)
76 struct prng_rand_t {
77  uint64_t s[RAND_SSIZE]; // Lags
78  uint_fast16_t i; // Location of the current lag
79  uint_fast16_t c; // Exhaustion count
80 };
81 
82 #define PRNG_RAND_MAX UINT64_MAX
83 
84 
85 static inline uint64_t prng_rand(struct prng_rand_t *state) {
86  uint_fast16_t i;
87  uint_fast16_t r, new_rands=0;
88 
89  if( !state->c ) { // Randomness exhausted, run forward to refill
90  new_rands += RAND_REFILL_COUNT+1;
91  state->c = RAND_EXHAUST_LIMIT-1;
92  } else {
93  new_rands = 1;
94  state->c--;
95  }
96 
97  for( r=0; r<new_rands; r++ ) {
98  i = state->i;
99  state->s[i&RAND_SMASK] = state->s[(i+RAND_SSIZE-LAG1)&RAND_SMASK]
100  + state->s[(i+RAND_SSIZE-LAG2)&RAND_SMASK];
101  state->i++;
102  }
103  return state->s[i&RAND_SMASK];
104 }
105 
106 static inline void prng_srand(uint64_t seed, struct prng_rand_t *state) {
107  uint_fast16_t i;
108  // Naive seed
109  state->c = RAND_EXHAUST_LIMIT;
110  state->i = 0;
111 
112  state->s[0] = seed;
113  for(i=1; i<RAND_SSIZE; i++) {
114  // Arbitrary magic, mostly to eliminate the effect of low-value seeds.
115  // Probably could be better, but the run-up obviates any real need to.
116  state->s[i] = i*(UINT64_C(2147483647)) + seed;
117  }
118 
119  // Run forward 10,000 numbers
120  for(i=0; i<10000; i++) {
121  prng_rand(state);
122  }
123 }
124 
125 // Clean up our macros
126 #undef LAG1
127 #undef LAG2
128 #undef RAND_SSIZE
129 #undef RAND_SMASK
130 #undef RAND_EXHAUST_LIMIT
131 #undef RAND_REFILL_COUNT
132 
133 // PRNG_RAND_MAX is exported
134 
135 #endif
#define RAND_SSIZE
Definition: support.h:71
static uint64_t prng_rand(struct prng_rand_t *state)
Definition: support.h:85
uint_fast16_t c
Definition: support.h:79
int write_int32_t_array(int fd, int32_t *arr, int n)
int parse_uint32_t_array(char *s, uint32_t *arr, int n)
void data_to_output(int fd, void *vdata)
Definition: local_support.c:63
char * find_section_start(char *s, int n)
Definition: support.c:56
int write_uint16_t_array(int fd, uint16_t *arr, int n)
void data_to_input(int fd, void *vdata)
Definition: local_support.c:34
void run_benchmark(void *vargs)
Definition: local_support.c:6
int parse_uint64_t_array(char *s, uint64_t *arr, int n)
int parse_int32_t_array(char *s, int32_t *arr, int n)
int write_float_array(int fd, float *arr, int n)
int parse_int16_t_array(char *s, int16_t *arr, int n)
#define RAND_REFILL_COUNT
Definition: support.h:75
int write_double_array(int fd, double *arr, int n)
#define LAG1
Definition: support.h:69
#define RAND_EXHAUST_LIMIT
Definition: support.h:73
uint_fast16_t i
Definition: support.h:78
int write_string(int fd, char *arr, int n)
int write_section_header(int fd)
int write_uint64_t_array(int fd, uint64_t *arr, int n)
#define RAND_SMASK
Definition: support.h:72
int write_uint32_t_array(int fd, uint32_t *arr, int n)
int parse_string(char *s, char *arr, int n)
Definition: support.c:77
int write_int16_t_array(int fd, int16_t *arr, int n)
uint64_t s[RAND_SSIZE]
Definition: support.h:77
char * readfile(int fd)
Definition: support.c:34
#define LAG2
Definition: support.h:70
int parse_uint16_t_array(char *s, uint16_t *arr, int n)
int write_int8_t_array(int fd, int8_t *arr, int n)
int write_int64_t_array(int fd, int64_t *arr, int n)
int write_uint8_t_array(int fd, uint8_t *arr, int n)
void output_to_data(int fd, void *vdata)
Definition: local_support.c:49
void input_to_data(int fd, void *vdata)
Definition: local_support.c:18
static void prng_srand(uint64_t seed, struct prng_rand_t *state)
Definition: support.h:106
int check_data(void *vdata, void *vref)
Definition: local_support.c:70
int parse_double_array(char *s, double *arr, int n)
int parse_uint8_t_array(char *s, uint8_t *arr, int n)
int parse_float_array(char *s, float *arr, int n)
int parse_int8_t_array(char *s, int8_t *arr, int n)
int INPUT_SIZE
Definition: local_support.c:4
int parse_int64_t_array(char *s, int64_t *arr, int n)

Generated on Mon Feb 12 2024 13:02:49 for PandA-2024.02 by doxygen 1.8.13