mirror of
https://github.com/bolero-MURAKAMI/Sprout.git
synced 2024-11-14 10:39:05 +00:00
fix ctz/clz bitwise operation implementation
This commit is contained in:
parent
fb69a146dd
commit
3e4dd5de00
3 changed files with 125 additions and 39 deletions
|
@ -30,7 +30,7 @@ namespace sprout {
|
||||||
# endif
|
# endif
|
||||||
template<typename Integral>
|
template<typename Integral>
|
||||||
inline SPROUT_CONSTEXPR int
|
inline SPROUT_CONSTEXPR int
|
||||||
clrsb(Integral x) {
|
clrsb(Integral x) SPROUT_NOEXCEPT {
|
||||||
return sprout::clz(~x);
|
return sprout::clz(~x);
|
||||||
}
|
}
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
|
@ -42,7 +42,7 @@ namespace sprout {
|
||||||
std::is_integral<Integral>::value,
|
std::is_integral<Integral>::value,
|
||||||
int
|
int
|
||||||
>::type
|
>::type
|
||||||
clrsb(Integral x) {
|
clrsb(Integral x) SPROUT_NOEXCEPT {
|
||||||
return sprout::detail::clrsb(x);
|
return sprout::detail::clrsb(x);
|
||||||
}
|
}
|
||||||
} // namespace sprout
|
} // namespace sprout
|
||||||
|
|
|
@ -11,6 +11,9 @@
|
||||||
#include <climits>
|
#include <climits>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <sprout/config.hpp>
|
#include <sprout/config.hpp>
|
||||||
|
#include <sprout/utility/pair/pair.hpp>
|
||||||
|
#include <sprout/bit/shlr.hpp>
|
||||||
|
#include <sprout/bit/shll.hpp>
|
||||||
|
|
||||||
namespace sprout {
|
namespace sprout {
|
||||||
namespace detail {
|
namespace detail {
|
||||||
|
@ -28,32 +31,68 @@ namespace sprout {
|
||||||
return __builtin_clzll(x);
|
return __builtin_clzll(x);
|
||||||
}
|
}
|
||||||
# endif
|
# endif
|
||||||
template<typename Integral>
|
template<std::size_t N, typename Integral>
|
||||||
inline SPROUT_CONSTEXPR int
|
inline SPROUT_CONSTEXPR typename std::enable_if<
|
||||||
clz_non0_impl(Integral x, Integral m = Integral(1) << (CHAR_BIT * sizeof(Integral) - 1)) {
|
(N == 1),
|
||||||
return m == 0 || x & m ? 0
|
sprout::pair<Integral, int>
|
||||||
: 1 + sprout::detail::clz_non0_impl(x, static_cast<Integral>(m >> 1))
|
>::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>
|
template<typename Integral>
|
||||||
inline SPROUT_CONSTEXPR typename std::enable_if<
|
inline SPROUT_CONSTEXPR sprout::pair<Integral, int>
|
||||||
std::is_unsigned<Integral>::value,
|
clz_bytes(sprout::pair<Integral, int> const& xn) SPROUT_NOEXCEPT {
|
||||||
int
|
return sprout::detail::clz_bytes<sizeof(Integral)>(xn);
|
||||||
>::type
|
|
||||||
clz_non0(Integral x) {
|
|
||||||
return sprout::detail::clz_non0_impl(static_cast<Integral>(x));
|
|
||||||
}
|
|
||||||
template<typename Integral>
|
|
||||||
inline SPROUT_CONSTEXPR typename std::enable_if<
|
|
||||||
std::is_signed<Integral>::value,
|
|
||||||
int
|
|
||||||
>::type
|
|
||||||
clz_non0(Integral x) {
|
|
||||||
return sprout::detail::clz_non0(static_cast<typename std::make_unsigned<Integral>::type>(x));
|
|
||||||
}
|
}
|
||||||
template<typename Integral>
|
template<typename Integral>
|
||||||
inline SPROUT_CONSTEXPR int
|
inline SPROUT_CONSTEXPR int
|
||||||
clz(Integral x) {
|
clz_bits1(sprout::pair<Integral, int> const& xn) SPROUT_NOEXCEPT {
|
||||||
|
return xn.second - sprout::shlr(xn.first, sizeof(Integral) * CHAR_BIT - 1);
|
||||||
|
}
|
||||||
|
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)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
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)));
|
||||||
|
}
|
||||||
|
template<typename Integral>
|
||||||
|
inline SPROUT_CONSTEXPR int
|
||||||
|
clz(Integral x) SPROUT_NOEXCEPT {
|
||||||
return x == 0 ? static_cast<int>(sizeof(x) * CHAR_BIT)
|
return x == 0 ? static_cast<int>(sizeof(x) * CHAR_BIT)
|
||||||
: sprout::detail::clz_non0(x)
|
: sprout::detail::clz_non0(x)
|
||||||
;
|
;
|
||||||
|
@ -67,7 +106,7 @@ namespace sprout {
|
||||||
std::is_integral<Integral>::value,
|
std::is_integral<Integral>::value,
|
||||||
int
|
int
|
||||||
>::type
|
>::type
|
||||||
clz(Integral x) {
|
clz(Integral x) SPROUT_NOEXCEPT {
|
||||||
return sprout::detail::clz(x);
|
return sprout::detail::clz(x);
|
||||||
}
|
}
|
||||||
} // namespace sprout
|
} // namespace sprout
|
||||||
|
|
|
@ -11,6 +11,10 @@
|
||||||
#include <climits>
|
#include <climits>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <sprout/config.hpp>
|
#include <sprout/config.hpp>
|
||||||
|
#include <sprout/limits.hpp>
|
||||||
|
#include <sprout/detail/integer.hpp>
|
||||||
|
#include <sprout/utility/pair/pair.hpp>
|
||||||
|
#include <sprout/bit/shlr.hpp>
|
||||||
|
|
||||||
namespace sprout {
|
namespace sprout {
|
||||||
namespace detail {
|
namespace detail {
|
||||||
|
@ -28,27 +32,70 @@ namespace sprout {
|
||||||
return __builtin_ctzll(x);
|
return __builtin_ctzll(x);
|
||||||
}
|
}
|
||||||
# endif
|
# endif
|
||||||
template<typename Integral>
|
template<std::size_t N, typename Integral>
|
||||||
inline SPROUT_CONSTEXPR typename std::enable_if<
|
inline SPROUT_CONSTEXPR typename std::enable_if<
|
||||||
std::is_unsigned<Integral>::value,
|
(N == 1),
|
||||||
int
|
sprout::pair<Integral, int>
|
||||||
>::type
|
>::type
|
||||||
ctz_non0(Integral x) {
|
ctz_bytes(sprout::pair<Integral, int> const& xn) SPROUT_NOEXCEPT {
|
||||||
return x & 1 ? 0
|
return xn;
|
||||||
: 1 + sprout::detail::ctz_non0(static_cast<Integral>(x >> 1))
|
}
|
||||||
;
|
template<std::size_t N, typename Integral>
|
||||||
|
inline SPROUT_CONSTEXPR typename std::enable_if<
|
||||||
|
(N > 1),
|
||||||
|
sprout::pair<Integral, int>
|
||||||
|
>::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>
|
template<typename Integral>
|
||||||
inline SPROUT_CONSTEXPR typename std::enable_if<
|
inline SPROUT_CONSTEXPR sprout::pair<Integral, int>
|
||||||
std::is_signed<Integral>::value,
|
ctz_bytes(sprout::pair<Integral, int> const& xn) SPROUT_NOEXCEPT {
|
||||||
int
|
return sprout::detail::ctz_bytes<sizeof(Integral)>(xn);
|
||||||
>::type
|
|
||||||
ctz_non0(Integral x) {
|
|
||||||
return sprout::detail::ctz_non0(static_cast<typename std::make_unsigned<Integral>::type>(x));
|
|
||||||
}
|
}
|
||||||
template<typename Integral>
|
template<typename Integral>
|
||||||
inline SPROUT_CONSTEXPR int
|
inline SPROUT_CONSTEXPR int
|
||||||
ctz(Integral x) {
|
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)));
|
||||||
|
}
|
||||||
|
template<typename Integral>
|
||||||
|
inline SPROUT_CONSTEXPR int
|
||||||
|
ctz(Integral x) SPROUT_NOEXCEPT {
|
||||||
return x == 0 ? static_cast<int>(sizeof(x) * CHAR_BIT)
|
return x == 0 ? static_cast<int>(sizeof(x) * CHAR_BIT)
|
||||||
: sprout::detail::ctz_non0(x)
|
: sprout::detail::ctz_non0(x)
|
||||||
;
|
;
|
||||||
|
@ -62,7 +109,7 @@ namespace sprout {
|
||||||
std::is_integral<Integral>::value,
|
std::is_integral<Integral>::value,
|
||||||
int
|
int
|
||||||
>::type
|
>::type
|
||||||
ctz(Integral x) {
|
ctz(Integral x) SPROUT_NOEXCEPT {
|
||||||
return sprout::detail::ctz(x);
|
return sprout::detail::ctz(x);
|
||||||
}
|
}
|
||||||
} // namespace sprout
|
} // namespace sprout
|
||||||
|
|
Loading…
Reference in a new issue