1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2024-11-12 21:09:01 +00:00
Sprout/sprout/bit/clz.hpp

76 lines
2.1 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 <climits>
#include <type_traits>
#include <sprout/config.hpp>
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
2014-03-31 05:58:24 +00:00
template<typename Integral>
2012-06-22 13:56:25 +00:00
inline SPROUT_CONSTEXPR int
2014-03-31 05:58:24 +00:00
clz_non0_impl(Integral x, Integral m = Integral(1) << (CHAR_BIT * sizeof(Integral) - 1)) {
return m == 0 || x & m ? 0
: 1 + sprout::detail::clz_non0_impl(x, static_cast<Integral>(m >> 1))
2012-06-22 13:56:25 +00:00
;
}
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_unsigned<Integral>::value,
2012-12-17 11:10:17 +00:00
int
>::type
2014-03-31 05:58:24 +00:00
clz_non0(Integral x) {
return sprout::detail::clz_non0_impl(static_cast<Integral>(x));
2012-06-22 13:56:25 +00:00
}
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_signed<Integral>::value,
2012-12-17 11:10:17 +00:00
int
>::type
2014-03-31 05:58:24 +00:00
clz_non0(Integral x) {
return sprout::detail::clz_non0(static_cast<typename std::make_unsigned<Integral>::type>(x));
}
template<typename Integral>
inline SPROUT_CONSTEXPR int
clz(Integral x) {
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
2014-03-31 05:58:24 +00:00
clz(Integral x) {
return sprout::detail::clz(x);
2012-06-22 13:56:25 +00:00
}
} // namespace sprout
#endif // #ifndef SPROUT_BIT_CLZ_HPP