Vc 1.4.5
SIMD Vector Classes for C++
 
Loading...
Searching...
No Matches
x86_prefetches.h
1/* This file is part of the Vc library. {{{
2Copyright © 2013-2015 Matthias Kretz <kretz@kde.org>
3
4Redistribution and use in source and binary forms, with or without
5modification, 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
15THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
19DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22ON 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
24SOFTWARE, 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
34namespace Vc_VERSIONED_NAMESPACE
35{
36namespace Common
37{
38
39static constexpr int exclusive_hint = 0;
40
41// TODO: support AMD's prefetchw with correct flags and checks via cpuid
42
43template <typename ExclusiveOrShared = Vc::Shared>
44Vc_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}
53template <typename ExclusiveOrShared = Vc::Shared>
54Vc_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}
63template <typename ExclusiveOrShared = Vc::Shared>
64Vc_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}
73template <typename ExclusiveOrShared = Vc::Shared>
74Vc_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{{{*/
85namespace
86{
87template<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}
93template<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}
98template<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}
103template<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
107template<typename Flags> Vc_INTRINSIC void handleLoadPrefetches(const void * , Flags, typename Flags::EnableIfNotPrefetch = nullptr) {}
108template<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
114template<typename Flags> Vc_INTRINSIC void handleStorePrefetches(const void * , Flags, typename Flags::EnableIfNotPrefetch = nullptr) {}
115template<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
126using Common::prefetchForOneRead;
127using Common::prefetchClose;
128using Common::prefetchMid;
129using Common::prefetchFar;
130} // namespace Vc
131
132#endif // VC_COMMON_X86_PREFETCHES_H_
133
134// vim: foldmethod=marker