Vc  1.4.2
SIMD Vector Classes for C++
x86_prefetches.h
1 /* This file is part of the Vc library. {{{
2 Copyright © 2013-2015 Matthias Kretz <kretz@kde.org>
3 
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
6  * Redistributions of source code must retain the above copyright
7  notice, this list of conditions and the following disclaimer.
8  * Redistributions in binary form must reproduce the above copyright
9  notice, this list of conditions and the following disclaimer in the
10  documentation and/or other materials provided with the distribution.
11  * Neither the names of contributing organizations nor the
12  names of its contributors may be used to endorse or promote products
13  derived from this software without specific prior written permission.
14 
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
19 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 }}}*/
27 
28 #ifndef VC_COMMON_X86_PREFETCHES_H_
29 #define VC_COMMON_X86_PREFETCHES_H_
30 
31 #include <xmmintrin.h>
32 #include "macros.h"
33 
34 namespace Vc_VERSIONED_NAMESPACE
35 {
36 namespace Common
37 {
38 
39 static constexpr int exclusive_hint = 0;
40 
41 // TODO: support AMD's prefetchw with correct flags and checks via cpuid
42 
43 template <typename ExclusiveOrShared = Vc::Shared>
44 Vc_INTRINSIC void prefetchForOneRead(const void *addr)
45 {
46  if (std::is_same<ExclusiveOrShared, Vc::Shared>::value) {
47  _mm_prefetch(static_cast<char *>(const_cast<void *>(addr)), _MM_HINT_NTA);
48  } else {
49  _mm_prefetch(static_cast<char *>(const_cast<void *>(addr)),
50  static_cast<decltype(_MM_HINT_NTA)>(_MM_HINT_NTA | exclusive_hint));
51  }
52 }
53 template <typename ExclusiveOrShared = Vc::Shared>
54 Vc_INTRINSIC void prefetchClose(const void *addr)
55 {
56  if (std::is_same<ExclusiveOrShared, Vc::Shared>::value) {
57  _mm_prefetch(static_cast<char *>(const_cast<void *>(addr)), _MM_HINT_T0);
58  } else {
59  _mm_prefetch(static_cast<char *>(const_cast<void *>(addr)),
60  static_cast<decltype(_MM_HINT_T0)>(_MM_HINT_T0 | exclusive_hint));
61  }
62 }
63 template <typename ExclusiveOrShared = Vc::Shared>
64 Vc_INTRINSIC void prefetchMid(const void *addr)
65 {
66  if (std::is_same<ExclusiveOrShared, Vc::Shared>::value) {
67  _mm_prefetch(static_cast<char *>(const_cast<void *>(addr)), _MM_HINT_T1);
68  } else {
69  _mm_prefetch(static_cast<char *>(const_cast<void *>(addr)),
70  static_cast<decltype(_MM_HINT_T1)>(_MM_HINT_T1 | exclusive_hint));
71  }
72 }
73 template <typename ExclusiveOrShared = Vc::Shared>
74 Vc_INTRINSIC void prefetchFar(const void *addr)
75 {
76  if (std::is_same<ExclusiveOrShared, Vc::Shared>::value) {
77  _mm_prefetch(static_cast<char *>(const_cast<void *>(addr)), _MM_HINT_T2);
78  } else {
79  _mm_prefetch(static_cast<char *>(const_cast<void *>(addr)),
80  static_cast<decltype(_MM_HINT_T2)>(_MM_HINT_T2 | exclusive_hint));
81  }
82 }
83 
84 /*handlePrefetch/handleLoadPrefetches/handleStorePrefetches{{{*/
85 namespace
86 {
87 template<size_t L1, size_t L2, bool UseExclusivePrefetch> Vc_INTRINSIC void handlePrefetch(const void *addr_, typename std::enable_if<L1 != 0 && L2 != 0, void *>::type = nullptr)
88 {
89  const char *addr = static_cast<const char *>(addr_);
90  prefetchClose<typename std::conditional<UseExclusivePrefetch, Vc::Exclusive, Vc::Shared>::type>(addr + L1);
91  prefetchMid <typename std::conditional<UseExclusivePrefetch, Vc::Exclusive, Vc::Shared>::type>(addr + L2);
92 }
93 template<size_t L1, size_t L2, bool UseExclusivePrefetch> Vc_INTRINSIC void handlePrefetch(const void *addr_, typename std::enable_if<L1 == 0 && L2 != 0, void *>::type = nullptr)
94 {
95  const char *addr = static_cast<const char *>(addr_);
96  prefetchMid <typename std::conditional<UseExclusivePrefetch, Vc::Exclusive, Vc::Shared>::type>(addr + L2);
97 }
98 template<size_t L1, size_t L2, bool UseExclusivePrefetch> Vc_INTRINSIC void handlePrefetch(const void *addr_, typename std::enable_if<L1 != 0 && L2 == 0, void *>::type = nullptr)
99 {
100  const char *addr = static_cast<const char *>(addr_);
101  prefetchClose<typename std::conditional<UseExclusivePrefetch, Vc::Exclusive, Vc::Shared>::type>(addr + L1);
102 }
103 template<size_t L1, size_t L2, bool UseExclusivePrefetch> Vc_INTRINSIC void handlePrefetch(const void *, typename std::enable_if<L1 == 0 && L2 == 0, void *>::type = nullptr)
104 {
105 }
106 
107 template<typename Flags> Vc_INTRINSIC void handleLoadPrefetches(const void * , Flags, typename Flags::EnableIfNotPrefetch = nullptr) {}
108 template<typename Flags> Vc_INTRINSIC void handleLoadPrefetches(const void *addr, Flags, typename Flags::EnableIfPrefetch = nullptr)
109 {
110  // load prefetches default to Shared unless Exclusive was explicitely selected
111  handlePrefetch<Flags::L1Stride, Flags::L2Stride, Flags::IsExclusivePrefetch>(addr);
112 }
113 
114 template<typename Flags> Vc_INTRINSIC void handleStorePrefetches(const void * , Flags, typename Flags::EnableIfNotPrefetch = nullptr) {}
115 template<typename Flags> Vc_INTRINSIC void handleStorePrefetches(const void *addr, Flags, typename Flags::EnableIfPrefetch = nullptr)
116 {
117  // store prefetches default to Exclusive unless Shared was explicitely selected
118  handlePrefetch<Flags::L1Stride, Flags::L2Stride, !Flags::IsSharedPrefetch>(addr);
119 }
120 
121 } // anonymous namespace
122 /*}}}*/
123 
124 } // namespace Common
125 
128 using Common::prefetchMid;
129 using Common::prefetchFar;
130 } // namespace Vc
131 
132 #endif // VC_COMMON_X86_PREFETCHES_H_
133 
134 // vim: foldmethod=marker
Vc::Common::prefetchFar
void prefetchFar(const void *addr)
Prefetch the cacheline containing addr to L3 cache.
Definition: memory.h:601
Vc::Common::prefetchMid
void prefetchMid(const void *addr)
Prefetch the cacheline containing addr to L2 cache.
Definition: memory.h:586
Vc::Common::prefetchClose
void prefetchClose(const void *addr)
Prefetch the cacheline containing addr to L1 cache.
Definition: memory.h:571
Vc::Common::prefetchForOneRead
void prefetchForOneRead(const void *addr)
Prefetch the cacheline containing addr for a single read access.
Definition: memory.h:539