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
18inline constexpr struct round {
19 template <typename Float>
20 [[nodiscard]] constexpr auto operator()(Float arg) const noexcept -> Float
21 {
23#if __has_builtin(__builtin_roundf)
24 if constexpr (etl::same_as<Float, float>) {
25 return __builtin_roundf(arg);
26 }
27#endif
28#if __has_builtin(__builtin_round)
29 if constexpr (etl::same_as<Float, double>) {
30 return __builtin_round(arg);
31 }
32#endif
33 }
34 return etl::detail::gcem::round(arg);
35 }
36} round;
37
38} // namespace detail
39
40/// \ingroup cmath
41/// @{
42
43/// Computes the nearest integer value to arg (in floating-point format),
44/// rounding halfway cases away from zero, regardless of the current rounding
45/// mode.
46///
47/// https://en.cppreference.com/w/cpp/numeric/math/round
48[[nodiscard]] constexpr auto round(float arg) noexcept -> float
49{
50 return etl::detail::round(arg);
51}
52[[nodiscard]] constexpr auto roundf(float arg) noexcept -> float
53{
54 return etl::detail::round(arg);
55}
56[[nodiscard]] constexpr auto round(double arg) noexcept -> double
57{
58 return etl::detail::round(arg);
59}
60[[nodiscard]] constexpr auto round(long double arg) noexcept -> long double
61{
62 return etl::detail::round(arg);
63}
64[[nodiscard]] constexpr auto roundl(long double arg) noexcept -> long double
65{
66 return etl::detail::round(arg);
67}
68[[nodiscard]] constexpr auto round(integral auto arg) noexcept -> double
69{
70 return etl::detail::round(static_cast<double>(arg));
71}
72
73/// @}
74
75} // namespace etl
76
77#endif // TETL_CMATH_ROUND_HPP
constexpr auto roundf(float arg) noexcept -> float
Definition round.hpp:52
constexpr auto round(long double arg) noexcept -> long double
Definition round.hpp:60
constexpr auto round(float arg) noexcept -> float
Definition round.hpp:48
constexpr auto round(double arg) noexcept -> double
Definition round.hpp:56
constexpr auto roundl(long double arg) noexcept -> long double
Definition round.hpp:64
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