-
Notifications
You must be signed in to change notification settings - Fork 116
/
prefetch.h
54 lines (47 loc) · 1.35 KB
/
prefetch.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
#ifndef _PREFETCH_H_
#define _PREFETCH_H_
#include <algorithm>
#include "util.h"
#include "macros.h"
#if !MASSTREE_COMPILER_HH
// assumes cache-aligned
static inline ALWAYS_INLINE void
prefetch(const void *ptr)
{
typedef struct { char x[CACHELINE_SIZE]; } cacheline_t;
asm volatile("prefetcht0 %0" : : "m" (*(const cacheline_t *) ptr));
}
#define PREFETCH_DEFINED 1
#endif
// assumes cache-aligned
template <typename T>
static inline ALWAYS_INLINE void
prefetch_object(const T *ptr)
{
for (unsigned i = CACHELINE_SIZE;
i < std::min(static_cast<unsigned>(sizeof(*ptr)),
static_cast<unsigned>(4 * CACHELINE_SIZE));
i += CACHELINE_SIZE)
prefetch((const char *) ptr + i);
}
// prefetch an object resident in [ptr, ptr + n). doesn't assume cache aligned
static inline ALWAYS_INLINE void
prefetch_bytes(const void *p, size_t n)
{
const char *ptr = (const char *) p;
// round down to nearest cacheline, then prefetch
const void * const pend =
std::min(ptr + n, ptr + 4 * CACHELINE_SIZE);
ptr = (const char *) util::round_down<uintptr_t, LG_CACHELINE_SIZE>((uintptr_t) ptr);
// manually unroll loop 3 times
ptr += CACHELINE_SIZE;
if (ptr < pend)
prefetch(ptr);
ptr += CACHELINE_SIZE;
if (ptr < pend)
prefetch(ptr);
ptr += CACHELINE_SIZE;
if (ptr < pend)
prefetch(ptr);
}
#endif /* _PREFETCH_H_ */