native 0.0.1
Vectors, masks and wide register packs for C++26
Loading...
Searching...
No Matches
native.x86.avxneconvert.ccm
1// SPDX-License-Identifier: BSD-2-Clause OR Apache-2.0
2module;
3#include "native/isa_import.h"
4#include "native/config.h"
5#include "native/detail/constexpr_float.h"
6#include "native/x86/instruction_storage.h"
7#include <hint.h>
8#include <array>
9#include <bit>
10#include <cstdint>
11#include <immintrin.h>
12import native.simd;
13
14namespace native::detail::x86_neconvert {
15 template<class T> constexpr std::uint32_t widen(T const & value) noexcept {
16 // Read the representation directly; a BF16 value copy can lower through a
17 // numerical compiler helper and alter signaling NaNs or the FP environment.
18 auto bits = std::bit_cast<std::uint16_t>(value);
19 if constexpr (std::same_as<T, bf16>)
20 return std::uint32_t(bits) << 16;
21 else
22 return constexpr_float::convert_bits<constexpr_float::binary32,constexpr_float::binary16>(bits);
23 }
24 template<isa<x86> A, std::size_t N, unsigned Stride, unsigned Offset, class T>
25 constexpr simd<float, N, A> widen_memory(T const * source) noexcept {
26 // Validate the complete instruction memory operand during constant evaluation.
27 if consteval {
28 if constexpr (Stride == 2) {
29 auto last = source[2 * N - 1].to_bits();
30 (void)last;
31 }
32 }
33 std::array<float, N> result{};
34 for (std::size_t i = 0; i < N; ++i)
35 result[i] = std::bit_cast<float>(widen(source[Stride * i + Offset]));
36 return simd<float, N, A>::load(result.data());
37 }
38 template<isa<x86> A, std::size_t N>
39 constexpr simd<bf16, N, A> narrow(simd<float, N, A> input) noexcept {
40 std::array<float, N> values{};
41 input.store(values.data());
42 std::array<std::uint16_t, N> result{};
43 for (std::size_t i = 0; i < N; ++i) {
44 auto bits = std::bit_cast<std::uint32_t>(values[i]);
45 auto magnitude = bits & 0x7fffffffu;
46 if (magnitude < 0x00800000u)
47 result[i] = std::uint16_t((bits >> 16) & 0x8000u);
48 else if (magnitude > 0x7f800000u)
49 result[i] = std::uint16_t((bits >> 16) | 0x0040u);
50 else
51 result[i] = std::uint16_t((bits + 0x7fffu + ((bits >> 16) & 1u)) >> 16);
52 }
53 return simd<bf16, N, A>::load_bits(result.data());
54 }
55} // namespace native::detail::x86_neconvert
56
57namespace native {
67
70 template<isa<x86> Arch, std::size_t N>
71 requires(Arch.has(x86_feature::avxneconvert) && Arch.has(x86_feature::avx) &&
72 (N == 4 || N == 8))
73 [[nodiscard]] hint_inline hint_target(
74 "avxneconvert") constexpr simd<float, N, Arch> bcstnebf16_ps(bf16 const * source) noexcept {
75 if consteval {
76 return detail::x86_neconvert::widen_memory<Arch, N, 0, 0>(source);
77 } else {
78 if constexpr (N == 4)
79 return detail::x86_instruction_result<simd<float, N, Arch>>(_mm_bcstnebf16_ps(source));
80 else
81 return detail::x86_instruction_result<simd<float, N, Arch>>(_mm256_bcstnebf16_ps(source));
82 }
83 }
84
86 template<isa<x86> Arch, std::size_t N>
87 requires(!(Arch.has(x86_feature::avxneconvert) && Arch.has(x86_feature::avx)) &&
88 (N == 4 || N == 8) && requires { sizeof(simd<float, N, Arch>); } && !Arch.has(polyfill))
89 [[nodiscard]] consteval simd<float, N, Arch> bcstnebf16_ps(bf16 const * source) noexcept {
90 return detail::x86_neconvert::widen_memory<Arch, N, 0, 0>(source);
91 }
92
94 template<isa<x86> Arch, std::size_t N>
95 requires(!(Arch.has(x86_feature::avxneconvert) && Arch.has(x86_feature::avx)) &&
96 (N == 4 || N == 8) && requires { sizeof(simd<float, N, Arch>); } && Arch.has(polyfill))
97 [[nodiscard]] hint_inline constexpr simd<float, N, Arch> bcstnebf16_ps(bf16 const * source) noexcept {
98 return detail::x86_neconvert::widen_memory<Arch, N, 0, 0>(source);
99 }
100
103 template<isa<x86> Arch, std::size_t N>
104 requires(Arch.has(x86_feature::avxneconvert) && Arch.has(x86_feature::avx) &&
105 (N == 4 || N == 8))
106 [[nodiscard]] hint_inline hint_target(
107 "avxneconvert") constexpr simd<float, N, Arch> bcstnesh_ps(fp16 const * source) noexcept {
108 if consteval {
109 return detail::x86_neconvert::widen_memory<Arch, N, 0, 0>(source);
110 } else {
111 if constexpr (N == 4)
112 return detail::x86_instruction_result<simd<float, N, Arch>>(_mm_bcstnesh_ps(source));
113 else
114 return detail::x86_instruction_result<simd<float, N, Arch>>(_mm256_bcstnesh_ps(source));
115 }
116 }
117
119 template<isa<x86> Arch, std::size_t N>
120 requires(!(Arch.has(x86_feature::avxneconvert) && Arch.has(x86_feature::avx)) &&
121 (N == 4 || N == 8) && requires { sizeof(simd<float, N, Arch>); } && !Arch.has(polyfill))
122 [[nodiscard]] consteval simd<float, N, Arch> bcstnesh_ps(fp16 const * source) noexcept {
123 return detail::x86_neconvert::widen_memory<Arch, N, 0, 0>(source);
124 }
125
127 template<isa<x86> Arch, std::size_t N>
128 requires(!(Arch.has(x86_feature::avxneconvert) && Arch.has(x86_feature::avx)) &&
129 (N == 4 || N == 8) && requires { sizeof(simd<float, N, Arch>); } && Arch.has(polyfill))
130 [[nodiscard]] hint_inline constexpr simd<float, N, Arch> bcstnesh_ps(fp16 const * source) noexcept {
131 return detail::x86_neconvert::widen_memory<Arch, N, 0, 0>(source);
132 }
133
136 template<isa<x86> Arch, std::size_t N>
137 requires(Arch.has(x86_feature::avxneconvert) && Arch.has(x86_feature::avx) &&
138 (N == 4 || N == 8))
139 [[nodiscard]] hint_inline hint_target(
140 "avxneconvert") constexpr simd<float, N, Arch> cvtneebf16_ps(bf16 const * source) noexcept {
141 if consteval {
142 return detail::x86_neconvert::widen_memory<Arch, N, 2, 0>(source);
143 } else {
144 if constexpr (N == 4)
145 return detail::x86_instruction_result<simd<float, N, Arch>>(
146 _mm_cvtneebf16_ps(reinterpret_cast<__m128bh const *>(source)));
147 else
148 return detail::x86_instruction_result<simd<float, N, Arch>>(
149 _mm256_cvtneebf16_ps(reinterpret_cast<__m256bh const *>(source)));
150 }
151 }
152
154 template<isa<x86> Arch, std::size_t N>
155 requires(!(Arch.has(x86_feature::avxneconvert) && Arch.has(x86_feature::avx)) &&
156 (N == 4 || N == 8) && requires { sizeof(simd<float, N, Arch>); } && !Arch.has(polyfill))
157 [[nodiscard]] consteval simd<float, N, Arch> cvtneebf16_ps(bf16 const * source) noexcept {
158 return detail::x86_neconvert::widen_memory<Arch, N, 2, 0>(source);
159 }
160
162 template<isa<x86> Arch, std::size_t N>
163 requires(!(Arch.has(x86_feature::avxneconvert) && Arch.has(x86_feature::avx)) &&
164 (N == 4 || N == 8) && requires { sizeof(simd<float, N, Arch>); } && Arch.has(polyfill))
165 [[nodiscard]] hint_inline constexpr simd<float, N, Arch> cvtneebf16_ps(bf16 const * source) noexcept {
166 return detail::x86_neconvert::widen_memory<Arch, N, 2, 0>(source);
167 }
168
171 template<isa<x86> Arch, std::size_t N>
172 requires(Arch.has(x86_feature::avxneconvert) && Arch.has(x86_feature::avx) &&
173 (N == 4 || N == 8))
174 [[nodiscard]] hint_inline hint_target(
175 "avxneconvert") constexpr simd<float, N, Arch> cvtneeph_ps(fp16 const * source) noexcept {
176 if consteval {
177 return detail::x86_neconvert::widen_memory<Arch, N, 2, 0>(source);
178 } else {
179 if constexpr (N == 4)
180 return detail::x86_instruction_result<simd<float, N, Arch>>(
181 _mm_cvtneeph_ps(reinterpret_cast<__m128h const *>(source)));
182 else
183 return detail::x86_instruction_result<simd<float, N, Arch>>(
184 _mm256_cvtneeph_ps(reinterpret_cast<__m256h const *>(source)));
185 }
186 }
187
189 template<isa<x86> Arch, std::size_t N>
190 requires(!(Arch.has(x86_feature::avxneconvert) && Arch.has(x86_feature::avx)) &&
191 (N == 4 || N == 8) && requires { sizeof(simd<float, N, Arch>); } && !Arch.has(polyfill))
192 [[nodiscard]] consteval simd<float, N, Arch> cvtneeph_ps(fp16 const * source) noexcept {
193 return detail::x86_neconvert::widen_memory<Arch, N, 2, 0>(source);
194 }
195
197 template<isa<x86> Arch, std::size_t N>
198 requires(!(Arch.has(x86_feature::avxneconvert) && Arch.has(x86_feature::avx)) &&
199 (N == 4 || N == 8) && requires { sizeof(simd<float, N, Arch>); } && Arch.has(polyfill))
200 [[nodiscard]] hint_inline constexpr simd<float, N, Arch> cvtneeph_ps(fp16 const * source) noexcept {
201 return detail::x86_neconvert::widen_memory<Arch, N, 2, 0>(source);
202 }
203
206 template<isa<x86> Arch, std::size_t N>
207 requires(Arch.has(x86_feature::avxneconvert) && Arch.has(x86_feature::avx) &&
208 (N == 4 || N == 8))
209 [[nodiscard]] hint_inline hint_target(
210 "avxneconvert") constexpr simd<float, N, Arch> cvtneobf16_ps(bf16 const * source) noexcept {
211 if consteval {
212 return detail::x86_neconvert::widen_memory<Arch, N, 2, 1>(source);
213 } else {
214 if constexpr (N == 4)
215 return detail::x86_instruction_result<simd<float, N, Arch>>(
216 _mm_cvtneobf16_ps(reinterpret_cast<__m128bh const *>(source)));
217 else
218 return detail::x86_instruction_result<simd<float, N, Arch>>(
219 _mm256_cvtneobf16_ps(reinterpret_cast<__m256bh const *>(source)));
220 }
221 }
222
224 template<isa<x86> Arch, std::size_t N>
225 requires(!(Arch.has(x86_feature::avxneconvert) && Arch.has(x86_feature::avx)) &&
226 (N == 4 || N == 8) && requires { sizeof(simd<float, N, Arch>); } && !Arch.has(polyfill))
227 [[nodiscard]] consteval simd<float, N, Arch> cvtneobf16_ps(bf16 const * source) noexcept {
228 return detail::x86_neconvert::widen_memory<Arch, N, 2, 1>(source);
229 }
230
232 template<isa<x86> Arch, std::size_t N>
233 requires(!(Arch.has(x86_feature::avxneconvert) && Arch.has(x86_feature::avx)) &&
234 (N == 4 || N == 8) && requires { sizeof(simd<float, N, Arch>); } && Arch.has(polyfill))
235 [[nodiscard]] hint_inline constexpr simd<float, N, Arch> cvtneobf16_ps(bf16 const * source) noexcept {
236 return detail::x86_neconvert::widen_memory<Arch, N, 2, 1>(source);
237 }
238
241 template<isa<x86> Arch, std::size_t N>
242 requires(Arch.has(x86_feature::avxneconvert) && Arch.has(x86_feature::avx) &&
243 (N == 4 || N == 8))
244 [[nodiscard]] hint_inline hint_target(
245 "avxneconvert") constexpr simd<float, N, Arch> cvtneoph_ps(fp16 const * source) noexcept {
246 if consteval {
247 return detail::x86_neconvert::widen_memory<Arch, N, 2, 1>(source);
248 } else {
249 if constexpr (N == 4)
250 return detail::x86_instruction_result<simd<float, N, Arch>>(
251 _mm_cvtneoph_ps(reinterpret_cast<__m128h const *>(source)));
252 else
253 return detail::x86_instruction_result<simd<float, N, Arch>>(
254 _mm256_cvtneoph_ps(reinterpret_cast<__m256h const *>(source)));
255 }
256 }
257
259 template<isa<x86> Arch, std::size_t N>
260 requires(!(Arch.has(x86_feature::avxneconvert) && Arch.has(x86_feature::avx)) &&
261 (N == 4 || N == 8) && requires { sizeof(simd<float, N, Arch>); } && !Arch.has(polyfill))
262 [[nodiscard]] consteval simd<float, N, Arch> cvtneoph_ps(fp16 const * source) noexcept {
263 return detail::x86_neconvert::widen_memory<Arch, N, 2, 1>(source);
264 }
265
267 template<isa<x86> Arch, std::size_t N>
268 requires(!(Arch.has(x86_feature::avxneconvert) && Arch.has(x86_feature::avx)) &&
269 (N == 4 || N == 8) && requires { sizeof(simd<float, N, Arch>); } && Arch.has(polyfill))
270 [[nodiscard]] hint_inline constexpr simd<float, N, Arch> cvtneoph_ps(fp16 const * source) noexcept {
271 return detail::x86_neconvert::widen_memory<Arch, N, 2, 1>(source);
272 }
273
276 template<isa<x86> Arch, std::size_t N>
277 requires(Arch.has(x86_feature::avxneconvert) && Arch.has(x86_feature::avx) &&
278 (N == 4 || N == 8))
279 [[nodiscard]] hint_inline
280 hint_target("avxneconvert") constexpr simd<bf16, N, Arch> cvtneps_bf16(
281 simd<float, N, Arch> input) noexcept {
282 if consteval {
283 return detail::x86_neconvert::narrow(input);
284 } else {
285 using R = simd<bf16, N, Arch>;
286 // Copy the complete register representation. An existing AVX512_BF16
287 // storage specialization may attach stronger targets to its named bridge.
288 if constexpr (N == 4)
289 return detail::x86_half_result<R>(_mm_cvtneps_avx_pbh(detail::x86_float_register(input)));
290 else
291 return detail::x86_half_result<R>(_mm256_cvtneps_avx_pbh(detail::x86_float_register(input)));
292 }
293 }
294
296 template<isa<x86> Arch, std::size_t N>
297 requires(!(Arch.has(x86_feature::avxneconvert) && Arch.has(x86_feature::avx)) &&
298 (N == 4 || N == 8) &&
299 requires {
300 sizeof(simd<float, N, Arch>);
301 sizeof(simd<bf16, N, Arch>);
302 } && !Arch.has(polyfill))
303 [[nodiscard]] consteval simd<bf16, N, Arch> cvtneps_bf16(simd<float, N, Arch> input) noexcept {
304 return detail::x86_neconvert::narrow(input);
305 }
306
308 template<isa<x86> Arch, std::size_t N>
309 requires(!(Arch.has(x86_feature::avxneconvert) && Arch.has(x86_feature::avx)) &&
310 (N == 4 || N == 8) &&
311 requires {
312 sizeof(simd<float, N, Arch>);
313 sizeof(simd<bf16, N, Arch>);
314 } && Arch.has(polyfill))
315 [[nodiscard]] hint_inline constexpr simd<bf16, N, Arch> cvtneps_bf16(simd<float, N, Arch> input) noexcept {
316 return detail::x86_neconvert::narrow(input);
317 }
318
319} // namespace native
320
321export module native.x86.avxneconvert;
322export import native.x86.features;
323export import native.simd;
324
325#if NATIVE_HOST_X86 || defined(NATIVE_DOXYGEN)
326export namespace native {
327 using ::native::bcstnebf16_ps;
328 using ::native::bcstnesh_ps;
329 using ::native::cvtneebf16_ps;
330 using ::native::cvtneeph_ps;
331 using ::native::cvtneobf16_ps;
332 using ::native::cvtneoph_ps;
333 using ::native::cvtneps_bf16;
334}
335#endif
constexpr simd< float, N, Arch > bcstnesh_ps(fp16 const *source) noexcept
constexpr simd< float, N, Arch > cvtneoph_ps(fp16 const *source) noexcept
constexpr simd< float, N, Arch > cvtneeph_ps(fp16 const *source) noexcept
constexpr simd< float, N, Arch > cvtneebf16_ps(bf16 const *source) noexcept
constexpr simd< bf16, N, Arch > cvtneps_bf16(simd< float, N, Arch > input) noexcept
constexpr simd< float, N, Arch > bcstnebf16_ps(bf16 const *source) noexcept
constexpr simd< float, N, Arch > cvtneobf16_ps(bf16 const *source) noexcept
Architecture-tagged vectors, register packs and supporting value types. Native arithmetic follows its...
Omitted architecture arguments use the native.simd provider's baseline.