Sprout/sprout/bit/ctz.hpp

118 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_CTZ_HPP
#define SPROUT_BIT_CTZ_HPP
2014-03-31 05:58:24 +00:00
#include <climits>
2012-06-22 13:56:25 +00:00
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/limits.hpp>
#include <sprout/detail/integer.hpp>
#include <sprout/utility/pair/pair.hpp>
#include <sprout/bit/shlr.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
ctz_non0(unsigned x) {
return __builtin_ctz(x);
2012-06-22 13:56:25 +00:00
}
inline SPROUT_CONSTEXPR int
2014-03-31 05:58:24 +00:00
ctz_non0(unsigned long x) {
return __builtin_ctzl(x);
2012-06-22 13:56:25 +00:00
}
inline SPROUT_CONSTEXPR int
2014-03-31 05:58:24 +00:00
ctz_non0(unsigned long long x) {
return __builtin_ctzll(x);
2012-06-22 13:56:25 +00:00
}
# endif
template<std::size_t N, typename Integral>
2012-12-17 11:10:17 +00:00
inline SPROUT_CONSTEXPR typename std::enable_if<
(N == 1),
sprout::pair<Integral, int>
2012-12-17 11:10:17 +00:00
>::type
ctz_bytes(sprout::pair<Integral, int> const& xn) SPROUT_NOEXCEPT {
return xn;
2012-06-22 13:56:25 +00:00
}
template<std::size_t N, typename Integral>
2012-12-17 11:10:17 +00:00
inline SPROUT_CONSTEXPR typename std::enable_if<
(N > 1),
sprout::pair<Integral, int>
2012-12-17 11:10:17 +00:00
>::type
ctz_bytes(sprout::pair<Integral, int> const& xn) SPROUT_NOEXCEPT {
typedef sprout::pair<Integral, int> type;
return sprout::detail::ctz_bytes<N / 2>(
(xn.first & static_cast<Integral>(sprout::numeric_limits<typename sprout::detail::uint_t<N / 2 * CHAR_BIT>::least>::max())) ? xn
: type(sprout::shlr(xn.first, N / 2 * CHAR_BIT), xn.second + N / 2 * CHAR_BIT)
);
}
template<typename Integral>
inline SPROUT_CONSTEXPR sprout::pair<Integral, int>
ctz_bytes(sprout::pair<Integral, int> const& xn) SPROUT_NOEXCEPT {
return sprout::detail::ctz_bytes<sizeof(Integral)>(xn);
}
template<typename Integral>
inline SPROUT_CONSTEXPR int
ctz_bits1(sprout::pair<Integral, int> const& xn) SPROUT_NOEXCEPT {
return (xn.first & Integral(0x1ul)) ? xn.second
: xn.second + 1
;
}
template<typename Integral>
inline SPROUT_CONSTEXPR int
ctz_bits2(sprout::pair<Integral, int> const& xn) SPROUT_NOEXCEPT {
typedef sprout::pair<Integral, int> type;
return sprout::detail::ctz_bits1(
(xn.first & Integral(0x3ul)) ? xn
: type(sprout::shlr(xn.first, 2), xn.second + 2)
);
}
template<typename Integral>
inline SPROUT_CONSTEXPR int
ctz_bits4(sprout::pair<Integral, int> const& xn) SPROUT_NOEXCEPT {
typedef sprout::pair<Integral, int> type;
return sprout::detail::ctz_bits2(
(xn.first & Integral(0xful)) ? xn
: type(sprout::shlr(xn.first, 4), xn.second + 4)
);
}
template<typename Integral>
inline SPROUT_CONSTEXPR int
ctz_bits(sprout::pair<Integral, int> const& xn) SPROUT_NOEXCEPT {
return sprout::detail::ctz_bits4(xn);
}
template<typename Integral>
inline SPROUT_CONSTEXPR int
ctz_non0(Integral x) SPROUT_NOEXCEPT {
typedef sprout::pair<Integral, int> type;
return sprout::detail::ctz_bits(sprout::detail::ctz_bytes(type(x, 0)));
2014-03-31 05:58:24 +00:00
}
template<typename Integral>
inline SPROUT_CONSTEXPR int
ctz(Integral x) SPROUT_NOEXCEPT {
2014-03-31 05:58:24 +00:00
return x == 0 ? static_cast<int>(sizeof(x) * CHAR_BIT)
: sprout::detail::ctz_non0(x)
;
2012-06-22 13:56:25 +00:00
}
} // namespace detail
//
// ctz
//
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
ctz(Integral x) SPROUT_NOEXCEPT {
2014-03-31 05:58:24 +00:00
return sprout::detail::ctz(x);
2012-06-22 13:56:25 +00:00
}
} // namespace sprout
#endif // #ifndef SPROUT_BIT_CTZ_HPP