111 lines
3.2 KiB
C++
111 lines
3.2 KiB
C++
|
/*
|
||
|
|
||
|
Copyright (c) 2014-2016, Arvid Norberg, Alden Torres
|
||
|
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/config.hpp"
|
||
|
#include "libtorrent/aux_/ffs.hpp"
|
||
|
#include "libtorrent/aux_/byteswap.hpp"
|
||
|
|
||
|
#include "libtorrent/aux_/disable_warnings_push.hpp"
|
||
|
|
||
|
#if (defined _MSC_VER && _MSC_VER >= 1600)
|
||
|
#include <nmmintrin.h>
|
||
|
#endif
|
||
|
|
||
|
#include "libtorrent/aux_/disable_warnings_pop.hpp"
|
||
|
|
||
|
namespace libtorrent { namespace aux
|
||
|
{
|
||
|
int clz_sw(span<std::uint32_t const> buf)
|
||
|
{
|
||
|
int num = int(buf.size());
|
||
|
std::uint32_t const* ptr = buf.data();
|
||
|
|
||
|
for (int i = 0; i < num; i++)
|
||
|
{
|
||
|
if (ptr[i] == 0) continue;
|
||
|
std::uint32_t v = aux::network_to_host(ptr[i]);
|
||
|
|
||
|
// http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious
|
||
|
static const int MultiplyDeBruijnBitPosition[32] =
|
||
|
{
|
||
|
0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
|
||
|
8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31
|
||
|
};
|
||
|
|
||
|
v |= v >> 1; // first round down to one less than a power of 2
|
||
|
v |= v >> 2;
|
||
|
v |= v >> 4;
|
||
|
v |= v >> 8;
|
||
|
v |= v >> 16;
|
||
|
|
||
|
return i * 32 + 31 - MultiplyDeBruijnBitPosition[
|
||
|
static_cast<std::uint32_t>(v * 0x07C4ACDDU) >> 27];
|
||
|
}
|
||
|
|
||
|
return num * 32;
|
||
|
}
|
||
|
|
||
|
int clz_hw(span<std::uint32_t const> buf)
|
||
|
{
|
||
|
int num = int(buf.size());
|
||
|
std::uint32_t const* ptr = buf.data();
|
||
|
|
||
|
for (int i = 0; i < num; i++)
|
||
|
{
|
||
|
if (ptr[i] == 0) continue;
|
||
|
std::uint32_t v = aux::network_to_host(ptr[i]);
|
||
|
|
||
|
#if TORRENT_HAS_BUILTIN_CLZ
|
||
|
return i * 32 + __builtin_clz(v);
|
||
|
#elif defined _MSC_VER
|
||
|
DWORD pos;
|
||
|
_BitScanReverse(&pos, v);
|
||
|
return i * 32 + 31 - pos;
|
||
|
#else
|
||
|
return -1;
|
||
|
#endif
|
||
|
}
|
||
|
|
||
|
return num * 32;
|
||
|
}
|
||
|
|
||
|
int clz(span<std::uint32_t const> buf)
|
||
|
{
|
||
|
#if TORRENT_HAS_BUILTIN_CLZ || defined _MSC_VER
|
||
|
return aux::clz_hw(buf);
|
||
|
#else
|
||
|
return aux::clz_sw(buf);
|
||
|
#endif
|
||
|
}
|
||
|
|
||
|
}}
|