-
Notifications
You must be signed in to change notification settings - Fork 7
/
util.h
35 lines (27 loc) · 875 Bytes
/
util.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
/* Every great project has to either have a util.h, or a misc.h */
#ifndef _UTIL_H
#define _UTIL_H 1
#include <assert.h>
#include <stdlib.h>
#include <ck_spinlock.h>
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#define P(percentile, arr) (int)(((double)percentile / 100.0) * (ARRAY_SIZE(arr) - 1))
#define likely(x) __builtin_expect((x),1)
#define unlikely(expr) __builtin_expect(!!(expr), 0)
#define compare_fn(TYPE) \
int compare_fn_##TYPE (const void *a, const void *b) { \
TYPE val1 = *((TYPE*)a); \
TYPE val2 = *((TYPE*)b); \
if (val1 > val2) return 1; \
if (val1 < val2) return -1; \
else return 0; \
}
#define sort_array(TYPE) \
compare_fn(TYPE) \
static inline void \
sort_##TYPE##_array(TYPE arr[], int length) { \
qsort(arr, length, \
sizeof(TYPE), compare_fn_##TYPE); \
}
sort_array(uint64_t)
#endif