-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements constexpr signbit with annotated caveats during compile time.
- Loading branch information
Showing
6 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |