weighted check

master
Avril 4 years ago
parent 368a61b6a8
commit a8ad26ec5e
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -9,9 +9,11 @@
#define BUFFER_SIZE 4096 #define BUFFER_SIZE 4096
#define IGNORE(param) do { (void)(param); } while(0) #define IGNORE(param) do { (void)(param); } while(0)
#define LIKELY(ex) __builtin_expect((ex), 1)
#define UNLIKELY(ex) __builtin_expect((ex), 0)
//TODO: For explicitly threaded version: Make a lazy thread spawner, spawning up to 8 threads which will each work on a byte and atomically write back to a central array of `uint64_t [num_of_bytes / num_of_threads]`. #define LIF(ex) if(LIKELY(!!(ex)))
//TODO: But how to sync the fwrites of this buffer? An atomic counter of which threads have completed their operations, and a condvar that allows the controlling thread to check if the counter is full before `fwrite_all()`ing the array, resetting the counter, and carrying on #define UIF(ex) if(UNLIKELY(!!(ex)))
static inline int sfread(void* out, size_t *restrict size, FILE* f) static inline int sfread(void* out, size_t *restrict size, FILE* f)
{ {
@ -28,15 +30,15 @@ static int fwrite_all(const void* _buf, size_t sz, size_t num, FILE* out)
else return 1; else return 1;
} }
static void pbits(char out[restrict 8], unsigned char byte, const char bit[static 2]) static inline void pbits(char out[restrict 8], unsigned char byte, const char bit[static 2])
{ {
for(register int i=8; i --> 0; byte >>= 1) *out++=bit[byte & 1]; for(register int i=8; i --> 0; byte >>= 1) *out++=bit[byte & 1];
//TODO: In explicitly threaded version, we can use an atomic (atomic.h) 64-bit integer `store()` to output those 8 bytes in `out`, which will be re-cast as `uint64_t out[restrict 1]`
} }
static inline int check(int val, const char* msg) // Light assertion (likely to succeed)
static inline int lcheck(int val, const char* msg)
{ {
if(val) return val; LIF(val) return val;
fprintf(stderr, "error: %s\n", msg); fprintf(stderr, "error: %s\n", msg);
exit(1); exit(1);
} }
@ -45,7 +47,7 @@ int main(int argc, char **argv)
{ {
IGNORE(argc); IGNORE(argc);
const char* mask = argv[1] && check(strlen(argv[1]) > 1, "expected 2 character mask") ? argv[1] : "01"; const char* mask = argv[1] && lcheck(strlen(argv[1]) > 1, "expected 2 character mask") ? argv[1] : "01";
unsigned char buf[BUFFER_SIZE]; unsigned char buf[BUFFER_SIZE];
char tbuf[BUFFER_SIZE * 8]; char tbuf[BUFFER_SIZE * 8];
size_t bsz = BUFFER_SIZE; size_t bsz = BUFFER_SIZE;

Loading…
Cancel
Save