Add signbit function

This commit is contained in:
Charles Schlosser
2022-11-04 00:31:20 +00:00
committed by Rasmus Munk Larsen
parent 8f8e36458f
commit 82b152dbe7
12 changed files with 332 additions and 9 deletions

View File

@@ -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) {