tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
rint.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2#ifndef TETL_CMATH_RINT_HPP
3#define TETL_CMATH_RINT_HPP
4
5#include <etl/_config/all.hpp>
6
10
11namespace etl {
12
13namespace detail {
14template <typename T>
15[[nodiscard]] constexpr auto rint_fallback(T arg) noexcept -> T
16{
17 if constexpr (sizeof(T) <= sizeof(long)) {
18 return static_cast<T>(static_cast<long>(arg));
19 } else {
20 return static_cast<T>(static_cast<long long>(arg));
21 }
22}
23
24template <typename T>
25[[nodiscard]] constexpr auto rint_impl(T arg) noexcept -> T
26{
27 if (!is_constant_evaluated()) {
28 if constexpr (is_same_v<T, float>) {
29#if __has_builtin(__builtin_rintf)
30 return __builtin_rintf(arg);
31#endif
32 }
33 if constexpr (is_same_v<T, double>) {
34#if __has_builtin(__builtin_rint)
35 return __builtin_rint(arg);
36#endif
37 }
38 if constexpr (is_same_v<T, long double>) {
39#if __has_builtin(__builtin_rintl)
40 return __builtin_rintl(arg);
41#endif
42 }
43 }
44 return rint_fallback(arg);
45}
46
47} // namespace detail
48
52[[nodiscard]] constexpr auto rint(float arg) noexcept -> float { return detail::rint_impl(arg); }
53
57[[nodiscard]] constexpr auto rintf(float arg) noexcept -> float { return detail::rint_impl(arg); }
58
62[[nodiscard]] constexpr auto rint(double arg) noexcept -> double { return detail::rint_impl(arg); }
63
67[[nodiscard]] constexpr auto rint(long double arg) noexcept -> long double { return detail::rint_impl(arg); }
68
72[[nodiscard]] constexpr auto rintl(long double arg) noexcept -> long double { return detail::rint_impl(arg); }
73
77template <integral T>
78[[nodiscard]] constexpr auto rint(T arg) noexcept -> double
79{
80 return rint(static_cast<double>(arg));
81}
82
83} // namespace etl
84
85#endif // TETL_CMATH_RINT_HPP
constexpr auto rintl(long double arg) noexcept -> long double
Rounds the floating-point argument arg to an integer value (in floating-point format),...
Definition rint.hpp:72
constexpr auto rint(float arg) noexcept -> float
Rounds the floating-point argument arg to an integer value (in floating-point format),...
Definition rint.hpp:52
constexpr auto rintf(float arg) noexcept -> float
Rounds the floating-point argument arg to an integer value (in floating-point format),...
Definition rint.hpp:57
constexpr auto arg(complex< T > const &z) noexcept -> T
Definition arg.hpp:15
Definition adjacent_find.hpp:8
constexpr auto is_constant_evaluated() noexcept -> bool
Detects whether the function call occurs within a constant-evaluated context. Returns true if the eva...
Definition is_constant_evaluated.hpp:16
constexpr bool is_same_v
Definition is_same.hpp:11