tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
copysign.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_CMATH_COPYSIGN_HPP
4#define TETL_CMATH_COPYSIGN_HPP
5
6#include <etl/_config/all.hpp>
7
10
11namespace etl {
12
13namespace detail {
14template <typename T>
15constexpr auto copysign_fallback(T x, T y) noexcept -> T
16{
17 if ((x < 0 and y > 0) or (x > 0 and y < 0)) {
18 return -x;
19 }
20 return x;
21}
22
23template <typename T>
24[[nodiscard]] constexpr auto copysign(T x, T y) noexcept -> T
25{
26 if (!is_constant_evaluated()) {
27 if constexpr (is_same_v<T, float>) {
28#if __has_builtin(__builtin_copysignf)
29 return __builtin_copysignf(x, y);
30#endif
31 }
32 if constexpr (is_same_v<T, double>) {
33#if __has_builtin(__builtin_copysign)
34 return __builtin_copysign(x, y);
35#endif
36 }
37 }
38 return copysign_fallback(x, y);
39}
40
41} // namespace detail
42
45
58[[nodiscard]] constexpr auto copysign(float mag, float sgn) -> float { return detail::copysign(mag, sgn); }
59[[nodiscard]] constexpr auto copysignf(float mag, float sgn) -> float { return detail::copysign(mag, sgn); }
60[[nodiscard]] constexpr auto copysign(double mag, double sgn) -> double { return detail::copysign(mag, sgn); }
61[[nodiscard]] constexpr auto copysign(long double mag, long double sgn) -> long double
62{
63 return detail::copysign(mag, sgn);
64}
65
66[[nodiscard]] constexpr auto copysignl(long double mag, long double sgn) -> long double
67{
68 return detail::copysign(mag, sgn);
69}
70
72
73} // namespace etl
74
75#endif // TETL_CMATH_COPYSIGN_HPP
constexpr auto copysign(float mag, float sgn) -> float
Composes a floating point value with the magnitude of mag and the sign of sgn.
Definition copysign.hpp:58
constexpr auto copysignf(float mag, float sgn) -> float
Composes a floating point value with the magnitude of mag and the sign of sgn.
Definition copysign.hpp:59
constexpr auto copysignl(long double mag, long double sgn) -> long double
Composes a floating point value with the magnitude of mag and the sign of sgn.
Definition copysign.hpp:66
Definition adjacent_find.hpp:8
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:16
constexpr bool is_same_v
Definition is_same.hpp:11