From d128d36faaf44f4ad2001cc41d887191869cc39a Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sun, 26 Apr 2015 18:46:45 +0000 Subject: [PATCH] fix cpuid use (apparently it's not appropriate to use in an inlined function) --- CMakeLists.txt | 1 + Jamfile | 1 + include/libtorrent/Makefile.am | 10 ++++----- include/libtorrent/{ => aux_}/cpuid.hpp | 28 +++++-------------------- include/libtorrent/bitfield.hpp | 6 ++---- src/Makefile.am | 1 + src/crc32c.cpp | 24 +++------------------ 7 files changed, 18 insertions(+), 53 deletions(-) rename include/libtorrent/{ => aux_}/cpuid.hpp (75%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3dbc3c633..b8b9fea6d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,7 @@ set(sources chained_buffer choker close_reason + cpuid crc32c create_torrent disk_buffer_holder diff --git a/Jamfile b/Jamfile index d9832fc0a..b98dc5299 100755 --- a/Jamfile +++ b/Jamfile @@ -584,6 +584,7 @@ SOURCES = chained_buffer choker close_reason + cpuid crc32c create_torrent disk_buffer_holder diff --git a/include/libtorrent/Makefile.am b/include/libtorrent/Makefile.am index 95be06ff6..f285972d7 100644 --- a/include/libtorrent/Makefile.am +++ b/include/libtorrent/Makefile.am @@ -30,7 +30,6 @@ nobase_include_HEADERS = \ config.hpp \ ConvertUTF.h \ copy_ptr.hpp \ - cpuid.hpp \ crc32c.hpp \ create_torrent.hpp \ deadline_timer.hpp \ @@ -157,15 +156,16 @@ nobase_include_HEADERS = \ tommath_superclass.h \ \ aux_/alert_manager_variadic_emplace.hpp \ + aux_/cpuid.hpp \ + aux_/disable_warnings_push.hpp \ + aux_/disable_warnings_pop.hpp \ + aux_/escape_string.hpp \ + aux_/merkle.hpp \ aux_/session_call.hpp \ aux_/session_impl.hpp \ aux_/session_settings.hpp \ aux_/session_interface.hpp \ aux_/time.hpp \ - aux_/escape_string.hpp \ - aux_/disable_warnings_push.hpp \ - aux_/disable_warnings_pop.hpp \ - aux_/merkle.hpp \ \ extensions/lt_trackers.hpp \ extensions/metadata_transfer.hpp \ diff --git a/include/libtorrent/cpuid.hpp b/include/libtorrent/aux_/cpuid.hpp similarity index 75% rename from include/libtorrent/cpuid.hpp rename to include/libtorrent/aux_/cpuid.hpp index ee41214f1..025cff274 100644 --- a/include/libtorrent/cpuid.hpp +++ b/include/libtorrent/aux_/cpuid.hpp @@ -34,31 +34,13 @@ POSSIBILITY OF SUCH DAMAGE. #define TORRENT_CPUID_HPP_INCLUDED #include "libtorrent/config.hpp" -#include -#if defined _MSC_VER && TORRENT_HAS_SSE -#include -#include -#endif - -namespace libtorrent +namespace libtorrent { namespace aux { - // internal - inline void cpuid(unsigned int info[4], int type) - { -#if TORRENT_HAS_SSE && defined _MSC_VER - __cpuid((int*)info, type); - -#elif TORRENT_HAS_SSE && defined __GNUC__ - asm volatile - ("cpuid" : "=a" (info[0]), "=b" (info[1]), "=c" (info[2]), "=d" (info[3]) - : "a" (type), "c" (0)); -#else - // for non-x86 and non-amd64, just return zeroes - std::memset(&info[0], 0, sizeof(unsigned int) * 4); -#endif - } -} + // initialized by static initializers (in cpuid.cpp) + extern bool sse42_support; + extern bool mmx_support; +} } #endif // TORRENT_CPUID_HPP_INCLUDED diff --git a/include/libtorrent/bitfield.hpp b/include/libtorrent/bitfield.hpp index 680fe5084..fac840959 100644 --- a/include/libtorrent/bitfield.hpp +++ b/include/libtorrent/bitfield.hpp @@ -36,7 +36,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/assert.hpp" #include "libtorrent/config.hpp" #include "libtorrent/byteswap.hpp" -#include "libtorrent/cpuid.hpp" +#include "libtorrent/aux_/cpuid.hpp" #include // for memset and memcpy #include // for malloc, free and realloc @@ -171,9 +171,7 @@ namespace libtorrent int ret = 0; const int words = num_words(); #if TORRENT_HAS_SSE - unsigned int cpui[4]; - cpuid(cpui, 1); - if (cpui[2] & (1 << 23)) + if (aux::mmx_support) { for (int i = 0; i < words; ++i) { diff --git a/src/Makefile.am b/src/Makefile.am index c9372f1f2..7c2872a5b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -53,6 +53,7 @@ libtorrent_rasterbar_la_SOURCES = \ choker.cpp \ close_reason.cpp \ ConvertUTF.cpp \ + cpuid.cpp \ crc32c.cpp \ create_torrent.cpp \ disk_buffer_holder.cpp \ diff --git a/src/crc32c.cpp b/src/crc32c.cpp index f5d6604e4..17dab32cb 100644 --- a/src/crc32c.cpp +++ b/src/crc32c.cpp @@ -31,8 +31,7 @@ POSSIBILITY OF SUCH DAMAGE. */ #include "libtorrent/crc32c.hpp" -#include "libtorrent/cpuid.hpp" - +#include "libtorrent/aux_/cpuid.hpp" #include "libtorrent/aux_/disable_warnings_push.hpp" #include @@ -41,27 +40,10 @@ POSSIBILITY OF SUCH DAMAGE. namespace libtorrent { - namespace { - - bool supports_sse42() - { -#if TORRENT_HAS_SSE - unsigned int cpui[4]; - cpuid(cpui, 1); - return cpui[2] & (1 << 20); -#else - return false; -#endif - } - - bool sse42_support = supports_sse42(); - - } // anonymous namespace - boost::uint32_t crc32c_32(boost::uint32_t v) { #if TORRENT_HAS_SSE - if (sse42_support) + if (aux::sse42_support) { boost::uint32_t ret = 0xffffffff; #ifdef __GNUC__ @@ -86,7 +68,7 @@ namespace libtorrent boost::uint32_t crc32c(boost::uint64_t const* buf, int num_words) { #if TORRENT_HAS_SSE - if (sse42_support) + if (aux::sse42_support) { #if defined _M_AMD64 || defined __x86_64__ \ || defined __x86_64 || defined _M_X64 || defined __amd64__