tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
trunc.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_TRUNC_HPP
5#define TETL_CMATH_TRUNC_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 {
17template <typename T>
18[[nodiscard]] constexpr auto trunc(T arg) noexcept -> T
19{
21 if constexpr (is_same_v<T, float>) {
22#if __has_builtin(__builtin_truncf)
23 return __builtin_truncf(arg);
24#endif
25 }
26 if constexpr (is_same_v<T, double>) {
27#if __has_builtin(__builtin_trunc)
28 return __builtin_trunc(arg);
29#endif
30 }
31 }
32
33 return detail::gcem::trunc(arg);
34}
35} // namespace detail
36
37/// \ingroup cmath
38/// @{
39
40/// Computes the nearest integer not greater in magnitude than arg.
41/// \details https://en.cppreference.com/w/cpp/numeric/math/trunc
42/// \ingroup cmath
43[[nodiscard]] constexpr auto trunc(float arg) noexcept -> float
44{
45 return detail::trunc(arg);
46}
47[[nodiscard]] constexpr auto truncf(float arg) noexcept -> float
48{
49 return detail::trunc(arg);
50}
51[[nodiscard]] constexpr auto trunc(double arg) noexcept -> double
52{
53 return detail::trunc(arg);
54}
55[[nodiscard]] constexpr auto trunc(long double arg) noexcept -> long double
56{
57 return detail::trunc(arg);
58}
59[[nodiscard]] constexpr auto truncl(long double arg) noexcept -> long double
60{
61 return detail::trunc(arg);
62}
63[[nodiscard]] constexpr auto trunc(integral auto arg) noexcept -> double
64{
65 return detail::trunc(double(arg));
66}
67
68/// @}
69
70} // namespace etl
71
72#endif // TETL_CMATH_TRUNC_HPP
constexpr auto trunc(double arg) noexcept -> double
Definition trunc.hpp:51
constexpr auto trunc(float arg) noexcept -> float
Definition trunc.hpp:43
constexpr auto truncf(float arg) noexcept -> float
Definition trunc.hpp:47
constexpr auto trunc(long double arg) noexcept -> long double
Definition trunc.hpp:55
constexpr auto truncl(long double arg) noexcept -> long double
Definition trunc.hpp:59
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