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