2016-04-20 13:50:57 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2008-2016, Arvid Norberg
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libtorrent/bitfield.hpp"
|
2017-01-26 17:13:40 +01:00
|
|
|
#include "libtorrent/aux_/numeric_cast.hpp"
|
2016-04-20 13:50:57 +02:00
|
|
|
#include "libtorrent/aux_/cpuid.hpp"
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#include <intrin.h>
|
|
|
|
#endif
|
|
|
|
|
2017-04-12 19:00:57 +02:00
|
|
|
namespace libtorrent {
|
|
|
|
|
2016-04-20 13:50:57 +02:00
|
|
|
bool bitfield::all_set() const
|
|
|
|
{
|
2017-01-13 23:08:54 +01:00
|
|
|
if(size() == 0) return false;
|
|
|
|
|
2016-07-16 23:31:25 +02:00
|
|
|
int const words = size() / 32;
|
|
|
|
for (int i = 1; i < words + 1; ++i)
|
2016-04-20 13:50:57 +02:00
|
|
|
{
|
|
|
|
if (m_buf[i] != 0xffffffff) return false;
|
|
|
|
}
|
2016-07-16 23:31:25 +02:00
|
|
|
int const rest = size() & 31;
|
2016-04-20 13:50:57 +02:00
|
|
|
if (rest > 0)
|
|
|
|
{
|
2017-01-24 20:03:17 +01:00
|
|
|
std::uint32_t const mask = aux::host_to_network(0xffffffff << (32 - rest));
|
|
|
|
if ((m_buf[words + 1] & mask) != mask) return false;
|
2016-04-20 13:50:57 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bitfield::count() const
|
|
|
|
{
|
|
|
|
int ret = 0;
|
2016-07-16 23:31:25 +02:00
|
|
|
int const words = num_words();
|
2016-04-20 13:50:57 +02:00
|
|
|
#if TORRENT_HAS_SSE
|
|
|
|
if (aux::mmx_support)
|
|
|
|
{
|
2016-08-07 22:21:08 +02:00
|
|
|
for (int i = 1; i < words + 1; ++i)
|
2016-04-20 13:50:57 +02:00
|
|
|
{
|
|
|
|
#ifdef __GNUC__
|
2016-06-28 00:51:00 +02:00
|
|
|
std::uint32_t cnt = 0;
|
|
|
|
__asm__("popcnt %1, %0"
|
|
|
|
: "=r"(cnt)
|
|
|
|
: "r"(m_buf[i]));
|
|
|
|
ret += cnt;
|
2016-04-20 13:50:57 +02:00
|
|
|
#else
|
|
|
|
ret += _mm_popcnt_u32(m_buf[i]);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-07-16 23:31:25 +02:00
|
|
|
TORRENT_ASSERT(ret <= size());
|
|
|
|
TORRENT_ASSERT(ret >= 0);
|
2016-04-20 13:50:57 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif // TORRENT_HAS_SSE
|
|
|
|
|
2016-10-12 06:02:02 +02:00
|
|
|
#if TORRENT_HAS_ARM_NEON && defined __arm__
|
2016-06-28 00:51:43 +02:00
|
|
|
if (aux::arm_neon_support)
|
|
|
|
{
|
2016-07-16 23:31:25 +02:00
|
|
|
for (int i = 1; i < words + 1; ++i)
|
2016-06-28 00:51:43 +02:00
|
|
|
{
|
2016-10-12 06:02:02 +02:00
|
|
|
std::uint32_t cnt;
|
|
|
|
__asm__(
|
|
|
|
"vld1.u32 d0[0], [%1] \n"
|
|
|
|
"vcnt.u8 d0, d0 \n"
|
|
|
|
"vpaddl.u8 d0, d0 \n"
|
|
|
|
"vpaddl.u16 d0, d0 \n"
|
|
|
|
"vst1.u32 d0[0], [%0]"
|
|
|
|
:: "r"(&cnt), "r"(&m_buf[i])
|
|
|
|
: "d0", "memory");
|
2016-06-28 00:51:43 +02:00
|
|
|
ret += cnt;
|
|
|
|
}
|
|
|
|
|
2016-07-16 23:31:25 +02:00
|
|
|
TORRENT_ASSERT(ret <= size());
|
|
|
|
TORRENT_ASSERT(ret >= 0);
|
2016-06-28 00:51:43 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif // TORRENT_HAS_ARM_NEON
|
|
|
|
|
2016-07-16 23:31:25 +02:00
|
|
|
for (int i = 1; i < words + 1; ++i)
|
2016-04-20 13:50:57 +02:00
|
|
|
{
|
2016-07-16 23:31:25 +02:00
|
|
|
std::uint32_t const v = m_buf[i];
|
2016-04-20 13:50:57 +02:00
|
|
|
// from:
|
|
|
|
// http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
|
|
|
|
static const int S[] = {1, 2, 4, 8, 16}; // Magic Binary Numbers
|
2017-01-26 17:13:40 +01:00
|
|
|
static const std::uint32_t B[] = {0x55555555, 0x33333333, 0x0F0F0F0F, 0x00FF00FF, 0x0000FFFF};
|
2016-04-20 13:50:57 +02:00
|
|
|
|
2016-06-18 20:01:38 +02:00
|
|
|
std::uint32_t c = v - ((v >> 1) & B[0]);
|
2016-04-20 13:50:57 +02:00
|
|
|
c = ((c >> S[1]) & B[1]) + (c & B[1]);
|
|
|
|
c = ((c >> S[2]) + c) & B[2];
|
|
|
|
c = ((c >> S[3]) + c) & B[3];
|
|
|
|
c = ((c >> S[4]) + c) & B[4];
|
|
|
|
ret += c;
|
2016-07-16 23:31:25 +02:00
|
|
|
TORRENT_ASSERT(ret <= size());
|
2016-04-20 13:50:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_ASSERT(ret <= size());
|
|
|
|
TORRENT_ASSERT(ret >= 0);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-11-27 20:13:37 +01:00
|
|
|
void bitfield::resize(int const bits, bool const val)
|
2016-04-20 13:50:57 +02:00
|
|
|
{
|
|
|
|
if (bits == size()) return;
|
|
|
|
|
2016-07-16 23:31:25 +02:00
|
|
|
int const s = size();
|
|
|
|
int const b = size() & 31;
|
2016-04-20 13:50:57 +02:00
|
|
|
resize(bits);
|
|
|
|
if (s >= size()) return;
|
2016-07-16 23:31:25 +02:00
|
|
|
int const old_size_words = (s + 31) / 32;
|
|
|
|
int const new_size_words = num_words();
|
2016-04-20 13:50:57 +02:00
|
|
|
if (val)
|
|
|
|
{
|
2017-09-12 23:10:11 +02:00
|
|
|
if (old_size_words && b) buf()[old_size_words - 1] |= aux::host_to_network(0xffffffff >> b);
|
2016-04-20 13:50:57 +02:00
|
|
|
if (old_size_words < new_size_words)
|
2016-07-16 23:31:25 +02:00
|
|
|
std::memset(buf() + old_size_words, 0xff
|
2017-01-24 20:03:17 +01:00
|
|
|
, std::size_t((new_size_words - old_size_words) * 4));
|
2016-04-20 13:50:57 +02:00
|
|
|
clear_trailing_bits();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (old_size_words < new_size_words)
|
2016-07-16 23:31:25 +02:00
|
|
|
std::memset(buf() + old_size_words, 0x00
|
2017-01-24 20:03:17 +01:00
|
|
|
, std::size_t((new_size_words - old_size_words) * 4));
|
2016-04-20 13:50:57 +02:00
|
|
|
}
|
|
|
|
TORRENT_ASSERT(size() == bits);
|
|
|
|
}
|
|
|
|
|
2017-01-24 20:03:17 +01:00
|
|
|
void bitfield::resize(int const bits)
|
2016-04-20 13:50:57 +02:00
|
|
|
{
|
|
|
|
if (bits == size()) return;
|
|
|
|
|
|
|
|
TORRENT_ASSERT(bits >= 0);
|
2016-04-30 06:24:45 +02:00
|
|
|
if (bits == 0)
|
|
|
|
{
|
2016-07-16 23:31:25 +02:00
|
|
|
m_buf.reset();
|
2016-04-30 06:24:45 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-07-16 23:31:25 +02:00
|
|
|
int const new_size_words = (bits + 31) / 32;
|
|
|
|
int const cur_size_words = num_words();
|
|
|
|
if (cur_size_words != new_size_words)
|
2016-04-20 13:50:57 +02:00
|
|
|
{
|
2017-01-26 17:13:40 +01:00
|
|
|
aux::unique_ptr<std::uint32_t[]> b(new std::uint32_t[new_size_words + 1]);
|
2016-07-16 23:31:25 +02:00
|
|
|
#ifdef BOOST_NO_EXCEPTIONS
|
|
|
|
if (b == nullptr) std::terminate();
|
2016-04-20 13:50:57 +02:00
|
|
|
#endif
|
2017-01-26 17:13:40 +01:00
|
|
|
b[0] = aux::numeric_cast<std::uint32_t>(bits);
|
|
|
|
if (m_buf) std::memcpy(&b[1], buf()
|
|
|
|
, aux::numeric_cast<std::size_t>(std::min(new_size_words, cur_size_words) * 4));
|
2016-07-16 23:31:25 +02:00
|
|
|
if (new_size_words > cur_size_words)
|
|
|
|
{
|
2016-08-07 22:21:08 +02:00
|
|
|
std::memset(&b[1 + cur_size_words], 0
|
2017-01-26 17:13:40 +01:00
|
|
|
, aux::numeric_cast<std::size_t>((new_size_words - cur_size_words) * 4));
|
2016-07-16 23:31:25 +02:00
|
|
|
}
|
|
|
|
m_buf = std::move(b);
|
2016-04-20 13:50:57 +02:00
|
|
|
}
|
2016-04-30 06:24:45 +02:00
|
|
|
else
|
2016-04-20 13:50:57 +02:00
|
|
|
{
|
2017-01-26 17:13:40 +01:00
|
|
|
m_buf[0] = aux::numeric_cast<std::uint32_t>(bits);
|
2016-04-20 13:50:57 +02:00
|
|
|
}
|
2016-07-16 23:31:25 +02:00
|
|
|
|
2016-04-20 13:50:57 +02:00
|
|
|
clear_trailing_bits();
|
|
|
|
TORRENT_ASSERT(size() == bits);
|
|
|
|
}
|
2016-09-26 16:58:09 +02:00
|
|
|
|
|
|
|
int bitfield::find_first_set() const
|
|
|
|
{
|
2017-01-26 17:13:40 +01:00
|
|
|
int const num = num_words();
|
2016-09-26 16:58:09 +02:00
|
|
|
if (num == 0) return -1;
|
2017-01-26 17:13:40 +01:00
|
|
|
int const count = aux::count_leading_zeros({&m_buf[1], std::size_t(num)});
|
|
|
|
return count != num * 32 ? count : -1;
|
2016-09-26 16:58:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int bitfield::find_last_clear() const
|
|
|
|
{
|
2017-01-26 17:13:40 +01:00
|
|
|
int const num = num_words();
|
2016-09-26 16:58:09 +02:00
|
|
|
if (num == 0) return - 1;
|
|
|
|
int const size = this->size();
|
|
|
|
std::uint32_t const mask = 0xffffffff << (32 - (size & 31));
|
|
|
|
std::uint32_t const last = m_buf[num] ^ aux::host_to_network(mask);
|
|
|
|
int const ext = aux::count_trailing_ones(~last) - (31 - (size % 32));
|
|
|
|
return last != 0
|
2017-01-26 17:13:40 +01:00
|
|
|
? (num - 1) * 32 + ext
|
|
|
|
: size - (aux::count_trailing_ones({&m_buf[1], std::size_t(num - 1)}) + ext);
|
2016-09-26 16:58:09 +02:00
|
|
|
}
|
2017-06-07 16:41:28 +02:00
|
|
|
|
|
|
|
static_assert(std::is_nothrow_move_constructible<bitfield>::value
|
|
|
|
, "should be nothrow move constructible");
|
|
|
|
static_assert(std::is_nothrow_move_assignable<bitfield>::value
|
|
|
|
, "should be nothrow move assignable");
|
|
|
|
static_assert(std::is_nothrow_default_constructible<bitfield>::value
|
|
|
|
, "should be nothrow default constructible");
|
|
|
|
|
|
|
|
static_assert(std::is_nothrow_move_constructible<typed_bitfield<int>>::value
|
|
|
|
, "should be nothrow move constructible");
|
|
|
|
static_assert(std::is_nothrow_move_assignable<typed_bitfield<int>>::value
|
|
|
|
, "should be nothrow move assignable");
|
|
|
|
static_assert(std::is_nothrow_default_constructible<typed_bitfield<int>>::value
|
|
|
|
, "should be nothrow default constructible");
|
2016-04-20 13:50:57 +02:00
|
|
|
}
|