tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
fdim.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_FDIM_HPP
5#define TETL_CMATH_FDIM_HPP
6
7#include <etl/_config/all.hpp>
8
9#include <etl/_cmath/fmax.hpp>
10#include <etl/_type_traits/is_constant_evaluated.hpp>
11
12namespace etl {
13
14namespace detail {
15
16inline constexpr struct fdim {
17 template <typename Float>
18 [[nodiscard]] constexpr auto operator()(Float x, Float y) const noexcept -> Float
19 {
21#if __has_builtin(__builtin_fdimf)
22 if constexpr (etl::same_as<Float, float>) {
23 return __builtin_fdimf(x, y);
24 }
25#endif
26#if __has_builtin(__builtin_fdim)
27 if constexpr (etl::same_as<Float, double>) {
28 return __builtin_fdim(x, y);
29 }
30#endif
31 }
32 return etl::fmax(x - y, static_cast<Float>(0));
33 }
34} fdim;
35
36} // namespace detail
37
38/// \ingroup cmath
39/// @{
40
41/// Returns the positive difference between x and y, that is, if x>y,
42/// returns x-y, otherwise (if x≤y), returns +0.
43/// \details https://en.cppreference.com/w/cpp/numeric/math/fdim
44[[nodiscard]] constexpr auto fdim(float x, float y) noexcept -> float
45{
46 return etl::detail::fdim(x, y);
47}
48[[nodiscard]] constexpr auto fdimf(float x, float y) noexcept -> float
49{
50 return etl::detail::fdim(x, y);
51}
52[[nodiscard]] constexpr auto fdim(double x, double y) noexcept -> double
53{
54 return etl::detail::fdim(x, y);
55}
56[[nodiscard]] constexpr auto fdim(long double x, long double y) noexcept -> long double
57{
58 return etl::detail::fdim(x, y);
59}
60[[nodiscard]] constexpr auto fdiml(long double x, long double y) noexcept -> long double
61{
62 return etl::detail::fdim(x, y);
63}
64
65/// @}
66
67} // namespace etl
68
69#endif // TETL_CMATH_FDIM_HPP
constexpr auto fdiml(long double x, long double y) noexcept -> long double
Definition fdim.hpp:60
constexpr auto fdim(long double x, long double y) noexcept -> long double
Definition fdim.hpp:56
constexpr auto fdim(double x, double y) noexcept -> double
Definition fdim.hpp:52
constexpr auto fdimf(float x, float y) noexcept -> float
Definition fdim.hpp:48
constexpr auto fdim(float x, float y) noexcept -> float
Definition fdim.hpp:44
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