native 0.0.1
Vectors, masks and wide register packs for C++26
Loading...
Searching...
No Matches
polyfill_dot2.h
1// SPDX-FileCopyrightText: 2026 Edward Kmett <ekmett@gmail.com>
2// SPDX-License-Identifier: BSD-2-Clause OR Apache-2.0
3#pragma once
4
5#if NATIVE_HOST_NEON
6#pragma clang attribute push(__attribute__((target("neon,bf16"))), apply_to=function)
7namespace native::detail {
8 // Reconstitute native register groups from exact logical representations.
9 // The last native group has zeroed inactive inputs and is trimmed on return.
10 template<class H,class V>
11 hint_inline constexpr V polyfill_dot2_native(H a,H b,V accumulator) noexcept {
12 if consteval { return half_constant::dot2<true>(a,b,accumulator); } else {
13 std::array<std::uint16_t,H::lanes> left{},right{};
14 std::array<std::uint32_t,V::lanes> result{};
15 a.store_bits(left.data()); b.store_bits(right.data()); accumulator.store_bits(result.data());
16 for(std::size_t begin=0;begin<V::lanes;begin+=4) {
17 auto const count=std::min(std::size_t{4},V::lanes-begin);
18 std::array<std::uint16_t,8> x{},y{}; std::array<std::uint32_t,4> z{};
19 for(std::size_t i=0;i<count;++i) {
20 x[2*i]=left[2*(begin+i)]; x[2*i+1]=left[2*(begin+i)+1];
21 y[2*i]=right[2*(begin+i)]; y[2*i+1]=right[2*(begin+i)+1]; z[i]=result[begin+i];
22 }
23 z=__builtin_bit_cast(decltype(z),neon_bf16_backend::dot2_native(
24 __builtin_bit_cast(bfloat16x8_t,x),__builtin_bit_cast(bfloat16x8_t,y),__builtin_bit_cast(float32x4_t,z)));
25 for(std::size_t i=0;i<count;++i) result[begin+i]=z[i];
26 }
27 return V::load_bits(result.data());
28 }
29 }
30}
31#pragma clang attribute pop
32#elif NATIVE_HOST_X86
33// The 512-bit opcode does not require AVX512VL or AVX512DQ. Tags carrying only
34// BF16 and its actual prerequisites still use this available native operation.
35#pragma clang attribute push(__attribute__((target("avx512bf16"))), apply_to=function)
36namespace native::detail {
37 template<class H,class V>
38 hint_inline constexpr V polyfill_dot2_native(H a,H b,V accumulator) noexcept {
39 if consteval { return half_constant::dot2<false>(a,b,accumulator); } else {
40 std::array<std::uint16_t,H::lanes> left{},right{};
41 std::array<std::uint32_t,V::lanes> result{};
42 a.store_bits(left.data()); b.store_bits(right.data()); accumulator.store_bits(result.data());
43 for(std::size_t begin=0;begin<V::lanes;begin+=16) {
44 auto const count=std::min(std::size_t{16},V::lanes-begin);
45 std::array<std::uint16_t,32> x{},y{}; std::array<std::uint32_t,16> z{};
46 for(std::size_t i=0;i<count;++i) {
47 x[2*i]=left[2*(begin+i)]; x[2*i+1]=left[2*(begin+i)+1];
48 y[2*i]=right[2*(begin+i)]; y[2*i+1]=right[2*(begin+i)+1]; z[i]=result[begin+i];
49 }
50 z=__builtin_bit_cast(decltype(z),_mm512_dpbf16_ps(__builtin_bit_cast(__m512,z),
51 __builtin_bit_cast(__m512bh,x),__builtin_bit_cast(__m512bh,y)));
52 for(std::size_t i=0;i<count;++i) result[begin+i]=z[i];
53 }
54 return V::load_bits(result.data());
55 }
56 }
57}
58#pragma clang attribute pop
59#endif