| 1 | // Copyright Oliver Kowalke 2017. |
| 2 | // Distributed under the Boost Software License, Version 1.0. |
| 3 | // (See accompanying file LICENSE_1_0.txt or copy at |
| 4 | // http://www.boost.org/LICENSE_1_0.txt) |
| 5 | |
| 6 | #ifndef BOOST_CONTEXT_DETAIL_PREFETCH_H |
| 7 | #define BOOST_CONTEXT_DETAIL_PREFETCH_H |
| 8 | |
| 9 | #include <cstddef> |
| 10 | #include <cstdint> |
| 11 | |
| 12 | #include <boost/config.hpp> |
| 13 | #include <boost/predef.h> |
| 14 | |
| 15 | #include <boost/context/detail/config.hpp> |
| 16 | |
| 17 | #if BOOST_COMP_INTEL || BOOST_COMP_INTEL_EMULATED |
| 18 | #include <immintrin.h> |
| 19 | #endif |
| 20 | |
| 21 | #if BOOST_COMP_MSVC && !defined(_M_ARM) && !defined(_M_ARM64) |
| 22 | #include <mmintrin.h> |
| 23 | #endif |
| 24 | |
| 25 | #ifdef BOOST_HAS_ABI_HEADERS |
| 26 | # include BOOST_ABI_PREFIX |
| 27 | #endif |
| 28 | |
| 29 | namespace boost { |
| 30 | namespace context { |
| 31 | namespace detail { |
| 32 | |
| 33 | #if BOOST_COMP_GNUC || BOOST_COMP_CLANG |
| 34 | #define BOOST_HAS_PREFETCH 1 |
| 35 | BOOST_FORCEINLINE |
| 36 | void prefetch( void * addr) { |
| 37 | // L1 cache : hint == 1 |
| 38 | __builtin_prefetch( addr, 1, 1); |
| 39 | } |
| 40 | #elif BOOST_COMP_INTEL || BOOST_COMP_INTEL_EMULATED |
| 41 | #define BOOST_HAS_PREFETCH 1 |
| 42 | BOOST_FORCEINLINE |
| 43 | void prefetch( void * addr) { |
| 44 | // L1 cache : hint == _MM_HINT_T0 |
| 45 | _mm_prefetch( (const char *)addr, _MM_HINT_T0); |
| 46 | } |
| 47 | #elif BOOST_COMP_MSVC && !defined(_M_ARM) && !defined(_M_ARM64) |
| 48 | #define BOOST_HAS_PREFETCH 1 |
| 49 | BOOST_FORCEINLINE |
| 50 | void prefetch( void * addr) { |
| 51 | // L1 cache : hint == _MM_HINT_T0 |
| 52 | _mm_prefetch( (const char *)addr, _MM_HINT_T0); |
| 53 | } |
| 54 | #endif |
| 55 | |
| 56 | inline |
| 57 | void prefetch_range( void * addr, std::size_t len) { |
| 58 | #if defined(BOOST_HAS_PREFETCH) |
| 59 | void * vp = addr; |
| 60 | void * end = reinterpret_cast< void * >( |
| 61 | reinterpret_cast< uintptr_t >( addr) + static_cast< uintptr_t >( len) ); |
| 62 | while ( vp < end) { |
| 63 | prefetch( addr: vp); |
| 64 | vp = reinterpret_cast< void * >( |
| 65 | reinterpret_cast< uintptr_t >( vp) + static_cast< uintptr_t >( prefetch_stride) ); |
| 66 | } |
| 67 | #endif |
| 68 | } |
| 69 | |
| 70 | #undef BOOST_HAS_PREFETCH |
| 71 | |
| 72 | }}} |
| 73 | |
| 74 | #ifdef BOOST_HAS_ABI_HEADERS |
| 75 | # include BOOST_ABI_SUFFIX |
| 76 | #endif |
| 77 | |
| 78 | #endif // BOOST_CONTEXT_DETAIL_PREFETCH_H |
| 79 | |