Skip to content

Commit 203244b

Browse files
committed
logb impl
1 parent 153d4ad commit 203244b

File tree

1 file changed

+44
-0
lines changed
  • include/boost/decimal/detail/cmath

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2023 Matt Borland
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// https://www.boost.org/LICENSE_1_0.txt
4+
5+
#ifndef BOOST_DECIMAL_DETAIL_CMATH_LOGB_HPP
6+
#define BOOST_DECIMAL_DETAIL_CMATH_LOGB_HPP
7+
8+
#include <cmath>
9+
#include <type_traits>
10+
11+
#include <boost/decimal/fwd.hpp> // NOLINT(llvm-include-order)
12+
#include <boost/decimal/detail/type_traits.hpp>
13+
14+
namespace boost {
15+
namespace decimal {
16+
17+
template <typename T>
18+
constexpr auto logb(T num) noexcept -> std::enable_if_t<detail::is_decimal_floating_point_v<T>, T>
19+
{
20+
const auto fpc {fpclassify(num)};
21+
22+
if (fpc == FP_ZERO)
23+
{
24+
return -std::numeric_limits<T>::infinity();
25+
}
26+
else if (fpc == FP_INFINITE)
27+
{
28+
return std::numeric_limits<T>::infinity();
29+
}
30+
else if (fpc == FP_NAN)
31+
{
32+
return num;
33+
}
34+
35+
const auto offset = detail::num_digits(num.full_significand()) - 1;
36+
const auto expval = static_cast<int>(static_cast<int>(num.unbiased_exponent()) + offset);
37+
38+
return static_cast<T>(expval);
39+
}
40+
41+
}
42+
}
43+
44+
#endif //BOOST_DECIMAL_DETAIL_CMATH_LOGB_HPP

0 commit comments

Comments
 (0)