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
121
122
123
124
125
#pragma once
#ifdef _MSC_VER
# include <immintrin.h>
# include <intrin.h>
#else
# include <cpuid.h>
# include <emmintrin.h>
#endif
#if defined(__linux__)
# include <x86intrin.h>
#endif
#if defined(__amd64__) || defined(__x86_64__) || defined(_M_X64) || \
defined(_M_AMD64)
# define SNMALLOC_VA_BITS_64
#else
# define SNMALLOC_VA_BITS_32
#endif
namespace snmalloc
{
/**
* x86-specific architecture abstraction layer.
*/
class AAL_x86
{
/**
* Read the timestamp counter, guaranteeing that all previous instructions
* have been retired.
*/
static inline uint64_t tickp()
{
unsigned int aux;
#if defined(_MSC_VER)
return __rdtscp(&aux);
#else
return __builtin_ia32_rdtscp(&aux);
#endif
}
/**
* Issue a fully serialising instruction.
*/
static inline void halt_out_of_order()
{
#if defined(_MSC_VER)
int cpu_info[4];
__cpuid(cpu_info, 0);
#else
unsigned int eax, ebx, ecx, edx;
__get_cpuid(0, &eax, &ebx, &ecx, &edx);
#endif
}
public:
/**
* Bitmap of AalFeature flags
*/
static constexpr uint64_t aal_features = IntegerPointers;
static constexpr enum AalName aal_name = X86;
static constexpr size_t smallest_page_size = 0x1000;
/**
* On pipelined processors, notify the core that we are in a spin loop and
* that speculative execution past this point may not be a performance gain.
*/
static inline void pause()
{
_mm_pause();
}
/**
* Issue a prefetch hint at the specified address.
*/
static inline void prefetch(void* ptr)
{
#if defined(_MSC_VER)
_m_prefetchw(ptr);
#else
_mm_prefetch(reinterpret_cast<const char*>(ptr), _MM_HINT_ET0);
#endif
}
/**
* Return a cycle counter value.
*/
static inline uint64_t tick()
{
#if defined(_MSC_VER)
return __rdtsc();
#else
return __builtin_ia32_rdtsc();
#endif
}
/**
* Return the cycle counter value that can be used to indicate the start of
* a benchmark run. This is responsible for ensuring any serialisation
* that the pipeline may require.
*/
static inline uint64_t benchmark_time_start()
{
halt_out_of_order();
return AAL_Generic<AAL_x86>::tick();
}
/**
* Return the cycle counter value that can be used to indicate the end of a
* benchmark run. This is responsible for ensuring any serialisation that
* the pipeline may require.
*/
static inline uint64_t benchmark_time_end()
{
uint64_t t = tickp();
halt_out_of_order();
return t;
}
};
using AAL_Arch = AAL_x86;
} // namespace snmalloc