mirror of
https://gitlab.com/libeigen/eigen.git
synced 2026-04-10 11:34:33 +08:00
Replaces `std::sqrt` with `complex_sqrt` for all platforms (previously `complex_sqrt` was only used for CUDA and MSVC), and implements custom `complex_rsqrt`. Also introduces `numext::rsqrt` to simplify implementation, and modified `numext::hypot` to adhere to IEEE IEC 6059 for special cases. The `complex_sqrt` and `complex_rsqrt` implementations were found to be significantly faster than `std::sqrt<std::complex<T>>` and `1/numext::sqrt<std::complex<T>>`. Benchmark file attached. ``` GCC 10, Intel Xeon, x86_64: --------------------------------------------------------------------------- Benchmark Time CPU Iterations --------------------------------------------------------------------------- BM_Sqrt<std::complex<float>> 9.21 ns 9.21 ns 73225448 BM_StdSqrt<std::complex<float>> 17.1 ns 17.1 ns 40966545 BM_Sqrt<std::complex<double>> 8.53 ns 8.53 ns 81111062 BM_StdSqrt<std::complex<double>> 21.5 ns 21.5 ns 32757248 BM_Rsqrt<std::complex<float>> 10.3 ns 10.3 ns 68047474 BM_DivSqrt<std::complex<float>> 16.3 ns 16.3 ns 42770127 BM_Rsqrt<std::complex<double>> 11.3 ns 11.3 ns 61322028 BM_DivSqrt<std::complex<double>> 16.5 ns 16.5 ns 42200711 Clang 11, Intel Xeon, x86_64: --------------------------------------------------------------------------- Benchmark Time CPU Iterations --------------------------------------------------------------------------- BM_Sqrt<std::complex<float>> 7.46 ns 7.45 ns 90742042 BM_StdSqrt<std::complex<float>> 16.6 ns 16.6 ns 42369878 BM_Sqrt<std::complex<double>> 8.49 ns 8.49 ns 81629030 BM_StdSqrt<std::complex<double>> 21.8 ns 21.7 ns 31809588 BM_Rsqrt<std::complex<float>> 8.39 ns 8.39 ns 82933666 BM_DivSqrt<std::complex<float>> 14.4 ns 14.4 ns 48638676 BM_Rsqrt<std::complex<double>> 9.83 ns 9.82 ns 70068956 BM_DivSqrt<std::complex<double>> 15.7 ns 15.7 ns 44487798 Clang 9, Pixel 2, aarch64: --------------------------------------------------------------------------- Benchmark Time CPU Iterations --------------------------------------------------------------------------- BM_Sqrt<std::complex<float>> 24.2 ns 24.1 ns 28616031 BM_StdSqrt<std::complex<float>> 104 ns 103 ns 6826926 BM_Sqrt<std::complex<double>> 31.8 ns 31.8 ns 22157591 BM_StdSqrt<std::complex<double>> 128 ns 128 ns 5437375 BM_Rsqrt<std::complex<float>> 31.9 ns 31.8 ns 22384383 BM_DivSqrt<std::complex<float>> 99.2 ns 98.9 ns 7250438 BM_Rsqrt<std::complex<double>> 46.0 ns 45.8 ns 15338689 BM_DivSqrt<std::complex<double>> 119 ns 119 ns 5898944 ```
103 lines
4.1 KiB
C++
103 lines
4.1 KiB
C++
// This file is part of Eigen, a lightweight C++ template library
|
|
// for linear algebra.
|
|
//
|
|
// Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@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/.
|
|
|
|
#ifndef EIGEN_COMPLEX_CUDA_H
|
|
#define EIGEN_COMPLEX_CUDA_H
|
|
|
|
// clang-format off
|
|
|
|
#if defined(EIGEN_CUDACC) && defined(EIGEN_GPU_COMPILE_PHASE)
|
|
|
|
namespace Eigen {
|
|
|
|
namespace internal {
|
|
|
|
// Many std::complex methods such as operator+, operator-, operator* and
|
|
// operator/ are not constexpr. Due to this, clang does not treat them as device
|
|
// functions and thus Eigen functors making use of these operators fail to
|
|
// compile. Here, we manually specialize these functors for complex types when
|
|
// building for CUDA to avoid non-constexpr methods.
|
|
|
|
// Sum
|
|
template<typename T> struct scalar_sum_op<const std::complex<T>, const std::complex<T> > : binary_op_base<const std::complex<T>, const std::complex<T> > {
|
|
typedef typename std::complex<T> result_type;
|
|
|
|
EIGEN_EMPTY_STRUCT_CTOR(scalar_sum_op)
|
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::complex<T> operator() (const std::complex<T>& a, const std::complex<T>& b) const {
|
|
return std::complex<T>(numext::real(a) + numext::real(b),
|
|
numext::imag(a) + numext::imag(b));
|
|
}
|
|
};
|
|
|
|
template<typename T> struct scalar_sum_op<std::complex<T>, std::complex<T> > : scalar_sum_op<const std::complex<T>, const std::complex<T> > {};
|
|
|
|
|
|
// Difference
|
|
template<typename T> struct scalar_difference_op<const std::complex<T>, const std::complex<T> > : binary_op_base<const std::complex<T>, const std::complex<T> > {
|
|
typedef typename std::complex<T> result_type;
|
|
|
|
EIGEN_EMPTY_STRUCT_CTOR(scalar_difference_op)
|
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::complex<T> operator() (const std::complex<T>& a, const std::complex<T>& b) const {
|
|
return std::complex<T>(numext::real(a) - numext::real(b),
|
|
numext::imag(a) - numext::imag(b));
|
|
}
|
|
};
|
|
|
|
template<typename T> struct scalar_difference_op<std::complex<T>, std::complex<T> > : scalar_difference_op<const std::complex<T>, const std::complex<T> > {};
|
|
|
|
|
|
// Product
|
|
template<typename T> struct scalar_product_op<const std::complex<T>, const std::complex<T> > : binary_op_base<const std::complex<T>, const std::complex<T> > {
|
|
enum {
|
|
Vectorizable = packet_traits<std::complex<T> >::HasMul
|
|
};
|
|
typedef typename std::complex<T> result_type;
|
|
|
|
EIGEN_EMPTY_STRUCT_CTOR(scalar_product_op)
|
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::complex<T> operator() (const std::complex<T>& a, const std::complex<T>& b) const {
|
|
const T a_real = numext::real(a);
|
|
const T a_imag = numext::imag(a);
|
|
const T b_real = numext::real(b);
|
|
const T b_imag = numext::imag(b);
|
|
return std::complex<T>(a_real * b_real - a_imag * b_imag,
|
|
a_real * b_imag + a_imag * b_real);
|
|
}
|
|
};
|
|
|
|
template<typename T> struct scalar_product_op<std::complex<T>, std::complex<T> > : scalar_product_op<const std::complex<T>, const std::complex<T> > {};
|
|
|
|
|
|
// Quotient
|
|
template<typename T> struct scalar_quotient_op<const std::complex<T>, const std::complex<T> > : binary_op_base<const std::complex<T>, const std::complex<T> > {
|
|
enum {
|
|
Vectorizable = packet_traits<std::complex<T> >::HasDiv
|
|
};
|
|
typedef typename std::complex<T> result_type;
|
|
|
|
EIGEN_EMPTY_STRUCT_CTOR(scalar_quotient_op)
|
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::complex<T> operator() (const std::complex<T>& a, const std::complex<T>& b) const {
|
|
const T a_real = numext::real(a);
|
|
const T a_imag = numext::imag(a);
|
|
const T b_real = numext::real(b);
|
|
const T b_imag = numext::imag(b);
|
|
const T norm = T(1) / (b_real * b_real + b_imag * b_imag);
|
|
return std::complex<T>((a_real * b_real + a_imag * b_imag) * norm,
|
|
(a_imag * b_real - a_real * b_imag) * norm);
|
|
}
|
|
};
|
|
|
|
template<typename T> struct scalar_quotient_op<std::complex<T>, std::complex<T> > : scalar_quotient_op<const std::complex<T>, const std::complex<T> > {};
|
|
|
|
} // namespace internal
|
|
} // namespace Eigen
|
|
|
|
#endif
|
|
|
|
#endif // EIGEN_COMPLEX_CUDA_H
|