From 08c1e93fa36a49f49325a07fe91ff92c964c2b6c Mon Sep 17 00:00:00 2001 From: Chanho Park Date: Thu, 11 Dec 2014 18:55:56 +0900 Subject: Imported Upstream version 1.57.0 --- boost/uuid/detail/config.hpp | 62 +++++++++++++++++++++ boost/uuid/detail/uuid_generic.hpp | 51 +++++++++++++++++ boost/uuid/detail/uuid_x86.hpp | 109 +++++++++++++++++++++++++++++++++++++ boost/uuid/name_generator.hpp | 20 +++---- boost/uuid/seed_rng.hpp | 12 ++-- boost/uuid/sha1.hpp | 16 ++++-- boost/uuid/uuid.hpp | 83 +++++++++++++--------------- boost/uuid/uuid_io.hpp | 2 +- 8 files changed, 286 insertions(+), 69 deletions(-) create mode 100644 boost/uuid/detail/config.hpp create mode 100644 boost/uuid/detail/uuid_generic.hpp create mode 100644 boost/uuid/detail/uuid_x86.hpp (limited to 'boost/uuid') diff --git a/boost/uuid/detail/config.hpp b/boost/uuid/detail/config.hpp new file mode 100644 index 0000000000..997f882131 --- /dev/null +++ b/boost/uuid/detail/config.hpp @@ -0,0 +1,62 @@ +/* + * Copyright Andrey Semashev 2013. + * 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) + */ +/*! + * \file uuid/detail/config.hpp + * + * \brief This header defines configuration macros for Boost.UUID. + */ + +#ifndef BOOST_UUID_DETAIL_CONFIG_HPP_INCLUDED_ +#define BOOST_UUID_DETAIL_CONFIG_HPP_INCLUDED_ + +#include + +#ifdef BOOST_HAS_PRAGMA_ONCE +#pragma once +#endif + +#if !defined(BOOST_UUID_NO_SIMD) + +#if defined(__GNUC__) && defined(__SSE2__) + +// GCC and its pretenders go here +#ifndef BOOST_UUID_USE_SSE2 +#define BOOST_UUID_USE_SSE2 +#endif + +#if defined(__SSE3__) && !defined(BOOST_UUID_USE_SSE3) +#define BOOST_UUID_USE_SSE3 +#endif + +#if defined(__SSE4_1__) && !defined(BOOST_UUID_USE_SSE41) +#define BOOST_UUID_USE_SSE41 +#endif + +#elif defined(_MSC_VER) && (defined(_M_X64) || (defined(_M_IX86) && defined(_M_IX86_FP) && _M_IX86_FP >= 2)) + +#ifndef BOOST_UUID_USE_SSE2 +#define BOOST_UUID_USE_SSE2 +#endif + +#elif !defined(BOOST_UUID_USE_SSE41) && !defined(BOOST_UUID_USE_SSE3) && !defined(BOOST_UUID_USE_SSE2) + +#define BOOST_UUID_NO_SIMD + +#endif + +// More advanced ISA extensions imply less advanced are also available +#if !defined(BOOST_UUID_USE_SSE3) && defined(BOOST_UUID_USE_SSE41) +#define BOOST_UUID_USE_SSE3 +#endif + +#if !defined(BOOST_UUID_USE_SSE2) && defined(BOOST_UUID_USE_SSE3) +#define BOOST_UUID_USE_SSE2 +#endif + +#endif // !defined(BOOST_UUID_NO_SIMD) + +#endif // BOOST_UUID_DETAIL_CONFIG_HPP_INCLUDED_ diff --git a/boost/uuid/detail/uuid_generic.hpp b/boost/uuid/detail/uuid_generic.hpp new file mode 100644 index 0000000000..1c4d775541 --- /dev/null +++ b/boost/uuid/detail/uuid_generic.hpp @@ -0,0 +1,51 @@ +/* + * Copyright Andy Tompkins 2006. + * 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) + */ +/*! + * \file uuid/detail/uuid_generic.hpp + * + * \brief This header contains generic implementation of \c boost::uuid operations. + */ + +#ifndef BOOST_UUID_DETAIL_UUID_GENERIC_HPP_INCLUDED_ +#define BOOST_UUID_DETAIL_UUID_GENERIC_HPP_INCLUDED_ + +#include + +namespace boost { +namespace uuids { + +inline bool uuid::is_nil() const BOOST_NOEXCEPT +{ + for (std::size_t i = 0; i < sizeof(data); ++i) + { + if (data[i] != 0U) + return false; + } + return true; +} + +inline void uuid::swap(uuid& rhs) BOOST_NOEXCEPT +{ + uuid tmp = *this; + *this = rhs; + rhs = tmp; +} + +inline bool operator== (uuid const& lhs, uuid const& rhs) BOOST_NOEXCEPT +{ + return memcmp(lhs.data, rhs.data, sizeof(lhs.data)) == 0; +} + +inline bool operator< (uuid const& lhs, uuid const& rhs) BOOST_NOEXCEPT +{ + return memcmp(lhs.data, rhs.data, sizeof(lhs.data)) < 0; +} + +} // namespace uuids +} // namespace boost + +#endif // BOOST_UUID_DETAIL_UUID_GENERIC_HPP_INCLUDED_ diff --git a/boost/uuid/detail/uuid_x86.hpp b/boost/uuid/detail/uuid_x86.hpp new file mode 100644 index 0000000000..1a329b02cc --- /dev/null +++ b/boost/uuid/detail/uuid_x86.hpp @@ -0,0 +1,109 @@ +/* + * Copyright Andrey Semashev 2013. + * 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) + */ +/*! + * \file uuid/detail/uuid_x86.hpp + * + * \brief This header contains optimized SSE implementation of \c boost::uuid operations. + */ + +#ifndef BOOST_UUID_DETAIL_UUID_X86_HPP_INCLUDED_ +#define BOOST_UUID_DETAIL_UUID_X86_HPP_INCLUDED_ + +// MSVC does not always have immintrin.h (at least, not up to MSVC 10), so include the appropriate header for each instruction set +#if defined(BOOST_UUID_USE_SSE41) +#include +#elif defined(BOOST_UUID_USE_SSE3) +#include +#else +#include +#endif + +namespace boost { +namespace uuids { +namespace detail { + +BOOST_FORCEINLINE __m128i load_unaligned_si128(const uint8_t* p) BOOST_NOEXCEPT +{ +#if defined(BOOST_UUID_USE_SSE3) + return _mm_lddqu_si128(reinterpret_cast< const __m128i* >(p)); +#else + return _mm_loadu_si128(reinterpret_cast< const __m128i* >(p)); +#endif +} + +} // namespace detail + +inline bool uuid::is_nil() const BOOST_NOEXCEPT +{ + register __m128i mm = uuids::detail::load_unaligned_si128(data); +#if defined(BOOST_UUID_USE_SSE41) + return _mm_test_all_zeros(mm, mm) != 0; +#else + mm = _mm_cmpeq_epi8(mm, _mm_setzero_si128()); + return _mm_movemask_epi8(mm) == 0xFFFF; +#endif +} + +inline void uuid::swap(uuid& rhs) BOOST_NOEXCEPT +{ + register __m128i mm_this = uuids::detail::load_unaligned_si128(data); + register __m128i mm_rhs = uuids::detail::load_unaligned_si128(rhs.data); + _mm_storeu_si128(reinterpret_cast< __m128i* >(rhs.data), mm_this); + _mm_storeu_si128(reinterpret_cast< __m128i* >(data), mm_rhs); +} + +inline bool operator== (uuid const& lhs, uuid const& rhs) BOOST_NOEXCEPT +{ + register __m128i mm_left = uuids::detail::load_unaligned_si128(lhs.data); + register __m128i mm_right = uuids::detail::load_unaligned_si128(rhs.data); + + register __m128i mm_cmp = _mm_cmpeq_epi32(mm_left, mm_right); +#if defined(BOOST_UUID_USE_SSE41) + return _mm_test_all_ones(mm_cmp); +#else + return _mm_movemask_epi8(mm_cmp) == 0xFFFF; +#endif +} + +inline bool operator< (uuid const& lhs, uuid const& rhs) BOOST_NOEXCEPT +{ + register __m128i mm_left = uuids::detail::load_unaligned_si128(lhs.data); + register __m128i mm_right = uuids::detail::load_unaligned_si128(rhs.data); + + // To emulate lexicographical_compare behavior we have to perform two comparisons - the forward and reverse one. + // Then we know which bytes are equivalent and which ones are different, and for those different the comparison results + // will be opposite. Then we'll be able to find the first differing comparison result (for both forward and reverse ways), + // and depending on which way it is for, this will be the result of the operation. There are a few notes to consider: + // + // 1. Due to little endian byte order the first bytes go into the lower part of the xmm registers, + // so the comparison results in the least significant bits will actually be the most signigicant for the final operation result. + // This means we have to determine which of the comparison results have the least significant bit on, and this is achieved with + // the "(x - 1) ^ x" trick. + // 2. Because there is only signed comparison in SSE/AVX, we have to invert byte comparison results whenever signs of the corresponding + // bytes are different. I.e. in signed comparison it's -1 < 1, but in unsigned it is the opposite (255 > 1). To do that we XOR left and right, + // making the most significant bit of each byte 1 if the signs are different, and later apply this mask with another XOR to the comparison results. + // 3. pcmpgtw compares for "greater" relation, so we swap the arguments to get what we need. + + const __m128i mm_signs_mask = _mm_xor_si128(mm_left, mm_right); + + __m128i mm_cmp = _mm_cmpgt_epi8(mm_right, mm_left), mm_rcmp = _mm_cmpgt_epi8(mm_left, mm_right); + + mm_cmp = _mm_xor_si128(mm_signs_mask, mm_cmp); + mm_rcmp = _mm_xor_si128(mm_signs_mask, mm_rcmp); + + uint32_t cmp = static_cast< uint32_t >(_mm_movemask_epi8(mm_cmp)), rcmp = static_cast< uint32_t >(_mm_movemask_epi8(mm_rcmp)); + + cmp = (cmp - 1u) ^ cmp; + rcmp = (rcmp - 1u) ^ rcmp; + + return static_cast< uint16_t >(cmp) < static_cast< uint16_t >(rcmp); +} + +} // namespace uuids +} // namespace boost + +#endif // BOOST_UUID_DETAIL_UUID_X86_HPP_INCLUDED_ diff --git a/boost/uuid/name_generator.hpp b/boost/uuid/name_generator.hpp index fe582b4583..2e5d8c16d8 100644 --- a/boost/uuid/name_generator.hpp +++ b/boost/uuid/name_generator.hpp @@ -30,8 +30,8 @@ class name_generator { public: typedef uuid result_type; - explicit name_generator(uuid const& namespace_uuid) - : namespace_uuid(namespace_uuid) + explicit name_generator(uuid const& namespace_uuid_) + : namespace_uuid(namespace_uuid_) {} uuid operator()(const char* name) { @@ -71,10 +71,10 @@ private: for (size_t i=0; i> 0) & 0xFF ); - sha.process_byte( (c >> 8) & 0xFF ); - sha.process_byte( (c >> 16) & 0xFF ); - sha.process_byte( (c >> 24) & 0xFF ); + sha.process_byte(static_cast((c >> 0) & 0xFF)); + sha.process_byte(static_cast((c >> 8) & 0xFF)); + sha.process_byte(static_cast((c >> 16) & 0xFF)); + sha.process_byte(static_cast((c >> 24) & 0xFF)); } } @@ -96,10 +96,10 @@ private: uuid u; for (int i=0; i<4; ++i) { - *(u.begin() + i*4+0) = ((digest[i] >> 24) & 0xFF); - *(u.begin() + i*4+1) = ((digest[i] >> 16) & 0xFF); - *(u.begin() + i*4+2) = ((digest[i] >> 8) & 0xFF); - *(u.begin() + i*4+3) = ((digest[i] >> 0) & 0xFF); + *(u.begin() + i*4+0) = static_cast((digest[i] >> 24) & 0xFF); + *(u.begin() + i*4+1) = static_cast((digest[i] >> 16) & 0xFF); + *(u.begin() + i*4+2) = static_cast((digest[i] >> 8) & 0xFF); + *(u.begin() + i*4+3) = static_cast((digest[i] >> 0) & 0xFF); } // set variant diff --git a/boost/uuid/seed_rng.hpp b/boost/uuid/seed_rng.hpp index d5d1e0fc6a..97b505f852 100644 --- a/boost/uuid/seed_rng.hpp +++ b/boost/uuid/seed_rng.hpp @@ -109,9 +109,10 @@ public: } private: + inline void ignore_size(size_t) {} + static unsigned int * sha1_random_digest_state_() { - // intentionally left uninitialized static unsigned int state[ 5 ]; return state; } @@ -153,12 +154,7 @@ private: if(random_) { - // the not_used variable is to suppress warnings -#if defined(__GNUC__) - __attribute__((unused)) -#endif - size_t not_used = 0; - not_used = std::fread( buffer, 1, 20, random_ ); + ignore_size(std::fread( buffer, 1, 20, random_ )); } // using an uninitialized buffer[] if fopen fails @@ -218,7 +214,7 @@ class generator_iterator > super_t; public: - generator_iterator() : m_g(NULL) {} + generator_iterator() : m_g(NULL), m_value(0) {} generator_iterator(Generator* g) : m_g(g), m_value((*m_g)()) {} void increment() diff --git a/boost/uuid/sha1.hpp b/boost/uuid/sha1.hpp index 3c1e341a5a..e695e13b21 100644 --- a/boost/uuid/sha1.hpp +++ b/boost/uuid/sha1.hpp @@ -19,6 +19,7 @@ #include #include #include +#include #ifdef BOOST_NO_STDC_NAMESPACE namespace std { @@ -89,10 +90,17 @@ inline void sha1::process_byte(unsigned char byte) { process_byte_impl(byte); - bit_count_low += 8; - if (bit_count_low == 0) { - ++bit_count_high; - if (bit_count_high == 0) { + // size_t max value = 0xFFFFFFFF + //if (bit_count_low + 8 >= 0x100000000) { // would overflow + //if (bit_count_low >= 0x100000000-8) { + if (bit_count_low < 0xFFFFFFF8) { + bit_count_low += 8; + } else { + bit_count_low = 0; + + if (bit_count_high <= 0xFFFFFFFE) { + ++bit_count_high; + } else { BOOST_THROW_EXCEPTION(std::runtime_error("sha1 too many bytes")); } } diff --git a/boost/uuid/uuid.hpp b/boost/uuid/uuid.hpp index 2678d856c0..d8593c0f75 100644 --- a/boost/uuid/uuid.hpp +++ b/boost/uuid/uuid.hpp @@ -28,20 +28,23 @@ // 28 Nov 2009 - disabled deprecated warnings for MSVC // 30 Nov 2009 - used BOOST_STATIC_CONSTANT // 02 Dec 2009 - removed BOOST_STATIC_CONSTANT - not all compilers like it +// 29 Apr 2013 - added support for noexcept and constexpr, added optimizations for SSE/AVX #ifndef BOOST_UUID_HPP #define BOOST_UUID_HPP -#include -#include +#include #include -#include -#include // for static assert +#include #ifndef BOOST_UUID_NO_TYPE_TRAITS #include #include #endif +#ifdef BOOST_HAS_PRAGMA_ONCE +#pragma once +#endif + #if defined(_MSC_VER) #pragma warning(push) // Save warning settings. #pragma warning(disable : 4996) // Disable deprecated std::swap_ranges, std::equal @@ -69,28 +72,20 @@ public: typedef std::ptrdiff_t difference_type; // This does not work on some compilers - // They seem to want the variable definec in + // They seem to want the variable definec in // a cpp file //BOOST_STATIC_CONSTANT(size_type, static_size = 16); - static size_type static_size() { return 16; } + static BOOST_CONSTEXPR size_type static_size() BOOST_NOEXCEPT { return 16; } public: - iterator begin() { return data; } /* throw() */ - const_iterator begin() const { return data; } /* throw() */ - iterator end() { return data+size(); } /* throw() */ - const_iterator end() const { return data+size(); } /* throw() */ + iterator begin() BOOST_NOEXCEPT { return data; } + const_iterator begin() const BOOST_NOEXCEPT { return data; } + iterator end() BOOST_NOEXCEPT { return data+size(); } + const_iterator end() const BOOST_NOEXCEPT { return data+size(); } - size_type size() const { return static_size(); } /* throw() */ + BOOST_CONSTEXPR size_type size() const BOOST_NOEXCEPT { return static_size(); } - bool is_nil() const /* throw() */ - { - for(size_t i=0; i uint8_t data[16]; }; -inline bool operator==(uuid const& lhs, uuid const& rhs) /* throw() */ -{ - return std::equal(lhs.begin(), lhs.end(), rhs.begin()); -} +bool operator== (uuid const& lhs, uuid const& rhs) BOOST_NOEXCEPT; +bool operator< (uuid const& lhs, uuid const& rhs) BOOST_NOEXCEPT; -inline bool operator!=(uuid const& lhs, uuid const& rhs) /* throw() */ +inline bool operator!=(uuid const& lhs, uuid const& rhs) BOOST_NOEXCEPT { return !(lhs == rhs); } -inline bool operator<(uuid const& lhs, uuid const& rhs) /* throw() */ -{ - return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end()); -} - -inline bool operator>(uuid const& lhs, uuid const& rhs) /* throw() */ +inline bool operator>(uuid const& lhs, uuid const& rhs) BOOST_NOEXCEPT { return rhs < lhs; } -inline bool operator<=(uuid const& lhs, uuid const& rhs) /* throw() */ +inline bool operator<=(uuid const& lhs, uuid const& rhs) BOOST_NOEXCEPT { return !(rhs < lhs); } -inline bool operator>=(uuid const& lhs, uuid const& rhs) /* throw() */ +inline bool operator>=(uuid const& lhs, uuid const& rhs) BOOST_NOEXCEPT { return !(lhs < rhs); } -inline void swap(uuid& lhs, uuid& rhs) /* throw() */ +inline void swap(uuid& lhs, uuid& rhs) BOOST_NOEXCEPT { lhs.swap(rhs); } // This is equivalent to boost::hash_range(u.begin(), u.end()); -inline std::size_t hash_value(uuid const& u) /* throw() */ +inline std::size_t hash_value(uuid const& u) BOOST_NOEXCEPT { std::size_t seed = 0; - for(uuid::const_iterator i=u.begin(); i != u.end(); ++i) + for(uuid::const_iterator i=u.begin(), e=u.end(); i != e; ++i) { seed ^= static_cast(*i) + 0x9e3779b9 + (seed << 6) + (seed >> 2); } @@ -214,6 +199,12 @@ struct is_pod : true_type {}; } // namespace boost #endif +#if defined(BOOST_UUID_USE_SSE2) +#include +#else +#include +#endif + #if defined(_MSC_VER) #pragma warning(pop) // Restore warnings to previous state. #endif diff --git a/boost/uuid/uuid_io.hpp b/boost/uuid/uuid_io.hpp index 592a5094df..1d30618aea 100644 --- a/boost/uuid/uuid_io.hpp +++ b/boost/uuid/uuid_io.hpp @@ -59,7 +59,7 @@ template } if (flags & std::ios_base::left) { - for (std::streamsize i=uuid_width; i