Intel xvec
Experimental C++ simd class library
Loading...
Searching...
No Matches
target_config.hpp
1//===----------------------------------------------------------------------===//
2//
3// Copyright (C) 2021 Intel Corporation
4//
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#pragma once
10
11#include <immintrin.h>
12
13#include <xvec/detail/config.hpp>
14
15#include <concepts>
16
17namespace _XVEC_NAMESPACE::simd
18{
19
23struct generic_tag {};
24
28
29#if defined(__SSE__) and !defined(_XVEC_FORCE_SCALAR)
30
31// These target tags are specific to x86 and provide a hierarchy of
32// capabilities. Note that one of the tags must be compact_mask_tag, which
33// indicates that the target supports compact masks. This is used to select the
34// default implementation of some operations which can be more efficient if
35// compact masks are supported. The tag must appear unambiguously in the
36// hierarchy so that the tag overloads always resolve to exactly one option
37// (e.g., no multiple inheritance to make several tags qualify).
38struct x86_tag : public generic_tag {};
39struct x86_sse_tag : public x86_tag {};
40struct x86_avx_tag : public x86_sse_tag {};
41struct x86_avx2_tag : public x86_avx_tag {};
42struct compact_mask_tag : public x86_avx2_tag {};
43struct x86_avx512_tag : public compact_mask_tag {};
44struct x86_avxsnc_tag : public x86_avx512_tag {};
45struct x86_avxspr_tag : public x86_avxsnc_tag {};
46struct x86_avx10_tag : public x86_avxspr_tag {};
47
48#if defined(__AVX10_2__)
49 using target_tag = x86_avx10_tag;
50#elif defined(__AVX512FP16__)
51 using target_tag = x86_avxspr_tag;
52#elif defined(__AVX512VPOPCNTDQ__)
53 using target_tag = x86_avxsnc_tag;
54#elif defined(__AVX512F__)
55 using target_tag = x86_avx512_tag;
56#elif defined (__AVX2__)
57 using target_tag = x86_avx2_tag;
58#elif defined (__AVX__)
59 using target_tag = x86_avx_tag;
60#else
61 using target_tag = x86_sse_tag;
62#endif
63
64namespace detail
65{
66
68inline constexpr bool hasSse = std::derived_from<target_tag, x86_sse_tag>;
69inline constexpr bool hasAvx = std::derived_from<target_tag, x86_avx_tag>;
70inline constexpr bool hasAvx2 = std::derived_from<target_tag, x86_avx2_tag>;
71inline constexpr bool hasAvx512 = std::derived_from<target_tag, x86_avx512_tag>;
72inline constexpr bool hasAvxFp16 = std::derived_from<target_tag, x86_avxspr_tag>;
73
75inline constexpr int maxBytesInVec = hasAvx512 ? 64 : hasAvx ? 32 : hasSse ? 16 : 0;
76
81template<typename _Tp, int _Np>
82inline constexpr int register_size =
83 (hasAvx512 && (sizeof(_Tp) * _Np) > 32) ? (64 / sizeof(_Tp)) :
84 (hasAvx && (sizeof(_Tp) * _Np) > 16) ? (32 / sizeof(_Tp)) :
85 16 / sizeof(_Tp);
86
92template<typename T>
93consteval auto get_register_type() {
94 constexpr auto bytes = sizeof(T);
95
96 using _Tp = std::remove_cvref_t<decltype(std::declval<T&>()[0])>;
97
98 constexpr bool isFloat = std::same_as<float, _Tp>;
99 constexpr bool isDouble = std::same_as<double, _Tp>;
100
101 if constexpr (bytes > maxBytesInVec)
102 return invalid_register{};
103 else
104#if defined(__AVX512FP16__)
105 if constexpr (std::same_as<_Tp, _Float16>)
106 {
107 if constexpr (bytes > 32) return __m512h();
108 else if constexpr (bytes > 16) return __m256h();
109 else return __m128h();
110 } else
111#endif
112#if defined(__AVX512F__)
113 if constexpr (bytes > 32)
114 {
115 if constexpr (isFloat) return __m512();
116 else if constexpr (isDouble) return __m512d();
117 else return __m512i();
118 } else
119#endif
120#if defined(__AVX__)
121 if constexpr (bytes > 16)
122 {
123 if constexpr (isFloat) return __m256();
124 else if constexpr (isDouble) return __m256d();
125 else return __m256i();
126 } else
127#endif
128 {
129 if constexpr (isFloat) return __m128();
130 else if constexpr (isDouble) return __m128d();
131 else return __m128i();
132 }
133}
134
135} // namespace detail
136
137#else
138
139namespace detail
140{
148inline constexpr int maxBytesInVec = 16;
149template<typename _Tp, int _Np> inline constexpr int register_size = maxBytesInVec / sizeof(_Tp);
150
157template<typename> consteval auto get_register_type() { return invalid_register{}; }
158
159}
160
161struct compact_mask_tag : public generic_tag {};
162
163#if defined(_XVEC_GENERIC_COMPACT)
164 using target_tag = compact_mask_tag;
165#else
166 using target_tag = generic_tag;
167#endif
168
169#endif
170
175template<typename Tag> concept has_compact_mask = std::derived_from<Tag, compact_mask_tag>;
176
177inline constexpr auto target = target_tag{};
178
179} // namespace _XVEC_NAMESPACE::simd
Definition target_config.hpp:175
consteval auto get_register_type()
Definition target_config.hpp:157
constexpr int maxBytesInVec
Definition target_config.hpp:148
Definition target_config.hpp:161
Definition target_config.hpp:23
Definition target_config.hpp:27