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// SPDX-FileCopyrightText: Copyright (C) 2021 Tobias Hienzsch
3#ifndef TETL_CMATH_RINT_HPP
4#define TETL_CMATH_RINT_HPP
5
6#include <etl/_config/all.hpp>
7
8#include <etl/_concepts/integral.hpp>
9#include <etl/_type_traits/is_constant_evaluated.hpp>
10#include <etl/_type_traits/is_same.hpp>
11
12namespace etl {
13
14namespace detail {
15template <typename T>
16[[nodiscard]] constexpr auto rint_fallback(T arg) noexcept -> T
17{
18 if constexpr (sizeof(T) <= sizeof(long)) {
19 return static_cast<T>(static_cast<long>(arg));
20 } else {
21 return static_cast<T>(static_cast<long long>(arg));
22 }
23}
24
25template <typename T>
26[[nodiscard]] constexpr auto rint_impl(T arg) noexcept -> T
27{
29 if constexpr (is_same_v<T, float>) {
30#if __has_builtin(__builtin_rintf)
31 return __builtin_rintf(arg);
32#endif
33 }
34 if constexpr (is_same_v<T, double>) {
35#if __has_builtin(__builtin_rint)
36 return __builtin_rint(arg);
37#endif
38 }
39 if constexpr (is_same_v<T, long double>) {
40#if __has_builtin(__builtin_rintl)
41 return __builtin_rintl(arg);
42#endif
43 }
44 }
45 return rint_fallback(arg);
46}
47
48} // namespace detail
49
50/// \ingroup cmath
51/// @{
52
53/// Rounds the floating-point argument arg to an integer value
54/// (in floating-point format), using the current rounding mode.
55[[nodiscard]] constexpr auto rint(float arg) noexcept -> float
56{
57 return detail::rint_impl(arg);
58}
59
60/// Rounds the floating-point argument arg to an integer value
61/// (in floating-point format), using the current rounding mode.
62[[nodiscard]] constexpr auto rintf(float arg) noexcept -> float
63{
64 return detail::rint_impl(arg);
65}
66
67/// Rounds the floating-point argument arg to an integer value
68/// (in floating-point format), using the current rounding mode.
69[[nodiscard]] constexpr auto rint(double arg) noexcept -> double
70{
71 return detail::rint_impl(arg);
72}
73
74/// Rounds the floating-point argument arg to an integer value
75/// (in floating-point format), using the current rounding mode.
76[[nodiscard]] constexpr auto rint(long double arg) noexcept -> long double
77{
78 return detail::rint_impl(arg);
79}
80
81/// Rounds the floating-point argument arg to an integer value
82/// (in floating-point format), using the current rounding mode.
83[[nodiscard]] constexpr auto rintl(long double arg) noexcept -> long double
84{
85 return detail::rint_impl(arg);
86}
87
88/// Rounds the floating-point argument arg to an integer value
89/// (in floating-point format), using the current rounding mode.
90template <integral T>
91[[nodiscard]] constexpr auto rint(T arg) noexcept -> double
92{
93 return rint(static_cast<double>(arg));
94}
95
96/// @}
97
98} // namespace etl
99
100#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:83
constexpr auto rint(long double arg) noexcept -> long double
Rounds the floating-point argument arg to an integer value (in floating-point format),...
Definition rint.hpp:76
constexpr auto rint(T arg) noexcept -> double
Rounds the floating-point argument arg to an integer value (in floating-point format),...
Definition rint.hpp:91
constexpr auto rint(float arg) noexcept -> float
Definition rint.hpp:55
constexpr auto rintf(float arg) noexcept -> float
Rounds the floating-point argument arg to an integer value (in floating-point format),...
Definition rint.hpp:62
constexpr auto rint(double arg) noexcept -> double
Rounds the floating-point argument arg to an integer value (in floating-point format),...
Definition rint.hpp:69
Definition adjacent_find.hpp:9
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:17