fixing sign-conversion warnings, part 9

This commit is contained in:
Alden Torres 2017-01-26 11:13:40 -05:00 committed by Arvid Norberg
parent a5284b583c
commit ddf1b62e4b
6 changed files with 29 additions and 23 deletions

View File

@ -50,6 +50,9 @@ namespace libtorrent { namespace aux {
using base = std::unique_ptr<T[]>; using base = std::unique_ptr<T[]>;
using underlying_index = typename underlying_index_t<IndexType>::type; using underlying_index = typename underlying_index_t<IndexType>::type;
unique_ptr() {}
explicit unique_ptr(T arr[]) : base(arr) {}
auto operator[](IndexType idx) const -> decltype(this->base::operator[](underlying_index())) auto operator[](IndexType idx) const -> decltype(this->base::operator[](underlying_index()))
{ {
TORRENT_ASSERT(idx >= IndexType(0)); TORRENT_ASSERT(idx >= IndexType(0));

View File

@ -35,12 +35,12 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/assert.hpp" #include "libtorrent/assert.hpp"
#include "libtorrent/config.hpp" #include "libtorrent/config.hpp"
#include "libtorrent/aux_/unique_ptr.hpp"
#include "libtorrent/aux_/byteswap.hpp" #include "libtorrent/aux_/byteswap.hpp"
#include "libtorrent/aux_/ffs.hpp" #include "libtorrent/aux_/ffs.hpp"
#include <cstring> // for memset and memcpy #include <cstring> // for memset and memcpy
#include <cstdint> // uint32_t #include <cstdint> // uint32_t
#include <memory> // for unique_ptr
namespace libtorrent namespace libtorrent
{ {
@ -253,7 +253,7 @@ namespace libtorrent
// the first element is not part of the bitfield, it's the // the first element is not part of the bitfield, it's the
// number of bits. // number of bits.
std::unique_ptr<std::uint32_t[]> m_buf; aux::unique_ptr<std::uint32_t[]> m_buf;
}; };
template <typename IndexType> template <typename IndexType>

View File

@ -142,7 +142,7 @@ namespace libtorrent
#endif #endif
{ {
#ifndef TORRENT_NO_DEPRECATE #ifndef TORRENT_NO_DEPRECATE
memset(num_fence_jobs, 0, sizeof(num_fence_jobs)); std::memset(num_fence_jobs, 0, sizeof(num_fence_jobs));
#endif #endif
} }

View File

@ -141,11 +141,11 @@ namespace std {
using type = libtorrent::aux::strong_typedef<UnderlyingType, Tag>; using type = libtorrent::aux::strong_typedef<UnderlyingType, Tag>;
public: public:
static constexpr type min() static constexpr type (min)()
{ return type(std::numeric_limits<UnderlyingType>::min()); } { return type((std::numeric_limits<UnderlyingType>::min)()); }
static constexpr type max() static constexpr type (max)()
{ return type(std::numeric_limits<UnderlyingType>::max()); } { return type((std::numeric_limits<UnderlyingType>::max)()); }
}; };
template<typename UnderlyingType, typename Tag> template<typename UnderlyingType, typename Tag>

View File

@ -31,6 +31,7 @@ POSSIBILITY OF SUCH DAMAGE.
*/ */
#include "libtorrent/bitfield.hpp" #include "libtorrent/bitfield.hpp"
#include "libtorrent/aux_/numeric_cast.hpp"
#include "libtorrent/aux_/cpuid.hpp" #include "libtorrent/aux_/cpuid.hpp"
#ifdef _MSC_VER #ifdef _MSC_VER
@ -112,7 +113,7 @@ namespace libtorrent
// from: // from:
// http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel // http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
static const int S[] = {1, 2, 4, 8, 16}; // Magic Binary Numbers static const int S[] = {1, 2, 4, 8, 16}; // Magic Binary Numbers
static const int B[] = {0x55555555, 0x33333333, 0x0F0F0F0F, 0x00FF00FF, 0x0000FFFF}; static const std::uint32_t B[] = {0x55555555, 0x33333333, 0x0F0F0F0F, 0x00FF00FF, 0x0000FFFF};
std::uint32_t c = v - ((v >> 1) & B[0]); std::uint32_t c = v - ((v >> 1) & B[0]);
c = ((c >> S[1]) & B[1]) + (c & B[1]); c = ((c >> S[1]) & B[1]) + (c & B[1]);
@ -169,22 +170,23 @@ namespace libtorrent
int const cur_size_words = num_words(); int const cur_size_words = num_words();
if (cur_size_words != new_size_words) if (cur_size_words != new_size_words)
{ {
std::unique_ptr<std::uint32_t[]> b(new std::uint32_t[new_size_words + 1]); aux::unique_ptr<std::uint32_t[]> b(new std::uint32_t[new_size_words + 1]);
#ifdef BOOST_NO_EXCEPTIONS #ifdef BOOST_NO_EXCEPTIONS
if (b == nullptr) std::terminate(); if (b == nullptr) std::terminate();
#endif #endif
b[0] = bits; b[0] = aux::numeric_cast<std::uint32_t>(bits);
if (m_buf) std::memcpy(&b[1], buf(), std::min(new_size_words, cur_size_words) * 4); if (m_buf) std::memcpy(&b[1], buf()
, aux::numeric_cast<std::size_t>(std::min(new_size_words, cur_size_words) * 4));
if (new_size_words > cur_size_words) if (new_size_words > cur_size_words)
{ {
std::memset(&b[1 + cur_size_words], 0 std::memset(&b[1 + cur_size_words], 0
, (new_size_words - cur_size_words) * 4); , aux::numeric_cast<std::size_t>((new_size_words - cur_size_words) * 4));
} }
m_buf = std::move(b); m_buf = std::move(b);
} }
else else
{ {
m_buf[0] = bits; m_buf[0] = aux::numeric_cast<std::uint32_t>(bits);
} }
clear_trailing_bits(); clear_trailing_bits();
@ -193,22 +195,22 @@ namespace libtorrent
int bitfield::find_first_set() const int bitfield::find_first_set() const
{ {
std::size_t const num = num_words(); int const num = num_words();
if (num == 0) return -1; if (num == 0) return -1;
int const count = aux::count_leading_zeros({&m_buf[1], num}); int const count = aux::count_leading_zeros({&m_buf[1], std::size_t(num)});
return count != int(num) * 32 ? count : -1; return count != num * 32 ? count : -1;
} }
int bitfield::find_last_clear() const int bitfield::find_last_clear() const
{ {
std::size_t const num = num_words(); int const num = num_words();
if (num == 0) return - 1; if (num == 0) return - 1;
int const size = this->size(); int const size = this->size();
std::uint32_t const mask = 0xffffffff << (32 - (size & 31)); std::uint32_t const mask = 0xffffffff << (32 - (size & 31));
std::uint32_t const last = m_buf[num] ^ aux::host_to_network(mask); std::uint32_t const last = m_buf[num] ^ aux::host_to_network(mask);
int const ext = aux::count_trailing_ones(~last) - (31 - (size % 32)); int const ext = aux::count_trailing_ones(~last) - (31 - (size % 32));
return last != 0 return last != 0
? (int(num) - 1) * 32 + ext ? (num - 1) * 32 + ext
: size - (aux::count_trailing_ones({&m_buf[1], num - 1}) + ext); : size - (aux::count_trailing_ones({&m_buf[1], std::size_t(num - 1)}) + ext);
} }
} }

View File

@ -31,6 +31,7 @@ POSSIBILITY OF SUCH DAMAGE.
*/ */
#include "libtorrent/bloom_filter.hpp" #include "libtorrent/bloom_filter.hpp"
#include "libtorrent/aux_/numeric_cast.hpp"
namespace libtorrent namespace libtorrent
{ {
@ -38,8 +39,8 @@ namespace libtorrent
{ {
std::uint32_t idx1 = std::uint32_t(k[0]) | (std::uint32_t(k[1]) << 8); std::uint32_t idx1 = std::uint32_t(k[0]) | (std::uint32_t(k[1]) << 8);
std::uint32_t idx2 = std::uint32_t(k[2]) | (std::uint32_t(k[3]) << 8); std::uint32_t idx2 = std::uint32_t(k[2]) | (std::uint32_t(k[3]) << 8);
idx1 %= len * 8; idx1 %= aux::numeric_cast<std::uint32_t>(len * 8);
idx2 %= len * 8; idx2 %= aux::numeric_cast<std::uint32_t>(len * 8);
return (bits[idx1 / 8] & (1 << (idx1 & 7))) != 0 return (bits[idx1 / 8] & (1 << (idx1 & 7))) != 0
&& (bits[idx2 / 8] & (1 << (idx2 & 7))) != 0; && (bits[idx2 / 8] & (1 << (idx2 & 7))) != 0;
} }
@ -48,8 +49,8 @@ namespace libtorrent
{ {
std::uint32_t idx1 = std::uint32_t(k[0]) | (std::uint32_t(k[1]) << 8); std::uint32_t idx1 = std::uint32_t(k[0]) | (std::uint32_t(k[1]) << 8);
std::uint32_t idx2 = std::uint32_t(k[2]) | (std::uint32_t(k[3]) << 8); std::uint32_t idx2 = std::uint32_t(k[2]) | (std::uint32_t(k[3]) << 8);
idx1 %= len * 8; idx1 %= aux::numeric_cast<std::uint32_t>(len * 8);
idx2 %= len * 8; idx2 %= aux::numeric_cast<std::uint32_t>(len * 8);
bits[idx1 / 8] |= (1 << (idx1 & 7)); bits[idx1 / 8] |= (1 << (idx1 & 7));
bits[idx2 / 8] |= (1 << (idx2 & 7)); bits[idx2 / 8] |= (1 << (idx2 & 7));
} }