tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
round.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2021 Tobias Hienzsch
3
4#ifndef TETL_CMATH_ROUND_HPP
5#define TETL_CMATH_ROUND_HPP
6
7#include <etl/_config/all.hpp>
8
9#include <etl/_3rd_party/gcem/gcem.hpp>
10#include <etl/_concepts/integral.hpp>
11#include <etl/_type_traits/is_constant_evaluated.hpp>
12#include <etl/_type_traits/is_same.hpp>
13
14namespace etl {
15
16namespace detail {
17
18template <typename T>
19[[nodiscard]] constexpr auto round(T arg) noexcept -> T
20{
22 if constexpr (is_same_v<T, float>) {
23#if __has_builtin(__builtin_roundf)
24 return __builtin_roundf(arg);
25#endif
26 }
27 if constexpr (is_same_v<T, double>) {
28#if __has_builtin(__builtin_round)
29 return __builtin_round(arg);
30#endif
31 }
32 }
33 return detail::gcem::round(arg);
34}
35
36} // namespace detail
37
38/// \ingroup cmath
39/// @{
40
41/// Computes the nearest integer value to arg (in floating-point format),
42/// rounding halfway cases away from zero, regardless of the current rounding
43/// mode.
44///
45/// https://en.cppreference.com/w/cpp/numeric/math/round
46[[nodiscard]] constexpr auto round(float arg) noexcept -> float
47{
48 return etl::detail::round(arg);
49}
50[[nodiscard]] constexpr auto roundf(float arg) noexcept -> float
51{
52 return etl::detail::round(arg);
53}
54[[nodiscard]] constexpr auto round(double arg) noexcept -> double
55{
56 return etl::detail::round(arg);
57}
58[[nodiscard]] constexpr auto round(long double arg) noexcept -> long double
59{
60 return etl::detail::round(arg);
61}
62[[nodiscard]] constexpr auto roundl(long double arg) noexcept -> long double
63{
64 return etl::detail::round(arg);
65}
66[[nodiscard]] constexpr auto round(integral auto arg) noexcept -> double
67{
68 return etl::detail::round(double(arg));
69}
70
71/// @}
72
73} // namespace etl
74
75#endif // TETL_CMATH_ROUND_HPP
constexpr auto roundf(float arg) noexcept -> float
Definition round.hpp:50
constexpr auto round(long double arg) noexcept -> long double
Definition round.hpp:58
constexpr auto round(float arg) noexcept -> float
Definition round.hpp:46
constexpr auto round(double arg) noexcept -> double
Definition round.hpp:54
constexpr auto roundl(long double arg) noexcept -> long double
Definition round.hpp:62
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