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/// Rounds the floating-point argument arg to an integer value, using the current rounding mode.
70/// \ingroup cmath
71[[nodiscard]] constexpr auto lrint(float arg) noexcept -> long
72{
73 return detail::lrint_impl(arg);
74}
75
76/// Rounds the floating-point argument arg to an integer value, using the current rounding mode.
77/// \ingroup cmath
78[[nodiscard]] constexpr auto lrintf(float arg) noexcept -> long
79{
80 return detail::lrint_impl(arg);
81}
82
83/// Rounds the floating-point argument arg to an integer value, using the current rounding mode.
84/// \ingroup cmath
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/// \ingroup cmath
92[[nodiscard]] constexpr auto lrint(long double arg) noexcept -> long
93{
94 return detail::lrint_impl(arg);
95}
96
97/// Rounds the floating-point argument arg to an integer value, using the current rounding mode.
98/// \ingroup cmath
99[[nodiscard]] constexpr auto lrintl(long double arg) noexcept -> long
100{
101 return detail::lrint_impl(arg);
102}
103
104/// Rounds the floating-point argument arg to an integer value, using the current rounding mode.
105/// \ingroup cmath
106template <integral T>
107[[nodiscard]] constexpr auto lrint(T arg) noexcept -> long
108{
109 return lrint(static_cast<double>(arg));
110}
111
112/// Rounds the floating-point argument arg to an integer value, using the current rounding mode.
113/// \ingroup cmath
114[[nodiscard]] constexpr auto llrint(float arg) noexcept -> long long
115{
116 return detail::llrint_impl(arg);
117}
118
119/// Rounds the floating-point argument arg to an integer value, using the current rounding mode.
120/// \ingroup cmath
121[[nodiscard]] constexpr auto llrintf(float arg) noexcept -> long long
122{
123 return detail::llrint_impl(arg);
124}
125
126/// Rounds the floating-point argument arg to an integer value, using the current rounding mode.
127/// \ingroup cmath
128[[nodiscard]] constexpr auto llrint(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/// \ingroup cmath
135[[nodiscard]] constexpr auto llrint(long double arg) noexcept -> long long
136{
137 return detail::llrint_impl(arg);
138}
139
140/// Rounds the floating-point argument arg to an integer value, using the current rounding mode.
141/// \ingroup cmath
142[[nodiscard]] constexpr auto llrintl(long double arg) noexcept -> long long
143{
144 return detail::llrint_impl(arg);
145}
146
147/// Rounds the floating-point argument arg to an integer value, using the current rounding mode.
148/// \ingroup cmath
149template <integral T>
150[[nodiscard]] constexpr auto llrint(T arg) noexcept -> long long
151{
152 return llrint(static_cast<double>(arg));
153}
154
155} // namespace etl
156
157#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:121
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:128
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:142
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:78
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:92
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:114
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:107
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:150
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:71
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:99
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:135
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