tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
lrint.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2#ifndef TETL_CMATH_LRINT_HPP
3#define TETL_CMATH_LRINT_HPP
4
5#include <etl/_config/all.hpp>
6
10
11namespace etl {
12
13namespace detail {
14template <typename T, typename U>
15[[nodiscard]] constexpr auto lrint_fallback(U arg) noexcept -> T
16{
17 return static_cast<T>(arg);
18}
19
20template <typename T>
21[[nodiscard]] constexpr auto lrint_impl(T arg) noexcept -> long
22{
23 if (!is_constant_evaluated()) {
24 if constexpr (is_same_v<T, float>) {
25#if __has_builtin(__builtin_lrintf)
26 return __builtin_lrintf(arg);
27#endif
28 }
29 if constexpr (is_same_v<T, double>) {
30#if __has_builtin(__builtin_lrint)
31 return __builtin_lrint(arg);
32#endif
33 }
34 if constexpr (is_same_v<T, long double>) {
35#if __has_builtin(__builtin_lrintl)
36 return __builtin_lrintl(arg);
37#endif
38 }
39 }
40 return lrint_fallback<long>(arg);
41}
42
43template <typename T>
44[[nodiscard]] constexpr auto llrint_impl(T arg) noexcept -> long long
45{
46 if (!is_constant_evaluated()) {
47 if constexpr (is_same_v<T, float>) {
48#if __has_builtin(__builtin_llrintf)
49 return __builtin_llrintf(arg);
50#endif
51 }
52 if constexpr (is_same_v<T, double>) {
53#if __has_builtin(__builtin_llrint)
54 return __builtin_llrint(arg);
55#endif
56 }
57 if constexpr (is_same_v<T, long double>) {
58#if __has_builtin(__builtin_llrintl)
59 return __builtin_llrintl(arg);
60#endif
61 }
62 }
63 return lrint_fallback<long long>(arg);
64}
65
66} // namespace detail
67
70[[nodiscard]] constexpr auto lrint(float arg) noexcept -> long { return detail::lrint_impl(arg); }
71
74[[nodiscard]] constexpr auto lrintf(float arg) noexcept -> long { return detail::lrint_impl(arg); }
75
78[[nodiscard]] constexpr auto lrint(double arg) noexcept -> long { return detail::lrint_impl(arg); }
79
82[[nodiscard]] constexpr auto lrint(long double arg) noexcept -> long { return detail::lrint_impl(arg); }
83
86[[nodiscard]] constexpr auto lrintl(long double arg) noexcept -> long { return detail::lrint_impl(arg); }
87
90template <integral T>
91[[nodiscard]] constexpr auto lrint(T arg) noexcept -> long
92{
93 return lrint(static_cast<double>(arg));
94}
95
98[[nodiscard]] constexpr auto llrint(float arg) noexcept -> long long { return detail::llrint_impl(arg); }
99
102[[nodiscard]] constexpr auto llrintf(float arg) noexcept -> long long { return detail::llrint_impl(arg); }
103
106[[nodiscard]] constexpr auto llrint(double arg) noexcept -> long long { return detail::llrint_impl(arg); }
107
110[[nodiscard]] constexpr auto llrint(long double arg) noexcept -> long long { return detail::llrint_impl(arg); }
111
114[[nodiscard]] constexpr auto llrintl(long double arg) noexcept -> long long { return detail::llrint_impl(arg); }
115
118template <integral T>
119[[nodiscard]] constexpr auto llrint(T arg) noexcept -> long long
120{
121 return llrint(static_cast<double>(arg));
122}
123
124} // namespace etl
125
126#endif // TETL_CMATH_LRINT_HPP
constexpr auto llrintf(float arg) noexcept -> long long
Rounds the floating-point argument arg to an integer value, using the current rounding mode.
Definition lrint.hpp:102
constexpr auto llrintl(long double arg) noexcept -> long long
Rounds the floating-point argument arg to an integer value, using the current rounding mode.
Definition lrint.hpp:114
constexpr auto lrintf(float arg) noexcept -> long
Rounds the floating-point argument arg to an integer value, using the current rounding mode.
Definition lrint.hpp:74
constexpr auto llrint(float arg) noexcept -> long long
Rounds the floating-point argument arg to an integer value, using the current rounding mode.
Definition lrint.hpp:98
constexpr auto lrint(float arg) noexcept -> long
Rounds the floating-point argument arg to an integer value, using the current rounding mode.
Definition lrint.hpp:70
constexpr auto lrintl(long double arg) noexcept -> long
Rounds the floating-point argument arg to an integer value, using the current rounding mode.
Definition lrint.hpp:86
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