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// SPDX-FileCopyrightText: Copyright (C) 2021 Tobias Hienzsch
3#ifndef TETL_CMATH_LRINT_HPP
4#define TETL_CMATH_LRINT_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, typename U>
16[[nodiscard]] constexpr auto lrint_fallback(U arg) noexcept -> T
17{
18 return static_cast<T>(arg);
19}
20
21template <typename T>
22[[nodiscard]] constexpr auto lrint_impl(T arg) noexcept -> long
23{
25 if constexpr (is_same_v<T, float>) {
26#if __has_builtin(__builtin_lrintf)
27 return __builtin_lrintf(arg);
28#endif
29 }
30 if constexpr (is_same_v<T, double>) {
31#if __has_builtin(__builtin_lrint)
32 return __builtin_lrint(arg);
33#endif
34 }
35 if constexpr (is_same_v<T, long double>) {
36#if __has_builtin(__builtin_lrintl)
37 return __builtin_lrintl(arg);
38#endif
39 }
40 }
41 return lrint_fallback<long>(arg);
42}
43
44template <typename T>
45[[nodiscard]] constexpr auto llrint_impl(T arg) noexcept -> long long
46{
48 if constexpr (is_same_v<T, float>) {
49#if __has_builtin(__builtin_llrintf)
50 return __builtin_llrintf(arg);
51#endif
52 }
53 if constexpr (is_same_v<T, double>) {
54#if __has_builtin(__builtin_llrint)
55 return __builtin_llrint(arg);
56#endif
57 }
58 if constexpr (is_same_v<T, long double>) {
59#if __has_builtin(__builtin_llrintl)
60 return __builtin_llrintl(arg);
61#endif
62 }
63 }
64 return lrint_fallback<long long>(arg);
65}
66
67} // namespace detail
68
69/// \ingroup cmath
70/// @{
71
72/// Rounds the floating-point argument arg to an integer value, using the current rounding mode.
73[[nodiscard]] constexpr auto lrint(float arg) noexcept -> long
74{
75 return detail::lrint_impl(arg);
76}
77
78/// Rounds the floating-point argument arg to an integer value, using the current rounding mode.
79[[nodiscard]] constexpr auto lrintf(float arg) noexcept -> long
80{
81 return detail::lrint_impl(arg);
82}
83
84/// Rounds the floating-point argument arg to an integer value, using the current rounding mode.
85[[nodiscard]] constexpr auto lrint(double arg) noexcept -> long
86{
87 return detail::lrint_impl(arg);
88}
89
90/// Rounds the floating-point argument arg to an integer value, using the current rounding mode.
91[[nodiscard]] constexpr auto lrint(long double arg) noexcept -> long
92{
93 return detail::lrint_impl(arg);
94}
95
96/// Rounds the floating-point argument arg to an integer value, using the current rounding mode.
97[[nodiscard]] constexpr auto lrintl(long double arg) noexcept -> long
98{
99 return detail::lrint_impl(arg);
100}
101
102/// Rounds the floating-point argument arg to an integer value, using the current rounding mode.
103template <integral T>
104[[nodiscard]] constexpr auto lrint(T arg) noexcept -> long
105{
106 return lrint(static_cast<double>(arg));
107}
108
109/// Rounds the floating-point argument arg to an integer value, using the current rounding mode.
110[[nodiscard]] constexpr auto llrint(float arg) noexcept -> long long
111{
112 return detail::llrint_impl(arg);
113}
114
115/// Rounds the floating-point argument arg to an integer value, using the current rounding mode.
116[[nodiscard]] constexpr auto llrintf(float arg) noexcept -> long long
117{
118 return detail::llrint_impl(arg);
119}
120
121/// Rounds the floating-point argument arg to an integer value, using the current rounding mode.
122[[nodiscard]] constexpr auto llrint(double arg) noexcept -> long long
123{
124 return detail::llrint_impl(arg);
125}
126
127/// Rounds the floating-point argument arg to an integer value, using the current rounding mode.
128[[nodiscard]] constexpr auto llrint(long double arg) noexcept -> long long
129{
130 return detail::llrint_impl(arg);
131}
132
133/// Rounds the floating-point argument arg to an integer value, using the current rounding mode.
134[[nodiscard]] constexpr auto llrintl(long double arg) noexcept -> long long
135{
136 return detail::llrint_impl(arg);
137}
138
139/// Rounds the floating-point argument arg to an integer value, using the current rounding mode.
140template <integral T>
141[[nodiscard]] constexpr auto llrint(T arg) noexcept -> long long
142{
143 return llrint(static_cast<double>(arg));
144}
145
146/// @}
147
148} // namespace etl
149
150#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:116
constexpr auto llrint(double arg) noexcept -> long long
Rounds the floating-point argument arg to an integer value, using the current rounding mode.
Definition lrint.hpp:122
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:134
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:79
constexpr auto lrint(double arg) noexcept -> long
Rounds the floating-point argument arg to an integer value, using the current rounding mode.
Definition lrint.hpp:85
constexpr auto lrint(long double arg) noexcept -> long
Rounds the floating-point argument arg to an integer value, using the current rounding mode.
Definition lrint.hpp:91
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:110
constexpr auto lrint(T arg) noexcept -> long
Rounds the floating-point argument arg to an integer value, using the current rounding mode.
Definition lrint.hpp:104
constexpr auto llrint(T arg) noexcept -> long long
Rounds the floating-point argument arg to an integer value, using the current rounding mode.
Definition lrint.hpp:141
constexpr auto lrint(float arg) noexcept -> long
Definition lrint.hpp:73
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:97
constexpr auto llrint(long double arg) noexcept -> long long
Rounds the floating-point argument arg to an integer value, using the current rounding mode.
Definition lrint.hpp:128
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