mirror of
https://gitlab.com/libeigen/eigen.git
synced 2026-04-10 11:34:33 +08:00
Add signbit function
This commit is contained in:
committed by
Rasmus Munk Larsen
parent
8f8e36458f
commit
82b152dbe7
@@ -1531,6 +1531,37 @@ double abs(const std::complex<double>& x) {
|
||||
}
|
||||
#endif
|
||||
|
||||
template <typename Scalar, bool IsInteger = NumTraits<Scalar>::IsInteger, bool IsSigned = NumTraits<Scalar>::IsSigned>
|
||||
struct signbit_impl;
|
||||
template <typename Scalar>
|
||||
struct signbit_impl<Scalar, false, true> {
|
||||
static constexpr size_t Size = sizeof(Scalar);
|
||||
static constexpr size_t Shift = (CHAR_BIT * Size) - 1;
|
||||
using intSize_t = typename get_integer_by_size<Size>::signed_type;
|
||||
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE static Scalar run(const Scalar& x) {
|
||||
intSize_t a = bit_cast<intSize_t, Scalar>(x);
|
||||
a = a >> Shift;
|
||||
Scalar result = bit_cast<Scalar, intSize_t>(a);
|
||||
return result;
|
||||
}
|
||||
};
|
||||
template <typename Scalar>
|
||||
struct signbit_impl<Scalar, true, true> {
|
||||
static constexpr size_t Size = sizeof(Scalar);
|
||||
static constexpr size_t Shift = (CHAR_BIT * Size) - 1;
|
||||
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE static constexpr Scalar run(const Scalar& x) { return x >> Shift; }
|
||||
};
|
||||
template <typename Scalar>
|
||||
struct signbit_impl<Scalar, true, false> {
|
||||
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE static constexpr Scalar run(const Scalar& ) {
|
||||
return Scalar(0);
|
||||
}
|
||||
};
|
||||
template <typename Scalar>
|
||||
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE static constexpr Scalar signbit(const Scalar& x) {
|
||||
return signbit_impl<Scalar>::run(x);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
|
||||
T exp(const T &x) {
|
||||
|
||||
Reference in New Issue
Block a user