native 0.0.1
Vectors, masks and wide register packs for C++26
Loading...
Searching...
No Matches
simd.h
1// SPDX-License-Identifier: BSD-2-Clause OR Apache-2.0
2#pragma once
3#include "native/config.h"
5#include "native/targets.h"
6#include "native/detail/constexpr_float.h"
7#include "native/arm/bf16_constexpr.h"
8#include <tuple>
9#include <algorithm>
10#include <array>
11#include <bit>
12#include <cassert>
13#include <cmath>
14#include <cstring>
15#include <span>
16#include <limits>
17#if NATIVE_HOST_WASM
18#include <wasm_simd128.h>
19#endif
20#if NATIVE_HOST_X86
21#include <immintrin.h>
22#endif
23#if NATIVE_HOST_NEON
24#include <arm_neon.h>
25#include "native/arm/detail/register_order.h"
26#include "native/arm/float_control.h"
27#endif
28
29namespace native::detail::float_constant {
30 namespace cf=constexpr_float;
31 using f32=cf::binary32;
32 constexpr std::uint32_t add(std::uint32_t a,std::uint32_t b) noexcept {return cf::add_bits<f32>(a,b);}
33 constexpr std::uint32_t subtract(std::uint32_t a,std::uint32_t b) noexcept {return cf::sub_bits<f32>(a,b);}
34 constexpr std::uint32_t multiply(std::uint32_t a,std::uint32_t b) noexcept {return cf::mul_bits<f32>(a,b);}
35 constexpr std::uint32_t divide(std::uint32_t a,std::uint32_t b) noexcept {return cf::div_bits<f32>(a,b);}
36 constexpr std::uint32_t negate(std::uint32_t a) noexcept {return a^f32::sign_mask;}
37 constexpr std::uint32_t multiply_add(std::uint32_t a,std::uint32_t b,std::uint32_t c) noexcept {return cf::fma_bits<f32>(a,b,c);}
38 constexpr std::uint32_t square_root(std::uint32_t a) noexcept {return cf::sqrt_bits<f32>(a);}
39 constexpr std::uint32_t nearest(std::uint32_t a) noexcept {return cf::round_integral_bits<f32>(a);}
40 constexpr std::uint32_t floor(std::uint32_t a) noexcept {return cf::round_integral_bits<f32>(a,cf::rounding::downward);}
41 constexpr std::uint32_t ceil(std::uint32_t a) noexcept {return cf::round_integral_bits<f32>(a,cf::rounding::upward);}
42 constexpr std::uint32_t trunc(std::uint32_t a) noexcept {return cf::round_integral_bits<f32>(a,cf::rounding::toward_zero);}
43 constexpr std::int32_t fcvtzs(std::uint32_t bits) noexcept {
44 auto const magnitude = bits & 0x7fffffffu;
45 if (magnitude > 0x7f800000u) return 0;
46 if (magnitude >= 0x4f000000u)
47 return (bits >> 31) ? INT32_MIN : INT32_MAX;
48 if (magnitude < 0x3f800000u) return 0;
49 auto const exponent = int(magnitude >> 23) - 127;
50 auto const significand = (magnitude & 0x007fffffu) | 0x00800000u;
51 auto const value = std::int32_t(exponent < 23
52 ? significand >> (23 - exponent) : significand << (exponent - 23));
53 return (bits >> 31) ? -value : value;
54 }
55 constexpr std::uint32_t fcvtzu(std::uint32_t bits) noexcept {
56 auto const magnitude = bits & 0x7fffffffu;
57 if ((bits >> 31) || magnitude > 0x7f800000u || magnitude < 0x3f800000u) return 0;
58 if (magnitude >= 0x4f800000u) return UINT32_MAX;
59 auto const exponent = int(magnitude >> 23) - 127;
60 auto const significand = (magnitude & 0x007fffffu) | 0x00800000u;
61 return exponent < 23 ? significand >> (23 - exponent) : significand << (exponent - 23);
62 }
63 constexpr std::uint32_t power_of_two(std::uint32_t a) noexcept {return std::uint32_t(int(std::bit_cast<float>(a))+127)<<23;}
64 constexpr bool less(std::uint32_t a,std::uint32_t b) noexcept {return cf::less_bits<f32>(a,b);}
65 constexpr bool equal(std::uint32_t a,std::uint32_t b) noexcept {return cf::equal_bits<f32>(a,b);}
66 constexpr std::uint32_t scale(std::uint32_t x,std::uint32_t exponent,
67 cf::rounding mode=cf::rounding::nearest_even,cf::policy p={}) noexcept {
68 x=cf::flush_input<f32>(x,p); exponent=cf::flush_input<f32>(exponent,p);
69 if(cf::is_signaling_nan<f32>(x)) return cf::select_nan<f32>(std::array{x,exponent},p);
70 if(cf::is_nan<f32>(exponent)) return cf::select_nan<f32>(std::array{x,exponent},p);
71 if(cf::is_infinite<f32>(exponent)) {
72 bool down=(exponent&f32::sign_mask)!=0;
73 if(cf::is_nan<f32>(x)) return down?0u:f32::exponent_mask;
74 if(cf::is_infinite<f32>(x)) return down?cf::default_nan<f32>(p):x;
75 if(cf::is_zero<f32>(x)) return down?x:cf::default_nan<f32>(p);
76 return (x&f32::sign_mask)|(down?0u:f32::exponent_mask);
77 }
78 if(cf::is_nan<f32>(x)) return cf::select_nan<f32>(std::array{x},p);
79 if(cf::is_infinite<f32>(x) || cf::is_zero<f32>(x)) return x;
80 float n=std::bit_cast<float>(floor(exponent));
81 int shift=n < -512.f?-512:n > 512.f?512:static_cast<int>(n);
82 auto a=cf::unpack<f32>(x);
83 return cf::round_pack<f32>(a.sign,cf::magnitude<1>{{a.significand}},a.exponent+shift,mode,p);
84 }
85 template<class V> constexpr auto words(V value) noexcept {
86 std::array<std::uint32_t,V::lanes> words{};
87 value.store_bits(words.data());
88 return words;
89 }
90 template<class F,class V,class... W>
91 constexpr V map(F operation,V value,W... rest) noexcept {
92 auto inputs=std::tuple{words(value),words(rest)...};
93 std::array<std::uint32_t,V::lanes> result{};
94 for(std::size_t i=0;i<V::lanes;++i)
95 result[i]=std::apply([&](auto const&... input) {return operation(input[i]...);},inputs);
96 return V::load_bits(result.data());
97 }
98 template<class F,class V>
99 constexpr auto compare(F operation,V a,V b) noexcept {
100 auto x=words(a),y=words(b);
101 std::uint64_t bits=0;
102 for(std::size_t i=0;i<V::lanes;++i) bits|=std::uint64_t(operation(x[i],y[i]))<<i;
103 return V::mask_type::from_bitset(bits);
104 }
105 template<class M,class V>
106 constexpr V select(M mask,V a,V b) noexcept {
107 auto x=words(a),y=words(b);
108 auto bits=mask.to_bitset();
109 for(std::size_t i=0;i<V::lanes;++i) if(!((bits>>i)&1)) x[i]=y[i];
110 return V::load_bits(x.data());
111 }
112}
113#define NATIVE_BACKEND_BODY "native/simd/simd_family.h"
114#include "native/simd/for_each_backend.h"
115#undef NATIVE_BACKEND_BODY
116
117#if NATIVE_HOST_X86 && (!defined(NATIVE_PROFILE) || NATIVE_PROFILE != 0)
118#define NATIVE_COMMON_ARCH(...) (::native::abi_lookup<__VA_ARGS__,::native::detail::memory_kernel_policies>::index == 1)
119#pragma clang attribute push(__attribute__((target(NATIVE_KERNEL_TARGET_9))), apply_to=function)
120#include "native/simd/common_body.h"
121#pragma clang attribute pop
122#undef NATIVE_COMMON_ARCH
123#define NATIVE_COMMON_ARCH(...) (::native::abi_lookup<__VA_ARGS__,::native::detail::memory_kernel_policies>::index == 2)
124#pragma clang attribute push(__attribute__((target(NATIVE_KERNEL_TARGET_13))), apply_to=function)
125#include "native/simd/common_body.h"
126#pragma clang attribute pop
127#undef NATIVE_COMMON_ARCH
128#define NATIVE_COMMON_ARCH(...) (::native::abi_lookup<__VA_ARGS__,::native::detail::memory_kernel_policies>::index == 0)
129#pragma clang attribute push(__attribute__((target(NATIVE_KERNEL_TARGET_17))), apply_to=function)
130#include "native/simd/common_body.h"
131#pragma clang attribute pop
132#undef NATIVE_COMMON_ARCH
133#endif
134
135#if NATIVE_HOST_NEON && (!defined(NATIVE_PROFILE) || NATIVE_PROFILE != 0)
136#define NATIVE_COMMON_ARCH(...) (::native::abi_lookup<__VA_ARGS__,::native::detail::memory_kernel_policies>::index == 1)
137#pragma clang attribute push(__attribute__((target(NATIVE_KERNEL_TARGET_21))), apply_to=function)
138#include "native/simd/common_body.h"
139#pragma clang attribute pop
140#undef NATIVE_COMMON_ARCH
141#define NATIVE_COMMON_ARCH(...) (::native::abi_lookup<__VA_ARGS__,::native::detail::memory_kernel_policies>::index == 2)
142#pragma clang attribute push(__attribute__((target(NATIVE_KERNEL_TARGET_22))), apply_to=function)
143#include "native/simd/common_body.h"
144#pragma clang attribute pop
145#undef NATIVE_COMMON_ARCH
146#define NATIVE_COMMON_ARCH(...) (::native::abi_lookup<__VA_ARGS__,::native::detail::memory_kernel_policies>::index == 0)
147#pragma clang attribute push(__attribute__((target(NATIVE_KERNEL_TARGET_23))), apply_to=function)
148#include "native/simd/common_body.h"
149#pragma clang attribute pop
150#undef NATIVE_COMMON_ARCH
151#endif
152
153namespace native::detail::half_constant {
154 namespace fp=constexpr_float;
155 using format=fp::binary16;
156
157 template<bool Arm> constexpr fp::policy arithmetic_policy() noexcept {
158 fp::policy p;
159 if constexpr(!Arm) {
160 p.nan=fp::nan_propagation::first;
161 p.fma_order=fp::fma_nan_order::multiplicands_first;
162 p.default_nan_negative=true;
163 p.invalid_product_overrides_quiet_addend=false;
164 }
165 return p;
166 }
167
168 template<class V,class F> constexpr V unary(V a,F operation) noexcept {
169 std::array<std::uint16_t,V::lanes> words{}; a.store_bits(words.data());
170 for(auto & word:words) word=operation(word);
171 return V::load_bits(words.data());
172 }
173 template<class V,class F> constexpr V binary(V a,V b,F operation) noexcept {
174 std::array<std::uint16_t,V::lanes> left{},right{};
175 a.store_bits(left.data()); b.store_bits(right.data());
176 for(std::size_t i=0;i<V::lanes;++i) left[i]=operation(left[i],right[i]);
177 return V::load_bits(left.data());
178 }
179 enum class operation { add, subtract, multiply, divide };
180 template<operation Op,bool Arm,class V> constexpr V arithmetic(V a,V b) noexcept {
181 return binary(a,b,[](auto x,auto y) {
182 constexpr auto mode=fp::rounding::nearest_even;
183 constexpr auto policy=arithmetic_policy<Arm>();
184 if constexpr(Op==operation::add) return fp::add_bits<format>(x,y,mode,policy);
185 else if constexpr(Op==operation::subtract) return fp::sub_bits<format>(x,y,mode,policy);
186 else if constexpr(Op==operation::multiply) return fp::mul_bits<format>(x,y,mode,policy);
187 else return fp::div_bits<format>(x,y,mode,policy);
188 });
189 }
190 template<bool Arm,class V> constexpr V square_root(V a) noexcept {
191 return unary(a,[](auto x) {
192 return fp::sqrt_bits<format>(x,fp::rounding::nearest_even,arithmetic_policy<Arm>());
193 });
194 }
195 template<bool Arm,class V> constexpr V fused(V a,V b,V c) noexcept {
196 std::array<std::uint16_t,V::lanes> left{},right{},result{};
197 a.store_bits(left.data()); b.store_bits(right.data()); c.store_bits(result.data());
198 for(std::size_t i=0;i<V::lanes;++i)
199 result[i]=fp::fma_bits<format>(left[i],right[i],result[i],
200 fp::rounding::nearest_even,arithmetic_policy<Arm>());
201 return V::load_bits(result.data());
202 }
203 template<class V,class F> constexpr typename V::mask compare(V a,V b,F operation) noexcept {
204 std::array<std::uint16_t,V::lanes> left{},right{};
205 a.store_bits(left.data()); b.store_bits(right.data());
206 std::uint64_t result=0;
207 for(std::size_t i=0;i<V::lanes;++i) result|=std::uint64_t(operation(left[i],right[i]))<<i;
208 return V::mask::from_bitset(result);
209 }
210 template<class V> constexpr V select(typename V::mask mask,V a,V b) noexcept {
211 std::array<std::uint16_t,V::lanes> left{},right{};
212 a.store_bits(left.data()); b.store_bits(right.data());
213 auto active=mask.to_bitset();
214 for(std::size_t i=0;i<V::lanes;++i) if(!((active>>i)&1)) left[i]=right[i];
215 return V::load_bits(left.data());
216 }
217
218 template<bool Arm,class H,class V> constexpr V dot2(H a,H b,V accumulator) noexcept {
219 std::array<std::uint16_t,H::lanes> left{},right{};
220 std::array<std::uint32_t,V::lanes> result{};
221 a.store_bits(left.data()); b.store_bits(right.data()); accumulator.store_bits(result.data());
222 fp::policy p;
223 p.nan=fp::nan_propagation::first;
224 p.fma_order=fp::fma_nan_order::multiplicands_first;
225 p.flush_inputs=p.flush_outputs=true;
226 p.default_nan_negative=true;
227 p.invalid_product_overrides_quiet_addend=false;
228 auto const control=Arm?arm_float_control::current():arm_float_control{};
229 for(std::size_t i=0;i<V::lanes;++i) {
230 if constexpr(Arm) result[i]=arm_bfdot_bits(result[i],left[2*i],left[2*i+1],right[2*i],right[2*i+1],control);
231 else {
232 // The instruction evaluates the high product first. The low product's
233 // NaNs therefore take precedence over both the high pair and addend.
234 auto high=fp::fma_bits<fp::binary32>(std::uint32_t(left[2*i+1])<<16,
235 std::uint32_t(right[2*i+1])<<16,result[i],fp::rounding::nearest_even,p);
236 result[i]=fp::fma_bits<fp::binary32>(std::uint32_t(left[2*i])<<16,
237 std::uint32_t(right[2*i])<<16,high,fp::rounding::nearest_even,p);
238 }
239 }
240 return V::load_bits(result.data());
241 }
242}
243
244#if NATIVE_HOST_X86
245// Intrinsic calls are owned by the global module fragment.
246#pragma clang attribute push(__attribute__((target("avx2,fma,avx512f,avx512dq,avx512bw,avx512vl,avx512bf16"))), apply_to=function)
247namespace native::detail::avx512_bf16_backend {
248 hint_inline __m128 dot2_native(__m128bh a, __m128bh b, __m128 accumulator) noexcept {
249 return _mm_dpbf16_ps(accumulator, a, b);
250 }
251 hint_inline __m256 dot2_native(__m256bh a, __m256bh b, __m256 accumulator) noexcept {
252 return _mm256_dpbf16_ps(accumulator, a, b);
253 }
254 hint_inline __m512 dot2_native(__m512bh a, __m512bh b, __m512 accumulator) noexcept {
255 return _mm512_dpbf16_ps(accumulator, a, b);
256 }
257}
258#pragma clang attribute pop
259// Intrinsic calls are owned by the global module fragment.
260#pragma clang attribute push(__attribute__((target("avx2,fma,avx512f,avx512dq,avx512bw,avx512vl,avx512fp16"))), apply_to=function)
261namespace native::detail::avx512_fp16_backend {
262 // Explicit native builtins avoid TU-level excess-precision widening when the
263 // provider is compiled below AVX512-FP16. MXCSR still supplies rounding.
264 hint_inline __m512h add_half(__m512h a, __m512h b) noexcept { return _mm512_add_round_ph(a,b,_MM_FROUND_CUR_DIRECTION); }
265 hint_inline __m512h sub_half(__m512h a, __m512h b) noexcept { return _mm512_sub_round_ph(a,b,_MM_FROUND_CUR_DIRECTION); }
266 hint_inline __m512h mul_half(__m512h a, __m512h b) noexcept { return _mm512_mul_round_ph(a,b,_MM_FROUND_CUR_DIRECTION); }
267 hint_inline __m512h div_half(__m512h a, __m512h b) noexcept { return _mm512_div_round_ph(a,b,_MM_FROUND_CUR_DIRECTION); }
268 hint_inline __m512h sqrt_half(__m512h a) noexcept { return _mm512_sqrt_ph(a); }
269 hint_inline __m512h neg_half(__m512h a) noexcept {
270 return _mm512_castsi512_ph(_mm512_xor_si512(_mm512_castph_si512(a),_mm512_set1_epi16(short(0x8000))));
271 }
272 hint_inline __m512h fma_half(__m512h a, __m512h b, __m512h c) noexcept { return _mm512_fmadd_ph(a,b,c); }
273 hint_inline __mmask32 eq_half(__m512h a, __m512h b) noexcept { return _mm512_cmp_ph_mask(a,b,_CMP_EQ_OQ); }
274 hint_inline __mmask32 lt_half(__m512h a, __m512h b) noexcept { return _mm512_cmp_ph_mask(a,b,_CMP_LT_OQ); }
275 hint_inline __mmask32 le_half(__m512h a, __m512h b) noexcept { return _mm512_cmp_ph_mask(a,b,_CMP_LE_OQ); }
276 hint_inline __m512h select_half(__mmask32 m, __m512h a, __m512h b) noexcept {
277 return _mm512_mask_blend_ph(m,b,a);
278 }
279}
280#pragma clang attribute pop
281#endif
282
283#if NATIVE_HOST_NEON
284// Native calls are owned by the global module fragment.
285#include "native/arm/bf16.h"
286#pragma clang attribute push(__attribute__((target("neon,bf16"))), apply_to=function)
287namespace native::detail::neon_bf16_backend {
288 hint_inline float32x4_t dot2_native(bfloat16x8_t a, bfloat16x8_t b, float32x4_t accumulator) noexcept {
289 // Share the instruction wrapper's FPCR-sensitive evaluation contract.
290 return native::detail::arm_bf16::bfdot<native::isa<>(native::arm_feature::neon_bf16)>(accumulator, a, b);
291 }
292}
293#pragma clang attribute pop
294// Intrinsic calls are owned by the global module fragment.
295#pragma clang attribute push(__attribute__((target("neon,fullfp16"))), apply_to=function)
296namespace native::detail::neon_fp16_backend {
297 hint_inline float16x8_t add_half(float16x8_t a, float16x8_t b) noexcept { return vaddq_f16(a,b); }
298 hint_inline float16x8_t sub_half(float16x8_t a, float16x8_t b) noexcept { return vsubq_f16(a,b); }
299 hint_inline float16x8_t mul_half(float16x8_t a, float16x8_t b) noexcept { return vmulq_f16(a,b); }
300 hint_inline float16x8_t div_half(float16x8_t a, float16x8_t b) noexcept { return vdivq_f16(a,b); }
301 hint_inline float16x8_t sqrt_half(float16x8_t a) noexcept { return vsqrtq_f16(a); }
302 hint_inline float16x8_t neg_half(float16x8_t a) noexcept { return vnegq_f16(a); }
303 hint_inline float16x8_t fma_half(float16x8_t a, float16x8_t b, float16x8_t c) noexcept { return vfmaq_f16(c,a,b); }
304 // Keep comparisons vectorized under strict FP flags and retain native status effects.
305 hint_inline uint8x16_t eq_half(float16x8_t a, float16x8_t b) noexcept {
306 a=arm_register_order(a);
307 b=arm_register_order(b);
308 uint16x8_t bits;
309 asm volatile("fcmeq %0.8h, %1.8h, %2.8h" : "=w"(bits) : "w"(a), "w"(b) : "memory");
310 return vreinterpretq_u8_u16(arm_register_order(bits));
311 }
312 hint_inline uint8x16_t lt_half(float16x8_t a, float16x8_t b) noexcept {
313 a=arm_register_order(a);
314 b=arm_register_order(b);
315 uint16x8_t bits;
316 asm volatile("fcmgt %0.8h, %1.8h, %2.8h" : "=w"(bits) : "w"(b), "w"(a) : "memory");
317 return vreinterpretq_u8_u16(arm_register_order(bits));
318 }
319 hint_inline uint8x16_t le_half(float16x8_t a, float16x8_t b) noexcept {
320 a=arm_register_order(a);
321 b=arm_register_order(b);
322 uint16x8_t bits;
323 asm volatile("fcmge %0.8h, %1.8h, %2.8h" : "=w"(bits) : "w"(b), "w"(a) : "memory");
324 return vreinterpretq_u8_u16(arm_register_order(bits));
325 }
326 hint_inline float16x8_t select_half(uint8x16_t m, float16x8_t a, float16x8_t b) noexcept {
327 return vbslq_f16(vreinterpretq_u16_u8(m),a,b);
328 }
329}
330#pragma clang attribute pop
331#endif
332
333// Emulated shapes are parsed after all native register specializations.
334#include "native/simd/polyfill.h"
335#define NATIVE_BACKEND_BODY "native/simd/polyfill_body.h"
336#include "native/simd/for_each_backend.h"
337#undef NATIVE_BACKEND_BODY
338
339#include "native/simd/polyfill_storage.h"
340
341#define NATIVE_BACKEND_BODY "native/simd/polyfill_helpers_body.h"
342#include "native/simd/for_each_backend.h"
343#undef NATIVE_BACKEND_BODY
344
345#if NATIVE_HOST_WASM
346#define NATIVE_BACKEND_BODY "native/simd/polyfill_wasm_body.h"
347#include "native/simd/for_each_backend.h"
348#undef NATIVE_BACKEND_BODY
349#endif
350
351// BF16 logical dot products retain family-specific instruction semantics.
352#include "native/simd/polyfill_dot2.h"
Declares SIMD element types and pointer access policies.
typename mask_traits< std::remove_cvref_t< T > >::type mask
Definition mask_traits.h:22