$darkmode
Eigen  5.0.1-dev
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 
10 #ifndef EIGEN_MATH_FUNCTIONS_AVX_H
11 #define EIGEN_MATH_FUNCTIONS_AVX_H
12 
13 /* The sin and cos functions of this file are loosely derived from
14  * Julien Pommier's sse math library: http://gruntthepeon.free.fr/ssemath/
15  */
16 
17 // IWYU pragma: private
18 #include "../../InternalHeaderCheck.h"
19 
20 namespace Eigen {
21 
22 namespace internal {
23 
24 EIGEN_INSTANTIATE_GENERIC_MATH_FUNCS_FLOAT(Packet8f)
25 
26 EIGEN_DOUBLE_PACKET_FUNCTION(atanh, Packet4d)
27 EIGEN_DOUBLE_PACKET_FUNCTION(log, Packet4d)
28 EIGEN_DOUBLE_PACKET_FUNCTION(log2, Packet4d)
29 EIGEN_DOUBLE_PACKET_FUNCTION(exp, Packet4d)
30 EIGEN_DOUBLE_PACKET_FUNCTION(tanh, Packet4d)
31 EIGEN_DOUBLE_PACKET_FUNCTION(cbrt, Packet4d)
32 #ifdef EIGEN_VECTORIZE_AVX2
33 EIGEN_DOUBLE_PACKET_FUNCTION(sin, Packet4d)
34 EIGEN_DOUBLE_PACKET_FUNCTION(cos, Packet4d)
35 #endif
36 EIGEN_GENERIC_PACKET_FUNCTION(atan, Packet4d)
37 EIGEN_GENERIC_PACKET_FUNCTION(exp2, Packet4d)
38 
39 // Notice that for newer processors, it is counterproductive to use Newton
40 // iteration for square root. In particular, Skylake and Zen2 processors
41 // have approximately doubled throughput of the _mm_sqrt_ps instruction
42 // compared to their predecessors.
43 template <>
44 EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS Packet8f psqrt<Packet8f>(const Packet8f& _x) {
45  return _mm256_sqrt_ps(_x);
46 }
47 template <>
48 EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS Packet4d psqrt<Packet4d>(const Packet4d& _x) {
49  return _mm256_sqrt_pd(_x);
50 }
51 
52 // Even on Skylake, using Newton iteration is a win for reciprocal square root.
53 #if EIGEN_FAST_MATH
54 template <>
55 EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS Packet8f prsqrt<Packet8f>(const Packet8f& a) {
56  // _mm256_rsqrt_ps returns -inf for negative denormals.
57  // _mm512_rsqrt**_ps returns -NaN for negative denormals. We may want
58  // consistency here.
59  // const Packet8f rsqrt = pselect(pcmp_lt(a, pzero(a)),
60  // pset1<Packet8f>(-NumTraits<float>::quiet_NaN()),
61  // _mm256_rsqrt_ps(a));
62  return generic_rsqrt_newton_step<Packet8f, /*Steps=*/1>::run(a, _mm256_rsqrt_ps(a));
63 }
64 
65 template <>
66 EIGEN_STRONG_INLINE Packet8f preciprocal<Packet8f>(const Packet8f& a) {
67  return generic_reciprocal_newton_step<Packet8f, /*Steps=*/1>::run(a, _mm256_rcp_ps(a));
68 }
69 
70 #endif
71 
72 template <>
73 EIGEN_STRONG_INLINE Packet8h pfrexp(const Packet8h& a, Packet8h& exponent) {
74  Packet8f fexponent;
75  const Packet8h out = float2half(pfrexp<Packet8f>(half2float(a), fexponent));
76  exponent = float2half(fexponent);
77  return out;
78 }
79 
80 template <>
81 EIGEN_STRONG_INLINE Packet8h pldexp(const Packet8h& a, const Packet8h& exponent) {
82  return float2half(pldexp<Packet8f>(half2float(a), half2float(exponent)));
83 }
84 
85 template <>
86 EIGEN_STRONG_INLINE Packet8bf pfrexp(const Packet8bf& a, Packet8bf& exponent) {
87  Packet8f fexponent;
88  const Packet8bf out = F32ToBf16(pfrexp<Packet8f>(Bf16ToF32(a), fexponent));
89  exponent = F32ToBf16(fexponent);
90  return out;
91 }
92 
93 template <>
94 EIGEN_STRONG_INLINE Packet8bf pldexp(const Packet8bf& a, const Packet8bf& exponent) {
95  return F32ToBf16(pldexp<Packet8f>(Bf16ToF32(a), Bf16ToF32(exponent)));
96 }
97 
98 BF16_PACKET_FUNCTION(Packet8f, Packet8bf, pcos)
99 BF16_PACKET_FUNCTION(Packet8f, Packet8bf, pexp)
100 BF16_PACKET_FUNCTION(Packet8f, Packet8bf, pexp2)
101 BF16_PACKET_FUNCTION(Packet8f, Packet8bf, pexpm1)
102 BF16_PACKET_FUNCTION(Packet8f, Packet8bf, plog)
103 BF16_PACKET_FUNCTION(Packet8f, Packet8bf, plog1p)
104 BF16_PACKET_FUNCTION(Packet8f, Packet8bf, plog2)
105 BF16_PACKET_FUNCTION(Packet8f, Packet8bf, preciprocal)
106 BF16_PACKET_FUNCTION(Packet8f, Packet8bf, prsqrt)
107 BF16_PACKET_FUNCTION(Packet8f, Packet8bf, psin)
108 BF16_PACKET_FUNCTION(Packet8f, Packet8bf, psqrt)
109 BF16_PACKET_FUNCTION(Packet8f, Packet8bf, ptanh)
110 
111 #ifndef EIGEN_VECTORIZE_AVX512FP16
112 F16_PACKET_FUNCTION(Packet8f, Packet8h, pcos)
113 F16_PACKET_FUNCTION(Packet8f, Packet8h, pexp)
114 F16_PACKET_FUNCTION(Packet8f, Packet8h, pexp2)
115 F16_PACKET_FUNCTION(Packet8f, Packet8h, pexpm1)
116 F16_PACKET_FUNCTION(Packet8f, Packet8h, plog)
117 F16_PACKET_FUNCTION(Packet8f, Packet8h, plog1p)
118 F16_PACKET_FUNCTION(Packet8f, Packet8h, plog2)
119 F16_PACKET_FUNCTION(Packet8f, Packet8h, preciprocal)
120 F16_PACKET_FUNCTION(Packet8f, Packet8h, prsqrt)
121 F16_PACKET_FUNCTION(Packet8f, Packet8h, psin)
122 F16_PACKET_FUNCTION(Packet8f, Packet8h, psqrt)
123 F16_PACKET_FUNCTION(Packet8f, Packet8h, ptanh)
124 #endif
125 
126 } // end namespace internal
127 
128 } // end namespace Eigen
129 
130 #endif // EIGEN_MATH_FUNCTIONS_AVX_H
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_tanh_op< typename Derived::Scalar >, const Derived > tanh(const Eigen::ArrayBase< Derived > &x)
Namespace containing all symbols from the Eigen library.
Definition: B01_Experimental.dox:1
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_exp2_op< typename Derived::Scalar >, const Derived > exp2(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_cos_op< typename Derived::Scalar >, const Derived > cos(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_log_op< typename Derived::Scalar >, const Derived > log(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_atanh_op< typename Derived::Scalar >, const Derived > atanh(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_log2_op< typename Derived::Scalar >, const Derived > log2(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_atan_op< typename Derived::Scalar >, const Derived > atan(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_sin_op< typename Derived::Scalar >, const Derived > sin(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_exp_op< typename Derived::Scalar >, const Derived > exp(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_cbrt_op< typename Derived::Scalar >, const Derived > cbrt(const Eigen::ArrayBase< Derived > &x)