mirror of
https://github.com/bolero-MURAKAMI/Sprout.git
synced 2025-05-10 09:23:30 +00:00
add c++1y rightmost bit manipurations
This commit is contained in:
parent
56c9d52436
commit
e244ea2932
21 changed files with 560 additions and 183 deletions
80
sprout/bit/bit_length.hpp
Normal file
80
sprout/bit/bit_length.hpp
Normal file
|
@ -0,0 +1,80 @@
|
|||
/*=============================================================================
|
||||
Copyright (c) 2011-2014 Bolero MURAKAMI
|
||||
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)
|
||||
=============================================================================*/
|
||||
#ifndef SPROUT_BIT_BIT_LENGTH_HPP
|
||||
#define SPROUT_BIT_BIT_LENGTH_HPP
|
||||
|
||||
#include <climits>
|
||||
#include <cstddef>
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace detail {
|
||||
SPROUT_STATIC_CONSTEXPR std::size_t bit_len_8_table[std::size_t(UCHAR_MAX) + 1] = {
|
||||
0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
|
||||
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
|
||||
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
|
||||
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
|
||||
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
|
||||
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
|
||||
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
|
||||
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
|
||||
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
|
||||
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
|
||||
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
|
||||
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
|
||||
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
|
||||
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
|
||||
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
|
||||
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8
|
||||
};
|
||||
template<std::size_t Size>
|
||||
struct bit_len {
|
||||
private:
|
||||
SPROUT_STATIC_CONSTEXPR std::size_t next_size = Size - 1;
|
||||
SPROUT_STATIC_CONSTEXPR std::size_t shift_bits = next_size * CHAR_BIT;
|
||||
private:
|
||||
template<typename Integral>
|
||||
SPROUT_CONSTEXPR Integral
|
||||
impl(Integral x, unsigned char i) const {
|
||||
return bit_len_8_table[i]
|
||||
? bit_len_8_table[i] + next_size * CHAR_BIT
|
||||
: sprout::detail::bit_len<next_size>().template operator()(x)
|
||||
;
|
||||
}
|
||||
public:
|
||||
template<typename Integral>
|
||||
SPROUT_CONSTEXPR Integral
|
||||
operator()(Integral x) const {
|
||||
return impl(x, static_cast<unsigned char>((x >> shift_bits) & UCHAR_MAX));
|
||||
}
|
||||
};
|
||||
template<>
|
||||
struct bit_len<1> {
|
||||
public:
|
||||
template<typename Integral>
|
||||
SPROUT_CONSTEXPR Integral
|
||||
operator()(Integral x) const {
|
||||
return bit_len_8_table[static_cast<unsigned char>(x & UCHAR_MAX)];
|
||||
}
|
||||
};
|
||||
} // namespace detail
|
||||
//
|
||||
// bit_length
|
||||
//
|
||||
template<typename Integral>
|
||||
inline SPROUT_CONSTEXPR typename std::enable_if<
|
||||
std::is_integral<Integral>::value,
|
||||
Integral
|
||||
>::type
|
||||
bit_length(Integral x) {
|
||||
return sprout::detail::bit_len<sizeof(Integral)>().template operator()(x);
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_BIT_BIT_LENGTH_HPP
|
90
sprout/bit/bit_reverse.hpp
Normal file
90
sprout/bit/bit_reverse.hpp
Normal file
|
@ -0,0 +1,90 @@
|
|||
/*=============================================================================
|
||||
Copyright (c) 2011-2014 Bolero MURAKAMI
|
||||
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)
|
||||
=============================================================================*/
|
||||
#ifndef SPROUT_BIT_BIT_REVERSE_HPP
|
||||
#define SPROUT_BIT_BIT_REVERSE_HPP
|
||||
|
||||
#include <climits>
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/assert.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace detail {
|
||||
SPROUT_STATIC_CONSTEXPR unsigned char bit_rev_8_table[std::size_t(UCHAR_MAX) + 1] = {
|
||||
0, 128, 64, 192, 32, 160, 96, 224, 16, 144, 80, 208, 48, 176, 112, 240, 8, 136, 72, 200,
|
||||
40, 168, 104, 232, 24, 152, 88, 216, 56, 184, 120, 248, 4, 132, 68, 196, 36, 164, 100, 228,
|
||||
20, 148, 84, 212, 52, 180, 116, 244, 12, 140, 76, 204, 44, 172, 108, 236, 28, 156, 92, 220,
|
||||
60, 188, 124, 252, 2, 130, 66, 194, 34, 162, 98, 226, 18, 146, 82, 210, 50, 178, 114, 242,
|
||||
10, 138, 74, 202, 42, 170, 106, 234, 26, 154, 90, 218, 58, 186, 122, 250, 6, 134, 70, 198,
|
||||
38, 166, 102, 230, 22, 150, 86, 214, 54, 182, 118, 246, 14, 142, 78, 206, 46, 174, 110, 238,
|
||||
30, 158, 94, 222, 62, 190, 126, 254, 1, 129, 65, 193, 33, 161, 97, 225, 17, 145, 81, 209,
|
||||
49, 177, 113, 241, 9, 137, 73, 201, 41, 169, 105, 233, 25, 153, 89, 217, 57, 185, 121, 249,
|
||||
5, 133, 69, 197, 37, 165, 101, 229, 21, 149, 85, 213, 53, 181, 117, 245, 13, 141, 77, 205,
|
||||
45, 173, 109, 237, 29, 157, 93, 221, 61, 189, 125, 253, 3, 131, 67, 195, 35, 163, 99, 227,
|
||||
19, 147, 83, 211, 51, 179, 115, 243, 11, 139, 75, 203, 43, 171, 107, 235, 27, 155, 91, 219,
|
||||
59, 187, 123, 251, 7, 135, 71, 199, 39, 167, 103, 231, 23, 151, 87, 215, 55, 183, 119, 247,
|
||||
15, 143, 79, 207, 47, 175, 111, 239, 31, 159, 95, 223, 63, 191, 127, 255
|
||||
};
|
||||
template<std::size_t Size>
|
||||
struct bit_rev {
|
||||
private:
|
||||
SPROUT_STATIC_CONSTEXPR std::size_t next_size = Size / 2;
|
||||
SPROUT_STATIC_CONSTEXPR std::size_t shift_bits = next_size * CHAR_BIT;
|
||||
public:
|
||||
template<typename Integral>
|
||||
SPROUT_CONSTEXPR Integral
|
||||
operator()(Integral x) const {
|
||||
return (sprout::detail::bit_rev<next_size>().template operator()(x) << shift_bits)
|
||||
| (sprout::detail::bit_rev<next_size>().template operator()(x >> shift_bits))
|
||||
;
|
||||
}
|
||||
};
|
||||
template<>
|
||||
struct bit_rev<1> {
|
||||
public:
|
||||
template<typename Integral>
|
||||
SPROUT_CONSTEXPR Integral
|
||||
operator()(Integral x) const {
|
||||
return sprout::detail::bit_rev_8_table[static_cast<unsigned char>(x & UCHAR_MAX)];
|
||||
}
|
||||
};
|
||||
} // namespace detail
|
||||
//
|
||||
// bit_reverse
|
||||
//
|
||||
template<typename Integral>
|
||||
SPROUT_CONSTEXPR typename std::enable_if<
|
||||
std::is_integral<Integral>::value,
|
||||
Integral
|
||||
>::type
|
||||
bit_reverse(Integral x) {
|
||||
typedef typename std::make_unsigned<Integral>::type unsigned_type;
|
||||
return static_cast<Integral>(
|
||||
sprout::detail::bit_rev<sizeof(Integral)>().template operator()<unsigned_type>(x)
|
||||
);
|
||||
}
|
||||
//
|
||||
// bit_reverse_in
|
||||
//
|
||||
template<typename Integral>
|
||||
inline SPROUT_CONSTEXPR typename std::enable_if<
|
||||
std::is_integral<Integral>::value,
|
||||
Integral
|
||||
>::type
|
||||
bit_reverse_in(Integral x, int length) {
|
||||
typedef typename std::make_unsigned<Integral>::type unsigned_type;
|
||||
return SPROUT_ASSERT(length <= sizeof(Integral) * CHAR_BIT),
|
||||
static_cast<Integral>(
|
||||
sprout::detail::bit_rev<sizeof(Integral)>().template operator()<unsigned_type>(x)
|
||||
>> (sizeof(Integral) * CHAR_BIT - length)
|
||||
)
|
||||
;
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_BIT_BIT_REVERSE_HPP
|
22
sprout/bit/count.hpp
Normal file
22
sprout/bit/count.hpp
Normal file
|
@ -0,0 +1,22 @@
|
|||
/*=============================================================================
|
||||
Copyright (c) 2011-2014 Bolero MURAKAMI
|
||||
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)
|
||||
=============================================================================*/
|
||||
#ifndef SPROUT_BIT_COUNT_HPP
|
||||
#define SPROUT_BIT_COUNT_HPP
|
||||
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/bit/cntl0.hpp>
|
||||
#include <sprout/bit/cntt0.hpp>
|
||||
#include <sprout/bit/cntl1.hpp>
|
||||
#include <sprout/bit/cntt1.hpp>
|
||||
#include <sprout/bit/popcount.hpp>
|
||||
#include <sprout/bit/parity.hpp>
|
||||
#include <sprout/bit/clz.hpp>
|
||||
#include <sprout/bit/ctz.hpp>
|
||||
#include <sprout/bit/clrsb.hpp>
|
||||
|
||||
#endif // #ifndef SPROUT_BIT_COUNT_HPP
|
|
@ -5,38 +5,24 @@
|
|||
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)
|
||||
=============================================================================*/
|
||||
#ifndef SPROUT_BIT_ROTATE_HPP
|
||||
#define SPROUT_BIT_ROTATE_HPP
|
||||
#ifndef SPROUT_BIT_ISOLS0B_HPP
|
||||
#define SPROUT_BIT_RSTLS0B_HPP
|
||||
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/bit/rotl.hpp>
|
||||
#include <sprout/bit/rotr.hpp>
|
||||
|
||||
namespace sprout {
|
||||
//
|
||||
// left_rotate
|
||||
// isols0b
|
||||
//
|
||||
template<typename Integral>
|
||||
inline SPROUT_CONSTEXPR typename std::enable_if<
|
||||
std::is_integral<Integral>::value,
|
||||
Integral
|
||||
>::type
|
||||
left_rotate(Integral x, int s) {
|
||||
return sprout::rotl(x, s);
|
||||
}
|
||||
|
||||
//
|
||||
// right_rotate
|
||||
//
|
||||
template<typename Integral>
|
||||
inline SPROUT_CONSTEXPR typename std::enable_if<
|
||||
std::is_integral<Integral>::value,
|
||||
Integral
|
||||
>::type
|
||||
right_rotate(Integral x, int s) {
|
||||
return sprout::rotr(x, s);
|
||||
isols0b(Integral x) SPROUT_NOEXCEPT {
|
||||
return ~x & (x + 1);
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_BIT_ROTATE_HPP
|
||||
#endif // #ifndef SPROUT_BIT_RSTLS0B_HPP
|
28
sprout/bit/isols1b.hpp
Normal file
28
sprout/bit/isols1b.hpp
Normal file
|
@ -0,0 +1,28 @@
|
|||
/*=============================================================================
|
||||
Copyright (c) 2011-2014 Bolero MURAKAMI
|
||||
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)
|
||||
=============================================================================*/
|
||||
#ifndef SPROUT_BIT_ISOLS1B_HPP
|
||||
#define SPROUT_BIT_RSTLS1B_HPP
|
||||
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
|
||||
namespace sprout {
|
||||
//
|
||||
// isols1b
|
||||
//
|
||||
template<typename Integral>
|
||||
inline SPROUT_CONSTEXPR typename std::enable_if<
|
||||
std::is_integral<Integral>::value,
|
||||
Integral
|
||||
>::type
|
||||
isols1b(Integral x) SPROUT_NOEXCEPT {
|
||||
return x & -x;
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_BIT_RSTLS1B_HPP
|
|
@ -8,73 +8,7 @@
|
|||
#ifndef SPROUT_BIT_LENGTH_HPP
|
||||
#define SPROUT_BIT_LENGTH_HPP
|
||||
|
||||
#include <climits>
|
||||
#include <cstddef>
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace detail {
|
||||
SPROUT_STATIC_CONSTEXPR std::size_t bit_len_8_table[std::size_t(UCHAR_MAX) + 1] = {
|
||||
0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
|
||||
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
|
||||
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
|
||||
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
|
||||
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
|
||||
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
|
||||
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
|
||||
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
|
||||
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
|
||||
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
|
||||
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
|
||||
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
|
||||
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
|
||||
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
|
||||
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
|
||||
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8
|
||||
};
|
||||
template<std::size_t Size>
|
||||
struct bit_len {
|
||||
private:
|
||||
SPROUT_STATIC_CONSTEXPR std::size_t next_size = Size - 1;
|
||||
SPROUT_STATIC_CONSTEXPR std::size_t shift_bits = next_size * CHAR_BIT;
|
||||
private:
|
||||
template<typename IntType>
|
||||
SPROUT_CONSTEXPR IntType
|
||||
impl(IntType x, unsigned char i) const {
|
||||
return bit_len_8_table[i]
|
||||
? bit_len_8_table[i] + next_size * CHAR_BIT
|
||||
: sprout::detail::bit_len<next_size>().template operator()(x)
|
||||
;
|
||||
}
|
||||
public:
|
||||
template<typename IntType>
|
||||
SPROUT_CONSTEXPR IntType
|
||||
operator()(IntType x) const {
|
||||
return impl(x, static_cast<unsigned char>((x >> shift_bits) & UCHAR_MAX));
|
||||
}
|
||||
};
|
||||
template<>
|
||||
struct bit_len<1> {
|
||||
public:
|
||||
template<typename IntType>
|
||||
SPROUT_CONSTEXPR IntType
|
||||
operator()(IntType x) const {
|
||||
return bit_len_8_table[static_cast<unsigned char>(x & UCHAR_MAX)];
|
||||
}
|
||||
};
|
||||
} // namespace detail
|
||||
//
|
||||
// bit_length
|
||||
//
|
||||
template<typename IntType>
|
||||
inline SPROUT_CONSTEXPR typename std::enable_if<
|
||||
std::is_integral<IntType>::value,
|
||||
IntType
|
||||
>::type
|
||||
bit_length(IntType x) {
|
||||
return sprout::detail::bit_len<sizeof(IntType)>().template operator()(x);
|
||||
}
|
||||
} // namespace sprout
|
||||
#include <sprout/bit/bit_length.hpp>
|
||||
|
||||
#endif // #ifndef SPROUT_BIT_LENGTH_HPP
|
||||
|
|
28
sprout/bit/maskt0.hpp
Normal file
28
sprout/bit/maskt0.hpp
Normal file
|
@ -0,0 +1,28 @@
|
|||
/*=============================================================================
|
||||
Copyright (c) 2011-2014 Bolero MURAKAMI
|
||||
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)
|
||||
=============================================================================*/
|
||||
#ifndef SPROUT_BIT_MASKT0_HPP
|
||||
#define SPROUT_BIT_MASKT0_HPP
|
||||
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
|
||||
namespace sprout {
|
||||
//
|
||||
// maskt0
|
||||
//
|
||||
template<typename Integral>
|
||||
inline SPROUT_CONSTEXPR typename std::enable_if<
|
||||
std::is_integral<Integral>::value,
|
||||
Integral
|
||||
>::type
|
||||
maskt0(Integral x) SPROUT_NOEXCEPT {
|
||||
return ~x & (x - 1);
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_BIT_MASKT0_HPP
|
28
sprout/bit/maskt0ls1b.hpp
Normal file
28
sprout/bit/maskt0ls1b.hpp
Normal file
|
@ -0,0 +1,28 @@
|
|||
/*=============================================================================
|
||||
Copyright (c) 2011-2014 Bolero MURAKAMI
|
||||
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)
|
||||
=============================================================================*/
|
||||
#ifndef SPROUT_BIT_MASKT0LS1B_HPP
|
||||
#define SPROUT_BIT_MASKT0LS1B_HPP
|
||||
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
|
||||
namespace sprout {
|
||||
//
|
||||
// maskt0ls1b
|
||||
//
|
||||
template<typename Integral>
|
||||
inline SPROUT_CONSTEXPR typename std::enable_if<
|
||||
std::is_integral<Integral>::value,
|
||||
Integral
|
||||
>::type
|
||||
maskt0ls1b(Integral x) SPROUT_NOEXCEPT {
|
||||
return (x - 1) ^ x;
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_BIT_MASKT0LS1B_HPP
|
28
sprout/bit/maskt1.hpp
Normal file
28
sprout/bit/maskt1.hpp
Normal file
|
@ -0,0 +1,28 @@
|
|||
/*=============================================================================
|
||||
Copyright (c) 2011-2014 Bolero MURAKAMI
|
||||
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)
|
||||
=============================================================================*/
|
||||
#ifndef SPROUT_BIT_MASKT1_HPP
|
||||
#define SPROUT_BIT_MASKT1_HPP
|
||||
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
|
||||
namespace sprout {
|
||||
//
|
||||
// maskt1
|
||||
//
|
||||
template<typename Integral>
|
||||
inline SPROUT_CONSTEXPR typename std::enable_if<
|
||||
std::is_integral<Integral>::value,
|
||||
Integral
|
||||
>::type
|
||||
maskt1(Integral x) SPROUT_NOEXCEPT {
|
||||
return ~(~x | (x + 1));
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_BIT_MASKT1_HPP
|
28
sprout/bit/maskt1ls0b.hpp
Normal file
28
sprout/bit/maskt1ls0b.hpp
Normal file
|
@ -0,0 +1,28 @@
|
|||
/*=============================================================================
|
||||
Copyright (c) 2011-2014 Bolero MURAKAMI
|
||||
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)
|
||||
=============================================================================*/
|
||||
#ifndef SPROUT_BIT_MASKT0LS0B_HPP
|
||||
#define SPROUT_BIT_MASKT0LS0B_HPP
|
||||
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
|
||||
namespace sprout {
|
||||
//
|
||||
// maskt1ls0b
|
||||
//
|
||||
template<typename Integral>
|
||||
inline SPROUT_CONSTEXPR typename std::enable_if<
|
||||
std::is_integral<Integral>::value,
|
||||
Integral
|
||||
>::type
|
||||
maskt1ls0b(Integral x) SPROUT_NOEXCEPT {
|
||||
return x ^ (x + 1);
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_BIT_MASKT0LS0B_HPP
|
|
@ -9,22 +9,17 @@
|
|||
#define SPROUT_BIT_OPERATION_HPP
|
||||
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/bit/shll.hpp>
|
||||
#include <sprout/bit/shlr.hpp>
|
||||
#include <sprout/bit/shal.hpp>
|
||||
#include <sprout/bit/shar.hpp>
|
||||
#include <sprout/bit/rotl.hpp>
|
||||
#include <sprout/bit/rotr.hpp>
|
||||
#include <sprout/bit/cntl0.hpp>
|
||||
#include <sprout/bit/cntt0.hpp>
|
||||
#include <sprout/bit/cntl1.hpp>
|
||||
#include <sprout/bit/cntt1.hpp>
|
||||
#include <sprout/bit/popcount.hpp>
|
||||
#include <sprout/bit/clz.hpp>
|
||||
#include <sprout/bit/ctz.hpp>
|
||||
#include <sprout/bit/clrsb.hpp>
|
||||
#include <sprout/bit/rotate.hpp>
|
||||
#include <sprout/bit/shift.hpp>
|
||||
#include <sprout/bit/count.hpp>
|
||||
#include <sprout/bit/rightmost.hpp>
|
||||
#include <sprout/bit/reverse.hpp>
|
||||
//#include <sprout/bit/single.hpp>
|
||||
//#include <sprout/bit/range.hpp>
|
||||
//#include <sprout/bit/pow2.hpp>
|
||||
//#include <sprout/bit/saturated.hpp>
|
||||
//#include <sprout/bit/align.hpp>
|
||||
//#include <sprout/bit/shuffle.hpp>
|
||||
//#include <sprout/bit/deposit.hpp>
|
||||
#include <sprout/bit/length.hpp>
|
||||
|
||||
#endif // #ifndef SPROUT_BIT_OPERATION_HPP
|
||||
|
|
51
sprout/bit/parity.hpp
Normal file
51
sprout/bit/parity.hpp
Normal file
|
@ -0,0 +1,51 @@
|
|||
/*=============================================================================
|
||||
Copyright (c) 2011-2014 Bolero MURAKAMI
|
||||
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)
|
||||
=============================================================================*/
|
||||
#ifndef SPROUT_BIT_PARITY_HPP
|
||||
#define SPROUT_BIT_PARITY_HPP
|
||||
|
||||
#include <climits>
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/bit/popcount.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace detail {
|
||||
# if SPROUT_USE_BUILTIN_BIT_OPERATION
|
||||
inline SPROUT_CONSTEXPR int
|
||||
parity(unsigned x) {
|
||||
return __builtin_parity(x);
|
||||
}
|
||||
inline SPROUT_CONSTEXPR int
|
||||
parity(unsigned long x) {
|
||||
return __builtin_parityl(x);
|
||||
}
|
||||
inline SPROUT_CONSTEXPR int
|
||||
parity(unsigned long long x) {
|
||||
return __builtin_parityll(x);
|
||||
}
|
||||
# endif
|
||||
template<typename Integral>
|
||||
inline SPROUT_CONSTEXPR int
|
||||
parity(Integral x) {
|
||||
return sprout::popcount(x) & 1;
|
||||
}
|
||||
} // namespace detail
|
||||
//
|
||||
// parity
|
||||
//
|
||||
template<typename Integral>
|
||||
inline SPROUT_CONSTEXPR typename std::enable_if<
|
||||
std::is_integral<Integral>::value,
|
||||
int
|
||||
>::type
|
||||
parity(Integral x) {
|
||||
return sprout::detail::parity(x);
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_BIT_PARITY_HPP
|
|
@ -8,84 +8,9 @@
|
|||
#ifndef SPROUT_BIT_REVERSE_HPP
|
||||
#define SPROUT_BIT_REVERSE_HPP
|
||||
|
||||
#include <climits>
|
||||
#include <cstddef>
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/assert.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace detail {
|
||||
SPROUT_STATIC_CONSTEXPR unsigned char bit_rev_8_table[std::size_t(UCHAR_MAX) + 1] = {
|
||||
0, 128, 64, 192, 32, 160, 96, 224, 16, 144, 80, 208, 48, 176, 112, 240, 8, 136, 72, 200,
|
||||
40, 168, 104, 232, 24, 152, 88, 216, 56, 184, 120, 248, 4, 132, 68, 196, 36, 164, 100, 228,
|
||||
20, 148, 84, 212, 52, 180, 116, 244, 12, 140, 76, 204, 44, 172, 108, 236, 28, 156, 92, 220,
|
||||
60, 188, 124, 252, 2, 130, 66, 194, 34, 162, 98, 226, 18, 146, 82, 210, 50, 178, 114, 242,
|
||||
10, 138, 74, 202, 42, 170, 106, 234, 26, 154, 90, 218, 58, 186, 122, 250, 6, 134, 70, 198,
|
||||
38, 166, 102, 230, 22, 150, 86, 214, 54, 182, 118, 246, 14, 142, 78, 206, 46, 174, 110, 238,
|
||||
30, 158, 94, 222, 62, 190, 126, 254, 1, 129, 65, 193, 33, 161, 97, 225, 17, 145, 81, 209,
|
||||
49, 177, 113, 241, 9, 137, 73, 201, 41, 169, 105, 233, 25, 153, 89, 217, 57, 185, 121, 249,
|
||||
5, 133, 69, 197, 37, 165, 101, 229, 21, 149, 85, 213, 53, 181, 117, 245, 13, 141, 77, 205,
|
||||
45, 173, 109, 237, 29, 157, 93, 221, 61, 189, 125, 253, 3, 131, 67, 195, 35, 163, 99, 227,
|
||||
19, 147, 83, 211, 51, 179, 115, 243, 11, 139, 75, 203, 43, 171, 107, 235, 27, 155, 91, 219,
|
||||
59, 187, 123, 251, 7, 135, 71, 199, 39, 167, 103, 231, 23, 151, 87, 215, 55, 183, 119, 247,
|
||||
15, 143, 79, 207, 47, 175, 111, 239, 31, 159, 95, 223, 63, 191, 127, 255
|
||||
};
|
||||
template<std::size_t Size>
|
||||
struct bit_rev {
|
||||
private:
|
||||
SPROUT_STATIC_CONSTEXPR std::size_t next_size = Size / 2;
|
||||
SPROUT_STATIC_CONSTEXPR std::size_t shift_bits = next_size * CHAR_BIT;
|
||||
public:
|
||||
template<typename IntType>
|
||||
SPROUT_CONSTEXPR IntType
|
||||
operator()(IntType x) const {
|
||||
return (sprout::detail::bit_rev<next_size>().template operator()(x) << shift_bits)
|
||||
| (sprout::detail::bit_rev<next_size>().template operator()(x >> shift_bits))
|
||||
;
|
||||
}
|
||||
};
|
||||
template<>
|
||||
struct bit_rev<1> {
|
||||
public:
|
||||
template<typename IntType>
|
||||
SPROUT_CONSTEXPR IntType
|
||||
operator()(IntType x) const {
|
||||
return sprout::detail::bit_rev_8_table[static_cast<unsigned char>(x & UCHAR_MAX)];
|
||||
}
|
||||
};
|
||||
} // namespace detail
|
||||
//
|
||||
// bit_reverse
|
||||
//
|
||||
template<typename IntType>
|
||||
SPROUT_CONSTEXPR typename std::enable_if<
|
||||
std::is_integral<IntType>::value,
|
||||
IntType
|
||||
>::type
|
||||
bit_reverse(IntType x) {
|
||||
typedef typename std::make_unsigned<IntType>::type unsigned_type;
|
||||
return static_cast<IntType>(
|
||||
sprout::detail::bit_rev<sizeof(IntType)>().template operator()<unsigned_type>(x)
|
||||
);
|
||||
}
|
||||
//
|
||||
// bit_reverse_in
|
||||
//
|
||||
template<typename IntType>
|
||||
inline SPROUT_CONSTEXPR typename std::enable_if<
|
||||
std::is_integral<IntType>::value,
|
||||
IntType
|
||||
>::type
|
||||
bit_reverse_in(IntType x, std::size_t length) {
|
||||
typedef typename std::make_unsigned<IntType>::type unsigned_type;
|
||||
return SPROUT_ASSERT(length <= sizeof(IntType) * CHAR_BIT),
|
||||
static_cast<IntType>(
|
||||
sprout::detail::bit_rev<sizeof(IntType)>().template operator()<unsigned_type>(x)
|
||||
>> (sizeof(IntType) * CHAR_BIT - length)
|
||||
)
|
||||
;
|
||||
}
|
||||
} // namespace sprout
|
||||
//#include <sprout/bit/revbits.hpp>
|
||||
//#include <sprout/bit/revbytes.hpp>
|
||||
#include <sprout/bit/bit_reverse.hpp>
|
||||
|
||||
#endif // #ifndef SPROUT_BIT_REVERSE_HPP
|
||||
|
|
23
sprout/bit/rightmost.hpp
Normal file
23
sprout/bit/rightmost.hpp
Normal file
|
@ -0,0 +1,23 @@
|
|||
/*=============================================================================
|
||||
Copyright (c) 2011-2014 Bolero MURAKAMI
|
||||
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)
|
||||
=============================================================================*/
|
||||
#ifndef SPROUT_BIT_RIGHTMOTS_HPP
|
||||
#define SPROUT_BIT_RIGHTMOTS_HPP
|
||||
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/bit/rstls1b.hpp>
|
||||
#include <sprout/bit/setls0b.hpp>
|
||||
#include <sprout/bit/isols1b.hpp>
|
||||
#include <sprout/bit/isols0b.hpp>
|
||||
#include <sprout/bit/rstt1.hpp>
|
||||
#include <sprout/bit/sett0.hpp>
|
||||
#include <sprout/bit/maskt0.hpp>
|
||||
#include <sprout/bit/maskt1.hpp>
|
||||
#include <sprout/bit/maskt0ls1b.hpp>
|
||||
#include <sprout/bit/maskt1ls0b.hpp>
|
||||
|
||||
#endif // #ifndef SPROUT_BIT_RIGHTMOTS_HPP
|
28
sprout/bit/rstls1b.hpp
Normal file
28
sprout/bit/rstls1b.hpp
Normal file
|
@ -0,0 +1,28 @@
|
|||
/*=============================================================================
|
||||
Copyright (c) 2011-2014 Bolero MURAKAMI
|
||||
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)
|
||||
=============================================================================*/
|
||||
#ifndef SPROUT_BIT_RSTLS1B_HPP
|
||||
#define SPROUT_BIT_RSTLS1B_HPP
|
||||
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
|
||||
namespace sprout {
|
||||
//
|
||||
// rstls1b
|
||||
//
|
||||
template<typename Integral>
|
||||
inline SPROUT_CONSTEXPR typename std::enable_if<
|
||||
std::is_integral<Integral>::value,
|
||||
Integral
|
||||
>::type
|
||||
rstls1b(Integral x) SPROUT_NOEXCEPT {
|
||||
return x & (x - 1);
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_BIT_RSTLS1B_HPP
|
28
sprout/bit/rstt1.hpp
Normal file
28
sprout/bit/rstt1.hpp
Normal file
|
@ -0,0 +1,28 @@
|
|||
/*=============================================================================
|
||||
Copyright (c) 2011-2014 Bolero MURAKAMI
|
||||
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)
|
||||
=============================================================================*/
|
||||
#ifndef SPROUT_BIT_RSTT1_HPP
|
||||
#define SPROUT_BIT_RSTT1_HPP
|
||||
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
|
||||
namespace sprout {
|
||||
//
|
||||
// rstt1
|
||||
//
|
||||
template<typename Integral>
|
||||
inline SPROUT_CONSTEXPR typename std::enable_if<
|
||||
std::is_integral<Integral>::value,
|
||||
Integral
|
||||
>::type
|
||||
rstt1(Integral x) SPROUT_NOEXCEPT {
|
||||
return x & (x + 1);
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_BIT_RSTT1_HPP
|
28
sprout/bit/setls0b.hpp
Normal file
28
sprout/bit/setls0b.hpp
Normal file
|
@ -0,0 +1,28 @@
|
|||
/*=============================================================================
|
||||
Copyright (c) 2011-2014 Bolero MURAKAMI
|
||||
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)
|
||||
=============================================================================*/
|
||||
#ifndef SPROUT_BIT_RSTLS0B_HPP
|
||||
#define SPROUT_BIT_RSTLS0B_HPP
|
||||
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
|
||||
namespace sprout {
|
||||
//
|
||||
// setls0b
|
||||
//
|
||||
template<typename Integral>
|
||||
inline SPROUT_CONSTEXPR typename std::enable_if<
|
||||
std::is_integral<Integral>::value,
|
||||
Integral
|
||||
>::type
|
||||
setls0b(Integral x) SPROUT_NOEXCEPT {
|
||||
return x | (x + 1);
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_BIT_RSTLS0B_HPP
|
28
sprout/bit/sett0.hpp
Normal file
28
sprout/bit/sett0.hpp
Normal file
|
@ -0,0 +1,28 @@
|
|||
/*=============================================================================
|
||||
Copyright (c) 2011-2014 Bolero MURAKAMI
|
||||
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)
|
||||
=============================================================================*/
|
||||
#ifndef SPROUT_BIT_SETT0_HPP
|
||||
#define SPROUT_BIT_SETT0_HPP
|
||||
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
|
||||
namespace sprout {
|
||||
//
|
||||
// sett0
|
||||
//
|
||||
template<typename Integral>
|
||||
inline SPROUT_CONSTEXPR typename std::enable_if<
|
||||
std::is_integral<Integral>::value,
|
||||
Integral
|
||||
>::type
|
||||
sett0(Integral x) SPROUT_NOEXCEPT {
|
||||
return x | (x - 1);
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_BIT_SETT0_HPP
|
19
sprout/bit/shift.hpp
Normal file
19
sprout/bit/shift.hpp
Normal file
|
@ -0,0 +1,19 @@
|
|||
/*=============================================================================
|
||||
Copyright (c) 2011-2014 Bolero MURAKAMI
|
||||
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)
|
||||
=============================================================================*/
|
||||
#ifndef SPROUT_BIT_SHIFT_HPP
|
||||
#define SPROUT_BIT_SHIFT_HPP
|
||||
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/bit/shll.hpp>
|
||||
#include <sprout/bit/shlr.hpp>
|
||||
#include <sprout/bit/shal.hpp>
|
||||
#include <sprout/bit/shar.hpp>
|
||||
#include <sprout/bit/rotl.hpp>
|
||||
#include <sprout/bit/rotr.hpp>
|
||||
|
||||
#endif // #ifndef SPROUT_BIT_SHIFT_HPP
|
|
@ -17,8 +17,8 @@
|
|||
#include <sprout/container/indexes.hpp>
|
||||
#include <sprout/iterator/operation.hpp>
|
||||
#include <sprout/algorithm/fixed/results.hpp>
|
||||
#include <sprout/bit/reverse.hpp>
|
||||
#include <sprout/bit/length.hpp>
|
||||
#include <sprout/bit/bit_reverse.hpp>
|
||||
#include <sprout/bit/bit_length.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace fixed {
|
||||
|
|
|
@ -69,7 +69,7 @@ namespace sprout {
|
|||
//
|
||||
template<typename Head, typename... Tail>
|
||||
inline SPROUT_CONSTEXPR Head&&
|
||||
head_get(Head&& head, Tail&&... tail) {
|
||||
head_get(Head&& head, Tail&&...) {
|
||||
return SPROUT_FORWARD(Head, head);
|
||||
}
|
||||
} // namespace sprout
|
||||
|
|
Loading…
Add table
Reference in a new issue