Skip to content

Commit

Permalink
constexpr signbit (#793)
Browse files Browse the repository at this point in the history
Implements constexpr signbit with annotated caveats during compile time.
  • Loading branch information
mborland authored Jul 2, 2022
1 parent ae0fe3d commit 1cbf7e2
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 0 deletions.
3 changes: 3 additions & 0 deletions doc/sf/ccmath.qbk
Original file line number Diff line number Diff line change
Expand Up @@ -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]
2 changes: 2 additions & 0 deletions include/boost/math/ccmath/ccmath.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 60 additions & 0 deletions include/boost/math/ccmath/signbit.hpp
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions test/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
Expand Down
43 changes: 43 additions & 0 deletions test/ccmath_signbit_test.cpp
Original file line number Diff line number Diff line change
@@ -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 changes: 16 additions & 0 deletions test/compile_test/ccmath_signbit_incl_test.cpp
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 1cbf7e2

Please sign in to comment.