forked from sysprog21/semu
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcommon.h
36 lines (29 loc) · 833 Bytes
/
common.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
#pragma once
#include <stdint.h>
#include "feature.h"
#define unlikely(x) __builtin_expect((x), 0)
#define likely(x) __builtin_expect((x), 1)
#define UNUSED __attribute__((unused))
#define MASK(n) (~((~0UL << (n))))
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a)))
/* Ensure that __builtin_clz is never called with 0 argument */
static inline int ilog2(uint32_t x)
{
// FIXME: likely inefficient (but also rarely used...)
#if C64
uint8_t lz=0;
while(x>>=1)
lz++;
return lz;
#else
return 31 - __builtin_clz(x | 1);
#endif
}
/* Range check
* For any variable range checking:
* if (x >= minx && x <= maxx) ...
* it is faster to use bit operation:
* if ((signed)((x - minx) | (maxx - x)) >= 0) ...
*/
#define RANGE_CHECK(x, minx, size) \
((int32_t) ((x - minx) | (minx + size - 1 - x)) >= 0)