-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.h
103 lines (82 loc) · 2.45 KB
/
utils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
*
* Some utility macros and functions lazily put into static form */
#ifndef _UTILS_H_
#define _UTILS_H_
#include <error.h>
#include <errno.h>
#include <assert.h>
#include "compiler.h"
#if 0
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>
#include <assert.h>
#endif
#define _do_error(fatal, err, file, line, fmt, ...) \
error_at_line(fatal, err, file, line, "%s - " fmt, \
__PRETTY_FUNCTION__, ## __VA_ARGS__)
#define fatal_error(fmt, ...) \
_do_error(1, errno, __FILE__, __LINE__, fmt, ## __VA_ARGS__)
static void randomize(void *p, size_t n, size_t size, unsigned int seed) {
unsigned long *arr = p;
const size_t LONG_BITS = sizeof(unsigned long) * 8;
const size_t RAND_BITS = LONG_BITS - __builtin_clzl((unsigned long)RAND_MAX);
const size_t bytes = n * size;
const size_t count = bytes / sizeof(unsigned long);
size_t i;
//assert(!(bytes % sizeof(*arr)));
//assert(size == sizeof(*arr));
srandom(seed);
for (i = 0; i < count; ++i) {
size_t bits;
arr[i] = (unsigned long)random();
for (bits = RAND_BITS; bits < LONG_BITS; bits += RAND_BITS) {
arr[i] <<= RAND_BITS;
arr[i] ^= (unsigned long)random();
}
// arr[i] >>= 56;
}
/* if not aligned to size of long get the last few bytes */
for (i = 0; i < bytes % sizeof(*arr); ++i) {
((char *)p) [count * sizeof(*arr) + i] = random();
}
}
static inline void timespec_set(struct timespec *ts) {
if (unlikely(errno = clock_gettime(CLOCK_THREAD_CPUTIME_ID, ts)))
fatal_error("clock_gettime");
}
static inline struct timespec timespec_subtract(struct timespec a, struct timespec b) {
const long ONE_BILLION = 1000000000ul;
struct timespec ret = {
.tv_sec = a.tv_sec - b.tv_sec,
.tv_nsec = a.tv_nsec - b.tv_nsec,
};
if (ret.tv_nsec < 0) {
ret.tv_sec -= 1;
ret.tv_nsec += ONE_BILLION;
}
return ret;
}
static inline struct timespec timespec_add(struct timespec a, struct timespec b) {
const long ONE_BILLION = 1000000000ul;
struct timespec ret = {
.tv_sec = a.tv_sec + b.tv_sec,
.tv_nsec = a.tv_nsec + b.tv_nsec,
};
if (ret.tv_nsec >= ONE_BILLION) {
ret.tv_sec += 1;
ret.tv_nsec -= ONE_BILLION;
}
return ret;
}
static double time_pct(struct timespec *a, struct timespec *b) {
const double ONE_BILLION = 1000000000.;
double da = (double)a->tv_nsec + ONE_BILLION *a->tv_sec;
double db = (double)b->tv_nsec + ONE_BILLION *b->tv_sec;
return ((da / db) - 1.) * 100.;
}
#endif /* _UTILS_H_ */