Eigen  5.0.1
 
Loading...
Searching...
No Matches
MathFunctions.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2014 Pedro Gonnet (pedro.gonnet@gmail.com)
5//
6// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_MATH_FUNCTIONS_AVX_H
12#define EIGEN_MATH_FUNCTIONS_AVX_H
13
14/* The sin and cos functions of this file are loosely derived from
15 * Julien Pommier's sse math library: http://gruntthepeon.free.fr/ssemath/
16 */
17
18// IWYU pragma: private
19#include "../../InternalHeaderCheck.h"
20
21namespace Eigen {
22
23namespace internal {
24
25EIGEN_INSTANTIATE_GENERIC_MATH_FUNCS_FLOAT(Packet8f)
26
27EIGEN_DOUBLE_PACKET_FUNCTION(atanh, Packet4d)
28EIGEN_DOUBLE_PACKET_FUNCTION(sinh, Packet4d)
29EIGEN_DOUBLE_PACKET_FUNCTION(cosh, Packet4d)
30EIGEN_DOUBLE_PACKET_FUNCTION(asinh, Packet4d)
31EIGEN_DOUBLE_PACKET_FUNCTION(acosh, Packet4d)
32EIGEN_DOUBLE_PACKET_FUNCTION(log, Packet4d)
33EIGEN_DOUBLE_PACKET_FUNCTION(log10, Packet4d)
34EIGEN_DOUBLE_PACKET_FUNCTION(exp, Packet4d)
35EIGEN_DOUBLE_PACKET_FUNCTION(log2, Packet4d)
36EIGEN_DOUBLE_PACKET_FUNCTION(tanh, Packet4d)
37EIGEN_DOUBLE_PACKET_FUNCTION(cbrt, Packet4d)
38#ifdef EIGEN_VECTORIZE_AVX2
39EIGEN_DOUBLE_PACKET_FUNCTION(sin, Packet4d)
40EIGEN_DOUBLE_PACKET_FUNCTION(cos, Packet4d)
41EIGEN_DOUBLE_PACKET_FUNCTION(tan, Packet4d)
42#else
43// Without AVX2, psincos_double<Packet4d> requires 256-bit integer operations (Packet4l)
44// that are not available. Process as two Packet2d halves using the SSE implementation.
45template <>
46EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS Packet4d psin<Packet4d>(const Packet4d& x) {
47 return _mm256_insertf128_pd(_mm256_castpd128_pd256(psin(_mm256_castpd256_pd128(x))),
48 psin(_mm256_extractf128_pd(x, 1)), 1);
49}
50template <>
51EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS Packet4d pcos<Packet4d>(const Packet4d& x) {
52 return _mm256_insertf128_pd(_mm256_castpd128_pd256(pcos(_mm256_castpd256_pd128(x))),
53 pcos(_mm256_extractf128_pd(x, 1)), 1);
54}
55template <>
56EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS Packet4d ptan<Packet4d>(const Packet4d& x) {
57 return _mm256_insertf128_pd(_mm256_castpd128_pd256(ptan(_mm256_castpd256_pd128(x))),
58 ptan(_mm256_extractf128_pd(x, 1)), 1);
59}
60
61// AVX-only Packet4d has no integer packet. Classify the bits using the SSE halves
62// so subnormals survive DAZ and NaNs survive fast-math.
63template <>
64EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS Packet4d psign<Packet4d>(const Packet4d& x) {
65 return _mm256_insertf128_pd(_mm256_castpd128_pd256(psign(_mm256_castpd256_pd128(x))),
66 psign(_mm256_extractf128_pd(x, 1)), 1);
67}
68#endif
69EIGEN_GENERIC_PACKET_FUNCTION(atan, Packet4d)
70EIGEN_GENERIC_PACKET_FUNCTION(exp2, Packet4d)
71EIGEN_GENERIC_PACKET_FUNCTION(expm1, Packet4d)
72EIGEN_DOUBLE_PACKET_FUNCTION(log1p, Packet4d)
73
74// Notice that for newer processors, it is counterproductive to use Newton
75// iteration for square root. In particular, Skylake and Zen2 processors
76// have approximately doubled throughput of the _mm_sqrt_ps instruction
77// compared to their predecessors.
78template <>
79EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS Packet8f psqrt<Packet8f>(const Packet8f& _x) {
80 return _mm256_sqrt_ps(_x);
81}
82template <>
83EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS Packet4d psqrt<Packet4d>(const Packet4d& _x) {
84 return _mm256_sqrt_pd(_x);
85}
86
87// Even on Skylake, using Newton iteration is a win for reciprocal square root.
88#if EIGEN_FAST_MATH
89template <>
90EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS Packet8f prsqrt<Packet8f>(const Packet8f& a) {
91 // _mm256_rsqrt_ps returns -inf for negative denormals.
92 // _mm512_rsqrt**_ps returns -NaN for negative denormals. We may want
93 // consistency here.
94 // const Packet8f rsqrt = pselect(pcmp_lt(a, pzero(a)),
95 // pset1<Packet8f>(-NumTraits<float>::quiet_NaN()),
96 // _mm256_rsqrt_ps(a));
97 return generic_rsqrt_newton_step<Packet8f, /*Steps=*/1>::run(a, _mm256_rsqrt_ps(a));
98}
99
100template <>
101EIGEN_STRONG_INLINE Packet8f preciprocal<Packet8f>(const Packet8f& a) {
102 // generic_reciprocal_newton_step, with its NaN-or-zero test as a single compare: r == 0 or unordered.
103 const Packet8f one = pset1<Packet8f>(1.0f);
104 const Packet8f x = _mm256_rcp_ps(a);
105 const Packet8f refined = pmadd(x, pnmadd(a, x, one), x);
106 const Packet8f redo = _mm256_cmp_ps(refined, _mm256_setzero_ps(), _CMP_EQ_UQ);
107 return predux_any(redo) ? pselect(redo, pdiv(one, a), refined) : refined;
108}
109
110#endif
111
112template <>
113EIGEN_STRONG_INLINE Packet8h pfrexp(const Packet8h& a, Packet8h& exponent) {
114 Packet8f fexponent;
115 const Packet8h out = float2half(pfrexp<Packet8f>(half2float(a), fexponent));
116 exponent = float2half(fexponent);
117 return out;
118}
119
120template <>
121EIGEN_STRONG_INLINE Packet8h pldexp(const Packet8h& a, const Packet8h& exponent) {
122 return float2half(pldexp<Packet8f>(half2float(a), half2float(exponent)));
123}
124
125template <>
126EIGEN_STRONG_INLINE Packet8bf pfrexp(const Packet8bf& a, Packet8bf& exponent) {
127 // Both results are exact: the mantissa keeps the input's significand, and the exponent is a
128 // small integer.
129 Packet8f fexponent;
130 const Packet8bf out = F32ToBf16Truncate(pfrexp<Packet8f>(Bf16ToF32(a), fexponent));
131 exponent = F32ToBf16Truncate(fexponent);
132 return out;
133}
134
135template <>
136EIGEN_STRONG_INLINE Packet8bf pldexp(const Packet8bf& a, const Packet8bf& exponent) {
137 return F32ToBf16(pldexp<Packet8f>(Bf16ToF32(a), Bf16ToF32(exponent)));
138}
139
140EIGEN_INSTANTIATE_GENERIC_MATH_FUNCS_BF16(Packet8f, Packet8bf)
141
142#ifndef EIGEN_VECTORIZE_AVX512FP16
143EIGEN_INSTANTIATE_GENERIC_MATH_FUNCS_F16(Packet8f, Packet8h)
144#endif
145
146} // end namespace internal
147
148} // end namespace Eigen
149
150#endif // EIGEN_MATH_FUNCTIONS_AVX_H