From 13ccdfde291683c011ae043ccf8795c92d5a3a81 Mon Sep 17 00:00:00 2001 From: Marcel Koch Date: Tue, 15 Oct 2024 07:38:32 +0000 Subject: [PATCH 1/2] [core] use only `constexpr` for math functions --- include/ginkgo/core/base/math.hpp | 172 ++++-------------------------- 1 file changed, 22 insertions(+), 150 deletions(-) diff --git a/include/ginkgo/core/base/math.hpp b/include/ginkgo/core/base/math.hpp index f6847743717..33b3a566b37 100644 --- a/include/ginkgo/core/base/math.hpp +++ b/include/ginkgo/core/base/math.hpp @@ -283,7 +283,7 @@ using is_complex_s = detail::is_complex_impl; * @return `true` if T is a complex type, `false` otherwise */ template -GKO_INLINE GKO_ATTRIBUTES constexpr bool is_complex() +GKO_INLINE constexpr bool is_complex() { return detail::is_complex_impl::value; } @@ -307,7 +307,7 @@ using is_complex_or_scalar_s = detail::is_complex_or_scalar_impl; * @return `true` if T is a complex/scalar type, `false` otherwise */ template -GKO_INLINE GKO_ATTRIBUTES constexpr bool is_complex_or_scalar() +GKO_INLINE constexpr bool is_complex_or_scalar() { return detail::is_complex_or_scalar_impl::value; } @@ -511,7 +511,7 @@ using highest_precision = * @return the rounded down value */ template -GKO_INLINE GKO_ATTRIBUTES constexpr reduce_precision round_down(T val) +GKO_INLINE constexpr reduce_precision round_down(T val) { return static_cast>(val); } @@ -527,7 +527,7 @@ GKO_INLINE GKO_ATTRIBUTES constexpr reduce_precision round_down(T val) * @return the rounded up value */ template -GKO_INLINE GKO_ATTRIBUTES constexpr increase_precision round_up(T val) +GKO_INLINE constexpr increase_precision round_up(T val) { return static_cast>(val); } @@ -609,141 +609,19 @@ struct default_converter { * * @return returns the ceiled quotient. */ -GKO_INLINE GKO_ATTRIBUTES constexpr int64 ceildiv(int64 num, int64 den) +GKO_INLINE constexpr int64 ceildiv(int64 num, int64 den) { return (num + den - 1) / den; } -#if defined(__HIPCC__) && GINKGO_HIP_PLATFORM_HCC - - -/** - * Returns the additive identity for T. - * - * @return additive identity for T - */ -template -GKO_INLINE __host__ constexpr T zero() -{ - return T{}; -} - - -/** - * Returns the additive identity for T. - * - * @return additive identity for T - * - * @note This version takes an unused reference argument to avoid - * complicated calls like `zero()`. Instead, it allows - * `zero(x)`. - */ -template -GKO_INLINE __host__ constexpr T zero(const T&) -{ - return zero(); -} - - -/** - * Returns the multiplicative identity for T. - * - * @return the multiplicative identity for T - */ -template -GKO_INLINE __host__ constexpr T one() -{ - return T(1); -} - - -/** - * Returns the multiplicative identity for T. - * - * @return the multiplicative identity for T - * - * @note This version takes an unused reference argument to avoid - * complicated calls like `one()`. Instead, it allows - * `one(x)`. - */ -template -GKO_INLINE __host__ constexpr T one(const T&) -{ - return one(); -} - - -/** - * Returns the additive identity for T. - * - * @return additive identity for T - */ -template -GKO_INLINE __device__ constexpr std::enable_if_t< - !std::is_same>>::value, T> -zero() -{ - return T{}; -} - - -/** - * Returns the additive identity for T. - * - * @return additive identity for T - * - * @note This version takes an unused reference argument to avoid - * complicated calls like `zero()`. Instead, it allows - * `zero(x)`. - */ -template -GKO_INLINE __device__ constexpr T zero(const T&) -{ - return zero(); -} - - -/** - * Returns the multiplicative identity for T. - * - * @return the multiplicative identity for T - */ -template -GKO_INLINE __device__ constexpr std::enable_if_t< - !std::is_same>>::value, T> -one() -{ - return T(1); -} - - -/** - * Returns the multiplicative identity for T. - * - * @return the multiplicative identity for T - * - * @note This version takes an unused reference argument to avoid - * complicated calls like `one()`. Instead, it allows - * `one(x)`. - */ -template -GKO_INLINE __device__ constexpr T one(const T&) -{ - return one(); -} - - -#else - - /** * Returns the additive identity for T. * * @return additive identity for T */ template -GKO_INLINE GKO_ATTRIBUTES constexpr T zero() +GKO_INLINE constexpr T zero() { return T{}; } @@ -759,7 +637,7 @@ GKO_INLINE GKO_ATTRIBUTES constexpr T zero() * `zero(x)`. */ template -GKO_INLINE GKO_ATTRIBUTES constexpr T zero(const T&) +GKO_INLINE constexpr T zero(const T&) { return zero(); } @@ -771,7 +649,7 @@ GKO_INLINE GKO_ATTRIBUTES constexpr T zero(const T&) * @return the multiplicative identity for T */ template -GKO_INLINE GKO_ATTRIBUTES constexpr T one() +GKO_INLINE constexpr T one() { return T(1); } @@ -787,15 +665,12 @@ GKO_INLINE GKO_ATTRIBUTES constexpr T one() * `one(x)`. */ template -GKO_INLINE GKO_ATTRIBUTES constexpr T one(const T&) +GKO_INLINE constexpr T one(const T&) { return one(); } -#endif // defined(__HIPCC__) && GINKGO_HIP_PLATFORM_HCC - - #undef GKO_BIND_ZERO_ONE @@ -808,7 +683,7 @@ GKO_INLINE GKO_ATTRIBUTES constexpr T one(const T&) * @return true iff the given value is zero, i.e. `value == zero()` */ template -GKO_INLINE GKO_ATTRIBUTES constexpr bool is_zero(T value) +GKO_INLINE constexpr bool is_zero(T value) { return value == zero(); } @@ -823,7 +698,7 @@ GKO_INLINE GKO_ATTRIBUTES constexpr bool is_zero(T value) * @return true iff the given value is not zero, i.e. `value != zero()` */ template -GKO_INLINE GKO_ATTRIBUTES constexpr bool is_nonzero(T value) +GKO_INLINE constexpr bool is_nonzero(T value) { return value != zero(); } @@ -841,7 +716,7 @@ GKO_INLINE GKO_ATTRIBUTES constexpr bool is_nonzero(T value) * */ template -GKO_INLINE GKO_ATTRIBUTES constexpr T max(const T& x, const T& y) +GKO_INLINE constexpr T max(const T& x, const T& y) { return x >= y ? x : y; } @@ -859,7 +734,7 @@ GKO_INLINE GKO_ATTRIBUTES constexpr T max(const T& x, const T& y) * */ template -GKO_INLINE GKO_ATTRIBUTES constexpr T min(const T& x, const T& y) +GKO_INLINE constexpr T min(const T& x, const T& y) { return x <= y ? x : y; } @@ -1053,7 +928,7 @@ GKO_ATTRIBUTES GKO_INLINE constexpr auto conj(const T& x) * @return The squared norm of the object. */ template -GKO_INLINE GKO_ATTRIBUTES constexpr auto squared_norm(const T& x) +GKO_INLINE constexpr auto squared_norm(const T& x) -> decltype(real(conj(x) * x)) { return real(conj(x) * x); @@ -1070,16 +945,15 @@ GKO_INLINE GKO_ATTRIBUTES constexpr auto squared_norm(const T& x) * @return x >= zero() ? x : -x; */ template -GKO_INLINE GKO_ATTRIBUTES constexpr std::enable_if_t::value, T> -abs(const T& x) +GKO_INLINE constexpr std::enable_if_t::value, T> abs( + const T& x) { return x >= zero() ? x : -x; } template -GKO_INLINE GKO_ATTRIBUTES constexpr std::enable_if_t::value, - remove_complex> +GKO_INLINE constexpr std::enable_if_t::value, remove_complex> abs(const T& x) { return sqrt(squared_norm(x)); @@ -1092,7 +966,7 @@ abs(const T& x) * @tparam T the value type to return */ template -GKO_INLINE GKO_ATTRIBUTES constexpr T pi() +GKO_INLINE constexpr T pi() { return static_cast(3.1415926535897932384626433); } @@ -1107,8 +981,8 @@ GKO_INLINE GKO_ATTRIBUTES constexpr T pi() * @tparam T the corresponding real value type. */ template -GKO_INLINE GKO_ATTRIBUTES constexpr std::complex> unit_root( - int64 n, int64 k = 1) +GKO_INLINE constexpr std::complex> unit_root(int64 n, + int64 k = 1) { return std::polar(one>(), remove_complex{2} * pi>() * k / n); @@ -1259,8 +1133,7 @@ GKO_INLINE GKO_ATTRIBUTES std::enable_if_t::value, bool> is_nan( * @return NaN. */ template -GKO_INLINE GKO_ATTRIBUTES constexpr std::enable_if_t::value, T> -nan() +GKO_INLINE constexpr std::enable_if_t::value, T> nan() { return std::numeric_limits::quiet_NaN(); } @@ -1274,8 +1147,7 @@ nan() * @return complex{NaN, NaN}. */ template -GKO_INLINE GKO_ATTRIBUTES constexpr std::enable_if_t::value, T> -nan() +GKO_INLINE constexpr std::enable_if_t::value, T> nan() { return T{nan>(), nan>()}; } From 98f29f658b2ec7287e166eec5e44ea9ee6d7266b Mon Sep 17 00:00:00 2001 From: Marcel Koch Date: Tue, 15 Oct 2024 07:38:51 +0000 Subject: [PATCH 2/2] [core] remove unused #undef --- include/ginkgo/core/base/math.hpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/include/ginkgo/core/base/math.hpp b/include/ginkgo/core/base/math.hpp index 33b3a566b37..cd5e489b95d 100644 --- a/include/ginkgo/core/base/math.hpp +++ b/include/ginkgo/core/base/math.hpp @@ -671,9 +671,6 @@ GKO_INLINE constexpr T one(const T&) } -#undef GKO_BIND_ZERO_ONE - - /** * Returns true if and only if the given value is zero. *