33 #ifndef GKO_CORE_BASE_MATH_HPP_ 34 #define GKO_CORE_BASE_MATH_HPP_ 37 #include <ginkgo/core/base/std_extensions.hpp> 38 #include <ginkgo/core/base/types.hpp> 64 struct remove_complex_impl {
72 struct remove_complex_impl<
std::complex<T>> {
78 struct is_complex_impl :
public std::integral_constant<bool, false> {};
81 struct is_complex_impl<
std::complex<T>>
82 :
public std::integral_constant<bool, true> {};
103 template <
typename T>
106 return detail::is_complex_impl<T>::value;
113 template <
typename T>
114 struct reduce_precision_impl {
118 template <
typename T>
119 struct reduce_precision_impl<std::complex<T>> {
120 using type = std::complex<typename reduce_precision_impl<T>::type>;
124 struct reduce_precision_impl<double> {
129 struct reduce_precision_impl<float> {
134 template <
typename T>
135 struct increase_precision_impl {
139 template <
typename T>
140 struct increase_precision_impl<std::complex<T>> {
141 using type = std::complex<typename increase_precision_impl<T>::type>;
145 struct increase_precision_impl<float> {
150 struct increase_precision_impl<half> {
161 template <
typename T>
168 template <
typename T>
181 template <
typename T>
197 template <
typename T>
204 template <
typename FloatType,
size_type NumComponents,
size_type ComponentId>
211 template <
typename T>
212 struct truncate_type_impl {
216 template <
typename T,
size_type Components>
217 struct truncate_type_impl<
truncated<T, Components, 0>> {
221 template <
typename T>
222 struct truncate_type_impl<
std::complex<T>> {
223 using type = std::complex<typename truncate_type_impl<T>::type>;
227 template <
typename T>
228 struct type_size_impl {
229 static constexpr
auto value =
sizeof(T) *
byte_size;
232 template <
typename T>
233 struct type_size_impl<
std::complex<T>> {
234 static constexpr
auto value =
sizeof(T) *
byte_size;
245 template <
typename T,
size_type Limit = sizeof(u
int16) * byte_size>
247 xstd::conditional_t<detail::type_size_impl<T>::value >= 2 * Limit,
257 template <
typename S,
typename R>
265 GKO_ATTRIBUTES R
operator()(S val) {
return static_cast<R
>(val); }
282 return (num + den - 1) / den;
291 template <
typename T>
292 GKO_INLINE GKO_ATTRIBUTES constexpr T
zero()
306 template <
typename T>
307 GKO_INLINE GKO_ATTRIBUTES constexpr T
zero(
const T &)
318 template <
typename T>
319 GKO_INLINE GKO_ATTRIBUTES constexpr T
one()
333 template <
typename T>
334 GKO_INLINE GKO_ATTRIBUTES constexpr T
one(
const T &)
349 template <
typename T>
350 GKO_INLINE GKO_ATTRIBUTES constexpr T
abs(
const T &x)
352 return x >= zero<T>() ? x : -x;
372 template <
typename T>
373 GKO_INLINE GKO_ATTRIBUTES constexpr T
max(
const T &x,
const T &y)
375 return x >= y ? x : y;
392 template <
typename T>
393 GKO_INLINE GKO_ATTRIBUTES constexpr T
min(
const T &x,
const T &y)
395 return x <= y ? x : y;
408 template <
typename T>
409 GKO_ATTRIBUTES GKO_INLINE constexpr T
real(
const T &x)
424 template <
typename T>
425 GKO_ATTRIBUTES GKO_INLINE constexpr T
imag(
const T &)
438 template <
typename T>
439 GKO_ATTRIBUTES GKO_INLINE T
conj(
const T &x)
455 template <
typename T>
475 template <
typename T>
477 const T &n,
uint32 hint = 0u) noexcept
494 template <
typename T>
496 const T &base,
const T &limit,
const T &hint = T{1}) noexcept
505 #endif // GKO_CORE_BASE_MATH_HPP_ constexpr int64 ceildiv(int64 num, int64 den)
Performs integer division with rounding up.
Definition: math.hpp:280
constexpr T get_superior_power(const T &base, const T &limit, const T &hint=T{1}) noexcept
Returns the smallest power of base not smaller than limit.
Definition: math.hpp:495
constexpr size_type byte_size
Number of bits in a byte.
Definition: types.hpp:185
typename detail::reduce_precision_impl< T >::type reduce_precision
Obtains the next type in the hierarchy with lower precision than T.
Definition: math.hpp:162
constexpr T zero()
Returns the additive identity for T.
Definition: math.hpp:292
constexpr reduce_precision< T > round_down(T val)
Reduces the precision of the input parameter.
Definition: math.hpp:182
std::uint32_t uint32
32-bit unsigned integral type.
Definition: types.hpp:134
constexpr T abs(const T &x)
Returns the absolute value of the object.
Definition: math.hpp:350
constexpr T imag(const T &)
Returns the imaginary part of the object.
Definition: math.hpp:425
The Ginkgo namespace.
Definition: abstract_factory.hpp:45
Used to convert objects of type S to objects of type R using static_cast.
Definition: math.hpp:258
constexpr uint32 get_significant_bit(const T &n, uint32 hint=0u) noexcept
Returns the position of the most significant bit of the number.
Definition: math.hpp:476
constexpr T real(const T &x)
Returns the real part of the object.
Definition: math.hpp:409
constexpr increase_precision< T > round_up(T val)
Increases the precision of the input parameter.
Definition: math.hpp:198
constexpr T min(const T &x, const T &y)
Returns the smaller of the arguments.
Definition: math.hpp:393
constexpr auto squared_norm(const T &x) -> decltype(real(conj(x) *x))
Returns the squared norm of the object.
Definition: math.hpp:456
std::int64_t int64
64-bit signed integral type.
Definition: types.hpp:117
T conj(const T &x)
Returns the conjugate of an object.
Definition: math.hpp:439
xstd::conditional_t< detail::type_size_impl< T >::value >=2 *Limit, typename detail::truncate_type_impl< T >::type, T > truncate_type
Truncates the type by half (by dropping bits), but ensures that it is at least Limit bits wide...
Definition: math.hpp:248
R operator()(S val)
Converts the object to result type.
Definition: math.hpp:265
typename detail::remove_complex_impl< T >::type remove_complex
Obtains a real counterpart of a std::complex type, and leaves the type unchanged if it is not a compl...
Definition: math.hpp:93
constexpr T max(const T &x, const T &y)
Returns the larger of the arguments.
Definition: math.hpp:373
constexpr bool is_complex()
Checks if T is a complex type.
Definition: math.hpp:104
constexpr T one()
Returns the multiplicative identity for T.
Definition: math.hpp:319
typename detail::increase_precision_impl< T >::type increase_precision
Obtains the next type in the hierarchy with higher precision than T.
Definition: math.hpp:169