Intel xvec
Experimental C++ simd class library
Loading...
Searching...
No Matches
std_math.hpp
1//===----------------------------------------------------------------------===//
2//
3// Copyright (C) 2021 Intel Corporation
4//
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#pragma once
10
11#include <concepts>
12#include <cstdint> // std::uint8_t
13#include <limits> // std::numeric_limits
14#include <type_traits>
15
16#include <xvec/detail/config.hpp>
17#include <xvec/detail/utilities.hpp>
18
19#if defined(__INTEL_CLANG_COMPILER) && !defined(_XVEC_FORCE_SCALAR)
20 #include <xvec/x86/math.hpp>
21#else
22 #include <xvec/generic/math.hpp>
23#endif
24
25namespace _XVEC_NAMESPACE::simd
26{
27
34
41
44
49template<detail::math_floating_point _Vp> constexpr detail::deduced_vec_t<_Vp> abs(const _Vp& v) { return detail::abs(target, v); }
50
56template<vec_signed_integral _Vp> constexpr _Vp abs(const _Vp& v) { return detail::abs(target, v); }
57
63template<detail::math_floating_point _Vp>
64constexpr detail::deduced_vec_t<_Vp> copysign(const _Vp& magnitude, const _Vp& sign)
65{
66 using detail::vec_as_container;
67
68 // It must be some sort of a float. Combine the sign bit from one parameter with the magnitude bits from the
69 // other parameter. This will turn into a single ternary logic instruction on machines that support Intel AVX-512.
70 const auto tAbs = vec_as_container(abs(magnitude));
71 constexpr auto signBit = vec_as_container(_Vp(typename _Vp::value_type(-0.0)));
72 const auto tSign = vec_as_container(sign) & signBit;
73 return std::bit_cast<_Vp>(tAbs | tSign);
74}
75
77
80
85template<detail::math_floating_point _Vp> constexpr detail::deduced_vec_t<_Vp> rcp (const _Vp& v) { return detail::rcp(target, v); }
86
91template<detail::math_floating_point _Vp> constexpr detail::deduced_vec_t<_Vp> rsqrt(const _Vp& v) { return detail::rsqrt(target, v); }
92
94
97
102template<detail::math_floating_point _Vp> constexpr detail::deduced_vec_t<_Vp> sqrt (const _Vp& v) { return detail::sqrt(target, v); }
103
110template<detail::math_floating_point _Vp> constexpr detail::deduced_vec_t<_Vp>
111 fma (const _Vp& lhs, const _Vp& rhs, const _Vp& acc) { return detail::fma(target, lhs, rhs, acc); }
112
114
117
124template<vec_complex _Vp>
125constexpr _Vp fma (const _Vp& lhs, const _Vp& rhs, const _Vp& acc)
126 { return detail::fma(target, lhs, rhs, acc); }
127
135template<detail::math_floating_point _Vp>
136constexpr detail::deduced_vec_t<_Vp> fmaddsub(const _Vp& lhs, const _Vp& rhs, const _Vp& acc)
137 { return detail::fmaddsub(target, lhs, rhs, acc); }
138
146template<detail::math_floating_point _Vp>
147constexpr detail::deduced_vec_t<_Vp> fmsubadd(const _Vp& lhs, const _Vp& rhs, const _Vp& acc)
148 { return detail::fmsubadd(target, lhs, rhs, acc); }
149
151
154
159template<detail::math_floating_point _Vp>
160constexpr detail::deduced_vec_t<_Vp> ceil(const _Vp& v) {
161 #if __has_builtin(__builtin_elementwise_ceil)
162 return __builtin_elementwise_ceil(v.to_builtin());
163 #else
165 #endif
166}
167
172template<detail::math_floating_point _Vp>
173constexpr detail::deduced_vec_t<_Vp> floor(const _Vp& v)
174{
175 #if __has_builtin(__builtin_elementwise_floor)
176 return __builtin_elementwise_floor(v.to_builtin());
177 #else
179 #endif
180}
181
186template<detail::math_floating_point _Vp>
187constexpr detail::deduced_vec_t<_Vp> trunc(const _Vp& v) {
188 #if __has_builtin(__builtin_elementwise_trunc)
189 return __builtin_elementwise_trunc(v.to_builtin());
190 #else
192 #endif
193}
194
199template<detail::math_floating_point _Vp>
200constexpr detail::deduced_vec_t<_Vp> rint(const _Vp& v) {
201 #if __has_builtin(__builtin_elementwise_rint)
202 return __builtin_elementwise_rint(v.to_builtin());
203 #else
205 #endif
206}
207
212template<detail::math_floating_point _Vp>
213constexpr detail::deduced_vec_t<_Vp> round(const _Vp& v) {
214 #if __has_builtin(__builtin_elementwise_roundeven)
215 return __builtin_elementwise_round(v.to_builtin());
216 #else
218 #endif
219}
220
222
223namespace detail
224{
227template<math_floating_point _Vp>
228constexpr deduced_mask_t<_Vp> isnan(generic_tag, const _Vp& v)
229{
230 // Exponent mask is the same as infinity (infinite has no sign and no
231 // fractional bits, just a fully populated exponent).
232 constexpr auto expMask = vec_as_container(std::numeric_limits<_Vp>::infinity());
233
234 // Fractional mask is the same as the largest subnormal number. This can be
235 // found by getting the smallest normal number and subtracting one from its integer
236 // representation.
237 using _Tp = typename _Vp::value_type;
238 constexpr auto fm = std::bit_cast<container_for_type<_Tp>>(std::numeric_limits<_Tp>::min()) - 1;
239 constexpr auto fractionMask = typename _Vp::traits::element_container_type(fm);
240
241 // Check for NaN by using the actual bit-pattern. The exponent must be all
242 // ones, and the fraction nonzero. Checking a NaN against itself is another
243 // way but is fragile with respect to compiler optimisations.
244 const auto asC = vec_as_container(v);
245 return ((asC & expMask) == expMask) & ((asC & fractionMask) != std::uint8_t(0));
246}
247
250template<detail::math_floating_point _Vp>
251constexpr detail::deduced_mask_t<_Vp> isinf(generic_tag tag, const _Vp& v)
252 { return !isnan(tag, v) && (abs(v) > std::numeric_limits<_Vp>::max()); }
253
256template<detail::math_floating_point _Vp>
257constexpr detail::deduced_mask_t<_Vp> isfinite(generic_tag tag, const _Vp& v)
258 { return !(isinf(tag, v) || isnan(tag, v)); }
259
262template<detail::math_floating_point _Vp>
263constexpr detail::deduced_mask_t<_Vp> isnormal(generic_tag, const _Vp& v)
264{
265 return (abs(v) >= std::numeric_limits<_Vp>::min()) &&
266 (abs(v) <= std::numeric_limits<_Vp>::max());
267}
268
271template<detail::math_floating_point _Vp>
272constexpr detail::deduced_mask_t<_Vp> signbit(generic_tag, const _Vp& v)
273{
274 constexpr auto signBit = vec_as_container(_Vp(typename _Vp::value_type(-0.0)));
275 auto m = (vec_as_container(v) & signBit) != std::uint8_t(0);
276 return detail::deduced_mask_t<_Vp>(m);
277}
278
279}
280
281// namespace detail
282
285
290template<detail::math_floating_point _Vp>
291constexpr detail::deduced_mask_t<_Vp> isinf(const _Vp& v) { return detail::isinf(target, v); }
292
297template<detail::math_floating_point _Vp>
298constexpr detail::deduced_mask_t<_Vp> isnan(const _Vp& v) { return detail::isnan(target, v); }
299
304template<detail::math_floating_point _Vp>
305constexpr detail::deduced_mask_t<_Vp> isfinite(const _Vp& v) { return detail::isfinite(target, v); }
306
311template<detail::math_floating_point _Vp>
312constexpr detail::deduced_mask_t<_Vp> isnormal(const _Vp& v) { return detail::isnormal(target, v); }
313
319template<detail::math_floating_point _Vp>
320constexpr detail::deduced_mask_t<_Vp> signbit(const _Vp& v) { return detail::signbit(target, v); }
321
323
330template<vec_integral _Vp>
331constexpr typename _Vp::mask_type signbit(const _Vp& v) {
332 if constexpr (vec_unsigned_integral<_Vp>)
333 return typename _Vp::mask_type(false); // Unsigned integer can never become signed
334 else
335 return v < _Vp(); // Signed integer
336}
337
340
345template<detail::math_floating_point _Vp>
346constexpr detail::deduced_mask_t<_Vp> isgreater(const _Vp& x, const _Vp& y) { return x > y; }
347
352template<detail::math_floating_point _Vp>
353constexpr detail::deduced_mask_t<_Vp> isgreaterequal(const _Vp& x, const _Vp& y) { return x >= y; }
354
359template<detail::math_floating_point _Vp>
360constexpr detail::deduced_mask_t<_Vp> isless(const _Vp& x, const _Vp& y) { return x < y; }
361
366template<detail::math_floating_point _Vp>
367constexpr detail::deduced_mask_t<_Vp> islessequal(const _Vp& x, const _Vp& y) { return x <= y; }
368
373template<detail::math_floating_point _Vp>
374constexpr detail::deduced_mask_t<_Vp> islessgreater(const _Vp& x, const _Vp& y) { return (x < y) | (x > y); }
375
380template<detail::math_floating_point _Vp>
381constexpr detail::deduced_mask_t<_Vp> isunordered(const _Vp& x, const _Vp& y) { return isnan(x) | isnan(y); }
382
383// Basic
384// :TODO: Move from elsewhere in some cases.
385
386// Exponential
387_XVEC_UNARY_MATHS_OP(exp);
388_XVEC_UNARY_MATHS_OP(exp2);
389_XVEC_UNARY_MATHS_OP(expm1);
390_XVEC_UNARY_MATHS_OP(log);
391_XVEC_UNARY_MATHS_OP(log2);
392_XVEC_UNARY_MATHS_OP(log1p);
393
394// Trig and hyperbolic
395_XVEC_UNARY_MATHS_OP(sin); _XVEC_UNARY_MATHS_OP(asin);
396_XVEC_UNARY_MATHS_OP(cos); _XVEC_UNARY_MATHS_OP(acos);
397_XVEC_UNARY_MATHS_OP(tan); _XVEC_UNARY_MATHS_OP(atan);
398
399_XVEC_UNARY_MATHS_OP(sinh); _XVEC_UNARY_MATHS_OP(asinh);
400_XVEC_UNARY_MATHS_OP(cosh); _XVEC_UNARY_MATHS_OP(acosh);
401_XVEC_UNARY_MATHS_OP(tanh); _XVEC_UNARY_MATHS_OP(atanh);
402// atan2
403
404// Power
405_XVEC_UNARY_MATHS_OP(cbrt);
406_XVEC_BINARY_MATHS_OP(hypot);
407_XVEC_BINARY_MATHS_OP(pow);
408
409// Error and gamma (lgamma not supported yet in SVML)
410_XVEC_UNARY_MATHS_OP(erf);
411_XVEC_UNARY_MATHS_OP(erfc);
412
414
415} // namespace _XVEC_NAMESPACE::simd
416
417namespace _XVEC_NAMESPACE
418{
419 using simd::abs;
420 using simd::copysign;
421 using simd::fma;
422 using simd::sqrt;
423 using simd::ceil;
424 using simd::floor;
425 using simd::trunc;
426 using simd::rint;
427 using simd::round;
428
429 using simd::isinf;
430 using simd::isnan;
431 using simd::isfinite;
432 using simd::isnormal;
433 using simd::signbit;
434 using simd::isgreater;
435 using simd::isgreaterequal;
436 using simd::isless;
437 using simd::islessequal;
438 using simd::islessgreater;
439 using simd::isunordered;
440
441 using simd::exp;
442 using simd::exp2;
443 using simd::expm1;
444 using simd::log;
445 using simd::log2;
446 using simd::log1p;
447
448 using simd::sin;
449 using simd::asin;
450 using simd::cos;
451 using simd::acos;
452 using simd::tan;
453 using simd::atan;
454 using simd::sinh;
455 using simd::asinh;
456 using simd::cosh;
457 using simd::acosh;
458 using simd::tanh;
459 using simd::atanh;
460
461 using simd::cbrt;
462 using simd::hypot;
463 using simd::pow;
464
465 using simd::erf;
466 using simd::erfc;
467}
468
469// The maths-op generator macros exist only to stamp out the declarations above,
470// so retire them here rather than leaking them into every translation unit that
471// includes <xvec/simd>. Undefining a macro that was never defined is well-formed,
472// so the target-specific helpers need no guards. Note this must happen after the
473// last expansion above, since the target-specific helpers are expanded lazily
474// inside _XVEC_UNARY_MATHS_OP / _XVEC_BINARY_MATHS_OP.
475#undef _XVEC_UNARY_MATHS_OP
476#undef _XVEC_BINARY_MATHS_OP
477
478#undef _XVEC_SSE_UNARY_MATHS_OP
479#undef _XVEC_SSE_BINARY_MATHS_OP
480#undef _XVEC_AVX_UNARY_MATHS_OP
481#undef _XVEC_AVX_BINARY_MATHS_OP
482#undef _XVEC_AVX512_UNARY_MATHS_OP
483#undef _XVEC_AVX512_BINARY_MATHS_OP
484#undef _XVEC_AVX512FP16_UNARY_MATHS_OP
485#undef _XVEC_AVX512FP16_BINARY_MATHS_OP
Definition core.hpp:67
constexpr auto abs(const _Vc &value)
Compute the magnitude of each complex element.
Definition std_complex.hpp:55
constexpr auto vec_as_container(const _Vp &v)
Generate a variable containing the iota sequence (i.e., 0, 1, 2, ... size-1) for the given simd::vec ...
Definition generic_impl.hpp:116
constexpr detail::deduced_mask_t< _Vp > isless(const _Vp &x, const _Vp &y)
Compare corresponding elements using less-than.
Definition std_math.hpp:360
constexpr detail::deduced_mask_t< _Vp > isgreaterequal(const _Vp &x, const _Vp &y)
Compare corresponding elements using greater-than-or-equal.
Definition std_math.hpp:353
constexpr detail::deduced_vec_t< _Vp > rint(const _Vp &v)
Round each element to an integer using the current rounding mode.
Definition std_math.hpp:200
constexpr detail::deduced_vec_t< _Vp > sqrt(const _Vp &v)
Compute the square root of each element.
Definition std_math.hpp:102
constexpr detail::deduced_mask_t< _Vp > isnormal(const _Vp &v)
Identify normal floating-point elements.
Definition std_math.hpp:312
constexpr detail::deduced_mask_t< _Vp > islessequal(const _Vp &x, const _Vp &y)
Compare corresponding elements using less-than-or-equal.
Definition std_math.hpp:367
constexpr detail::deduced_mask_t< _Vp > signbit(const _Vp &v)
Identify elements whose sign bit is set. This includes negative zero and negative values of infinity ...
Definition std_math.hpp:320
constexpr detail::deduced_vec_t< _Vp > fma(const _Vp &lhs, const _Vp &rhs, const _Vp &acc)
Compute (lhs * rhs) + acc element-wise with a single rounding.
Definition std_math.hpp:111
constexpr detail::deduced_mask_t< _Vp > isgreater(const _Vp &x, const _Vp &y)
Compare corresponding elements using greater-than.
Definition std_math.hpp:346
constexpr detail::deduced_mask_t< _Vp > isfinite(const _Vp &v)
Identify finite elements.
Definition std_math.hpp:305
constexpr detail::deduced_mask_t< _Vp > isunordered(const _Vp &x, const _Vp &y)
Identify unordered element pairs.
Definition std_math.hpp:381
constexpr detail::deduced_mask_t< _Vp > islessgreater(const _Vp &x, const _Vp &y)
Identify corresponding elements that compare less or greater.
Definition std_math.hpp:374
constexpr detail::deduced_mask_t< _Vp > isnan(const _Vp &v)
Identify elements representing NaN.
Definition std_math.hpp:298
constexpr detail::deduced_vec_t< _Vp > copysign(const _Vp &magnitude, const _Vp &sign)
Combine corresponding magnitudes and signs.
Definition std_math.hpp:64
constexpr detail::deduced_mask_t< _Vp > isinf(const _Vp &v)
Identify elements representing positive or negative infinity.
Definition std_math.hpp:291
constexpr detail::deduced_vec_t< _Vp > fmaddsub(const _Vp &lhs, const _Vp &rhs, const _Vp &acc)
Compute lhs * rhs - acc at even indexes and lhs * rhs + acc at odd indexes. Each expression is comput...
Definition std_math.hpp:136
_Vc floor(const _Vc &value)
Round the real and imaginary components of each element downward.
Definition std_complex.hpp:73
_Vc round(const _Vc &value)
Apply round independently to the real and imaginary components.
Definition std_complex.hpp:87
constexpr detail::deduced_vec_t< _Vp > rcp(const _Vp &v)
Compute the approximate reciprocal of each element.
Definition std_math.hpp:85
constexpr detail::deduced_vec_t< _Vp > fmsubadd(const _Vp &lhs, const _Vp &rhs, const _Vp &acc)
Compute lhs * rhs + acc at even indexes and lhs * rhs - acc at odd indexes. Each expression is comput...
Definition std_math.hpp:147
_Vc trunc(const _Vc &value)
Truncate the real and imaginary components of each element.
Definition std_complex.hpp:80
constexpr detail::deduced_vec_t< _Vp > rsqrt(const _Vp &v)
Compute the approximate reciprocal square root of each element.
Definition std_math.hpp:91
_Vc ceil(const _Vc &value)
Round the real and imaginary components of each element upward.
Definition std_complex.hpp:66
constexpr detail::deduced_mask_t< _Vp > isnormal(generic_tag, const _Vp &v)
Definition std_math.hpp:263
constexpr basic_vec< _Tp, _Abi > rsqrt(x86_sse_tag, const basic_vec< _Tp, _Abi > &v)
Constants which specify to the compiler intrinsic which FP classification operation to perform.
Definition x86_impl.hpp:1493
constexpr basic_vec< _Tp, _Abi > sqrt(x86_sse_tag, const basic_vec< _Tp, _Abi > &v)
Constants which specify to the compiler intrinsic which FP classification operation to perform.
Definition x86_impl.hpp:1544
constexpr detail::deduced_mask_t< _Vp > isinf(generic_tag tag, const _Vp &v)
Definition std_math.hpp:251
constexpr detail::deduced_mask_t< _Vp > signbit(generic_tag, const _Vp &v)
Definition std_math.hpp:272
constexpr basic_vec< _Tp, _Abi > rcp(x86_sse_tag, const basic_vec< _Tp, _Abi > &v)
Constants which specify to the compiler intrinsic which FP classification operation to perform.
Definition x86_impl.hpp:1443
constexpr deduced_mask_t< _Vp > isnan(generic_tag, const _Vp &v)
Definition std_math.hpp:228
constexpr basic_vec< _Tp, _Abi > round_op(x86_tag, const basic_vec< _Tp, _Abi > &v)
Rounding operations (ceil, floor, etc.).
Definition x86_impl.hpp:353
constexpr detail::deduced_mask_t< _Vp > isfinite(generic_tag tag, const _Vp &v)
Definition std_math.hpp:257
Definition target_config.hpp:23