-
Notifications
You must be signed in to change notification settings - Fork 0
/
cutils.h
121 lines (105 loc) · 2.06 KB
/
cutils.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#ifndef CUTILS_H
#define CUTILS_H
#define force_inline inline __attribute__((always_inline))
#define no_inline __attribute__((noinline))
#define __unused __attribute__((unused))
#define xglue(x, y) x ## y
#define glue(x, y) xglue(x, y)
#ifndef offsetof
#define offsetof(type, field) ((size_t) &((type *)0)->field)
#endif
#define countof(x) (sizeof(x) / sizeof(x[0]))
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
typedef int BOOL;
#ifndef FALSE
enum {
FALSE = 0,
TRUE = 1,
};
#endif
#if defined(__x86_64__)
static inline int64_t get_cycles(void)
{
uint32_t low,high;
int64_t val;
asm volatile("rdtsc" : "=a" (low), "=d" (high));
val = high;
val <<= 32;
val |= low;
return val;
}
#else
static inline int64_t get_cycles(void)
{
int64_t val;
asm volatile ("rdtsc" : "=A" (val));
return val;
}
#endif
static inline int max_int(int a, int b)
{
if (a > b)
return a;
else
return b;
}
static inline int min_int(int a, int b)
{
if (a < b)
return a;
else
return b;
}
static inline size_t max_size_t(size_t a, size_t b)
{
if (a > b)
return a;
else
return b;
}
static inline size_t min_size_t(size_t a, size_t b)
{
if (a < b)
return a;
else
return b;
}
static inline ssize_t max_ssize_t(ssize_t a, ssize_t b)
{
if (a > b)
return a;
else
return b;
}
static inline ssize_t min_ssize_t(ssize_t a, ssize_t b)
{
if (a < b)
return a;
else
return b;
}
static inline int clamp_int(int val, int min_val, int max_val)
{
if (val < min_val)
return min_val;
else if (val > max_val)
return max_val;
else
return val;
}
static inline float clamp_float(float val, float min_val, float max_val)
{
if (val < min_val)
return min_val;
else if (val > max_val)
return max_val;
else
return val;
}
static inline float squaref(float x)
{
return x * x;
}
#define DUP8(a) a, a, a, a, a, a, a, a
#endif /* CUTILS_H */