native 0.0.1
Vectors, masks and wide register packs for C++26
Loading...
Searching...
No Matches
polyfill_scalar.h
1// SPDX-FileCopyrightText: 2026 Edward Kmett <ekmett@gmail.com>
2// SPDX-License-Identifier: BSD-2-Clause OR Apache-2.0
3#pragma once
4namespace native::detail {
5 struct polyfill_half_control {
6 constexpr_float::rounding mode=constexpr_float::rounding::nearest_even;
7 constexpr_float::policy policy{};
8 };
9 // These are baseline control-register reads, never permission to execute a
10 // half instruction. Keep them in the GMF with every other platform definition.
11 template<class F> inline polyfill_half_control polyfill_float_environment() noexcept {
12 polyfill_half_control result;
13#if NATIVE_HOST_NEON
14 auto control=arm_float_control::current();
15 result.mode=control.rounding_mode(); result.policy=control.template policy<F>();
16#elif NATIVE_HOST_X86
17 std::uint32_t control;
18 __asm__ volatile("stmxcsr %0":"=m"(control)::"memory");
19 constexpr constexpr_float::rounding modes[]{constexpr_float::rounding::nearest_even,
20 constexpr_float::rounding::downward,constexpr_float::rounding::upward,constexpr_float::rounding::toward_zero};
21 result.mode=modes[(control>>13)&3];
22 if constexpr(std::same_as<F,constexpr_float::binary16>) result.policy=half_constant::arithmetic_policy<false>();
23 else result.policy.default_nan_negative=true;
24 // Native x86 half arithmetic ignores MXCSR DAZ/FTZ.
25 if constexpr(!std::same_as<F,constexpr_float::binary16>) {
26 result.policy.flush_inputs=(control&(1u<<6))!=0;
27 result.policy.flush_outputs_after_rounding=(control&(1u<<15))!=0;
28 }
29#endif
30 return result;
31 }
32
33 inline polyfill_half_control polyfill_half_environment() noexcept {
34 return polyfill_float_environment<constexpr_float::binary16>();
35 }
36
40 template<class T,isa<> A> struct polyfill_scalar {
41 using value_type=T;
42 using format=typename polyfill_element_traits<T>::format;
43 using word=typename format::bits_type;
44 // Scalar half storage crosses aggregate ABI boundaries as integer bits.
45 // A typed __bf16 field can be widened and narrowed by the compiler even
46 // for a representation-only copy, changing NaNs, zeros or FP status.
47 using native_type=std::conditional_t<sizeof(T)==2,word,T>;
48 static constexpr auto constant_policy=[] {
49 if constexpr(polyfill_element_traits<T>::kind==polyfill_element_kind::binary16)
50 return half_constant::arithmetic_policy<!NATIVE_HOST_X86>();
51 else return constexpr_float::policy{};
52 }();
53 using mask=polyfill_predicate<1,A>;
54 native_type value{};
55 constexpr polyfill_scalar() noexcept=default;
56 constexpr polyfill_scalar(T x) noexcept : value(std::bit_cast<native_type>(x)) {}
57 constexpr polyfill_scalar(native_type x) noexcept requires(!std::same_as<T,native_type>) : value(x) {}
58 static constexpr word bits(native_type x) noexcept { return std::bit_cast<word>(x); }
59 static constexpr native_type element(word x) noexcept { return std::bit_cast<native_type>(x); }
60 static constexpr polyfill_scalar from_native(native_type x) noexcept { return x; }
61 constexpr native_type to_native() const noexcept { return value; }
62 template<std::size_t Alignment=1>
63 static constexpr polyfill_scalar load_memory(T const * p) noexcept {
64 if consteval { return *p; } else { native_type x; std::memcpy(&x,reinterpret_cast<unsigned char const *>(p),sizeof(T)); return from_native(x); }
65 }
66 static constexpr polyfill_scalar load(T const * p) noexcept { return load_memory(p); }
67 template<std::size_t Alignment=1>
68 constexpr void store_memory(T * p) const noexcept {
69 if consteval { *p=std::bit_cast<T>(value); } else { std::memcpy(reinterpret_cast<unsigned char *>(p),&value,sizeof(T)); }
70 }
71 constexpr void store(T * p) const noexcept { store_memory(p); }
72#define NATIVE_POLYFILL_SCALAR_BINARY(OP,FN) \
73 \
74 friend constexpr polyfill_scalar operator OP(polyfill_scalar a,polyfill_scalar b) noexcept \
75 requires(polyfill_element_traits<T>::arithmetic) { \
76 if consteval { return element(constexpr_float::FN<format>(bits(a.value),bits(b.value),constexpr_float::rounding::nearest_even,constant_policy)); } \
77 else { \
78 if constexpr(sizeof(T)==2) { \
79 auto control=polyfill_half_environment(); \
80 return element(constexpr_float::FN<format>(bits(a.value),bits(b.value),control.mode,control.policy)); \
81 } \
82 else return T(a.value OP b.value); \
83 } \
84 }
85 NATIVE_POLYFILL_SCALAR_BINARY(+,add_bits)
86 NATIVE_POLYFILL_SCALAR_BINARY(-,sub_bits)
87 NATIVE_POLYFILL_SCALAR_BINARY(*,mul_bits)
88 NATIVE_POLYFILL_SCALAR_BINARY(/,div_bits)
89#undef NATIVE_POLYFILL_SCALAR_BINARY
91 friend constexpr polyfill_scalar operator-(polyfill_scalar a) noexcept
92 requires(polyfill_element_traits<T>::arithmetic) {
93#if NATIVE_HOST_NEON
94 if !consteval { return element(arm_float_control::current().template negate<format>(bits(a.value))); }
95#endif
96 return element(bits(a.value)^format::sign_mask);
97 }
99 friend constexpr mask operator<(polyfill_scalar a,polyfill_scalar b) noexcept
100 requires(polyfill_element_traits<T>::arithmetic) {
101 if consteval { return mask::from_bitset(constexpr_float::less_bits<format>(bits(a.value),bits(b.value))); }
102 else {
103 if constexpr(sizeof(T)==2) {
104 auto p=polyfill_half_environment().policy;
105 return mask::from_bitset(constexpr_float::less_bits<format>(constexpr_float::flush_input<format>(bits(a.value),p),
106 constexpr_float::flush_input<format>(bits(b.value),p)));
107 } else return mask::from_bitset(a.value<b.value);
108 }
109 }
111 friend constexpr mask operator==(polyfill_scalar a,polyfill_scalar b) noexcept
112 requires(polyfill_element_traits<T>::arithmetic) {
113 if consteval { return mask::from_bitset(constexpr_float::equal_bits<format>(bits(a.value),bits(b.value))); }
114 else {
115 if constexpr(sizeof(T)==2) {
116 auto p=polyfill_half_environment().policy;
117 return mask::from_bitset(constexpr_float::equal_bits<format>(constexpr_float::flush_input<format>(bits(a.value),p),
118 constexpr_float::flush_input<format>(bits(b.value),p)));
119 } else return mask::from_bitset(a.value==b.value);
120 }
121 }
123 friend constexpr polyfill_scalar fma(polyfill_scalar a,polyfill_scalar b,polyfill_scalar c) noexcept
124 requires(polyfill_element_traits<T>::arithmetic) {
125 if consteval { return element(constexpr_float::fma_bits<format>(bits(a.value),bits(b.value),bits(c.value),constexpr_float::rounding::nearest_even,constant_policy)); }
126 else {
127 if constexpr(sizeof(T)==2) {
128 auto control=polyfill_half_environment();
129 return element(constexpr_float::fma_bits<format>(bits(a.value),bits(b.value),bits(c.value),control.mode,control.policy));
130 }
131 else return std::fma(a.value,b.value,c.value);
132 }
133 }
135 friend constexpr polyfill_scalar sqrt(polyfill_scalar a) noexcept
136 requires(polyfill_element_traits<T>::arithmetic) {
137 if consteval { return element(constexpr_float::sqrt_bits<format>(bits(a.value),constexpr_float::rounding::nearest_even,constant_policy)); }
138 else {
139 if constexpr(sizeof(T)==2) {
140 auto control=polyfill_half_environment();
141 return element(constexpr_float::sqrt_bits<format>(bits(a.value),control.mode,control.policy));
142 }
143 else return std::sqrt(a.value);
144 }
145 }
147 friend constexpr polyfill_scalar abs(polyfill_scalar a) noexcept { return element(bits(a.value)&~format::sign_mask); }
148#define NATIVE_POLYFILL_SCALAR_ROUND(NAME,MODE) \
149 \
150 friend constexpr polyfill_scalar NAME(polyfill_scalar a) noexcept \
151 requires(polyfill_element_traits<T>::arithmetic) { \
152 if consteval { return element(constexpr_float::round_integral_bits<format>(bits(a.value),constexpr_float::rounding::MODE,constant_policy)); } \
153 else { return element(constexpr_float::round_integral_bits<format>(bits(a.value),constexpr_float::rounding::MODE,polyfill_float_environment<format>().policy)); } \
154 }
155 NATIVE_POLYFILL_SCALAR_ROUND(floor,downward)
156 NATIVE_POLYFILL_SCALAR_ROUND(ceil,upward)
157 NATIVE_POLYFILL_SCALAR_ROUND(trunc,toward_zero)
158 NATIVE_POLYFILL_SCALAR_ROUND(round_even,nearest_even)
159#undef NATIVE_POLYFILL_SCALAR_ROUND
160 };
161 template<class T,std::size_t N,isa<> A,bool Native=requires { typename simd<T,N,A>::native_type; }>
162 struct polyfill_chunk { using type=polyfill_scalar<T,A>; };
163 template<class T,std::size_t N,isa<> A> struct polyfill_chunk<T,N,A,true> { using type=simd<T,N,A>; };
164 template<class T,std::size_t N,isa<> A> requires requires { sizeof(polyfill_feature_chunk<T,N,A>); }
165 struct polyfill_chunk<T,N,A,true> { using type=polyfill_feature_chunk<T,N,A>; };
166}
constexpr simd< float, N, Arch > ceil(simd< float, N, Arch > x) noexcept
constexpr simd< float, N, Arch > trunc(simd< float, N, Arch > x) noexcept
constexpr simd< float, N, Arch > floor(simd< float, N, Arch > x) noexcept
constexpr simd< T, N, A > round_even(simd< T, N, A > v) noexcept
Round floating lanes to nearest integers, choosing even at ties.