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