Vc  1.0.0-dev
SIMD Vector Classes for C++
loadstoreflags.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_LOADSTOREFLAGS_H_
30 #define VC_COMMON_LOADSTOREFLAGS_H_
31 
32 #include "../traits/type_traits.h"
33 
34 namespace Vc_VERSIONED_NAMESPACE
35 {
36 
42 struct Exclusive {};
46 struct Shared {};
47 
48 namespace LoadStoreFlags
49 {
50 
51 struct StreamingFlag {};
52 struct UnalignedFlag {};
53 struct PrefetchFlagBase {};
54 #ifdef Vc_IMPL_MIC
55 template<size_t L1 = 8 * 64, size_t L2 = 64 * 64,
56 #else
57 // TODO: determine a good default for typical CPU use
58 template<size_t L1 = 16 * 64, size_t L2 = 128 * 64,
59 #endif
60  typename ExclusiveOrShared_ = void> struct PrefetchFlag : public PrefetchFlagBase
61 {
62  typedef ExclusiveOrShared_ ExclusiveOrShared;
63  static constexpr size_t L1Stride = L1;
64  static constexpr size_t L2Stride = L2;
65  static constexpr bool IsExclusive = std::is_same<ExclusiveOrShared, Exclusive>::value;
66  static constexpr bool IsShared = std::is_same<ExclusiveOrShared, Shared>::value;
67 };
68 
69 template<typename Base, typename Default, typename... LoadStoreFlags> struct ExtractType
70 {
71  typedef Default type;
72 };
73 template<typename Base, typename Default, typename T, typename... LoadStoreFlags> struct ExtractType<Base, Default, T, LoadStoreFlags...>
74 {
75  typedef typename std::conditional<std::is_base_of<Base, T>::value, T, typename ExtractType<Base, Default, LoadStoreFlags...>::type>::type type;
76 };
77 
78 // ICC warns about the constexpr members in LoadStoreFlags: member "LoadStoreFlags<Flags...>::IsAligned" was declared but never referenced
79 // who needs that warning, especially if it was referenced...
80 // The warning cannot be reenabled because it gets emitted whenever the LoadStoreFlags is instantiated
81 // somewhere, so it could be anywhere.
82 #ifdef Vc_ICC
83 #pragma warning(disable: 177)
84 #endif
85 
89 template<typename... Flags> struct LoadStoreFlags
90 {
91 private:
92  // ICC doesn't grok this line:
93  //template<typename Test> using TestFlag = std::is_same<typename ExtractType<StreamingFlag, void, Flags...>::type, void>;
94  typedef typename ExtractType<PrefetchFlagBase, PrefetchFlag<0, 0>, Flags...>::type Prefetch;
95 
96 public:
97  constexpr LoadStoreFlags() {}
98 
99  static constexpr bool IsStreaming = !std::is_same<typename ExtractType<StreamingFlag, void, Flags...>::type, void>::value;
100  static constexpr bool IsUnaligned = !std::is_same<typename ExtractType<UnalignedFlag, void, Flags...>::type, void>::value;
101  static constexpr bool IsAligned = !IsUnaligned;
102  static constexpr bool IsPrefetch = !std::is_same<typename ExtractType<PrefetchFlagBase, void, Flags...>::type, void>::value;
103  static constexpr bool IsExclusivePrefetch = Prefetch::IsExclusive;
104  static constexpr bool IsSharedPrefetch = Prefetch::IsShared;
105  static constexpr size_t L1Stride = Prefetch::L1Stride;
106  static constexpr size_t L2Stride = Prefetch::L2Stride;
107 
108  typedef LoadStoreFlags<typename std::conditional<std::is_same<Flags, UnalignedFlag>::value, void, Flags>::type...> UnalignedRemoved;
109 
110  // The following EnableIf* convenience types cannot use enable_if because then no LoadStoreFlags type
111  // could ever be instantiated. Instead these types are defined either as void* or void. The
112  // function that does SFINAE then assigns "= nullptr" to this type. Thus, the ones with just
113  // void result in substitution failure.
114  typedef typename std::conditional<IsAligned && !IsStreaming, void *, void>::type EnableIfAligned;
115  typedef typename std::conditional<IsAligned && IsStreaming, void *, void>::type EnableIfStreaming;
116  typedef typename std::conditional<IsUnaligned && !IsStreaming, void *, void>::type EnableIfUnalignedNotStreaming;
117  typedef typename std::conditional<IsUnaligned && IsStreaming, void *, void>::type EnableIfUnalignedAndStreaming;
118  typedef typename std::conditional<IsUnaligned , void *, void>::type EnableIfUnaligned;
119  typedef typename std::conditional<!IsUnaligned , void *, void>::type EnableIfNotUnaligned;
120  typedef typename std::conditional<IsPrefetch , void *, void>::type EnableIfPrefetch;
121  typedef typename std::conditional<!IsPrefetch , void *, void>::type EnableIfNotPrefetch;
122 };
123 
127 template<> struct LoadStoreFlags<>
128 {
129  constexpr LoadStoreFlags() {}
130 
131  static constexpr bool IsStreaming = false;
132  static constexpr bool IsUnaligned = false;
133  static constexpr bool IsAligned = !IsUnaligned;
134  static constexpr bool IsPrefetch = false;
135  static constexpr bool IsExclusivePrefetch = false;
136  static constexpr bool IsSharedPrefetch = false;
137  static constexpr size_t L1Stride = 0;
138  static constexpr size_t L2Stride = 0;
139  typedef void* EnableIfAligned;
140  typedef void* EnableIfNotUnaligned;
141  typedef void* EnableIfNotPrefetch;
142 };
143 
152 template<typename... LFlags, typename... RFlags>
153 constexpr LoadStoreFlags<LFlags..., RFlags...> operator|(LoadStoreFlags<LFlags...>, LoadStoreFlags<RFlags...>)
154 {
155  return LoadStoreFlags<LFlags..., RFlags...>();
156 }
157 
158 } // LoadStoreFlags namespace
159 
160 using LoadStoreFlags::PrefetchFlag;
161 
162 typedef LoadStoreFlags::LoadStoreFlags<> AlignedTag;
163 typedef LoadStoreFlags::LoadStoreFlags<LoadStoreFlags::StreamingFlag> StreamingTag;
164 typedef LoadStoreFlags::LoadStoreFlags<LoadStoreFlags::UnalignedFlag> UnalignedTag;
165 
167 typedef UnalignedTag DefaultLoadTag;
169 typedef UnalignedTag DefaultStoreTag;
170 
184 constexpr AlignedTag Aligned;
185 
197 constexpr UnalignedTag Unaligned;
198 
212 constexpr StreamingTag Streaming;
213 
218 constexpr LoadStoreFlags::LoadStoreFlags<PrefetchFlag<>> PrefetchDefault;
220 
226 template <size_t L1 = PrefetchFlag<>::L1Stride,
227  size_t L2 = PrefetchFlag<>::L2Stride,
228  typename ExclusiveOrShared = PrefetchFlag<>::ExclusiveOrShared>
229 struct Prefetch : public LoadStoreFlags::LoadStoreFlags<PrefetchFlag<L1, L2, ExclusiveOrShared>>
230 {
231 };
232 
233 namespace Traits
234 {
236 template <typename... Ts>
237 struct is_loadstoreflag_internal<LoadStoreFlags::LoadStoreFlags<Ts...>> : public std::true_type
238 {
239 };
242 template <size_t L1, size_t L2, typename ExclusiveOrShared>
243 struct is_loadstoreflag_internal<Prefetch<L1, L2, ExclusiveOrShared>> : public std::true_type
244 {
245 };
246 } // namespace Traits
247 } // namespace Vc
248 
249 #endif // VC_COMMON_LOADSTOREFLAGS_H_
Hint for Prefetch to select prefetches that mark the memory as shared.
UnalignedTag DefaultLoadTag
The default load tag type uses unaligned (non-streaming) loads.
constexpr LoadStoreFlags::LoadStoreFlags< PrefetchFlag<> > PrefetchDefault
Use this object for a flags parameter to request default software prefetches to be emitted...
Hint for Prefetch to select prefetches that mark the memory as exclusive.
UnalignedTag DefaultStoreTag
The default store tag type uses unaligned (non-streaming) stores.
constexpr AlignedTag Aligned
Use this object for a flags parameter to request aligned loads and stores.
constexpr StreamingTag Streaming
Use this object for a flags parameter to request streaming loads and stores.
constexpr UnalignedTag Unaligned
Use this object for a flags parameter to request unaligned loads and stores.