Vc  1.3.2-dev
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 #if defined(Vc_IMPL_MIC)
40 static constexpr int exclusive_hint = 0x4;
41 #else
42 static constexpr int exclusive_hint = 0;
43 #endif
44 
45 // TODO: support AMD's prefetchw with correct flags and checks via cpuid
46 
47 template <typename ExclusiveOrShared = Vc::Shared>
48 Vc_INTRINSIC void prefetchForOneRead(const void *addr)
49 {
50  if (std::is_same<ExclusiveOrShared, Vc::Shared>::value) {
51  _mm_prefetch(static_cast<char *>(const_cast<void *>(addr)), _MM_HINT_NTA);
52  } else {
53  _mm_prefetch(static_cast<char *>(const_cast<void *>(addr)),
54  static_cast<decltype(_MM_HINT_NTA)>(_MM_HINT_NTA | exclusive_hint));
55  }
56 }
57 template <typename ExclusiveOrShared = Vc::Shared>
58 Vc_INTRINSIC void prefetchClose(const void *addr)
59 {
60  if (std::is_same<ExclusiveOrShared, Vc::Shared>::value) {
61  _mm_prefetch(static_cast<char *>(const_cast<void *>(addr)), _MM_HINT_T0);
62  } else {
63  _mm_prefetch(static_cast<char *>(const_cast<void *>(addr)),
64  static_cast<decltype(_MM_HINT_T0)>(_MM_HINT_T0 | exclusive_hint));
65  }
66 }
67 template <typename ExclusiveOrShared = Vc::Shared>
68 Vc_INTRINSIC void prefetchMid(const void *addr)
69 {
70  if (std::is_same<ExclusiveOrShared, Vc::Shared>::value) {
71  _mm_prefetch(static_cast<char *>(const_cast<void *>(addr)), _MM_HINT_T1);
72  } else {
73  _mm_prefetch(static_cast<char *>(const_cast<void *>(addr)),
74  static_cast<decltype(_MM_HINT_T1)>(_MM_HINT_T1 | exclusive_hint));
75  }
76 }
77 template <typename ExclusiveOrShared = Vc::Shared>
78 Vc_INTRINSIC void prefetchFar(const void *addr)
79 {
80  if (std::is_same<ExclusiveOrShared, Vc::Shared>::value) {
81  _mm_prefetch(static_cast<char *>(const_cast<void *>(addr)), _MM_HINT_T2);
82  } else {
83  _mm_prefetch(static_cast<char *>(const_cast<void *>(addr)),
84  static_cast<decltype(_MM_HINT_T2)>(_MM_HINT_T2 | exclusive_hint));
85  }
86 }
87 
88 /*handlePrefetch/handleLoadPrefetches/handleStorePrefetches{{{*/
89 namespace
90 {
91 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)
92 {
93  const char *addr = static_cast<const char *>(addr_);
94  prefetchClose<typename std::conditional<UseExclusivePrefetch, Vc::Exclusive, Vc::Shared>::type>(addr + L1);
95  prefetchMid <typename std::conditional<UseExclusivePrefetch, Vc::Exclusive, Vc::Shared>::type>(addr + L2);
96 }
97 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)
98 {
99  const char *addr = static_cast<const char *>(addr_);
100  prefetchMid <typename std::conditional<UseExclusivePrefetch, Vc::Exclusive, Vc::Shared>::type>(addr + L2);
101 }
102 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)
103 {
104  const char *addr = static_cast<const char *>(addr_);
105  prefetchClose<typename std::conditional<UseExclusivePrefetch, Vc::Exclusive, Vc::Shared>::type>(addr + L1);
106 }
107 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)
108 {
109 }
110 
111 template<typename Flags> Vc_INTRINSIC void handleLoadPrefetches(const void * , Flags, typename Flags::EnableIfNotPrefetch = nullptr) {}
112 template<typename Flags> Vc_INTRINSIC void handleLoadPrefetches(const void *addr, Flags, typename Flags::EnableIfPrefetch = nullptr)
113 {
114  // load prefetches default to Shared unless Exclusive was explicitely selected
115  handlePrefetch<Flags::L1Stride, Flags::L2Stride, Flags::IsExclusivePrefetch>(addr);
116 }
117 
118 template<typename Flags> Vc_INTRINSIC void handleStorePrefetches(const void * , Flags, typename Flags::EnableIfNotPrefetch = nullptr) {}
119 template<typename Flags> Vc_INTRINSIC void handleStorePrefetches(const void *addr, Flags, typename Flags::EnableIfPrefetch = nullptr)
120 {
121  // store prefetches default to Exclusive unless Shared was explicitely selected
122  handlePrefetch<Flags::L1Stride, Flags::L2Stride, !Flags::IsSharedPrefetch>(addr);
123 }
124 
125 } // anonymous namespace
126 /*}}}*/
127 
128 } // namespace Common
129 
132 using Common::prefetchMid;
133 using Common::prefetchFar;
134 } // namespace Vc
135 
136 #endif // VC_COMMON_X86_PREFETCHES_H_
137 
138 // vim: foldmethod=marker
void prefetchClose(const void *addr)
Prefetch the cacheline containing addr to L1 cache.
Definition: memory.h:635
void prefetchMid(const void *addr)
Prefetch the cacheline containing addr to L2 cache.
Definition: memory.h:650
void prefetchForOneRead(const void *addr)
Prefetch the cacheline containing addr for a single read access.
Definition: memory.h:603
void prefetchFar(const void *addr)
Prefetch the cacheline containing addr to L3 cache.
Definition: memory.h:665