blob: 65dc758b66eaf50459fee0ade2bc38b8fba04892 [file]
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2014 Pedro Gonnet (pedro.gonnet@gmail.com)
//
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
// SPDX-License-Identifier: MPL-2.0
#ifndef EIGEN_MATH_FUNCTIONS_AVX_H
#define EIGEN_MATH_FUNCTIONS_AVX_H
/* The sin and cos functions of this file are loosely derived from
* Julien Pommier's sse math library: http://gruntthepeon.free.fr/ssemath/
*/
// IWYU pragma: private
#include "../../InternalHeaderCheck.h"
namespace Eigen {
namespace internal {
EIGEN_INSTANTIATE_GENERIC_MATH_FUNCS_FLOAT(Packet8f)
EIGEN_DOUBLE_PACKET_FUNCTION(atanh, Packet4d)
EIGEN_DOUBLE_PACKET_FUNCTION(sinh, Packet4d)
EIGEN_DOUBLE_PACKET_FUNCTION(cosh, Packet4d)
EIGEN_DOUBLE_PACKET_FUNCTION(asinh, Packet4d)
EIGEN_DOUBLE_PACKET_FUNCTION(acosh, Packet4d)
EIGEN_DOUBLE_PACKET_FUNCTION(log, Packet4d)
EIGEN_DOUBLE_PACKET_FUNCTION(log10, Packet4d)
EIGEN_DOUBLE_PACKET_FUNCTION(exp, Packet4d)
EIGEN_DOUBLE_PACKET_FUNCTION(log2, Packet4d)
EIGEN_DOUBLE_PACKET_FUNCTION(tanh, Packet4d)
#if defined(EIGEN_VECTORIZE_AVX2) && defined(EIGEN_VECTORIZE_FMA)
// Division-free binary64 cbrt in round-to-nearest. A subdivided Bernstein bound on the seed defect
// 1 - m*P(m)^3 and forward binary64 error bounds for the fused update and reconstruction give an absolute
// error below 9.180e-31 for the real sum q + low on the reduced interval before the final rounding.
template <>
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS Packet4d pcbrt<Packet4d>(const Packet4d& x) {
const __m256i bits = _mm256_castpd_si256(x);
const __m256i sign = _mm256_and_si256(bits, _mm256_set1_epi64x(INT64_MIN));
const __m256i magnitude = _mm256_xor_si256(bits, sign);
const __m256i biased = _mm256_srli_epi64(magnitude, 52);
const __m256i zero = _mm256_setzero_si256();
const __m256i is_zero = _mm256_cmpeq_epi64(magnitude, zero);
const __m256i is_subnormal = _mm256_andnot_si256(is_zero, _mm256_cmpeq_epi64(biased, zero));
const __m256i special = _mm256_or_si256(is_zero, _mm256_cmpeq_epi64(biased, _mm256_set1_epi64x(2047)));
const __m256i fraction_mask = _mm256_set1_epi64x(0x000fffffffffffffLL);
__m256i reduced = magnitude;
__m256i e = _mm256_add_epi64(biased, _mm256_set1_epi64x(2050));
if (_mm256_movemask_pd(_mm256_castsi256_pd(is_subnormal))) {
// For a subnormal with fraction F, (0.25 + F*2^-54) - 0.25 is exact and equals |x|*2^1020.
// All nonzero operands and results of this subtraction are normal, even with DAZ/FTZ enabled.
const __m256i encoded =
_mm256_or_si256(_mm256_and_si256(magnitude, fraction_mask), _mm256_set1_epi64x(0x3fd0000000000000LL));
const __m256i normalized = _mm256_castpd_si256(psub(_mm256_castsi256_pd(encoded), pset1<Packet4d>(0.25)));
reduced = _mm256_blendv_epi8(magnitude, normalized, is_subnormal);
const __m256i e_tiny = _mm256_add_epi64(_mm256_srli_epi64(normalized, 52), _mm256_set1_epi64x(1030));
e = _mm256_blendv_epi8(e, e_tiny, is_subnormal);
}
// For finite nonzero lanes, |x| = m * 2^(3*(k - 1024) + j), with 0.5 <= m < 1 and j in {0,1,2}.
// For the nonnegative biased exponent e, the unsigned multiply and shift give floor(e / 3).
const __m256i k = _mm256_srli_epi64(_mm256_mul_epu32(e, _mm256_set1_epi64x(0xaaaaaaabLL)), 33);
const __m256i j = _mm256_sub_epi64(e, _mm256_add_epi64(k, _mm256_slli_epi64(k, 1)));
const __m256i fraction = _mm256_and_si256(reduced, fraction_mask);
const __m256i m_bits = _mm256_or_si256(fraction, _mm256_set1_epi64x(0x3fe0000000000000LL));
const Packet4d m = _mm256_castsi256_pd(m_bits);
const Packet4d a = _mm256_castsi256_pd(_mm256_add_epi64(m_bits, _mm256_slli_epi64(j, 52)));
const Packet4d one = pset1<Packet4d>(1.0);
const Packet4d j_is_one = _mm256_castsi256_pd(_mm256_cmpeq_epi64(j, _mm256_set1_epi64x(1)));
const Packet4d j_is_two = _mm256_castsi256_pd(_mm256_cmpeq_epi64(j, _mm256_set1_epi64x(2)));
const Packet4d scale = pselect(j_is_two, pset1<Packet4d>(0.6299605249474366),
pselect(j_is_one, pset1<Packet4d>(0.79370052598409979), one));
// Estrin evaluation of a degree-six relative Remez fit to m^(-1/3) on [0.5, 1].
// python3 scripts/generate_cbrt_seed.py reproduces the binary64 coefficients using 75 decimal digits.
constexpr double c[] = {2.3683759611198685, -5.0028369232691947, 9.9633714496342254, -12.834623865684151,
10.119441486803904, -4.4513957707001701, 0.83766855496838499};
const Packet4d m2 = pmul(m, m);
const Packet4d p0 = pmadd(m, pset1<Packet4d>(c[1]), pset1<Packet4d>(c[0]));
const Packet4d p1 = pmadd(m, pset1<Packet4d>(c[3]), pset1<Packet4d>(c[2]));
const Packet4d p2 = pmadd(m, pset1<Packet4d>(c[5]), pset1<Packet4d>(c[4]));
const Packet4d y0 = pmul(scale, pmadd(pmul(m2, m2), pmadd(m2, pset1<Packet4d>(c[6]), p2), pmadd(m2, p1, p0)));
// Refine y0 from the defect 1 - a*y0^3 using its quadratic weight in the inverse cube root update.
const Packet4d defect = pnmadd(pmul(a, y0), pmul(y0, y0), one);
const Packet4d weight = pmadd(defect, pset1<Packet4d>(2.0 / 9.0), pset1<Packet4d>(1.0 / 3.0));
const Packet4d y = pmadd(pmul(y0, defect), weight, y0);
// Form a*y*y as q + q_lo, and use the compensated defect to correct the root.
const Packet4d p = pmul(a, y);
const Packet4d q = pmul(p, y);
const Packet4d q_lo = pmadd(pmsub(a, y, p), y, pmsub(p, y, q));
const Packet4d r = pnmadd(q_lo, y, pnmadd(q, y, one));
// Three binary64 steps above rounded 2/3, selected by screening rounding mismatches on hard inputs.
const Packet4d low = pmadd(pmul(q, pset1<Packet4d>(0.66666666666666696)), r, q_lo);
// The restored root is normal. Adjusting its exponent and sign is exact.
const __m256i delta = _mm256_slli_epi64(_mm256_sub_epi64(k, _mm256_set1_epi64x(1024)), 52);
const __m256i scaled = _mm256_add_epi64(_mm256_castpd_si256(padd(q, low)), delta);
const Packet4d root = _mm256_castsi256_pd(_mm256_or_si256(scaled, sign));
const Packet4d exceptional = _mm256_castsi256_pd(_mm256_and_si256(special, bits));
return pselect(_mm256_castsi256_pd(special), padd(exceptional, exceptional), root);
}
#else
EIGEN_DOUBLE_PACKET_FUNCTION(cbrt, Packet4d)
#endif
#ifdef EIGEN_VECTORIZE_AVX2
EIGEN_DOUBLE_PACKET_FUNCTION(sin, Packet4d)
EIGEN_DOUBLE_PACKET_FUNCTION(cos, Packet4d)
EIGEN_DOUBLE_PACKET_FUNCTION(tan, Packet4d)
#else
// Without AVX2, psincos_double<Packet4d> requires 256-bit integer operations (Packet4l)
// that are not available. Process as two Packet2d halves using the SSE implementation.
template <>
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS Packet4d psin<Packet4d>(const Packet4d& x) {
return _mm256_insertf128_pd(_mm256_castpd128_pd256(psin(_mm256_castpd256_pd128(x))),
psin(_mm256_extractf128_pd(x, 1)), 1);
}
template <>
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS Packet4d pcos<Packet4d>(const Packet4d& x) {
return _mm256_insertf128_pd(_mm256_castpd128_pd256(pcos(_mm256_castpd256_pd128(x))),
pcos(_mm256_extractf128_pd(x, 1)), 1);
}
template <>
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS Packet4d ptan<Packet4d>(const Packet4d& x) {
return _mm256_insertf128_pd(_mm256_castpd128_pd256(ptan(_mm256_castpd256_pd128(x))),
ptan(_mm256_extractf128_pd(x, 1)), 1);
}
// AVX-only Packet4d has no integer packet. Classify the bits using the SSE halves
// so subnormals survive DAZ and NaNs survive fast-math.
template <>
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS Packet4d psign<Packet4d>(const Packet4d& x) {
return _mm256_insertf128_pd(_mm256_castpd128_pd256(psign(_mm256_castpd256_pd128(x))),
psign(_mm256_extractf128_pd(x, 1)), 1);
}
#endif
EIGEN_GENERIC_PACKET_FUNCTION(atan, Packet4d)
EIGEN_GENERIC_PACKET_FUNCTION(exp2, Packet4d)
EIGEN_GENERIC_PACKET_FUNCTION(expm1, Packet4d)
EIGEN_DOUBLE_PACKET_FUNCTION(log1p, Packet4d)
// Notice that for newer processors, it is counterproductive to use Newton
// iteration for square root. In particular, Skylake and Zen2 processors
// have approximately doubled throughput of the _mm_sqrt_ps instruction
// compared to their predecessors.
template <>
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS Packet8f psqrt<Packet8f>(const Packet8f& _x) {
return _mm256_sqrt_ps(_x);
}
template <>
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS Packet4d psqrt<Packet4d>(const Packet4d& _x) {
return _mm256_sqrt_pd(_x);
}
// Even on Skylake, using Newton iteration is a win for reciprocal square root.
#if EIGEN_FAST_MATH
template <>
EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS Packet8f prsqrt<Packet8f>(const Packet8f& a) {
// vrsqrtps reads a subnormal a as zero, giving e = +-inf. a * e, the Newton step's first product, is therefore +inf
// for subnormal a, and finite or NaN for every other a; those lanes take 1 / sqrt(a) instead. Under DAZ the multiply
// reads a subnormal as zero too, and the estimate's +-inf is the DAZ result.
const Packet8f e = _mm256_rsqrt_ps(a);
const Packet8f r = generic_rsqrt_newton_step<Packet8f, /*Steps=*/1>::run(a, e);
const Packet8f redo = pcmp_eq(pmul(a, e), pinf<Packet8f>());
return predux_any(redo) ? pselect(redo, pdiv(pset1<Packet8f>(1.0f), psqrt(a)), r) : r;
}
template <>
EIGEN_STRONG_INLINE Packet8f preciprocal<Packet8f>(const Packet8f& a) {
// generic_reciprocal_newton_step, with its NaN-or-zero test as a single compare: r == 0 or unordered.
const Packet8f one = pset1<Packet8f>(1.0f);
const Packet8f x = _mm256_rcp_ps(a);
const Packet8f refined = pmadd(x, pnmadd(a, x, one), x);
const Packet8f redo = _mm256_cmp_ps(refined, _mm256_setzero_ps(), _CMP_EQ_UQ);
return predux_any(redo) ? pselect(redo, pdiv(one, a), refined) : refined;
}
#endif
template <>
EIGEN_STRONG_INLINE Packet8h pfrexp(const Packet8h& a, Packet8h& exponent) {
Packet8f fexponent;
const Packet8h out = float2half(pfrexp<Packet8f>(half2float(a), fexponent));
exponent = float2half(fexponent);
return out;
}
template <>
EIGEN_STRONG_INLINE Packet8h pldexp(const Packet8h& a, const Packet8h& exponent) {
return float2half(pldexp<Packet8f>(half2float(a), half2float(exponent)));
}
template <>
EIGEN_STRONG_INLINE Packet8bf pfrexp(const Packet8bf& a, Packet8bf& exponent) {
// Both results are exact: the mantissa keeps the input's significand, and the exponent is a
// small integer.
Packet8f fexponent;
const Packet8bf out = F32ToBf16Truncate(pfrexp<Packet8f>(Bf16ToF32(a), fexponent));
exponent = F32ToBf16Truncate(fexponent);
return out;
}
template <>
EIGEN_STRONG_INLINE Packet8bf pldexp(const Packet8bf& a, const Packet8bf& exponent) {
return F32ToBf16(pldexp<Packet8f>(Bf16ToF32(a), Bf16ToF32(exponent)));
}
EIGEN_INSTANTIATE_GENERIC_MATH_FUNCS_BF16(Packet8f, Packet8bf)
EIGEN_INSTANTIATE_GENERIC_SQRT_FUNCS_BF16(Packet8f, Packet8bf)
#ifndef EIGEN_VECTORIZE_AVX512FP16
EIGEN_INSTANTIATE_GENERIC_MATH_FUNCS_F16(Packet8f, Packet8h)
#endif
} // end namespace internal
} // end namespace Eigen
#endif // EIGEN_MATH_FUNCTIONS_AVX_H