From f563191617777bb35a906567c3bebbf7f555cd27 Mon Sep 17 00:00:00 2001 From: Matt Borland Date: Sat, 9 Oct 2021 10:04:52 +0300 Subject: [PATCH 1/2] constexpr copysign --- doc/sf/ccmath.qbk | 6 ++ include/boost/math/ccmath/ccmath.hpp | 1 + include/boost/math/ccmath/copysign.hpp | 95 +++++++++++++++++++ test/Jamfile.v2 | 1 + test/ccmath_copysign_test.cpp | 55 +++++++++++ .../ccmath_copysign_incl_test.cpp | 16 ++++ 6 files changed, 174 insertions(+) create mode 100644 include/boost/math/ccmath/copysign.hpp create mode 100644 test/ccmath_copysign_test.cpp create mode 100644 test/compile_test/ccmath_copysign_incl_test.cpp diff --git a/doc/sf/ccmath.qbk b/doc/sf/ccmath.qbk index 373a7b9530..ab25aa900d 100644 --- a/doc/sf/ccmath.qbk +++ b/doc/sf/ccmath.qbk @@ -137,6 +137,12 @@ All of the following functions require C++17 or greater. template inline constexpr Promoted remainder(Arithmetic1 x, Arithmetic2 y) noexcept + template + inline constexpr Real copysign(Real mag, Real sgn) noexcept + + template + inline constexpr Promoted copysign(Arithmetic1 mag, Arithmetic2 sgn) noexcept + } // Namespaces [endsect] [/section:ccmath Constexpr CMath] diff --git a/include/boost/math/ccmath/ccmath.hpp b/include/boost/math/ccmath/ccmath.hpp index 8f5cb0edf2..73e30c0b11 100644 --- a/include/boost/math/ccmath/ccmath.hpp +++ b/include/boost/math/ccmath/ccmath.hpp @@ -28,5 +28,6 @@ #include #include #include +#include #endif // BOOST_MATH_CCMATH_HPP diff --git a/include/boost/math/ccmath/copysign.hpp b/include/boost/math/ccmath/copysign.hpp new file mode 100644 index 0000000000..9894d754a3 --- /dev/null +++ b/include/boost/math/ccmath/copysign.hpp @@ -0,0 +1,95 @@ +// (C) Copyright Matt Borland 2021. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#ifndef BOOST_MATH_CCMATH_COPYSIGN_HPP +#define BOOST_MATH_CCMATH_COPYSIGN_HPP + +#include +#include +#include +#include +#include +#include + +namespace boost::math::ccmath { + +namespace detail { + +template +inline constexpr T copysign_impl(const T mag, const T sgn) noexcept +{ + if(sgn >= 0) + { + return boost::math::ccmath::abs(mag); + } + else + { + return -boost::math::ccmath::abs(mag); + } +} + +} // Namespace detail + +template , bool> = true> +inline constexpr Real copysign(Real mag, Real sgn) noexcept +{ + if(BOOST_MATH_IS_CONSTANT_EVALUATED(x)) + { + return boost::math::ccmath::detail::copysign_impl(mag, sgn); + } + else + { + using std::copysign; + return copysign(mag, sgn); + } +} + +template +inline constexpr auto copysign(T1 mag, T2 sgn) noexcept +{ + if(BOOST_MATH_IS_CONSTANT_EVALUATED(x)) + { + // If the type is an integer (e.g. epsilon == 0) then set the epsilon value to 1 so that type is at a minimum + // cast to double + constexpr auto T1p = std::numeric_limits::epsilon() > 0 ? std::numeric_limits::epsilon() : 1; + constexpr auto T2p = std::numeric_limits::epsilon() > 0 ? std::numeric_limits::epsilon() : 1; + + using promoted_type = + #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS + std::conditional_t>>>; + #else + >>; + #endif + + return boost::math::ccmath::copysign(promoted_type(mag), promoted_type(sgn)); + } + else + { + using std::copysign; + return copysign(mag, sgn); + } +} + +inline constexpr float copysignf(float mag, float sgn) noexcept +{ + return boost::math::ccmath::copysign(mag, sgn); +} + +#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS +inline constexpr long double copysignl(long double mag, long double sgn) noexcept +{ + return boost::math::ccmath::copysign(mag, sgn); +} +#endif + +} // Namespaces + +#endif // BOOST_MATH_CCMATH_COPYSIGN_HPP diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 9b06070d2c..dc6682abaf 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -142,6 +142,7 @@ test-suite special_fun : [ run ccmath_round_test.cpp ../../test/build//boost_unit_test_framework : : : [ requires cxx17_if_constexpr ] ] [ run ccmath_fmod_test.cpp ../../test/build//boost_unit_test_framework : : : [ requires cxx17_if_constexpr ] ] [ run ccmath_remainder_test.cpp ../../test/build//boost_unit_test_framework : : : [ requires cxx17_if_constexpr ] ] + [ run ccmath_copysign_test.cpp ../../test/build//boost_unit_test_framework : : : [ requires cxx17_if_constexpr ] ] [ run log1p_expm1_test.cpp test_instances//test_instances pch_light ../../test/build//boost_unit_test_framework ] [ run powm1_sqrtp1m1_test.cpp test_instances//test_instances pch_light ../../test/build//boost_unit_test_framework ] [ run special_functions_test.cpp ../../test/build//boost_unit_test_framework ] diff --git a/test/ccmath_copysign_test.cpp b/test/ccmath_copysign_test.cpp new file mode 100644 index 0000000000..6eb980bd4f --- /dev/null +++ b/test/ccmath_copysign_test.cpp @@ -0,0 +1,55 @@ +// (C) Copyright Matt Borland 2021. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef BOOST_HAS_FLOAT128 +#include +#endif + +template +constexpr void test() +{ + if constexpr (std::numeric_limits::has_quiet_NaN) + { + static_assert(boost::math::ccmath::isnan(boost::math::ccmath::copysign(std::numeric_limits::quiet_NaN(), T(1)))); + static_assert(boost::math::ccmath::isnan(boost::math::ccmath::copysign(std::numeric_limits::quiet_NaN(), T(-1)))); + } + + static_assert(boost::math::ccmath::copysign(T(1), T(2)) == T(1)); + static_assert(boost::math::ccmath::copysign(T(1), T(-2)) == T(-1)); + static_assert(boost::math::ccmath::copysign(std::numeric_limits::infinity(), T(2)) == std::numeric_limits::infinity()); + static_assert(boost::math::ccmath::copysign(std::numeric_limits::infinity(), T(-2)) == -std::numeric_limits::infinity()); +} + +#if !defined(BOOST_MATH_NO_CONSTEXPR_DETECTION) && !defined(BOOST_MATH_USING_BUILTIN_CONSTANT_P) +int main() +{ + test(); + test(); + + #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS + test(); + #endif + + #ifdef BOOST_HAS_FLOAT128 + test(); + #endif + + return 0; +} +#else +int main() +{ + return 0; +} +#endif diff --git a/test/compile_test/ccmath_copysign_incl_test.cpp b/test/compile_test/ccmath_copysign_incl_test.cpp new file mode 100644 index 0000000000..22e3cf28ef --- /dev/null +++ b/test/compile_test/ccmath_copysign_incl_test.cpp @@ -0,0 +1,16 @@ +// (C) Copyright Matt Borland 2021. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include +#include "test_compile_result.hpp" + +void compile_and_link_test() +{ + check_result(boost::math::ccmath::copysign(1.0f, 1.0f)); + check_result(boost::math::ccmath::copysign(1.0, 1.0)); +#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS + check_result(boost::math::ccmath::copysign(1.0l, 1.0l)); +#endif +} From 3c026f4f67dc17d3345718a307f08d0a174b420d Mon Sep 17 00:00:00 2001 From: Matt Borland Date: Sat, 9 Oct 2021 17:33:19 +0300 Subject: [PATCH 2/2] Fix for mingw test failure --- include/boost/math/ccmath/copysign.hpp | 4 ++-- test/ccmath_copysign_test.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/boost/math/ccmath/copysign.hpp b/include/boost/math/ccmath/copysign.hpp index 9894d754a3..6121ac5fe5 100644 --- a/include/boost/math/ccmath/copysign.hpp +++ b/include/boost/math/ccmath/copysign.hpp @@ -35,7 +35,7 @@ inline constexpr T copysign_impl(const T mag, const T sgn) noexcept template , bool> = true> inline constexpr Real copysign(Real mag, Real sgn) noexcept { - if(BOOST_MATH_IS_CONSTANT_EVALUATED(x)) + if(BOOST_MATH_IS_CONSTANT_EVALUATED(mag)) { return boost::math::ccmath::detail::copysign_impl(mag, sgn); } @@ -49,7 +49,7 @@ inline constexpr Real copysign(Real mag, Real sgn) noexcept template inline constexpr auto copysign(T1 mag, T2 sgn) noexcept { - if(BOOST_MATH_IS_CONSTANT_EVALUATED(x)) + if(BOOST_MATH_IS_CONSTANT_EVALUATED(mag)) { // If the type is an integer (e.g. epsilon == 0) then set the epsilon value to 1 so that type is at a minimum // cast to double diff --git a/test/ccmath_copysign_test.cpp b/test/ccmath_copysign_test.cpp index 6eb980bd4f..1998910563 100644 --- a/test/ccmath_copysign_test.cpp +++ b/test/ccmath_copysign_test.cpp @@ -16,6 +16,7 @@ #include #endif +#if !defined(BOOST_MATH_NO_CONSTEXPR_DETECTION) && !defined(BOOST_MATH_USING_BUILTIN_CONSTANT_P) template constexpr void test() { @@ -31,7 +32,6 @@ constexpr void test() static_assert(boost::math::ccmath::copysign(std::numeric_limits::infinity(), T(-2)) == -std::numeric_limits::infinity()); } -#if !defined(BOOST_MATH_NO_CONSTEXPR_DETECTION) && !defined(BOOST_MATH_USING_BUILTIN_CONSTANT_P) int main() { test();