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/// Rounds the floating-point argument arg to an integer value
51/// (in floating-point format), using the current rounding mode.
52/// \ingroup cmath
53[[nodiscard]] constexpr auto rint(float arg) noexcept -> float
54{
55 return detail::rint_impl(arg);
56}
57
58/// Rounds the floating-point argument arg to an integer value
59/// (in floating-point format), using the current rounding mode.
60/// \ingroup cmath
61[[nodiscard]] constexpr auto rintf(float arg) noexcept -> float
62{
63 return detail::rint_impl(arg);
64}
65
66/// Rounds the floating-point argument arg to an integer value
67/// (in floating-point format), using the current rounding mode.
68/// \ingroup cmath
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/// \ingroup cmath
77[[nodiscard]] constexpr auto rint(long double arg) noexcept -> long double
78{
79 return detail::rint_impl(arg);
80}
81
82/// Rounds the floating-point argument arg to an integer value
83/// (in floating-point format), using the current rounding mode.
84/// \ingroup cmath
85[[nodiscard]] constexpr auto rintl(long double arg) noexcept -> long double
86{
87 return detail::rint_impl(arg);
88}
89
90/// Rounds the floating-point argument arg to an integer value
91/// (in floating-point format), using the current rounding mode.
92/// \ingroup cmath
93template <integral T>
94[[nodiscard]] constexpr auto rint(T arg) noexcept -> double
95{
96 return rint(static_cast<double>(arg));
97}
98
99} // namespace etl
100
101#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:85
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:77
constexpr auto rint(T arg) noexcept -> double
Rounds the floating-point argument arg to an integer value (in floating-point format),...
Definition rint.hpp:94
constexpr auto rint(float arg) noexcept -> float
Rounds the floating-point argument arg to an integer value (in floating-point format),...
Definition rint.hpp:53
constexpr auto rintf(float arg) noexcept -> float
Rounds the floating-point argument arg to an integer value (in floating-point format),...
Definition rint.hpp:61
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