Grok  9.7.5
highway.h
Go to the documentation of this file.
1 // Copyright 2020 Google LLC
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 // This include guard is checked by foreach_target, so avoid the usual _H_
17 // suffix to prevent copybara from renaming it. NOTE: ops/*-inl.h are included
18 // after/outside this include guard.
19 #ifndef HWY_HIGHWAY_INCLUDED
20 #define HWY_HIGHWAY_INCLUDED
21 
22 // Main header required before using vector types.
23 
24 #include "hwy/base.h"
25 #include "hwy/targets.h"
26 
27 namespace hwy {
28 
29 // API version (https://semver.org/); keep in sync with CMakeLists.txt.
30 #define HWY_MAJOR 0
31 #define HWY_MINOR 16
32 #define HWY_PATCH 0
33 
34 //------------------------------------------------------------------------------
35 // Shorthand for tags (defined in shared-inl.h) used to select overloads.
36 // Note that ScalableTag<T> is preferred over HWY_FULL, and CappedTag<T, N> over
37 // HWY_CAPPED(T, N).
38 
39 // HWY_FULL(T[,LMUL=1]) is a native vector/group. LMUL is the number of
40 // registers in the group, and is ignored on targets that do not support groups.
41 #define HWY_FULL1(T) hwy::HWY_NAMESPACE::ScalableTag<T>
42 #define HWY_FULL2(T, LMUL) \
43  hwy::HWY_NAMESPACE::ScalableTag<T, CeilLog2(HWY_MAX(0, LMUL))>
44 #define HWY_3TH_ARG(arg1, arg2, arg3, ...) arg3
45 // Workaround for MSVC grouping __VA_ARGS__ into a single argument
46 #define HWY_FULL_RECOMPOSER(args_with_paren) HWY_3TH_ARG args_with_paren
47 // Trailing comma avoids -pedantic false alarm
48 #define HWY_CHOOSE_FULL(...) \
49  HWY_FULL_RECOMPOSER((__VA_ARGS__, HWY_FULL2, HWY_FULL1, ))
50 #define HWY_FULL(...) HWY_CHOOSE_FULL(__VA_ARGS__())(__VA_ARGS__)
51 
52 // Vector of up to MAX_N lanes. It's better to use full vectors where possible.
53 #define HWY_CAPPED(T, MAX_N) \
54  hwy::HWY_NAMESPACE::CappedTag<T, HWY_MIN(MAX_N, HWY_LANES(T))>
55 
56 //------------------------------------------------------------------------------
57 // Export user functions for static/dynamic dispatch
58 
59 // Evaluates to 0 inside a translation unit if it is generating anything but the
60 // static target (the last one if multiple targets are enabled). Used to prevent
61 // redefinitions of HWY_EXPORT. Unless foreach_target.h is included, we only
62 // compile once anyway, so this is 1 unless it is or has been included.
63 #ifndef HWY_ONCE
64 #define HWY_ONCE 1
65 #endif
66 
67 // HWY_STATIC_DISPATCH(FUNC_NAME) is the namespace-qualified FUNC_NAME for
68 // HWY_STATIC_TARGET (the only defined namespace unless HWY_TARGET_INCLUDE is
69 // defined), and can be used to deduce the return type of Choose*.
70 #if HWY_STATIC_TARGET == HWY_SCALAR
71 #define HWY_STATIC_DISPATCH(FUNC_NAME) N_SCALAR::FUNC_NAME
72 #elif HWY_STATIC_TARGET == HWY_RVV
73 #define HWY_STATIC_DISPATCH(FUNC_NAME) N_RVV::FUNC_NAME
74 #elif HWY_STATIC_TARGET == HWY_WASM2
75 #define HWY_STATIC_DISPATCH(FUNC_NAME) N_WASM2::FUNC_NAME
76 #elif HWY_STATIC_TARGET == HWY_WASM
77 #define HWY_STATIC_DISPATCH(FUNC_NAME) N_WASM::FUNC_NAME
78 #elif HWY_STATIC_TARGET == HWY_NEON
79 #define HWY_STATIC_DISPATCH(FUNC_NAME) N_NEON::FUNC_NAME
80 #elif HWY_STATIC_TARGET == HWY_SVE
81 #define HWY_STATIC_DISPATCH(FUNC_NAME) N_SVE::FUNC_NAME
82 #elif HWY_STATIC_TARGET == HWY_SVE2
83 #define HWY_STATIC_DISPATCH(FUNC_NAME) N_SVE2::FUNC_NAME
84 #elif HWY_STATIC_TARGET == HWY_PPC8
85 #define HWY_STATIC_DISPATCH(FUNC_NAME) N_PPC8::FUNC_NAME
86 #elif HWY_STATIC_TARGET == HWY_SSSE3
87 #define HWY_STATIC_DISPATCH(FUNC_NAME) N_SSSE3::FUNC_NAME
88 #elif HWY_STATIC_TARGET == HWY_SSE4
89 #define HWY_STATIC_DISPATCH(FUNC_NAME) N_SSE4::FUNC_NAME
90 #elif HWY_STATIC_TARGET == HWY_AVX2
91 #define HWY_STATIC_DISPATCH(FUNC_NAME) N_AVX2::FUNC_NAME
92 #elif HWY_STATIC_TARGET == HWY_AVX3
93 #define HWY_STATIC_DISPATCH(FUNC_NAME) N_AVX3::FUNC_NAME
94 #elif HWY_STATIC_TARGET == HWY_AVX3_DL
95 #define HWY_STATIC_DISPATCH(FUNC_NAME) N_AVX3_DL::FUNC_NAME
96 #endif
97 
98 // Dynamic dispatch declarations.
99 
100 template <typename RetType, typename... Args>
102  public:
103  typedef RetType(FunctionType)(Args...);
104 
105  // A template function that when instantiated has the same signature as the
106  // function being called. This function initializes the global cache of the
107  // current supported targets mask used for dynamic dispatch and calls the
108  // appropriate function. Since this mask used for dynamic dispatch is a
109  // global cache, all the highway exported functions, even those exposed by
110  // different modules, will be initialized after this function runs for any one
111  // of those exported functions.
112  template <FunctionType* const table[]>
113  static RetType ChooseAndCall(Args... args) {
114  // If we are running here it means we need to update the chosen target.
115  ChosenTarget& chosen_target = GetChosenTarget();
116  chosen_target.Update();
117  return (table[chosen_target.GetIndex()])(args...);
118  }
119 };
120 
121 // Factory function only used to infer the template parameters RetType and Args
122 // from a function passed to the factory.
123 template <typename RetType, typename... Args>
124 FunctionCache<RetType, Args...> FunctionCacheFactory(RetType (*)(Args...)) {
125  return FunctionCache<RetType, Args...>();
126 }
127 
128 // HWY_CHOOSE_*(FUNC_NAME) expands to the function pointer for that target or
129 // nullptr is that target was not compiled.
130 #if HWY_TARGETS & HWY_SCALAR
131 #define HWY_CHOOSE_SCALAR(FUNC_NAME) &N_SCALAR::FUNC_NAME
132 #else
133 // When scalar is not present and we try to use scalar because other targets
134 // were disabled at runtime we fall back to the baseline with
135 // HWY_STATIC_DISPATCH()
136 #define HWY_CHOOSE_SCALAR(FUNC_NAME) &HWY_STATIC_DISPATCH(FUNC_NAME)
137 #endif
138 
139 #if HWY_TARGETS & HWY_WASM2
140 #define HWY_CHOOSE_WASM2(FUNC_NAME) &N_WASM2::FUNC_NAME
141 #else
142 #define HWY_CHOOSE_WASM2(FUNC_NAME) nullptr
143 #endif
144 
145 #if HWY_TARGETS & HWY_WASM
146 #define HWY_CHOOSE_WASM(FUNC_NAME) &N_WASM::FUNC_NAME
147 #else
148 #define HWY_CHOOSE_WASM(FUNC_NAME) nullptr
149 #endif
150 
151 #if HWY_TARGETS & HWY_RVV
152 #define HWY_CHOOSE_RVV(FUNC_NAME) &N_RVV::FUNC_NAME
153 #else
154 #define HWY_CHOOSE_RVV(FUNC_NAME) nullptr
155 #endif
156 
157 #if HWY_TARGETS & HWY_NEON
158 #define HWY_CHOOSE_NEON(FUNC_NAME) &N_NEON::FUNC_NAME
159 #else
160 #define HWY_CHOOSE_NEON(FUNC_NAME) nullptr
161 #endif
162 
163 #if HWY_TARGETS & HWY_SVE
164 #define HWY_CHOOSE_SVE(FUNC_NAME) &N_SVE::FUNC_NAME
165 #else
166 #define HWY_CHOOSE_SVE(FUNC_NAME) nullptr
167 #endif
168 
169 #if HWY_TARGETS & HWY_SVE2
170 #define HWY_CHOOSE_SVE2(FUNC_NAME) &N_SVE2::FUNC_NAME
171 #else
172 #define HWY_CHOOSE_SVE2(FUNC_NAME) nullptr
173 #endif
174 
175 #if HWY_TARGETS & HWY_PPC8
176 #define HWY_CHOOSE_PCC8(FUNC_NAME) &N_PPC8::FUNC_NAME
177 #else
178 #define HWY_CHOOSE_PPC8(FUNC_NAME) nullptr
179 #endif
180 
181 #if HWY_TARGETS & HWY_SSSE3
182 #define HWY_CHOOSE_SSSE3(FUNC_NAME) &N_SSSE3::FUNC_NAME
183 #else
184 #define HWY_CHOOSE_SSSE3(FUNC_NAME) nullptr
185 #endif
186 
187 #if HWY_TARGETS & HWY_SSE4
188 #define HWY_CHOOSE_SSE4(FUNC_NAME) &N_SSE4::FUNC_NAME
189 #else
190 #define HWY_CHOOSE_SSE4(FUNC_NAME) nullptr
191 #endif
192 
193 #if HWY_TARGETS & HWY_AVX2
194 #define HWY_CHOOSE_AVX2(FUNC_NAME) &N_AVX2::FUNC_NAME
195 #else
196 #define HWY_CHOOSE_AVX2(FUNC_NAME) nullptr
197 #endif
198 
199 #if HWY_TARGETS & HWY_AVX3
200 #define HWY_CHOOSE_AVX3(FUNC_NAME) &N_AVX3::FUNC_NAME
201 #else
202 #define HWY_CHOOSE_AVX3(FUNC_NAME) nullptr
203 #endif
204 
205 #if HWY_TARGETS & HWY_AVX3_DL
206 #define HWY_CHOOSE_AVX3_DL(FUNC_NAME) &N_AVX3_DL::FUNC_NAME
207 #else
208 #define HWY_CHOOSE_AVX3_DL(FUNC_NAME) nullptr
209 #endif
210 
211 #define HWY_DISPATCH_TABLE(FUNC_NAME) \
212  HWY_CONCAT(FUNC_NAME, HighwayDispatchTable)
213 
214 // HWY_EXPORT(FUNC_NAME); expands to a static array that is used by
215 // HWY_DYNAMIC_DISPATCH() to call the appropriate function at runtime. This
216 // static array must be defined at the same namespace level as the function
217 // it is exporting.
218 // After being exported, it can be called from other parts of the same source
219 // file using HWY_DYNAMIC_DISTPATCH(), in particular from a function wrapper
220 // like in the following example:
221 //
222 // #include "hwy/highway.h"
223 // HWY_BEFORE_NAMESPACE();
224 // namespace skeleton {
225 // namespace HWY_NAMESPACE {
226 //
227 // void MyFunction(int a, char b, const char* c) { ... }
228 //
229 // // NOLINTNEXTLINE(google-readability-namespace-comments)
230 // } // namespace HWY_NAMESPACE
231 // } // namespace skeleton
232 // HWY_AFTER_NAMESPACE();
233 //
234 // namespace skeleton {
235 // HWY_EXPORT(MyFunction); // Defines the dispatch table in this scope.
236 //
237 // void MyFunction(int a, char b, const char* c) {
238 // return HWY_DYNAMIC_DISPATCH(MyFunction)(a, b, c);
239 // }
240 // } // namespace skeleton
241 //
242 
243 #if HWY_IDE || ((HWY_TARGETS & (HWY_TARGETS - 1)) == 0)
244 
245 // Simplified version for IDE or the dynamic dispatch case with only one target.
246 // This case still uses a table, although of a single element, to provide the
247 // same compile error conditions as with the dynamic dispatch case when multiple
248 // targets are being compiled.
249 #define HWY_EXPORT(FUNC_NAME) \
250  HWY_MAYBE_UNUSED static decltype(&HWY_STATIC_DISPATCH(FUNC_NAME)) \
251  const HWY_DISPATCH_TABLE(FUNC_NAME)[1] = { \
252  &HWY_STATIC_DISPATCH(FUNC_NAME)}
253 #define HWY_DYNAMIC_DISPATCH(FUNC_NAME) HWY_STATIC_DISPATCH(FUNC_NAME)
254 
255 #else
256 
257 // Dynamic dispatch case with one entry per dynamic target plus the scalar
258 // mode and the initialization wrapper.
259 #define HWY_EXPORT(FUNC_NAME) \
260  static decltype(&HWY_STATIC_DISPATCH(FUNC_NAME)) \
261  const HWY_DISPATCH_TABLE(FUNC_NAME)[HWY_MAX_DYNAMIC_TARGETS + 2] = { \
262  /* The first entry in the table initializes the global cache and \
263  * calls the appropriate function. */ \
264  &decltype(hwy::FunctionCacheFactory(&HWY_STATIC_DISPATCH( \
265  FUNC_NAME)))::ChooseAndCall<HWY_DISPATCH_TABLE(FUNC_NAME)>, \
266  HWY_CHOOSE_TARGET_LIST(FUNC_NAME), \
267  HWY_CHOOSE_SCALAR(FUNC_NAME), \
268  }
269 #define HWY_DYNAMIC_DISPATCH(FUNC_NAME) \
270  (*(HWY_DISPATCH_TABLE(FUNC_NAME)[hwy::GetChosenTarget().GetIndex()]))
271 
272 #endif // HWY_IDE || ((HWY_TARGETS & (HWY_TARGETS - 1)) == 0)
273 
274 // DEPRECATED names; please use HWY_HAVE_* instead.
275 #define HWY_CAP_INTEGER64 HWY_HAVE_INTEGER64
276 #define HWY_CAP_FLOAT16 HWY_HAVE_FLOAT16
277 #define HWY_CAP_FLOAT64 HWY_HAVE_FLOAT64
278 
279 } // namespace hwy
280 
281 #endif // HWY_HIGHWAY_INCLUDED
282 
283 //------------------------------------------------------------------------------
284 
285 // NOTE: the following definitions and ops/*.h depend on HWY_TARGET, so we want
286 // to include them once per target, which is ensured by the toggle check.
287 // Because ops/*.h are included under it, they do not need their own guard.
288 #if defined(HWY_HIGHWAY_PER_TARGET) == defined(HWY_TARGET_TOGGLE)
289 #ifdef HWY_HIGHWAY_PER_TARGET
290 #undef HWY_HIGHWAY_PER_TARGET
291 #else
292 #define HWY_HIGHWAY_PER_TARGET
293 #endif
294 
295 // These define ops inside namespace hwy::HWY_NAMESPACE.
296 #if HWY_TARGET == HWY_SSSE3 || HWY_TARGET == HWY_SSE4
297 #include "hwy/ops/x86_128-inl.h"
298 #elif HWY_TARGET == HWY_AVX2
299 #include "hwy/ops/x86_256-inl.h"
300 #elif HWY_TARGET == HWY_AVX3 || HWY_TARGET == HWY_AVX3_DL
301 #include "hwy/ops/x86_512-inl.h"
302 #elif HWY_TARGET == HWY_PPC8
303 #error "PPC is not yet supported"
304 #elif HWY_TARGET == HWY_NEON
305 #include "hwy/ops/arm_neon-inl.h"
306 #elif HWY_TARGET == HWY_SVE || HWY_TARGET == HWY_SVE2
307 #include "hwy/ops/arm_sve-inl.h"
308 #elif HWY_TARGET == HWY_WASM2
309 #include "hwy/ops/wasm_256-inl.h"
310 #elif HWY_TARGET == HWY_WASM
311 #include "hwy/ops/wasm_128-inl.h"
312 #elif HWY_TARGET == HWY_RVV
313 #include "hwy/ops/rvv-inl.h"
314 #elif HWY_TARGET == HWY_SCALAR
315 #include "hwy/ops/scalar-inl.h"
316 #else
317 #pragma message("HWY_TARGET does not match any known target")
318 #endif // HWY_TARGET
319 
320 #include "hwy/ops/generic_ops-inl.h"
321 
322 #endif // HWY_HIGHWAY_PER_TARGET
Definition: aligned_allocator.h:27
FunctionCache< RetType, Args... > FunctionCacheFactory(RetType(*)(Args...))
Definition: highway.h:124
HWY_DLLEXPORT ChosenTarget & GetChosenTarget()
Definition: targets.h:230
HWY_DLLEXPORT void Update()
size_t HWY_INLINE GetIndex() const
Definition: targets.h:248
Definition: highway.h:101
RetType() FunctionType(Args...)
Definition: highway.h:103
static RetType ChooseAndCall(Args... args)
Definition: highway.h:113