native 0.0.1
Vectors, masks and wide register packs for C++26
Loading...
Searching...
No Matches
wide_math.h
1// SPDX-FileCopyrightText: 2026 Edward Kmett <ekmett@gmail.com>
2// SPDX-License-Identifier: (BSD-2-Clause OR Apache-2.0) AND BSL-1.0
3#pragma once
4#include "native/simd.h"
5#include "native/wide_pack.h"
6#include <tuple>
7
8namespace native {
9 namespace detail {
10 // Metadata only: recommendations can be queried for another ISA family
11 // without instantiating a foreign SIMD type or enabling its instructions.
12 template<class T, std::size_t K, isa A>
13 inline constexpr bool math_vector_shape = [] {
14 if constexpr (!std::same_as<T, float> || K <= 1 || !A.valid()) return false;
15 else if constexpr (A.family == x86)
16 return A.has(avx2) && ((K >= 2 && K <= 4) || K == 8 ||
17 (K == 16 && A.has(kernel_base)));
18 else if constexpr (A.family == arm)
19 return A.has(neon) && K >= 2 && K <= 4;
20 else if constexpr (A.family == wasm)
21 return A.has(wasm_feature::simd128) && K == 4;
22 else return false;
23 }();
24
25 // A conservative starting point for kernels without a measured width sweep.
26 template<class T, std::size_t K, isa A>
27 inline constexpr std::size_t math_polynomial_width =
28 !math_vector_shape<T, K, A> ? 1 : K >= 4 && A.family != wasm ? 4 : 2;
29
30 template<class T, std::size_t K, isa A>
31 inline constexpr std::size_t math_branching_width = math_vector_shape<T, K, A> ? 2 : 1;
32 }
33
37 template<class T, std::size_t K, isa A = NATIVE_BASELINE> requires (K > 0)
38 inline constexpr std::size_t exp_width = [] {
39 if constexpr (!detail::math_vector_shape<T, K, A>) return std::size_t{1};
40 else if constexpr (A.family == wasm) return std::size_t{2};
41 else if constexpr (A.family == x86 && K >= 8) return std::size_t{6};
42 else return std::size_t{4};
43 }();
44
46 template<class T, std::size_t K, isa A = NATIVE_BASELINE> requires (K > 0)
47 inline constexpr std::size_t exp2_width = detail::math_branching_width<T, K, A>;
48
50 template<class T, std::size_t K, isa A = NATIVE_BASELINE> requires (K > 0)
51 inline constexpr std::size_t expm1_width = detail::math_polynomial_width<T, K, A>;
52
54 template<class T, std::size_t K, isa A = NATIVE_BASELINE> requires (K > 0)
55 inline constexpr std::size_t damping_gain_width = expm1_width<T, K, A>;
56
58 template<class T, std::size_t K, isa A = NATIVE_BASELINE> requires (K > 0)
59 inline constexpr std::size_t log_width = detail::math_polynomial_width<T, K, A>;
60
62 template<class T, std::size_t K, isa A = NATIVE_BASELINE> requires (K > 0)
63 inline constexpr std::size_t log2_width = log_width<T, K, A>;
64
66 template<class T, std::size_t K, isa A = NATIVE_BASELINE> requires (K > 0)
67 inline constexpr std::size_t log1p_width = detail::math_polynomial_width<T, K, A>;
68
70 template<class T, std::size_t K, isa A = NATIVE_BASELINE> requires (K > 0)
71 inline constexpr std::size_t tanh_width = detail::math_branching_width<T, K, A>;
72
75 template<class T, std::size_t K, isa A = NATIVE_BASELINE> requires (K > 0)
76 inline constexpr std::size_t atan2_width = detail::math_branching_width<T, K, A>;
77
79 template<class T, std::size_t K, isa A = NATIVE_BASELINE> requires (K > 0)
80 inline constexpr std::size_t sincos_width =
81 detail::math_vector_shape<T, K, A> && A.family == arm && K == 4
82 ? 4 : detail::math_branching_width<T, K, A>;
83
85 template<class T, std::size_t K, isa A = NATIVE_BASELINE> requires (K > 0)
86 inline constexpr std::size_t sin_width = sincos_width<T, K, A>;
87
89 template<class T, std::size_t K, isa A = NATIVE_BASELINE> requires (K > 0)
90 inline constexpr std::size_t cos_width = sincos_width<T, K, A>;
91}
92
93namespace math {
94 using ::native::exp_width;
95 using ::native::exp2_width;
96 using ::native::expm1_width;
98 using ::native::log_width;
99 using ::native::log2_width;
100 using ::native::log1p_width;
101 using ::native::tanh_width;
102 using ::native::atan2_width;
103 using ::native::sincos_width;
104 using ::native::sin_width;
105 using ::native::cos_width;
106}
107
108namespace wide::detail {
109 template<class V> struct native_ops;
110}
111#define NATIVE_BACKEND_BODY "native/wide_native_body.h"
112#include "native/simd/for_each_backend.h"
113#undef NATIVE_BACKEND_BODY
114
115namespace wide::detail {
116 template<class P> inline constexpr bool native_pack = false;
117 template<class V, std::size_t N>
118 inline constexpr bool native_pack<std::array<V, N>> = requires { sizeof(native_ops<V>); };
119 template<class P> concept native_pack_argument = native_pack<std::remove_cvref_t<P>>;
120 template<class V> concept native_leaf_argument = (!pack<V>) &&
121 requires { sizeof(native_ops<std::remove_cvref_t<V>>); };
122
123 // Every batch is homogeneous. Select each array's element directly and
124 // broadcast native operands by reference, without tuple argument binding.
125 template<class A> struct lift_argument { using type=std::remove_cvref_t<A>; };
126 template<class V,std::size_t N> struct lift_argument<std::array<V,N>> { using type=V; };
127 template<class A> using lift_argument_t=typename lift_argument<std::remove_cvref_t<A>>::type;
128 template<class A,class... Rest> consteval std::size_t lift_size() {
129 if constexpr (pack<A>) return shape_t<A>::size;
130 else return lift_size<Rest...>();
131 }
132 template<std::size_t N,class A> consteval bool lift_size_matches() {
133 if constexpr (pack<A>) return shape_t<A>::size==N;
134 else return true;
135 }
136 template<class F,class... A> consteval bool lift_result_valid() {
137 if constexpr (!std::is_invocable_v<F const &,lift_argument_t<A> const &...>) return false;
138 else {
139 using R=std::invoke_result_t<F const &,lift_argument_t<A> const &...>;
140 return std::is_object_v<std::remove_cvref_t<R>> &&
141 std::is_constructible_v<std::remove_cvref_t<R>,R>;
142 }
143 }
144 template<class F,class... A> concept liftable = (pack<A> || ...) &&
145 ((native_pack_argument<A> || native_leaf_argument<A>) && ...) &&
146 (lift_size_matches<lift_size<A...>(),A>() && ...) && lift_result_valid<F,A...>();
147 template<std::size_t I,class A>
148 hint_inline constexpr decltype(auto) lift_operand(A const & value) {
149 if constexpr (pack<A>) return std::get<I>(value);
150 else return (value);
151 }
152 template<std::size_t I,class F,class... A>
153 hint_inline constexpr auto lift_element(F const & function,A const &... arguments) {
154 return function(lift_operand<I>(arguments)...);
155 }
156 template<class F,class... A,std::size_t... I>
157 hint_inline constexpr auto lift_array(F const & function,std::index_sequence<I...>,A const &... arguments) {
158 using R=std::remove_cvref_t<std::invoke_result_t<F const &,lift_argument_t<A> const &...>>;
159 return std::array<R,sizeof...(I)>{{lift_element<I>(function,arguments...)...}};
160 }
161 template<class F,class... A> requires liftable<F,A...>
162 hint_inline constexpr auto lift(F function,A const &... arguments) {
163 return lift_array(function,std::make_index_sequence<lift_size<A...>()>{},arguments...);
164 }
165
166 // Pattern: [declaration macros](../README.md#declaration-macros).
167#define NATIVE_WIDE_BINARY_OPERATION(name, bridge, op) \
168 struct name { \
169 template<class V> \
170 requires requires(V a) { a op a; } \
171 hint_inline constexpr auto operator()(V const & a, V const & b) const { \
172 return native_ops<V>::bridge(a, b); \
173 } \
174 };
175
176 NATIVE_WIDE_BINARY_OPERATION(add, add, +)
177 NATIVE_WIDE_BINARY_OPERATION(subtract, sub, -)
178 NATIVE_WIDE_BINARY_OPERATION(multiply, mul, *)
179 NATIVE_WIDE_BINARY_OPERATION(divide, div, /)
180 NATIVE_WIDE_BINARY_OPERATION(bit_and, bit_and, &)
181 NATIVE_WIDE_BINARY_OPERATION(bit_or, bit_or, |)
182 NATIVE_WIDE_BINARY_OPERATION(bit_xor, bit_xor, ^)
183 NATIVE_WIDE_BINARY_OPERATION(equal, equal, ==)
184 NATIVE_WIDE_BINARY_OPERATION(unequal, unequal, !=)
185 NATIVE_WIDE_BINARY_OPERATION(less, less, <)
186 NATIVE_WIDE_BINARY_OPERATION(less_equal, less_equal, <=)
187 NATIVE_WIDE_BINARY_OPERATION(greater, greater, >)
188 NATIVE_WIDE_BINARY_OPERATION(greater_equal, greater_equal, >=)
189#undef NATIVE_WIDE_BINARY_OPERATION
190
191#define NATIVE_WIDE_UNARY_OPERATION(name, bridge, op) \
192 struct name { \
193 template<class V> \
194 requires requires(V a) { { op a } -> std::same_as<V>; } \
195 hint_inline constexpr auto operator()(V const & a) const { \
196 return native_ops<V>::bridge(a); \
197 } \
198 };
199
200 NATIVE_WIDE_UNARY_OPERATION(negate, negate, -)
201 NATIVE_WIDE_UNARY_OPERATION(bit_not, bit_not, ~)
202 NATIVE_WIDE_UNARY_OPERATION(logical_not, logical_not, !)
203#undef NATIVE_WIDE_UNARY_OPERATION
204
205#define NATIVE_WIDE_UNARY_MATH(name, bridge, operation) \
206 struct name { \
207 template<class V> \
208 requires requires(V a) { { operation(a) } -> std::same_as<V>; } \
209 hint_inline constexpr auto operator()(V const & a) const { \
210 return native_ops<V>::bridge(a); \
211 } \
212 };
213
214 NATIVE_WIDE_UNARY_MATH(absolute, absolute, abs)
215 NATIVE_WIDE_UNARY_MATH(root, root, sqrt)
216 NATIVE_WIDE_UNARY_MATH(downward, downward, floor)
217 NATIVE_WIDE_UNARY_MATH(upward, upward, ceil)
218 NATIVE_WIDE_UNARY_MATH(truncate, truncate, trunc)
219 NATIVE_WIDE_UNARY_MATH(round, round, round_even)
220#undef NATIVE_WIDE_UNARY_MATH
221 struct minimum {
222 template<class V> requires requires(V a) { select(a<a,a,a); }
223 hint_inline constexpr auto operator()(V const & a,V const & b) const { return native_ops<V>::minimum(a,b); }
224 };
225 struct maximum {
226 template<class V> requires requires(V a) { select(a>a,a,a); }
227 hint_inline constexpr auto operator()(V const & a,V const & b) const { return native_ops<V>::maximum(a,b); }
228 };
229 struct fused {
230 template<class V> requires requires(V a) { fma(a,a,a); }
231 hint_inline constexpr auto operator()(V const & a,V const & b,V const & c) const { return native_ops<V>::fused(a,b,c); }
232 };
233 struct polynomial_madd {
234 template<class V>
235 hint_inline constexpr auto operator()(V const & a,V const & b,V const & c) const {
236 return native_ops<V>::polynomial_madd(a,b,c);
237 }
238 };
239 struct exp_power_operation {
240 template<class V>
241 hint_inline constexpr auto operator()(V const & n) const { return native_ops<V>::exp_power(n); }
242 };
243 template<class P> requires liftable<exp_power_operation,P>
244 hint_inline constexpr auto exp_power(P const & n) noexcept { return lift(exp_power_operation{},n); }
245 struct exp_scale {
246 template<class M,class V>
247 hint_inline constexpr auto operator()(M const & m,V const & replacement,V const & a,V const & n) const {
248 return native_ops<V>::exp_scale(m,replacement,a,n);
249 }
250 };
251 template<class P,class Q,class R> requires liftable<polynomial_madd,P,Q,R>
252 hint_inline constexpr auto madd(P const & a,Q const & b,R const & c) noexcept {
253 return lift(polynomial_madd{},a,b,c);
254 }
255 template<class M,class P,class Q> requires liftable<exp_scale,M,P,P,Q>
256 hint_inline constexpr auto scale_exp(M const & in_range,P const & replacement,P const & y,Q const & n) noexcept {
257 return lift(exp_scale{},in_range,replacement,y,n);
258 }
259 struct scale {
260 template<class M,class V> requires requires(M m,V a) { masked_scaleb_zero(m,a,a); }
261 hint_inline constexpr auto operator()(M const & m,V const & a,V const & n) const { return native_ops<V>::scale(m,a,n); }
262 };
263 struct scale_all {
264 template<class V> requires requires(V a) { scaleb(a,a); }
265 hint_inline constexpr auto operator()(V const & a,V const & n) const { return native_ops<V>::scale_all(a,n); }
266 };
267 struct choose {
268 template<class M,class V> requires requires(M m,V a) { select(m,a,a); }
269 hint_inline constexpr auto operator()(M const & m,V const & a,V const & b) const { return native_ops<V>::choose(m,a,b); }
270 };
271 struct scale_merge {
272 template<class M,class V> requires requires(M m,V a) { masked_scaleb(m,a,a,a); }
273 hint_inline constexpr auto operator()(M const & m,V const & prior,V const & a,V const & n) const {
274 return native_ops<V>::scale_merge(m,prior,a,n);
275 }
276 };
277 struct encode {
278 template<class V> requires std::same_as<typename V::value_type,float> && requires(V a) { a.bits(); }
279 hint_inline constexpr auto operator()(V const & a) const { return native_ops<V>::encode(a); }
280 };
281 struct decode {
282 template<class V> requires std::same_as<typename V::value_type,std::uint32_t> &&
283 requires(V a) { V::template rebind<float>::from_bits(a); }
284 hint_inline constexpr auto operator()(V const & a) const { return native_ops<V>::decode(a); }
285 };
286 template<unsigned Shift> struct shift_left {
287 template<class V> requires requires(V a) { a.template left<Shift>(); }
288 hint_inline constexpr auto operator()(V const & a) const { return native_ops<V>::template left<Shift>(a); }
289 };
290 template<unsigned Shift> struct shift_right {
291 template<class V> requires requires(V a) { a.template right<Shift>(); }
292 hint_inline constexpr auto operator()(V const & a) const { return native_ops<V>::template right<Shift>(a); }
293 };
294 template<class T> struct mask_words {
295 template<class V> requires requires(V a) { ::native::mask_bits<T>(a); }
296 hint_inline constexpr auto operator()(V const & a) const { return native_ops<V>::template mask_words<T>(a); }
297 };
298 struct trig_integer_operation {
299 template<class V> requires std::same_as<typename V::value_type,float>
300 hint_inline constexpr auto operator()(V const & a) const { return native_ops<V>::trig_integer(a); }
301 };
302 struct tanh_coefficient {
303 std::array<std::uint32_t,8> table;
304 template<class V> requires std::same_as<typename V::value_type,std::uint32_t>
305 hint_inline constexpr auto operator()(V const & index) const noexcept {
306 return native_ops<V>::tanh_coefficient(index,table);
307 }
308 };
309 struct signed_float_operation {
310 template<class V> requires std::same_as<typename V::value_type,std::uint32_t>
311 hint_inline constexpr auto operator()(V const & a) const { return native_ops<V>::signed_float(a); }
312 };
313 template<class P> requires liftable<trig_integer_operation,P>
314 hint_inline constexpr auto trig_integer(P const & a) noexcept { return lift(trig_integer_operation{},a); }
315 template<class P> requires liftable<signed_float_operation,P>
316 hint_inline constexpr auto signed_float(P const & a) noexcept { return lift(signed_float_operation{},a); }
317
318}
319
320namespace wide {
322 template<class V,std::size_t N,class T>
323 requires detail::native_leaf_argument<V> &&
324 (std::same_as<T,float> || std::same_as<T,std::uint32_t>) && std::same_as<typename V::value_type,T>
325 [[nodiscard]] hint_inline constexpr V constant_like(std::array<V,N> const &, T value) noexcept {
326 return detail::native_ops<V>::constant(value);
327 }
328#define NATIVE_WIDE_BINARY_API(name, operation) \
329 template<class P, class Q> \
330 requires detail::liftable<detail::operation, P, Q> \
331 [[nodiscard]] hint_inline constexpr auto name(P const & a, Q const & b) noexcept { \
332 return detail::lift(detail::operation{}, a, b); \
333 }
334
335 NATIVE_WIDE_BINARY_API(add, add)
336 NATIVE_WIDE_BINARY_API(sub, subtract)
337 NATIVE_WIDE_BINARY_API(mul, multiply)
338 NATIVE_WIDE_BINARY_API(div, divide)
339 NATIVE_WIDE_BINARY_API(bit_and, bit_and)
340 NATIVE_WIDE_BINARY_API(bit_or, bit_or)
341 NATIVE_WIDE_BINARY_API(bit_xor, bit_xor)
342 NATIVE_WIDE_BINARY_API(cmp_eq, equal)
343 NATIVE_WIDE_BINARY_API(cmp_ne, unequal)
344 NATIVE_WIDE_BINARY_API(cmp_lt, less)
345 NATIVE_WIDE_BINARY_API(cmp_le, less_equal)
346 NATIVE_WIDE_BINARY_API(cmp_gt, greater)
347 NATIVE_WIDE_BINARY_API(cmp_ge, greater_equal)
348 NATIVE_WIDE_BINARY_API(min, minimum)
349 NATIVE_WIDE_BINARY_API(max, maximum)
350 NATIVE_WIDE_BINARY_API(scaleb, scale_all)
351#undef NATIVE_WIDE_BINARY_API
352
353#define NATIVE_WIDE_UNARY_API(name, operation) \
354 template<class P> \
355 requires detail::liftable<detail::operation, P> \
356 [[nodiscard]] hint_inline constexpr auto name(P const & a) noexcept { \
357 return detail::lift(detail::operation{}, a); \
358 }
359
360 NATIVE_WIDE_UNARY_API(negate, negate)
361 NATIVE_WIDE_UNARY_API(bit_not, bit_not)
362 NATIVE_WIDE_UNARY_API(mask_not, logical_not)
363 NATIVE_WIDE_UNARY_API(abs, absolute)
364 NATIVE_WIDE_UNARY_API(sqrt, root)
365 NATIVE_WIDE_UNARY_API(floor, downward)
366 NATIVE_WIDE_UNARY_API(ceil, upward)
367 NATIVE_WIDE_UNARY_API(trunc, truncate)
368 NATIVE_WIDE_UNARY_API(round_even, round)
369 NATIVE_WIDE_UNARY_API(bits, encode)
370 NATIVE_WIDE_UNARY_API(from_bits, decode)
371#undef NATIVE_WIDE_UNARY_API
374 template<class P,class Q,class R> requires detail::liftable<detail::fused,P,Q,R>
375 [[nodiscard]] hint_inline constexpr auto fma(P const & a, Q const & b, R const & c) noexcept {
376 return detail::lift(detail::fused{},a,b,c);
377 }
380 template<class P,class Q,class R> requires detail::liftable<detail::scale,P,Q,R>
381 [[nodiscard]] hint_inline constexpr auto masked_scaleb_zero(P const & m, Q const & a, R const & n) noexcept {
382 return detail::lift(detail::scale{},m,a,n);
383 }
386 template<class M,class P,class Q> requires detail::liftable<detail::choose,M,P,Q>
387 [[nodiscard]] hint_inline constexpr auto select(M const & m,P const & a,Q const & b) noexcept {
388 return detail::lift(detail::choose{},m,a,b);
389 }
392 template<class M,class P,class Q,class R> requires detail::liftable<detail::scale_merge,M,P,Q,R>
393 [[nodiscard]] hint_inline constexpr auto masked_scaleb(M const & m,P const & prior,Q const & a,R const & n) noexcept {
394 return detail::lift(detail::scale_merge{},m,prior,a,n);
395 }
398 template<unsigned Shift,class P> requires detail::liftable<detail::shift_left<Shift>,P>
399 [[nodiscard]] hint_inline constexpr auto left(P const & a) noexcept {
400 return detail::lift(detail::shift_left<Shift>{},a);
401 }
403 template<unsigned Shift,class P> requires detail::liftable<detail::shift_right<Shift>,P>
404 [[nodiscard]] hint_inline constexpr auto right(P const & a) noexcept {
405 return detail::lift(detail::shift_right<Shift>{},a);
406 }
409 template<class T,class P> requires detail::liftable<detail::mask_words<T>,P>
410 [[nodiscard]] hint_inline constexpr auto mask_bits(P const & a) noexcept {
411 return detail::lift(detail::mask_words<T>{},a);
412 }
413
414 namespace detail {
415 template<class T> inline constexpr bool binary32_register = false;
416 template<std::size_t N, ::native::isa<> A>
417 inline constexpr bool binary32_register<::native::simd<float, N, A>> =
418 requires(::native::simd<float,N,A> a) { { a+a } -> std::same_as<::native::simd<float,N,A>>; };
419 template<class P> inline constexpr bool binary32_array = false;
420 template<class V, std::size_t N>
421 inline constexpr bool binary32_array<std::array<V, N>> = binary32_register<V>;
422 template<class P> inline constexpr bool binary32_pack = binary32_array<P>;
423 }
424}
425
426namespace math {
427 namespace detail {
428 template<class T> using horner_register_t = typename ::wide::canonical_t<T>::value_type;
429 template<class T, class C> concept horner_coefficient = std::same_as<C, float> ||
430 std::same_as<C, horner_register_t<T>> ||
431 (::wide::detail::is_array<T> && ::wide::detail::is_array<C> &&
432 ::wide::detail::shape_t<T>::size == ::wide::detail::shape_t<C>::size &&
433 (std::same_as<typename ::wide::detail::shape_t<C>::template element_type<0>, float> ||
434 std::same_as<typename ::wide::detail::shape_t<C>::template element_type<0>, horner_register_t<T>>));
435
436 template<class P, class C0, class C1, class... C>
437 hint_inline constexpr auto horner_kernel(P const & z, C0 const & first,
438 C1 const & second, C const &... rest) noexcept {
439 auto h = ::wide::detail::madd(first, z, second);
440 ((h = ::wide::detail::madd(h, z, rest)), ...);
441 return h;
442 }
443
444 // Inline broadcasts with their consuming stages so scalar coefficient packs
445 // do not survive as temporary arrays in the generated code.
446 template<class P> struct horner_coefficient_conversion {
447 P const & input;
448
449 template<class C>
450 hint_inline constexpr auto operator()(C const & value) const noexcept {
451 using shape = ::wide::detail::shape_t<C>;
452 if constexpr (shape::kind == ::wide::detail::family::legacy)
453 return ::wide::map(*this, value.registers);
454 else if constexpr (shape::kind == ::wide::detail::family::std_array)
455 return ::wide::map(*this, value);
456 else if constexpr (std::same_as<C, float>)
457 return ::wide::constant_like(input, value);
458 else return value;
459 }
460 };
461
462 template<class T, class C0, class... C>
463 hint_inline constexpr auto evaluate_horner(T const & z,
464 C0 const & first, C const &... rest) noexcept {
465 auto const input = ::wide::promote(z);
466 auto const coefficient = horner_coefficient_conversion<decltype(input)>{input};
467 if constexpr (sizeof...(C) == 0) {
468 auto const value = coefficient(first);
469 if constexpr (::wide::pack<decltype(value)>) return ::wide::demote<T>(value);
470 else return ::wide::demote<T>(::wide::map([&](auto const &) { return value; }, input));
471 } else {
472 return ::wide::demote<T>(horner_kernel(input,
473 coefficient(first), coefficient(rest)...));
474 }
475 }
476
477 template<class... C> struct horner_polynomial {
478 std::tuple<C...> coefficients;
479
480 template<::wide::promotable T>
481 requires (::wide::detail::binary32_array<::wide::canonical_t<T>>) &&
482 (horner_coefficient<T, C> && ...)
483 [[nodiscard]] hint_inline constexpr auto operator()(T const & z) const noexcept {
484 auto const & [...values] = coefficients;
485 return evaluate_horner(z, values...);
486 }
487 };
488 }
489
498 template<::wide::promotable C0, ::wide::promotable... C>
499 requires (::wide::detail::binary32_array<::wide::canonical_t<C0>>) &&
500 ((::wide::detail::binary32_array<::wide::canonical_t<C>>) && ...)
501 [[nodiscard]] hint_inline constexpr auto horner(C0 first, C... rest) noexcept {
502 return detail::horner_polynomial<C0, C...>{{first, rest...}};
503 }
504
505 namespace detail {
506 // Each degree shares the reduction and scaling graph. Sollya binary32 fits
507 // keep the constant exactly one; six is the default accuracy/cost choice.
508 template<unsigned Degree, class V, std::size_t N>
509 requires (Degree >= 1 && Degree <= 7) && (::wide::detail::binary32_register<V>)
510 hint_inline constexpr auto exp_polynomial(std::array<V, N> const & r) noexcept {
511 auto const c = [&](float value) { return ::wide::constant_like(r, value); };
512 if constexpr (Degree == 1)
513 return ::math::horner(c(0x1.ec7054p-1f), c(1.f))(r);
514 else if constexpr (Degree == 2)
515 return ::math::horner(c(0x1.ff3a12p-2f), c(0x1.039e16p+0f), c(1.f))(r);
516 else if constexpr (Degree == 3)
517 return ::math::horner(c(0x1.5249a2p-3f), c(0x1.021d64p-1f),
518 c(0x1.000cd6p+0f), c(1.f))(r);
519 else if constexpr (Degree == 4)
520 return ::math::horner(c(0x1.541326p-5f), c(0x1.57ce98p-3f),
521 c(0x1.0003f6p-1f), c(0x1.fffba8p-1f), c(1.f))(r);
522 else if constexpr (Degree == 5)
523 return ::math::horner(c(0x1.0f9fa4p-7f), c(0x1.573a1cp-5f),
524 c(0x1.555a8p-3f), c(0x1.fffdc6p-2f), c(0x1.fffff6p-1f), c(1.f))(r);
525 else if constexpr (Degree == 6)
526 return ::math::horner(c(0x1.6d55f4p-10f), c(0x1.123e2cp-7f),
527 c(0x1.5554ep-5f), c(0x1.55548ap-3f), c(0.5f), c(1.f), c(1.f))(r);
528 else
529 return ::math::horner(c(0x1.a1d714d7b1510dp-13f), c(0x1.6da756e670ea6p-10f),
530 c(0x1.11105b3161a6fp-7f), c(0x1.5554649b7487fp-5f),
531 c(0x1.555555c673724p-3f), c(0x1.0000005c8dd89p-1f), c(1.f), c(1.f))(r);
532 }
533
534 // The single polynomial body, shared by generic and targeted entry points.
535 template<bool Flush, unsigned Degree = 6, class V, std::size_t N>
536 requires (Degree >= 1 && Degree <= 7) && (::wide::detail::binary32_register<V>)
537 [[nodiscard]] hint_inline constexpr auto exp_reduced(std::array<V, N> const & x) noexcept {
538 auto const c = [&](float value) { return ::wide::constant_like(x, value); };
539 auto const active = ::wide::mask_not(::wide::cmp_lt(x, c(Flush ? -87.33654022216796875f : -104.f)));
540 // Classify independently; range flags are consumed only at the finish.
541 // Unordered comparisons leave NaNs on the arithmetic propagation path.
542 // The last binary32 input whose exact exponential rounds finite. n=128
543 // is valid: bounded reconstruction splits its scale into 2^127 and 2.
544 // Strict comparison keeps NaNs on the arithmetic propagation path.
545 auto const overflow = ::wide::cmp_gt(x, c(88.72283172607421875f));
546 auto const in_range = ::wide::bit_and(active, ::wide::mask_not(overflow));
547 auto const replacement = ::wide::select(overflow,
548 c(std::bit_cast<float>(0x7f800000u)), c(0.f));
549 auto r = x;
550 auto const n = ::wide::round_even(::wide::mul(r, c(1.4426950408889634f)));
551 r = ::wide::detail::madd(n, c(-0x1.62e400p-1f), r);
552 r = ::wide::detail::madd(n, c(-0x1.7f7d1cp-20f), r);
553
554 auto const y = exp_polynomial<Degree>(r);
555 return std::tuple{in_range, replacement, y, n};
556 }
557 }
558
563 template<bool Flush = false, unsigned Degree = 6, ::wide::promotable T>
564 requires (Degree >= 1 && Degree <= 7) && (::wide::detail::binary32_array<::wide::canonical_t<T>>)
565 [[nodiscard]] hint_inline constexpr auto exp(T const & input) noexcept {
566 // MSVC's array<T,0> may construct a dummy T; an empty batch needs no work.
567 if constexpr (::wide::detail::shape_t<::wide::canonical_t<T>>::size == 0) {
568 return std::remove_cvref_t<T>(input);
569 } else {
570 auto const x = ::wide::promote(input);
571 auto const [in_range, replacement, y, n] = detail::exp_reduced<Flush, Degree>(x);
572 return ::wide::demote<T>(::wide::detail::scale_exp(in_range, replacement, y, n));
573 }
574 }
575
579 template<bool Flush = false, ::wide::promotable T>
580 requires (::wide::detail::binary32_array<::wide::canonical_t<T>>)
581 [[nodiscard]] hint_inline constexpr auto exp2(T const & input) noexcept {
582 if constexpr (::wide::detail::shape_t<::wide::canonical_t<T>>::size == 0) {
583 return std::remove_cvref_t<T>(input);
584 } else {
585 namespace w = ::wide;
586 auto const x = w::promote(input);
587 auto const c = [&](float value) { return w::constant_like(x, value); };
588 auto const active = w::mask_not(w::cmp_lt(x, c(Flush ? -126.f : -150.f)));
589 auto const overflow = w::cmp_ge(x, c(127.5f));
590 auto const in_range = w::bit_and(active, w::mask_not(overflow));
591 auto const replacement = w::select(overflow,
592 c(std::bit_cast<float>(0x7f800000u)), c(0.f));
593 auto const n = w::round_even(x);
594 auto const r = w::sub(x, n);
595 // Sollya fpminimax(2^x,[|1,...,6|],[|single...|],[-1/2;1/2],relative,1).
596 // All coefficients are binary32; the fixed constant preserves exp2(0).
597 auto const y = ::math::horner(
598 c(0x1.3fa206p-13f), c(0x1.5f0b82p-10f), c(0x1.3b30ap-7f),
599 c(0x1.c6af76p-5f), c(0x1.ebfbd8p-3f), c(0x1.62e43p-1f), c(1.f))(r);
600 return w::demote<T>(w::detail::scale_exp(in_range, replacement, y, n));
601 }
602 }
603}
604// Adapted from FTZ's seven-interval tanh polynomial, with native arithmetic.
605namespace math::detail {
606 template<class V,std::size_t N> requires (::wide::detail::binary32_register<V>)
607 [[nodiscard]] hint_inline constexpr auto tanh_kernel(std::array<V,N> const & input) noexcept {
608 namespace w=::wide;
609 auto const word=w::bits(input);
610 auto const u=[&](std::uint32_t value) {return w::constant_like(word,value);};
611 auto const magnitude=w::bit_and(word,u(0x7fffffffu));
612 // Bound large inputs before squaring; the tiny-region result below keeps
613 // original words, including subnormals under the caller's FP environment.
614 auto const safe=w::select(w::cmp_lt(magnitude,u(0x41200000u)),magnitude,u(0x3f800000u));
615 auto const x=w::from_bits(safe);
616 auto const z=w::mul(x,x);
617 auto const interval=w::select(w::cmp_le(safe,u(0x40800000u)),
618 w::select(w::cmp_le(safe,u(0x40000000u)),
619 w::select(w::cmp_le(safe,u(0x3f800000u)),u(0),u(1)),
620 w::select(w::cmp_le(safe,u(0x40400000u)),u(2),u(3))),
621 w::select(w::cmp_le(safe,u(0x41000000u)),
622 w::select(w::cmp_le(safe,u(0x40c00000u)),u(4),u(5)),u(6)));
623 auto const coefficient=[&](std::uint32_t c0,std::uint32_t c1,std::uint32_t c2,
624 std::uint32_t c3,std::uint32_t c4,std::uint32_t c5,std::uint32_t c6) {
625 return w::detail::lift(w::detail::tanh_coefficient{{c0,c1,c2,c3,c4,c5,c6,c6}},interval);
626 };
627 auto const t=w::detail::madd(z,
628 coefficient(0x40000000u,0x3f000000u,0x3e800000u,0x3e800000u,0x3d800000u,0x3d800000u,0x3d000000u),
629 coefficient(0xbf800000u,0xbfa00000u,0xbfd00000u,0xc0480000u,0xbfd00000u,0xc0480000u,0xc0240000u));
630 auto h=coefficient(0x00000000u,0x00000000u,0x00000000u,0x00000000u,0xb9405fbfu,0x00000000u,0x00000000u);
631 h=w::detail::madd(h,t,coefficient(0x00000000u,0x38752140u,0x00000000u,0x00000000u,0x39ab4d5fu,0x00000000u,0x00000000u));
632 h=w::detail::madd(h,t,coefficient(0x00000000u,0xb9183513u,0xb947bd66u,0xb58f6d10u,0xb9c08feeu,0xb59171b1u,0x00000000u));
633 h=w::detail::madd(h,t,coefficient(0x34facb37u,0x398d9ee0u,0x39dfe5a4u,0x36863471u,0x3a2bf5fau,0x3671d749u,0x00000000u));
634 h=w::detail::madd(h,t,coefficient(0xb63a0d2du,0xba2fdf3au,0xba4a3864u,0xb758ec9du,0xbaa65b2au,0xb72757e1u,0xb811c415u));
635 h=w::detail::madd(h,t,coefficient(0x37813497u,0x3ae1130du,0x3ae2b84bu,0x384b3fb7u,0x3b1584c1u,0x380d4deeu,0x38c8e73cu));
636 h=w::detail::madd(h,t,coefficient(0xb8bfb3f8u,0xbb8bc302u,0xbb813c08u,0xb9404721u,0xbb86bc79u,0xb8f4646bu,0xb980a2b8u));
637 h=w::detail::madd(h,t,coefficient(0x3a0e6d24u,0x3c2d6773u,0x3c1129ceu,0x3a356f7bu,0x3bf71905u,0x39d46b54u,0x3a37296bu));
638 h=w::detail::madd(h,t,coefficient(0xbb535f6cu,0xbcd7a178u,0xbca3c0b8u,0xbb2d3d49u,0xbc67da1fu,0xbabdbc02u,0xbb06690eu));
639 h=w::detail::madd(h,t,coefficient(0x3c9d20e4u,0x3d86d45du,0x3d3bcdf4u,0x3c2a7e14u,0x3ce35ff2u,0x3bb1ed8au,0x3bcea86au));
640 h=w::detail::madd(h,t,coefficient(0xbded544du,0xbe2e2df9u,0xbde4f8bcu,0xbd36d397u,0xbd76f5e5u,0xbcb95c19u,0xbcb08499u));
641 h=w::detail::madd(h,t,coefficient(0x3f5c6e3eu,0x3f14c222u,0x3ec662fcu,0x3e9091d7u,0x3e48ced7u,0x3e10d0b5u,0x3de229ecu));
642
643 auto result=w::bits(w::mul(x,h));
644 auto const sign=w::bit_and(word,u(0x80000000u));
645 result=w::bit_or(w::min(result,u(0x3f800000u)),sign);
646 result=w::select(w::cmp_ge(magnitude,u(0x41200000u)),w::bit_or(sign,u(0x3f800000u)),result);
647 result=w::select(w::cmp_le(magnitude,u(0x39800000u)),word,result);
648 result=w::select(w::cmp_gt(magnitude,u(0x7f800000u)),u(0x7fc00000u),result);
649 return w::from_bits(result);
650 }
651}
652namespace math {
655 template<::wide::promotable T> requires (::wide::detail::binary32_array<::wide::canonical_t<T>>)
656 [[nodiscard]] hint_inline constexpr auto tanh(T const & input) noexcept {
657 if constexpr (::wide::detail::shape_t<::wide::canonical_t<T>>::size==0) return std::remove_cvref_t<T>(input);
658 else return ::wide::demote<T>(detail::tanh_kernel(::wide::promote(input)));
659 }
660}
661
662// Adapted coefficient use from SLEEF 3.9.0 atan2kf; Boost 1.0 notice below.
663namespace math::detail {
664 // Raw subnormal inputs are signed zero. Normal operands use one packed
665 // min/max ratio, with no software flushing or changes to FP controls.
666 // A tiny finite result may underflow according to the caller's FP mode.
667 template<class V, std::size_t N>
668 requires (::wide::detail::binary32_register<V>)
669 [[nodiscard]] hint_inline constexpr auto atan2_kernel(
670 std::array<V, N> const & y, std::array<V, N> const & x) noexcept {
671 namespace w = ::wide;
672 auto const c = [&](float value) { return w::constant_like(y, value); };
673 auto const yw = w::bits(y), xw = w::bits(x);
674 auto const u = [&](std::uint32_t value) { return w::constant_like(yw, value); };
675 auto const f = [&](std::uint32_t value) { return c(std::bit_cast<float>(value)); };
676 auto ay = w::bit_and(yw, u(0x7fffffffu));
677 auto ax = w::bit_and(xw, u(0x7fffffffu));
678 ay = w::select(w::cmp_lt(ay, u(0x00800000u)), u(0), ay);
679 ax = w::select(w::cmp_lt(ax, u(0x00800000u)), u(0), ax);
680 auto const swap = w::cmp_gt(ay, ax);
681 auto const negative_x = w::cmp_ne(w::bit_and(xw, u(0x80000000u)), u(0));
682 auto const a = w::select(swap, ax, ay);
683 auto const b = w::select(swap, ay, ax);
684 auto const ratio = w::div(w::from_bits(a), w::from_bits(b));
685 auto const tiny = w::cmp_le(w::bits(ratio), u(0x39800000u));
686 auto const z = w::mul(ratio, ratio);
687 auto const h = ::math::horner(
688 f(0x3b390ccdu), f(0xbc82b80du), f(0x3d2e19b6u), f(0xbd995ffau),
689 f(0x3dd9ccf2u), f(0xbe116f9fu), f(0x3e4cb9a7u), f(0xbeaaaa5du))(z);
690 auto angle = w::detail::madd(w::mul(z, h), ratio, ratio);
691 angle = w::select(tiny, ratio, angle);
692 angle = w::select(swap, w::sub(f(0x3fc90fdbu), angle), angle);
693 angle = w::select(negative_x, w::sub(f(0x40490fdbu), angle), angle);
694 auto result = w::bits(angle);
695 auto const axis = w::select(negative_x, u(0x40490fdbu), u(0));
696 result = w::select(w::cmp_eq(ax, u(0)), u(0x3fc90fdbu), result);
697 result = w::select(w::cmp_eq(ax, u(0x7f800000u)), axis, result);
698 result = w::select(w::cmp_eq(ay, u(0x7f800000u)),
699 w::select(w::cmp_eq(ax, u(0x7f800000u)),
700 w::select(negative_x, u(0x4016cbe4u), u(0x3f490fdbu)), u(0x3fc90fdbu)), result);
701 result = w::select(w::cmp_eq(ay, u(0)), axis, result);
702 result = w::bit_or(result, w::bit_and(yw, u(0x80000000u)));
703 result = w::select(w::bit_or(w::cmp_gt(ay, u(0x7f800000u)),
704 w::cmp_gt(ax, u(0x7f800000u))), u(0x7fc00000u), result);
705 return w::from_bits(result);
706 }
707}
708namespace math {
711 template<::wide::promotable T>
712 requires (::wide::detail::binary32_array<::wide::canonical_t<T>>)
713 [[nodiscard]] hint_inline constexpr auto atan2(T const & y, T const & x) noexcept {
714 if constexpr (::wide::detail::shape_t<::wide::canonical_t<T>>::size == 0)
715 return std::remove_cvref_t<T>(y);
716 else return ::wide::demote<T>(detail::atan2_kernel(::wide::promote(y), ::wide::promote(x)));
717 }
718}
719
720/*
721Copyright Naoki Shibata and contributors 2010 - 2025.
722Boost Software License - Version 1.0 - August 17th, 2003
723
724Permission is hereby granted, free of charge, to any person or organization
725obtaining a copy of the software and accompanying documentation covered by
726this license (the "Software") to use, reproduce, display, distribute,
727execute, and transmit the Software, and to prepare derivative works of the
728Software, and to permit third-parties to whom the Software is furnished to
729do so, all subject to the following:
730
731The copyright notices in the Software and this entire statement, including
732the above license grant, this restriction and the following disclaimer,
733must be included in all copies of the Software, in whole or in part, and
734all derivative works of the Software, unless such copies or derivative
735works are solely in the form of machine-executable object code generated by
736a source language processor.
737
738THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
739IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
740FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
741SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
742FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
743ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
744DEALINGS IN THE SOFTWARE.
745*/
746
747// Adapted from FTZ's cancellation-safe expm1 and reduced log/log1p graphs.
748namespace math {
749 namespace detail {
750 template<bool Gain, class V, std::size_t N>
751 requires (::wide::detail::binary32_register<V>)
752 [[nodiscard]] hint_inline constexpr auto expm1_kernel(
753 std::array<V, N> const & input) noexcept {
754 if constexpr (N == 0) return input;
755 else {
756 auto const c = [&](float value) { return ::wide::constant_like(input, value); };
757 auto const w = [&](std::uint32_t bits) { return c(std::bit_cast<float>(bits)); };
758 auto const x = [&] {
759 if constexpr (Gain) return ::wide::negate(input);
760 else return input;
761 }();
762 auto const n = ::wide::round_even(::wide::mul(x, w(0x3fb8aa3bu)));
763 auto const n_zero = ::wide::cmp_eq(n, c(0.f));
764 auto r = ::wide::detail::madd(n, w(0xbf317200u), x);
765 r = ::wide::detail::madd(n, w(0xb5bfbe8eu), r);
766 r = ::wide::select(n_zero, x, r);
767 auto const z = ::wide::mul(r, r);
768 auto h = ::wide::detail::madd(r, w(0x3493f27eu), w(0x3638ef1du));
769 h = ::wide::detail::madd(r, h, w(0x37d00d01u));
770 h = ::wide::detail::madd(r, h, w(0x39500d01u));
771 h = ::wide::detail::madd(r, h, w(0x3ab60b61u));
772 h = ::wide::detail::madd(r, h, w(0x3c088889u));
773 h = ::wide::detail::madd(r, h, w(0x3d2aaaabu));
774 h = ::wide::detail::madd(r, h, w(0x3e2aaaabu));
775 h = ::wide::detail::madd(r, h, w(0x3f000000u));
776 auto p = ::wide::detail::madd(z, h, r);
777 auto const bits = ::wide::bits(r);
778 auto const u = [&](std::uint32_t value) { return ::wide::constant_like(bits, value); };
779 auto const not_tiny = ::wide::cmp_gt(::wide::bit_and(bits, u(0x7fffffffu)), u(0x33000000u));
780 p = ::wide::select(not_tiny, p, r);
781
782 // Only the reconstruction factor is bounded below; the input and
783 // polynomial are untouched. max's unordered case supplies -24 so a NaN
784 // polynomial propagates while the factor remains well-defined.
785 auto const factor_n = ::wide::max(n, c(-24.f));
786 auto const scale = ::wide::detail::exp_power(factor_n);
787 auto result = ::wide::detail::madd(scale, p, ::wide::sub(scale, c(1.f)));
788 result = ::wide::select(n_zero, p, result);
789 result = ::wide::select(::wide::cmp_eq(n, c(-25.f)),
790 ::wide::select(::wide::cmp_gt(p, c(0.f)), w(0xbf7fffffu), c(-1.f)), result);
791 result = ::wide::select(::wide::cmp_lt(n, c(-25.f)), c(-1.f), result);
792 // Retain general exp's accepted early positive overflow threshold.
793 result = ::wide::select(::wide::cmp_gt(x, c(88.37625885009765625f)),
794 w(0x7f800000u), result);
795 if constexpr (Gain) return ::wide::negate(result);
796 else return result;
797 }
798 }
799 }
800
803 template<::wide::promotable T>
804 requires (::wide::detail::binary32_array<::wide::canonical_t<T>>)
805 [[nodiscard]] hint_inline constexpr auto expm1(T const & input) noexcept {
806 if constexpr (::wide::detail::shape_t<::wide::canonical_t<T>>::size == 0)
807 return std::remove_cvref_t<T>(input);
808 else return ::wide::demote<T>(detail::expm1_kernel<false>(::wide::promote(input)));
809 }
810
813 template<::wide::promotable T>
814 requires (::wide::detail::binary32_array<::wide::canonical_t<T>>)
815 [[nodiscard]] hint_inline constexpr auto damping_gain(T const & input) noexcept {
816 if constexpr (::wide::detail::shape_t<::wide::canonical_t<T>>::size == 0)
817 return std::remove_cvref_t<T>(input);
818 else return ::wide::demote<T>(detail::expm1_kernel<true>(::wide::promote(input)));
819 }
820}
821
822namespace math::detail {
823 // Direct base-two reduction and a newly generated Sollya polynomial.
824 template<class V, std::size_t N>
825 requires (::wide::detail::binary32_register<V>)
826 [[nodiscard]] hint_inline constexpr auto log2_kernel(std::array<V, N> const & input) noexcept {
827 namespace w = ::wide;
828 auto const c = [&](float value) { return w::constant_like(input, value); };
829 auto const word = w::bits(input);
830 auto const u = [&](std::uint32_t value) { return w::constant_like(word, value); };
831 // The sign bit adds a harmless 256 for negative inputs, replaced below.
832 // Delay the magnitude mask so it need not stay live through the polynomial.
833 auto exponent = w::sub(w::right<23>(word), u(127));
834 auto mantissa = w::bit_or(w::bit_and(word, u(0x007fffffu)), u(0x3f800000u));
835 // Fold at binary32 sqrt(2), keeping log2(m) close to [-0.5, 0.5].
836 // The bit reduction is finite even for inputs classified below.
837 auto const upper = w::cmp_ge(mantissa, u(0x3fb504f3u));
838 mantissa = w::sub(mantissa, w::select(upper, u(0x00800000u), u(0)));
839 exponent = w::add(exponent, w::select(upper, u(1), u(0)));
840 auto const r = w::sub(w::from_bits(mantissa), c(1.f));
841 // Sollya 8.0 fpminimax of log2(1+r)/r, degree 9, binary32
842 // coefficients, absolute error on [-0x1.2bec34p-2, 0x1.a82790p-2].
843 auto const h = ::math::horner(
844 c(-0x1.9c1d9ep-4f), c(0x1.729888p-3f), c(-0x1.8ac44cp-3f),
845 c(0x1.a55be8p-3f), c(-0x1.ea87d4p-3f), c(0x1.2767d4p-2f),
846 c(-0x1.715b1p-2f), c(0x1.ec713ap-2f), c(-0x1.71547p-1f),
847 c(0x1.715476p+0f))(r);
848 auto result = w::bits(w::detail::madd(r, h, w::detail::signed_float(exponent)));
849 auto const magnitude = w::bit_and(word, u(0x7fffffffu));
850 auto const sign = w::bit_and(word, u(0x80000000u));
851 result = w::select(w::cmp_eq(word, u(0x7f800000u)), word, result);
852 result = w::select(w::cmp_ne(sign, u(0)), u(0x7fc00000u), result);
853 result = w::select(w::cmp_lt(magnitude, u(0x00800000u)), u(0xff800000u), result);
854 result = w::select(w::cmp_gt(magnitude, u(0x7f800000u)), u(0x7fc00000u), result);
855 return w::from_bits(result);
856 }
857
858 // Normal inputs retain the FTZ hardware polynomial and reduction graph.
859 // There is no software flushing between arithmetic operations and no FP
860 // control change. log treats subnormal inputs as signed zero; log1p returns
861 // the original bits for |x|<=2^-25, including signed zero and subnormals.
862 // NaNs and domain errors return the FTZ graph's canonical quiet NaN.
863 template<bool OnePlus, class V, std::size_t N>
864 requires (::wide::detail::binary32_register<V>)
865 [[nodiscard]] hint_inline constexpr auto log_kernel(std::array<V, N> const & input) noexcept {
866 namespace w = ::wide;
867 auto const c = [&](float value) { return w::constant_like(input, value); };
868 auto const word = w::bits(input);
869 auto const u = [&](std::uint32_t value) { return w::constant_like(word, value); };
870 auto const magnitude = w::bit_and(word, u(0x7fffffffu));
871 auto const sign = w::bit_and(word, u(0x80000000u));
872 auto const valid = [&] {
873 if constexpr (OnePlus) return w::bit_and(w::cmp_lt(magnitude, u(0x7f800000u)),
874 w::bit_or(w::cmp_eq(sign, u(0)), w::cmp_lt(magnitude, u(0x3f800000u))));
875 else return w::bit_and(w::cmp_ge(word, u(0x00800000u)), w::cmp_lt(word, u(0x7f800000u)));
876 }();
877 auto const direct = [&] {
878 if constexpr (OnePlus) return w::bit_and(valid, w::cmp_le(magnitude,
879 w::select(w::cmp_ne(sign, u(0)), u(0x3f000000u), u(0x3f800000u))));
880 else return valid;
881 }();
882 auto const positive = [&] {
883 if constexpr (OnePlus) return w::bits(w::add(c(1.f), w::from_bits(
884 w::select(w::bit_and(valid, w::mask_not(direct)), word, u(0)))));
885 else return w::select(valid, word, u(0x3f800000u));
886 }();
887 auto exponent = w::sub(w::right<23>(positive), u(127));
888 auto mantissa = w::bit_or(w::bit_and(positive, u(0x007fffffu)), u(0x3f800000u));
889 auto const upper = w::cmp_ge(mantissa, u(0x3fc00000u));
890 mantissa = w::sub(mantissa, w::select(upper, u(0x00800000u), u(0)));
891 exponent = w::add(exponent, w::select(upper, u(1), u(0)));
892 auto const reduced = w::sub(w::from_bits(mantissa), c(1.f));
893 auto const argument = [&] {
894 if constexpr (OnePlus) return w::select(direct, input, reduced);
895 else return reduced;
896 }();
897 auto const negative = w::cmp_ne(w::bit_and(w::bits(argument), u(0x80000000u)), u(0));
898 auto const coefficient = [&](std::uint32_t negative_word, std::uint32_t positive_word) {
899 return w::select(negative, c(std::bit_cast<float>(negative_word)), c(std::bit_cast<float>(positive_word)));
900 };
901 auto const square = w::mul(argument, argument);
902 auto const t = w::detail::madd(argument, coefficient(0x40800000u, 0x40000000u),
903 coefficient(0x3f800000u, 0xbf800000u));
904 auto h = coefficient(0x00000000u, 0xb29c7ee2u);
905 h = w::detail::madd(h, t, coefficient(0x00000000u, 0x3378ea39u));
906 h = w::detail::madd(h, t, coefficient(0xb44f5480u, 0xb3faaccbu));
907 h = w::detail::madd(h, t, coefficient(0x352754efu, 0x34c9e1cdu));
908 h = w::detail::madd(h, t, coefficient(0xb5bb75dbu, 0xb5b13b5eu));
909 h = w::detail::madd(h, t, coefficient(0x369a1c19u, 0x36902a0au));
910 h = w::detail::madd(h, t, coefficient(0xb7866f43u, 0xb76af011u));
911 h = w::detail::madd(h, t, coefficient(0x3861235au, 0x38423d8au));
912 h = w::detail::madd(h, t, coefficient(0xb93e98dfu, 0xb9225d51u));
913 h = w::detail::madd(h, t, coefficient(0x3a24a041u, 0x3a0988b0u));
914 h = w::detail::madd(h, t, coefficient(0xbb117f6au, 0xbaed1a41u));
915 h = w::detail::madd(h, t, coefficient(0x3c04b7c5u, 0x3bd13ce0u));
916 h = w::detail::madd(h, t, coefficient(0xbcfda364u, 0xbcbeef90u));
917 h = w::detail::madd(h, t, coefficient(0x3e029133u, 0x3db786beu));
918 h = w::detail::madd(h, t, coefficient(0xbf1a5884u, 0xbec19b82u));
919 auto const polynomial = w::detail::madd(square, h, argument);
920 auto const e = w::detail::signed_float(exponent);
921 auto const low = w::detail::madd(e, c(std::bit_cast<float>(0x35bfbe8eu)), polynomial);
922 auto result = w::bits(w::detail::madd(e, c(std::bit_cast<float>(0x3f317200u)), low));
923 if constexpr (OnePlus) {
924 result = w::select(direct, w::bits(polynomial), result);
925 result = w::select(w::cmp_le(magnitude, u(0x33000000u)), word, result);
926 result = w::select(w::cmp_eq(word, u(0xbf800000u)), u(0xff800000u), result);
927 result = w::select(w::cmp_eq(word, u(0x7f800000u)), word, result);
928 result = w::select(w::bit_or(w::cmp_gt(magnitude, u(0x7f800000u)),
929 w::bit_and(w::cmp_ne(sign, u(0)), w::cmp_gt(magnitude, u(0x3f800000u)))), u(0x7fc00000u), result);
930 } else {
931 result = w::select(w::cmp_eq(word, u(0x7f800000u)), word, result);
932 result = w::select(w::cmp_ne(sign, u(0)), u(0x7fc00000u), result);
933 result = w::select(w::cmp_lt(magnitude, u(0x00800000u)), u(0xff800000u), result);
934 result = w::select(w::cmp_gt(magnitude, u(0x7f800000u)), u(0x7fc00000u), result);
935 }
936 return w::from_bits(result);
937 }
938}
939namespace math {
941 template<::wide::promotable T> requires (::wide::detail::binary32_array<::wide::canonical_t<T>>)
942 [[nodiscard]] hint_inline constexpr auto log(T const & input) noexcept {
943 if constexpr (::wide::detail::shape_t<::wide::canonical_t<T>>::size == 0) return std::remove_cvref_t<T>(input);
944 else return ::wide::demote<T>(detail::log_kernel<false>(::wide::promote(input)));
945 }
948 template<::wide::promotable T> requires (::wide::detail::binary32_array<::wide::canonical_t<T>>)
949 [[nodiscard]] hint_inline constexpr auto log2(T const & input) noexcept {
950 if constexpr (::wide::detail::shape_t<::wide::canonical_t<T>>::size == 0) return std::remove_cvref_t<T>(input);
951 else return ::wide::demote<T>(detail::log2_kernel(::wide::promote(input)));
952 }
954 template<::wide::promotable T> requires (::wide::detail::binary32_array<::wide::canonical_t<T>>)
955 [[nodiscard]] hint_inline constexpr auto log1p(T const & input) noexcept {
956 if constexpr (::wide::detail::shape_t<::wide::canonical_t<T>>::size == 0) return std::remove_cvref_t<T>(input);
957 else return ::wide::demote<T>(detail::log_kernel<true>(::wide::promote(input)));
958 }
959}
960
961// SPDX-FileCopyrightText: 2012 Giovanni Garberoglio
962// SPDX-FileCopyrightText: 2017 Edward Kmett
963// SPDX-FileCopyrightText: 2026 Edward Kmett <ekmett@gmail.com>
964// SPDX-License-Identifier: Zlib AND BSD-2-Clause
965// Altered source: the original trig reducer and polynomials now operate on
966// standard arrays, preserving each binary32 dependency stage.
967
968namespace math {
969 namespace detail {
970 enum class trig_kind { sine, cosine, paired };
971
972 // Precondition: every lane is finite and |x| < 8192.
973 template<trig_kind K, ::wide::pack P>
974 [[nodiscard]] hint_inline constexpr auto trig(P const & original) noexcept {
975 auto const encoded = ::wide::bits(original);
976 auto const c = [&](float value) { return ::wide::constant_like(original, value); };
977 auto const i = [&](std::uint32_t value) { return ::wide::constant_like(encoded, value); };
978 auto sign_sine = ::wide::bit_and(encoded, i(0x80000000u));
979 auto x = ::wide::from_bits(::wide::bit_and(encoded, i(0x7fffffffu)));
980 auto y = ::wide::mul(x, c(1.27323954473516f));
981 auto const j = ::wide::bit_and(::wide::add(::wide::detail::trig_integer(y), i(1)), i(0xfffffffeu));
982 y = ::wide::detail::signed_float(j);
983 sign_sine = ::wide::bit_xor(sign_sine, ::wide::left<29>(::wide::bit_and(j, i(4))));
984 auto const sign_cosine = ::wide::left<29>(::wide::bit_and(::wide::bit_xor(::wide::sub(j, i(2)), i(0xffffffffu)), i(4)));
985 auto quadrant = j;
986 if constexpr (K == trig_kind::cosine) quadrant = ::wide::sub(quadrant, i(2));
987 auto const mask = ::wide::mask_bits<std::uint32_t>(::wide::cmp_eq(::wide::bit_and(quadrant, i(2)), i(0)));
988 x = ::wide::detail::madd(y, c(-0.78515625f), x);
989 x = ::wide::detail::madd(y, c(-2.4187564849853515625e-4f), x);
990 x = ::wide::detail::madd(y, c(-3.77489497744594108e-8f), x);
991 auto const z = ::wide::mul(x, x);
992 auto cosine = ::wide::detail::madd(c(2.443315711809948e-5f), z, c(-1.388731625493765e-3f));
993 cosine = ::wide::detail::madd(cosine, z, c(4.166664568298827e-2f));
994 cosine = ::wide::mul(cosine, z);
995 cosine = ::wide::mul(cosine, z);
996 cosine = ::wide::sub(cosine, ::wide::mul(z, c(0.5f)));
997 cosine = ::wide::add(cosine, c(1.f));
998 auto sine = ::wide::detail::madd(c(-1.9515295891e-4f), z, c(8.3321608736e-3f));
999 sine = ::wide::detail::madd(sine, z, c(-1.6666654611e-1f));
1000 sine = ::wide::mul(sine, z);
1001 sine = ::wide::detail::madd(sine, x, x);
1002 auto selected_sine = ::wide::from_bits(::wide::bit_and(mask, ::wide::bits(sine)));
1003 auto selected_cosine = ::wide::from_bits(::wide::bit_and(::wide::bit_xor(mask, i(0xffffffffu)), ::wide::bits(cosine)));
1004 if constexpr (K == trig_kind::paired) {
1005 // Preserve subtraction selection, including its signed-zero effects.
1006 sine = ::wide::sub(sine, selected_sine);
1007 cosine = ::wide::sub(cosine, selected_cosine);
1008 selected_sine = ::wide::from_bits(::wide::bit_xor(::wide::bits(::wide::add(selected_cosine, selected_sine)), sign_sine));
1009 selected_cosine = ::wide::from_bits(::wide::bit_xor(::wide::bits(::wide::add(cosine, sine)), sign_cosine));
1010 return std::pair{selected_sine, selected_cosine};
1011 } else if constexpr (K == trig_kind::sine)
1012 return ::wide::from_bits(::wide::bit_xor(::wide::bits(::wide::add(selected_cosine, selected_sine)), sign_sine));
1013 else return ::wide::from_bits(::wide::bit_xor(::wide::bits(::wide::add(selected_cosine, selected_sine)), sign_cosine));
1014 }
1015
1016 template<trig_kind K, ::wide::promotable T>
1017 requires (::wide::detail::binary32_pack<::wide::canonical_t<T>>)
1018 [[nodiscard]] hint_inline constexpr auto trig_result(T const & input) noexcept {
1019 if constexpr (::wide::detail::shape_t<::wide::canonical_t<T>>::size == 0) {
1020 if constexpr (K == trig_kind::paired)
1021 return std::pair{std::remove_cvref_t<T>(input), std::remove_cvref_t<T>(input)};
1022 else return std::remove_cvref_t<T>(input);
1023 } else if constexpr (K == trig_kind::paired) {
1024 auto const [sine, cosine] = trig<K>(::wide::promote(input));
1025 return std::pair{::wide::demote<T>(sine), ::wide::demote<T>(cosine)};
1026 } else return ::wide::demote<T>(trig<K>(::wide::promote(input)));
1027 }
1028 }
1029
1031 template<::wide::promotable T> requires (::wide::detail::binary32_pack<::wide::canonical_t<T>>)
1032 [[nodiscard]] hint_inline constexpr auto sin(T const & input) noexcept {
1033 return detail::trig_result<detail::trig_kind::sine>(input);
1034 }
1036 template<::wide::promotable T> requires (::wide::detail::binary32_pack<::wide::canonical_t<T>>)
1037 [[nodiscard]] hint_inline constexpr auto cos(T const & input) noexcept {
1038 return detail::trig_result<detail::trig_kind::cosine>(input);
1039 }
1041 template<::wide::promotable T> requires (::wide::detail::binary32_pack<::wide::canonical_t<T>>)
1042 [[nodiscard]] hint_inline constexpr auto sincos(T const & input) noexcept {
1043 return detail::trig_result<detail::trig_kind::paired>(input);
1044 }
1045
1047 template<::wide::promotable T> requires (::wide::detail::binary32_pack<::wide::canonical_t<T>>)
1048 [[nodiscard]] hint_inline constexpr auto flush_to_zero(T const & input) noexcept {
1049 if constexpr (::wide::detail::shape_t<::wide::canonical_t<T>>::size == 0)
1050 return std::remove_cvref_t<T>(input);
1051 else {
1052 auto bits = ::wide::bits(::wide::promote(input));
1053 auto const i = [&](std::uint32_t value) { return ::wide::constant_like(bits, value); };
1054 auto const exponent = ::wide::bit_and(bits, i(0x7f800000u));
1055 auto const zero = ::wide::mask_bits<std::uint32_t>(::wide::cmp_eq(exponent, i(0)));
1056 auto const clear = ::wide::bit_and(zero, i(0x007fffffu));
1057 bits = ::wide::bit_and(bits, ::wide::bit_xor(clear, i(0xffffffffu)));
1058 return ::wide::demote<T>(::wide::from_bits(bits));
1059 }
1060 }
1061
1062 // These primitives retain their native leaf semantics inside one wide stage.
1063#define NATIVE_PROMOTED_UNARY(name) \
1064 template<::wide::promotable T> \
1065 requires (::wide::detail::binary32_pack<::wide::canonical_t<T>>) \
1066 [[nodiscard]] hint_inline constexpr auto name(T const & input) noexcept { \
1067 if constexpr (::wide::detail::shape_t<::wide::canonical_t<T>>::size == 0) \
1068 return std::remove_cvref_t<T>(input); \
1069 else return ::wide::demote<T>(::wide::name(::wide::promote(input))); \
1070 }
1071
1072 NATIVE_PROMOTED_UNARY(abs)
1073 NATIVE_PROMOTED_UNARY(sqrt)
1074 NATIVE_PROMOTED_UNARY(floor)
1075 NATIVE_PROMOTED_UNARY(ceil)
1076 NATIVE_PROMOTED_UNARY(trunc)
1077 NATIVE_PROMOTED_UNARY(round_even)
1078#undef NATIVE_PROMOTED_UNARY
1079}
1080
1081namespace wide {
1082 // Qualified convenience aliases; standard arrays keep their ordinary ADL.
1083 using ::math::horner;
1084 using ::math::exp;
1085 using ::math::exp2;
1086 using ::math::expm1;
1087 using ::math::damping_gain;
1088 using ::math::log;
1089 using ::math::log2;
1090 using ::math::log1p;
1091 using ::math::tanh;
1092 using ::math::atan2;
1093 using ::math::sin;
1094 using ::math::cos;
1095 using ::math::sincos;
1096 using ::math::flush_to_zero;
1097}
1098
1099/*
1100 AVX implementation of sin, cos, sincos, exp and log
1101
1102 Based on "sse_mathfun.h", by Julien Pommier
1103 http://gruntthepeon.free.fr/ssemath/
1104
1105 Copyright (C) 2012 Giovanni Garberoglio
1106 Interdisciplinary Laboratory for Computational Science (LISC)
1107 Fondazione Bruno Kessler and University of Trento
1108 via Sommarive, 18
1109 I-38123 Trento (Italy)
1110
1111 This software is provided 'as-is', without any express or implied
1112 warranty. In no event will the authors be held liable for any damages
1113 arising from the use of this software.
1114
1115 Permission is granted to anyone to use this software for any purpose,
1116 including commercial applications, and to alter it and redistribute it
1117 freely, subject to the following restrictions:
1118
1119 1. The origin of this software must not be misrepresented; you must not
1120 claim that you wrote the original software. If you use this software
1121 in a product, an acknowledgment in the product documentation would be
1122 appreciated but is not required.
1123 2. Altered source versions must be plainly marked as such, and must not be
1124 misrepresented as being the original software.
1125 3. This notice may not be removed or altered from any source distribution.
1126*/
1127
1128/* RTS repository license (retained verbatim):
1129Software License Agreement (BSD 2-Clause License)
1130========================================
1131
1132Copyright 2017 Edward Kmett
1133
1134Redistribution and use in source and binary forms, with or without
1135modification, are permitted provided that the following conditions are met:
1136
1137 * Redistributions of source code must retain the above copyright
1138 notice, this list of conditions and the following disclaimer.
1139
1140 * Redistributions in binary form must reproduce the above copyright
1141 notice, this list of conditions and the following disclaimer in the
1142 documentation and/or other materials provided with the distribution.
1143
1144THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
1145ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1146WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1147DISCLAIMED. IN NO EVENT SHALL YAHOO! INC. BE LIABLE FOR ANY
1148DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
1149(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
1150LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
1151ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1152(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
1153SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1154*/
typename mask_traits< std::remove_cvref_t< T > >::type mask
Definition mask_traits.h:22
Architecture-tagged vectors, register packs and supporting value types. Native arithmetic follows its...
constexpr architecture arm
Select ARM feature metadata independently of the compilation target.
Definition config.h:28
constexpr std::size_t sin_width
Suggested sine register count, matching the paired trigonometric kernel.
Definition wide_math.h:86
constexpr architecture x86
Select x86 feature metadata independently of the compilation target.
Definition config.h:26
constexpr simd< std::make_unsigned_t< T >, N, A > mask_bits(detail::polyfill_predicate< N, A > mask) noexcept
Expand each logical predicate bit into an unsigned zero/all-one lane.
constexpr std::size_t log_width
Conservative independent register count for the logarithm kernel.
Definition wide_math.h:59
constexpr std::size_t cos_width
Suggested cosine register count, matching the paired trigonometric kernel.
Definition wide_math.h:90
constexpr architecture wasm
Select WebAssembly feature metadata independently of the compilation target.
Definition config.h:30
constexpr simd< fp16, 32, Arch > fma(simd< fp16, 32, Arch > a, simd< fp16, 32, Arch > b, simd< fp16, 32, Arch > c) noexcept
constexpr simd< float, N, A > masked_scaleb(M mask, simd< float, N, A > prior, simd< float, N, A > value, simd< float, N, A > exponent) noexcept
constexpr std::size_t log1p_width
Conservative independent register count for cancellation-safe log1p.
Definition wide_math.h:67
constexpr std::size_t damping_gain_width
Suggested damping_gain register count, matching its expm1 graph.
Definition wide_math.h:55
constexpr std::size_t atan2_width
Definition wide_math.h:76
constexpr std::size_t log2_width
Suggested log2 register count, matching the logarithm recommendation.
Definition wide_math.h:63
constexpr simd< float, N, A > scaleb(simd< float, N, A > value, simd< float, N, A > exponent) noexcept
Scale every logical lane, preferring each available native scaling register.
constexpr std::size_t exp2_width
Recommended exp2 register count; its shorter reduction favors small batches.
Definition wide_math.h:47
constexpr simd< float, N, A > masked_scaleb_zero(M mask, simd< float, N, A > value, simd< float, N, A > exponent) noexcept
Scale active lanes and write positive zero to every inactive lane.
constexpr std::size_t expm1_width
Conservative independent register count for cancellation-safe expm1.
Definition wide_math.h:51
constexpr std::size_t tanh_width
Conservative independent register count for tanh's table and polynomial.
Definition wide_math.h:71
constexpr std::size_t exp_width
Definition wide_math.h:38
constexpr std::size_t sincos_width
Recommended independent register count for paired sine and cosine.
Definition wide_math.h:80