QuickCppLib 0.10
Eliminate all the tedious hassle when making state-of-the-art C++ 14 - 23 libraries!
Loading...
Searching...
No Matches
quickcpplib::_xxx::mem_flush_loads_stores::detail Namespace Reference

Typedefs

using flush_impl_type = memory_flush (*)(const void *addr, size_t bytes, memory_flush kind)
using potentially_unknown_jump_ptr = void (*)(const byte *data, size_t bytes)

Functions

QUICKCPPLIB_NOINLINE flush_impl_type make_flush_impl () noexcept
flush_impl_type flush_impl () noexcept
potentially_unknown_jump_ptr potentially_unknown_jump (potentially_unknown_jump_ptr set=nullptr)

Typedef Documentation

◆ flush_impl_type

◆ potentially_unknown_jump_ptr

Function Documentation

◆ make_flush_impl()

QUICKCPPLIB_NOINLINE flush_impl_type quickcpplib::_xxx::mem_flush_loads_stores::detail::make_flush_impl ( )
inlinenoexcept
68 {
69#if defined(__i386__) || defined(_M_IX86) || defined(__x86_64__) || defined(_M_X64)
70#if !defined(_MSC_VER) || (defined(_MSC_VER) && defined(__clang__) && !defined(__c2__))
71 static const auto __cpuidex = [](int *cpuInfo, int func1, int func2) {
72 __asm__ __volatile__("cpuid\n\t" : "=a"(cpuInfo[0]), "=b"(cpuInfo[1]), "=c"(cpuInfo[2]), "=d"(cpuInfo[3]) : "a"(func1), "c"(func2));
73 }; // NOLINT
74 // static constexpr auto _mm_clwb = [](const void *addr) { __asm__ __volatile__("clwb (%0)\n\t" : : "r"(addr)); }; // NOLINT
75 static const auto _mm_clwb = [](const void *addr) { __asm__ __volatile__(".byte 0x66, 0x0f, 0xae, 0x30\n\t" : : "a"(addr)); }; // NOLINT
76 static const auto _mm_clflushopt = [](const void *addr) { __asm__ __volatile__("clflushopt (%0)\n\t" : : "r"(addr)); }; // NOLINT
77 static const auto _mm_clflush = [](const void *addr) { __asm__ __volatile__("clflush (%0)\n\t" : : "r"(addr)); }; // NOLINT
78 static const auto _mm_sfence = []() { __asm__ __volatile__("sfence\n\t"); }; // NOLINT
79#endif
80 int nBuff[4];
81 __cpuidex(nBuff, 0x7, 0x0);
82 if(nBuff[1] & (1 << 24)) // has CLWB instruction
83 {
84 return [](const void *addr, size_t bytes, memory_flush kind) -> memory_flush {
85 if(kind == memory_flush_retain)
86 {
87 while(bytes > 0)
88 {
89 _mm_clwb(addr);
90 addr = (void *) ((uintptr_t) addr + 64);
91 bytes -= 64;
92 }
93 _mm_sfence();
95 }
96 while(bytes > 0)
97 {
98 _mm_clflushopt(addr);
99 addr = (void *) ((uintptr_t) addr + 64);
100 bytes -= 64;
101 }
102 _mm_sfence();
103 return memory_flush_evict;
104 };
105 }
106 if(nBuff[1] & (1 << 23)) // has CLFLUSHOPT instruction
107 {
108 return [](const void *addr, size_t bytes, memory_flush /*unused*/) -> memory_flush {
109 while(bytes > 0)
110 {
111 _mm_clflushopt(addr);
112 addr = (void *) ((uintptr_t) addr + 64);
113 bytes -= 64;
114 }
115 _mm_sfence();
116 return memory_flush_evict;
117 };
118 }
119 else
120 {
121 // Use CLFLUSH instruction
122 return [](const void *addr, size_t bytes, memory_flush /*unused*/) -> memory_flush {
123 while(bytes > 0)
124 {
125 _mm_clflush(addr);
126 addr = (void *) ((uintptr_t) addr + 64);
127 bytes -= 64;
128 }
129 return memory_flush_evict;
130 };
131 }
132#elif defined(__aarch64__) || defined(_M_ARM64)
133#if !defined(_MSC_VER) || (defined(_MSC_VER) && defined(__clang__) && !defined(__c2__))
134 static const auto _dmb_ish = []() { __asm__ __volatile__("dmb ish" : : : "memory"); };
135 static const auto _dc_cvac = [](const void *addr) { __asm__ __volatile__("dc cvac, %0" : : "r"(addr) : "memory"); };
136 static const auto _dc_civac = [](const void *addr) { __asm__ __volatile__("dc civac, %0" : : "r"(addr) : "memory"); };
137#else
138 static const auto _dmb_ish = []() { __dmb(_ARM64_BARRIER_ISH); };
139 static const auto _dc_cvac = [](const void *addr) {
140 (void) addr;
141 abort(); // currently MSVC doesn't have an intrinsic for this, could use __emit()?
142 };
143 static const auto _dc_civac = [](const void *addr) {
144 (void) addr;
145 abort(); // currently MSVC doesn't have an intrinsic for this, could use __emit()?
146 };
147#endif
148 return [](const void *addr, size_t bytes, memory_flush kind) -> memory_flush {
149 if(kind == memory_flush_retain)
150 {
151 while(bytes > 0)
152 {
153 _dc_cvac(addr);
154 addr = (void *) ((uintptr_t) addr + 64);
155 bytes -= 64;
156 }
157 _dmb_ish();
158 return memory_flush_retain;
159 }
160 while(bytes > 0)
161 {
162 _dc_civac(addr);
163 addr = (void *) ((uintptr_t) addr + 64);
164 bytes -= 64;
165 }
166 _dmb_ish();
167 return memory_flush_evict;
168 };
169#elif defined(__arm__) || defined(_M_ARM)
170#if !defined(_MSC_VER) || (defined(_MSC_VER) && defined(__clang__) && !defined(__c2__))
171#undef _MoveToCoprocessor
172#define _MoveToCoprocessor(value, coproc, opcode1, crn, crm, opcode2) \
173 __asm__ __volatile__("MCR p" #coproc ", " #opcode1 ", %0, c" #crn ", c" #crm ", " #opcode2 : : "r"(value) : "memory"); // NOLINT
174#endif
175 return [](const void *addr, size_t bytes, memory_flush kind) -> memory_flush {
176 if(kind == memory_flush_retain)
177 {
178 while(bytes > 0)
179 {
180 // __asm__ __volatile__("MCR p15, 0, %0, c7, c10, 1" : : "r"(addr) : "memory");
181 _MoveToCoprocessor(addr, 15, 0, 7, 10, 1);
182 addr = (void *) ((uintptr_t) addr + 64);
183 bytes -= 64;
184 }
185 // __asm__ __volatile__("mcr p15, 0, %0, c7, c10, 5" : : "r"(0) : "memory");
186 _MoveToCoprocessor(0, 15, 0, 7, 10, 5);
187 return memory_flush_retain;
188 }
189 while(bytes > 0)
190 {
191 // __asm__ __volatile__("MCR p15, 0, %0, c7, c14, 1" : : "r"(addr) : "memory");
192 _MoveToCoprocessor(addr, 15, 0, 7, 14, 1);
193 addr = (void *) ((uintptr_t) addr + 64);
194 bytes -= 64;
195 }
196 // __asm__ __volatile__("mcr p15, 0, %0, c7, c10, 5" : : "r"(0) : "memory");
197 _MoveToCoprocessor(0, 15, 0, 7, 10, 5);
198 return memory_flush_evict;
199 };
200#elif defined(__powerpc__)
201 // dcbst writes a modified line back but keeps it valid, dcbf also
202 // invalidates it; sync waits for the writebacks to complete.
203 static const auto _sync = []() { __asm__ __volatile__("sync" : : : "memory"); };
204 static const auto _dcbst = [](const void *addr) { __asm__ __volatile__("dcbst 0, %0" : : "r"(addr) : "memory"); };
205 static const auto _dcbf = [](const void *addr) { __asm__ __volatile__("dcbf 0, %0" : : "r"(addr) : "memory"); };
206 return [](const void *addr, size_t bytes, memory_flush kind) -> memory_flush {
207 if(kind == memory_flush_retain)
208 {
209 while(bytes > 0)
210 {
211 _dcbst(addr);
212 addr = (void *) ((uintptr_t) addr + 64);
213 bytes -= 64;
214 }
215 _sync();
216 return memory_flush_retain;
217 }
218 while(bytes > 0)
219 {
220 _dcbf(addr);
221 addr = (void *) ((uintptr_t) addr + 64);
222 bytes -= 64;
223 }
224 _sync();
225 return memory_flush_evict;
226 };
227#else
228#error Unsupported platform
229#endif
230 }
constexpr memory_flush memory_flush_retain
Flush modified cache line to memory, but retain as unmodified in cache.
Definition mem_flush_loads_stores.hpp:60
constexpr memory_flush memory_flush_evict
Flush modified cache line to memory, and evict completely from all caches.
Definition mem_flush_loads_stores.hpp:62
memory_flush
The kinds of cache line flushing which can be performed.
Definition mem_flush_loads_stores.hpp:52

◆ flush_impl()

flush_impl_type quickcpplib::_xxx::mem_flush_loads_stores::detail::flush_impl ( )
inlinenoexcept
232 {
233 static flush_impl_type f;
234 if(f != nullptr)
235 return f;
236 f = make_flush_impl();
237 return f;
238 }
memory_flush(*)(const void *addr, size_t bytes, memory_flush kind) flush_impl_type
Definition mem_flush_loads_stores.hpp:66
QUICKCPPLIB_NOINLINE flush_impl_type make_flush_impl() noexcept
Definition mem_flush_loads_stores.hpp:67

◆ potentially_unknown_jump()

potentially_unknown_jump_ptr quickcpplib::_xxx::mem_flush_loads_stores::detail::potentially_unknown_jump ( potentially_unknown_jump_ptr set = nullptr)
externinline
242 {
243 static potentially_unknown_jump_ptr f = +[](const byte * /*unused*/, size_t /*unused*/) -> void {};
244 if(set != nullptr)
245 {
246 f = set;
247 }
248 return f;
249 }
void(*)(const byte *data, size_t bytes) potentially_unknown_jump_ptr
Definition mem_flush_loads_stores.hpp:240