mirror of
https://github.com/boostorg/math.git
synced 2026-01-19 04:22:09 +00:00
constexpr signbit (#793)
Implements constexpr signbit with annotated caveats during compile time.
This commit is contained in:
@@ -195,6 +195,9 @@ All of the following functions require C++17 or greater.
|
||||
template <typename T>
|
||||
constexpr Promoted nexttoward(T from, long double to)
|
||||
|
||||
template <typename T>
|
||||
constexpr bool signbit(T arg)
|
||||
|
||||
} // Namespaces
|
||||
|
||||
[endsect] [/section:ccmath Constexpr CMath]
|
||||
|
||||
@@ -39,5 +39,7 @@
|
||||
#include <boost/math/ccmath/islessequal.hpp>
|
||||
#include <boost/math/ccmath/isunordered.hpp>
|
||||
#include <boost/math/ccmath/fma.hpp>
|
||||
#include <boost/math/ccmath/next.hpp>
|
||||
#include <boost/math/ccmath/signbit.hpp>
|
||||
|
||||
#endif // BOOST_MATH_CCMATH_HPP
|
||||
|
||||
60
include/boost/math/ccmath/signbit.hpp
Normal file
60
include/boost/math/ccmath/signbit.hpp
Normal file
@@ -0,0 +1,60 @@
|
||||
// (C) Copyright Matt Borland 2022.
|
||||
// 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_SIGNBIT_HPP
|
||||
#define BOOST_MATH_CCMATH_SIGNBIT_HPP
|
||||
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
#include <type_traits>
|
||||
#include <boost/math/tools/is_constant_evaluated.hpp>
|
||||
#include <boost/math/ccmath/isnan.hpp>
|
||||
|
||||
namespace boost::math::ccmath {
|
||||
|
||||
namespace detail {
|
||||
|
||||
// Typical implementations of signbit involve type punning via union and manipulating
|
||||
// overflow (see libc++ or musl). Neither of these are allowed in constexpr contexts
|
||||
// (technically type punning via union in general is UB in c++ but well defined in C)
|
||||
// therefore NANs and 0s are treated as positive
|
||||
|
||||
template <typename T>
|
||||
constexpr bool signbit_impl(T arg)
|
||||
{
|
||||
if (boost::math::ccmath::isnan(arg))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return arg < static_cast<T>(0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Return value: true if arg is negative, false if arg is 0, NAN, or positive
|
||||
template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true>
|
||||
constexpr bool signbit(Real arg)
|
||||
{
|
||||
if (BOOST_MATH_IS_CONSTANT_EVALUATED(arg))
|
||||
{
|
||||
return boost::math::ccmath::detail::signbit_impl(arg);
|
||||
}
|
||||
else
|
||||
{
|
||||
using std::signbit;
|
||||
return signbit(arg);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Z, std::enable_if_t<std::is_integral_v<Z>, bool> = true>
|
||||
constexpr bool signbit(Z arg)
|
||||
{
|
||||
return boost::math::ccmath::signbit(static_cast<double>(arg));
|
||||
}
|
||||
|
||||
} // Namespaces
|
||||
|
||||
#endif // BOOST_MATH_CCMATH_SIGNBIT_HPP
|
||||
@@ -161,6 +161,7 @@ test-suite special_fun :
|
||||
[ run ccmath_isunordered_test.cpp ../../test/build//boost_unit_test_framework : : : [ requires cxx17_if_constexpr ] ]
|
||||
[ run ccmath_next_test.cpp ../../test/build//boost_unit_test_framework : : : [ requires cxx17_if_constexpr ] ]
|
||||
[ run ccmath_fma_test.cpp ../../test/build//boost_unit_test_framework : : : [ requires cxx17_if_constexpr ] ]
|
||||
[ run ccmath_signbit_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 git_issue_705.cpp ../../test/build//boost_unit_test_framework ]
|
||||
|
||||
43
test/ccmath_signbit_test.cpp
Normal file
43
test/ccmath_signbit_test.cpp
Normal file
@@ -0,0 +1,43 @@
|
||||
// (C) Copyright Matt Borland 2022.
|
||||
// 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 <type_traits>
|
||||
#include <boost/math/ccmath/signbit.hpp>
|
||||
|
||||
#if !defined(BOOST_MATH_NO_CONSTEXPR_DETECTION) && !defined(BOOST_MATH_USING_BUILTIN_CONSTANT_P)
|
||||
template <typename T>
|
||||
void test()
|
||||
{
|
||||
// Edge cases
|
||||
static_assert(boost::math::ccmath::signbit(T(0)) == false);
|
||||
static_assert(boost::math::ccmath::signbit(std::numeric_limits<T>::quiet_NaN()) == false);
|
||||
static_assert(boost::math::ccmath::signbit(std::numeric_limits<T>::signaling_NaN()) == false);
|
||||
|
||||
// Positive numbers
|
||||
static_assert(boost::math::ccmath::signbit(std::numeric_limits<T>::infinity()) == false);
|
||||
static_assert(boost::math::ccmath::signbit(T(1)) == false);
|
||||
|
||||
// Negative numbers
|
||||
static_assert(boost::math::ccmath::signbit(-std::numeric_limits<T>::infinity()) == true);
|
||||
static_assert(boost::math::ccmath::signbit(T(-1)) == true);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
test<float>();
|
||||
test<double>();
|
||||
|
||||
#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
|
||||
test<long double>();
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
#else
|
||||
int main(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
16
test/compile_test/ccmath_signbit_incl_test.cpp
Normal file
16
test/compile_test/ccmath_signbit_incl_test.cpp
Normal file
@@ -0,0 +1,16 @@
|
||||
// (C) Copyright Matt Borland 2022.
|
||||
// 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 <boost/math/ccmath/signbit.hpp>
|
||||
#include "test_compile_result.hpp"
|
||||
|
||||
void compile_and_link_test()
|
||||
{
|
||||
check_result<bool>(boost::math::ccmath::signbit(1.0F));
|
||||
check_result<bool>(boost::math::ccmath::signbit(1.0));
|
||||
#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
|
||||
check_result<bool>(boost::math::ccmath::signbit(1.0L));
|
||||
#endif
|
||||
}
|
||||
Reference in New Issue
Block a user