You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

26 lines
610 B

#pragma once
#include <stdexcept>
#include <type_traits>
namespace detail
{
template <typename Integer>
constexpr Integer intlog2_impl(Integer integer)
{
auto degree = Integer{0};
while ((integer >>= 1) > 0) { ++degree; }
return degree;
}
} // namespace detail
template <typename Integer>
constexpr Integer intlog2(Integer integer)
{
static_assert(std::is_integral_v<Integer>, "Input type must be an integral type.");
return integer > 0 ? detail::intlog2_impl(integer)
: throw std::domain_error("The binary logarithm is not defined on non-positive numbers.");
}