Sprout/sprout/bit/clz.hpp

116 lines
3.8 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
2014-01-08 07:48:12 +00:00
Copyright (c) 2011-2014 Bolero MURAKAMI
2013-08-08 09:54:33 +00:00
https://github.com/bolero-MURAKAMI/Sprout
Distributed under 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)
=============================================================================*/
2012-06-22 13:56:25 +00:00
#ifndef SPROUT_BIT_CLZ_HPP
#define SPROUT_BIT_CLZ_HPP
#include <cstddef>
2012-06-22 13:56:25 +00:00
#include <climits>
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/utility/pair/pair.hpp>
#include <sprout/bit/shlr.hpp>
#include <sprout/bit/shll.hpp>
2012-06-22 13:56:25 +00:00
namespace sprout {
namespace detail {
# if SPROUT_USE_BUILTIN_BIT_OPERATION
inline SPROUT_CONSTEXPR int
2014-03-31 05:58:24 +00:00
clz_non0(unsigned x) {
return __builtin_clz(x);
2012-06-22 13:56:25 +00:00
}
inline SPROUT_CONSTEXPR int
2014-03-31 05:58:24 +00:00
clz_non0(unsigned long x) {
return __builtin_clzl(x);
2012-06-22 13:56:25 +00:00
}
inline SPROUT_CONSTEXPR int
2014-03-31 05:58:24 +00:00
clz_non0(unsigned long long x) {
return __builtin_clzll(x);
2012-06-22 13:56:25 +00:00
}
# endif
template<std::size_t N, typename Integral>
inline SPROUT_CONSTEXPR typename std::enable_if<
(N == 1),
sprout::pair<Integral, int>
>::type
clz_bytes(sprout::pair<Integral, int> const& xn) SPROUT_NOEXCEPT {
return xn;
}
template<std::size_t N, typename Integral>
inline SPROUT_CONSTEXPR typename std::enable_if<
(N > 1),
sprout::pair<Integral, int>
>::type
clz_bytes(sprout::pair<Integral, int> const& xn) SPROUT_NOEXCEPT {
typedef sprout::pair<Integral, int> type;
return sprout::detail::clz_bytes<N / 2>(
sprout::shlr(xn.first, (sizeof(Integral) - N / 2) * CHAR_BIT) ? xn
: type(sprout::shll(xn.first, N / 2 * CHAR_BIT), xn.second + N / 2 * CHAR_BIT)
);
}
template<typename Integral>
inline SPROUT_CONSTEXPR sprout::pair<Integral, int>
clz_bytes(sprout::pair<Integral, int> const& xn) SPROUT_NOEXCEPT {
return sprout::detail::clz_bytes<sizeof(Integral)>(xn);
}
2014-03-31 05:58:24 +00:00
template<typename Integral>
2012-06-22 13:56:25 +00:00
inline SPROUT_CONSTEXPR int
clz_bits1(sprout::pair<Integral, int> const& xn) SPROUT_NOEXCEPT {
return xn.second - sprout::shlr(xn.first, sizeof(Integral) * CHAR_BIT - 1);
2012-06-22 13:56:25 +00:00
}
2014-03-31 05:58:24 +00:00
template<typename Integral>
inline SPROUT_CONSTEXPR int
clz_bits2(sprout::pair<Integral, int> const& xn) SPROUT_NOEXCEPT {
typedef sprout::pair<Integral, int> type;
return sprout::detail::clz_bits1(
sprout::shlr(xn.first, sizeof(Integral) * CHAR_BIT - 2) ? xn
: type(sprout::shll(xn.first, 2), xn.second + 2)
);
2012-06-22 13:56:25 +00:00
}
2014-03-31 05:58:24 +00:00
template<typename Integral>
inline SPROUT_CONSTEXPR int
clz_bits4(sprout::pair<Integral, int> const& xn) SPROUT_NOEXCEPT {
typedef sprout::pair<Integral, int> type;
return sprout::detail::clz_bits2(
sprout::shlr(xn.first, sizeof(Integral) * CHAR_BIT - 4) ? xn
: type(sprout::shll(xn.first, 4), xn.second + 4)
);
}
template<typename Integral>
inline SPROUT_CONSTEXPR int
clz_bits(sprout::pair<Integral, int> const& xn) SPROUT_NOEXCEPT {
return sprout::detail::clz_bits4(xn);
}
template<typename Integral>
inline SPROUT_CONSTEXPR int
clz_non0(Integral x) SPROUT_NOEXCEPT {
typedef sprout::pair<Integral, int> type;
return sprout::detail::clz_bits(sprout::detail::clz_bytes(type(x, 1)));
2014-03-31 05:58:24 +00:00
}
template<typename Integral>
inline SPROUT_CONSTEXPR int
clz(Integral x) SPROUT_NOEXCEPT {
2014-03-31 05:58:24 +00:00
return x == 0 ? static_cast<int>(sizeof(x) * CHAR_BIT)
: sprout::detail::clz_non0(x)
;
2012-06-22 13:56:25 +00:00
}
} // namespace detail
//
// clz
//
2014-03-31 05:58:24 +00:00
template<typename Integral>
2012-12-17 11:10:17 +00:00
inline SPROUT_CONSTEXPR typename std::enable_if<
2014-03-31 05:58:24 +00:00
std::is_integral<Integral>::value,
2012-12-17 11:10:17 +00:00
int
>::type
clz(Integral x) SPROUT_NOEXCEPT {
2014-03-31 05:58:24 +00:00
return sprout::detail::clz(x);
2012-06-22 13:56:25 +00:00
}
} // namespace sprout
#endif // #ifndef SPROUT_BIT_CLZ_HPP