Debian Codegraph @

third-party/boost-math/include/boost/math/ccmath/copysign.hpp

raw
1 // (C) Copyright Matt Borland 2021.
2 // Use, modification and distribution are subject to the
3 // Boost Software License, Version 1.0. (See accompanying file
4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5
6 #ifndef BOOST_MATH_CCMATH_COPYSIGN_HPP
7 #define BOOST_MATH_CCMATH_COPYSIGN_HPP
8
9 #include <cmath>
10 #include <cstdint>
11 #include <limits>
12 #include <type_traits>
13 #include <boost/math/tools/is_constant_evaluated.hpp>
14 #include <boost/math/tools/promotion.hpp>
15 #include <boost/math/tools/config.hpp>
16 #include <boost/math/ccmath/abs.hpp>
17 #include <boost/math/ccmath/signbit.hpp>
18
19 namespace boost::math::ccmath {
20
21 namespace detail {
22
23 template <typename T>
24 constexpr T copysign_impl(const T mag, const T sgn) noexcept
25 {
26 if (boost::math::ccmath::signbit(sgn))
27 {
28 return -boost::math::ccmath::abs(mag);
29 }
30 else
31 {
32 return boost::math::ccmath::abs(mag);
33 }
34 }
35
36 } // Namespace detail
37
38 template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true>
39 constexpr Real copysign(Real mag, Real sgn) noexcept
40 {
41 if(BOOST_MATH_IS_CONSTANT_EVALUATED(mag))
42 {
43 return boost::math::ccmath::detail::copysign_impl(mag, sgn);
44 }
45 else
46 {
47 using std::copysign;
48 return copysign(mag, sgn);
49 }
50 }
51
52 template <typename T1, typename T2>
53 constexpr auto copysign(T1 mag, T2 sgn) noexcept
54 {
55 if (BOOST_MATH_IS_CONSTANT_EVALUATED(mag))
56 {
57 using promoted_type = boost::math::tools::promote_args_t<T1, T2>;
58 return boost::math::ccmath::copysign(static_cast<promoted_type>(mag), static_cast<promoted_type>(sgn));
59 }
60 else
61 {
62 using std::copysign;
63 return copysign(mag, sgn);
64 }
65 }
66
67 constexpr float copysignf(float mag, float sgn) noexcept
68 {
69 return boost::math::ccmath::copysign(mag, sgn);
70 }
71
72 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
73 constexpr long double copysignl(long double mag, long double sgn) noexcept
74 {
75 return boost::math::ccmath::copysign(mag, sgn);
76 }
77 #endif
78
79 } // Namespaces
80
81 #endif // BOOST_MATH_CCMATH_COPYSIGN_HPP