prefetch.hpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. #ifndef BOOST_CONTEXT_DETAIL_PREFETCH_H
  6. #define BOOST_CONTEXT_DETAIL_PREFETCH_H
  7. #include <cstddef>
  8. #include <cstdint>
  9. #include <boost/config.hpp>
  10. #include <boost/predef.h>
  11. #include <boost/context/detail/config.hpp>
  12. #if BOOST_COMP_INTEL || BOOST_COMP_INTEL_EMULATED
  13. #include <immintrin.h>
  14. #endif
  15. #if BOOST_COMP_MSVC && !defined(_M_ARM) && !defined(_M_ARM64)
  16. #include <mmintrin.h>
  17. #endif
  18. #ifdef BOOST_HAS_ABI_HEADERS
  19. # include BOOST_ABI_PREFIX
  20. #endif
  21. namespace boost {
  22. namespace context {
  23. namespace detail {
  24. #if BOOST_COMP_GNUC || BOOST_COMP_CLANG
  25. #define BOOST_HAS_PREFETCH 1
  26. BOOST_FORCEINLINE
  27. void prefetch( void * addr) {
  28. // L1 cache : hint == 1
  29. __builtin_prefetch( addr, 1, 1);
  30. }
  31. #elif BOOST_COMP_INTEL || BOOST_COMP_INTEL_EMULATED
  32. #define BOOST_HAS_PREFETCH 1
  33. BOOST_FORCEINLINE
  34. void prefetch( void * addr) {
  35. // L1 cache : hint == _MM_HINT_T0
  36. _mm_prefetch( (const char *)addr, _MM_HINT_T0);
  37. }
  38. #elif BOOST_COMP_MSVC && !defined(_M_ARM) && !defined(_M_ARM64)
  39. #define BOOST_HAS_PREFETCH 1
  40. BOOST_FORCEINLINE
  41. void prefetch( void * addr) {
  42. // L1 cache : hint == _MM_HINT_T0
  43. _mm_prefetch( (const char *)addr, _MM_HINT_T0);
  44. }
  45. #endif
  46. inline
  47. void prefetch_range( void * addr, std::size_t len) {
  48. #if defined(BOOST_HAS_PREFETCH)
  49. void * vp = addr;
  50. void * end = reinterpret_cast< void * >(
  51. reinterpret_cast< uintptr_t >( addr) + static_cast< uintptr_t >( len) );
  52. while ( vp < end) {
  53. prefetch( vp);
  54. vp = reinterpret_cast< void * >(
  55. reinterpret_cast< uintptr_t >( vp) + static_cast< uintptr_t >( prefetch_stride) );
  56. }
  57. #endif
  58. }
  59. #undef BOOST_HAS_PREFETCH
  60. }}}
  61. #ifdef BOOST_HAS_ABI_HEADERS
  62. # include BOOST_ABI_SUFFIX
  63. #endif
  64. #endif // BOOST_CONTEXT_DETAIL_PREFETCH_H