VCTR
Loading...
Searching...
No Matches
Config.h
1/*
2 ==============================================================================
3 DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
5 Copyright 2022- by sonible GmbH.
6
7 This file is part of VCTR - Versatile Container Templates Reconceptualized.
8
9 VCTR is free software: you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License version 3
11 only, as published by the Free Software Foundation.
12
13 VCTR is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Lesser General Public License version 3 for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 version 3 along with VCTR. If not, see <https://www.gnu.org/licenses/>.
20 ==============================================================================
21*/
22
23//==============================================================================
24// Platform defines
25//==============================================================================
26#if defined(_WIN32) || defined(_WIN64)
27#define VCTR_WINDOWS 1
28#elif defined(LINUX) || defined(__linux__)
29#define VCTR_LINUX 1
30#elif defined(__APPLE_CPP__) || defined(__APPLE_CC__)
31#define VCTR_APPLE 1
32#elif defined(__wasm__)
33#define VCTR_WASM 1
34#endif
35
36#ifndef VCTR_WINDOWS
37#define VCTR_WINDOWS 0
38#endif
39
40#ifndef VCTR_LINUX
41#define VCTR_LINUX 0
42#endif
43
44#ifndef VCTR_APPLE
45#define VCTR_APPLE 0
46#endif
47
48#ifndef VCTR_WASM
49#define VCTR_WASM 0
50#endif
51
52//==============================================================================
53// CPU architecture defines
54//==============================================================================
55#if defined(__aarch64__) || defined(_M_ARM64)
56#define VCTR_ARM 1
57#define VCTR_X64 0
58#define VCTR_MAX_SIMD_REGISTER_SIZE 16
59#else
60#define VCTR_ARM 0
61#define VCTR_X64 1
62#define VCTR_MAX_SIMD_REGISTER_SIZE 32
63#endif
64
65//==============================================================================
66// Compiler defines
67//==============================================================================
68#if defined(__clang__)
69#define VCTR_CLANG 1
70#define VCTR_COMPILER_NAME clang
71#elif defined(__GNUC__)
72#define VCTR_GCC 1
73#define VCTR_COMPILER_NAME GCC
74#elif defined(_MSC_VER)
75#define VCTR_MSVC 1
76#define VCTR_COMPILER_NAME MSVC
77// Since Visual Studio 2019, there is builtin SVML support in MSVC
78// https://devblogs.microsoft.com/cppblog/msvc-backend-updates-in-visual-studio-2019-preview-2/
79#define VCTR_HAS_SVML (_MSC_VER >= 1921)
80#endif
81
82#ifndef VCTR_CLANG
83#define VCTR_CLANG 0
84#endif
85
86#ifndef VCTR_GCC
87#define VCTR_GCC 0
88#endif
89
90#ifndef VCTR_MSVC
91#define VCTR_MSVC 0
92#endif
93
94#ifndef VCTR_HAS_SVML
95#define VCTR_HAS_SVML 0
96#endif
97
98//==============================================================================
99// Build type defines
100//==============================================================================
101#if defined(DEBUG) || defined(_DEBUG) || ! (defined(NDEBUG) || defined(_NDEBUG))
102#define VCTR_DEBUG 1
103#else
104#define VCTR_DEBUG 0
105#endif
106
107//==============================================================================
108// User supplied defines
109//==============================================================================
110
115#ifndef VCTR_USE_IPP
116#if __has_include(<ipp.h>)
117#define VCTR_USE_IPP 1
118#else
119#define VCTR_USE_IPP 0
120#endif
121#endif
122
127#ifndef VCTR_USE_GCEM
128#if __has_include(<gcem.hpp>)
129#define VCTR_USE_GCEM 1
130#else
131#define VCTR_USE_GCEM 0
132#endif
133#endif
134
144#ifndef VCTR_ALIGNED_ARRAY
145#define VCTR_ALIGNED_ARRAY 1
146#endif
147
148#if VCTR_MSVC
149#define VCTR_TARGET(arch)
150#else
151#define VCTR_TARGET(arch) __attribute__ ((target (arch)))
152#endif
153
154#if VCTR_WINDOWS && VCTR_X64
155// This is needed for the CPU instruction set check found below
156#include <intrin.h>
157#endif
158
159namespace vctr
160{
161
163{
164 uint16_t sse4_1 : 1;
165
166 uint16_t avx : 1;
167
168 // Note: CPUs that support AVX2 always support FMA and AVX
169 uint16_t avx2 : 1;
170
171 // Note: CPUs that support FMA always support AVX
172 uint16_t fma : 1;
173
174 uint16_t neon : 1;
175};
176
177namespace detail
178{
187constexpr bool osSupportsAVXState (bool osxsave, uint64_t xcr0)
188{
189 constexpr uint64_t sseAndAVXStateMask = 0x6;
190
191 return osxsave && (xcr0 & sseAndAVXStateMask) == sseAndAVXStateMask;
192}
193
202constexpr bool osSupportsAVX512State (bool osxsave, uint64_t xcr0)
203{
204 constexpr uint64_t avx512StateMask = 0xe0;
205
206 return osSupportsAVXState (osxsave, xcr0) && (xcr0 & avx512StateMask) == avx512StateMask;
207}
208
209constexpr CPUInstructionSets makeSupportedX64InstructionSets (bool sse4_1,
210 bool avx,
211 bool avx2,
212 bool fma,
213 bool osSupportsAVX)
214{
215 const bool supportedAVX = avx && osSupportsAVX;
216
217 return {
218 .sse4_1 = sse4_1,
219 .avx = supportedAVX,
220 .avx2 = avx2 && supportedAVX,
221 .fma = fma && supportedAVX,
222 .neon = false
223 };
224}
225} // namespace detail
226
227#if VCTR_WINDOWS && VCTR_X64
228namespace detail
229{
231class X64InstructionSets
232{
233private:
234 struct CPUFeatureFinder
235 {
236 CPUFeatureFinder()
237 {
238 std::array<int, 4> cpui;
239 std::vector<std::array<int, 4>> data, extdata;
240
241 // Calling __cpuid with 0x0 as the function_id argument
242 // gets the number of the highest valid function ID.
243 __cpuid (cpui.data(), 0);
244 auto nIds = cpui[0];
245
246 for (int i = 0; i <= nIds; ++i)
247 {
248 __cpuidex (cpui.data(), i, 0);
249 data.push_back (cpui);
250 }
251
252 // load bitset with flags for function 0x00000001
253 if (nIds >= 1)
254 {
255 f_1_ECX_ = data[1][2];
256 f_1_EDX_ = data[1][3];
257
258 if (f_1_ECX_[27])
259 xcr0_ = []() VCTR_TARGET ("xsave") -> uint64_t { return _xgetbv (0); }();
260 }
261
262 // load bitset with flags for function 0x00000007
263 if (nIds >= 7)
264 {
265 f_7_EBX_ = data[7][1];
266 f_7_ECX_ = data[7][2];
267 }
268
269 // Calling __cpuid with 0x80000000 as the function_id argument
270 // gets the number of the highest valid extended ID.
271 __cpuid (cpui.data(), 0x80000000);
272 auto nExIds = cpui[0];
273
274 for (int i = 0x80000000; i <= nExIds; ++i)
275 {
276 __cpuidex (cpui.data(), i, 0);
277 extdata.push_back (cpui);
278 }
279
280 // load bitset with flags for function 0x80000001
281 if (nExIds >= 0x80000001)
282 {
283 f_81_ECX_ = extdata[1][2];
284 f_81_EDX_ = extdata[1][3];
285 }
286 };
287
288 std::bitset<32> f_1_ECX_;
289 std::bitset<32> f_1_EDX_;
290 std::bitset<32> f_7_EBX_;
291 std::bitset<32> f_7_ECX_;
292 std::bitset<32> f_81_ECX_;
293 std::bitset<32> f_81_EDX_;
294 uint64_t xcr0_ = 0;
295 };
296
297 inline static const CPUFeatureFinder cpuFeatures;
298
299public:
300 static bool hasOSAVXSupport() { return osSupportsAVXState (cpuFeatures.f_1_ECX_[27], cpuFeatures.xcr0_); }
301 static bool hasOSAVX512Support() { return osSupportsAVX512State (cpuFeatures.f_1_ECX_[27], cpuFeatures.xcr0_); }
302 static bool hasSSE41() { return cpuFeatures.f_1_ECX_[19]; }
303 static bool hasSSE42() { return cpuFeatures.f_1_ECX_[20]; }
304 static bool hasAVX() { return cpuFeatures.f_1_ECX_[28]; }
305 static bool hasAVX2() { return cpuFeatures.f_7_EBX_[5]; }
306 static bool hasFMA() { return cpuFeatures.f_1_ECX_[12]; }
307 static bool hasAVX512F() { return hasAVX() && hasOSAVX512Support() && cpuFeatures.f_7_EBX_[16]; }
308
309private:
310};
311} // namespace detail
312
313inline CPUInstructionSets getSupportedCPUInstructionSets()
314{
315 return detail::makeSupportedX64InstructionSets (detail::X64InstructionSets::hasSSE41(),
316 detail::X64InstructionSets::hasAVX(),
317 detail::X64InstructionSets::hasAVX2(),
318 detail::X64InstructionSets::hasFMA(),
319 detail::X64InstructionSets::hasOSAVXSupport());
320}
321
322#elif VCTR_ARM
323
324constexpr CPUInstructionSets getSupportedCPUInstructionSets()
325{
326 return {
327 .sse4_1 = false,
328 .avx = false,
329 .avx2 = false,
330 .fma = false,
331 .neon = true
332 };
333}
334
335#else
336
337inline CPUInstructionSets getSupportedCPUInstructionSets()
338{
339 __builtin_cpu_init();
340
341 // GCC and Clang already include OS support in __builtin_cpu_supports.
342 const bool avx = __builtin_cpu_supports ("avx");
343
344 return detail::makeSupportedX64InstructionSets (__builtin_cpu_supports ("sse4.1"),
345 avx,
346 __builtin_cpu_supports ("avx2"),
347 __builtin_cpu_supports ("fma"),
348 avx);
349}
350
351#endif
352
353namespace detail
354{
355
357template <bool... settings>
358consteval size_t trueCount()
359{
360 return (size_t (settings) + ...);
361}
362
363} // namespace detail
364
365struct Config
366{
367 static const inline auto supportedCPUInstructionSets = getSupportedCPUInstructionSets();
368
369 //==============================================================================
370 // Platform config
371 //==============================================================================
372 static constexpr bool platformWindows = VCTR_WINDOWS;
373
374 static constexpr bool platformApple = VCTR_APPLE;
375
376 static constexpr bool platformLinux = VCTR_LINUX;
377
378 static constexpr bool platformWasm = VCTR_WASM;
379
380 static_assert (detail::trueCount<platformWindows, platformApple, platformLinux, platformWasm>() == 1, "Unsupported platform or platform detection error");
381
382 //==============================================================================
383 // CPU architecture config
384 //==============================================================================
385 static constexpr bool archARM = VCTR_ARM;
386
387 static constexpr bool archX64 = VCTR_X64;
388
389 static_assert (detail::trueCount<archARM, archX64>() == 1, "Unsupported architecture or architecture detection error");
390
391 //==============================================================================
392 // Compiler config
393 //==============================================================================
394 static constexpr bool compilerClang = VCTR_CLANG;
395
396 static constexpr bool compilerGCC = VCTR_GCC;
397
398 static constexpr bool compilerMSVC = VCTR_MSVC;
399
400 static_assert (detail::trueCount<compilerClang, compilerGCC, compilerMSVC>() == 1, "Unsupported compiler or compiler detection error");
401
402 static constexpr bool hasSVML = VCTR_HAS_SVML;
403
404 //==============================================================================
405 // User supplied config
406 //==============================================================================
407 static constexpr bool hasIPP = VCTR_USE_IPP && archX64;
408
409 static constexpr bool alignedArray = VCTR_ALIGNED_ARRAY;
410
411 //==============================================================================
412 // Auto generated config
413 //==============================================================================
414 static constexpr size_t maxSIMDRegisterSize = VCTR_MAX_SIMD_REGISTER_SIZE;
415};
416
417} // namespace vctr
The main namespace of the VCTR project.
Definition: Array.h:24
Definition: Config.h:163
Definition: Config.h:366