From 96c2e6dacbd70d8e203764be0d0427e455edf788 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Thu, 6 Dec 2018 20:34:15 +0200 Subject: [PATCH] Cosmetic fixes --- include/boost/endian/buffers.hpp | 54 +++++++++++--------------------- 1 file changed, 18 insertions(+), 36 deletions(-) diff --git a/include/boost/endian/buffers.hpp b/include/boost/endian/buffers.hpp index 1f35c47..039c93e 100644 --- a/include/boost/endian/buffers.hpp +++ b/include/boost/endian/buffers.hpp @@ -284,18 +284,13 @@ namespace endian T load_big_endian(const void* bytes) BOOST_NOEXCEPT { # if defined(__x86_64__) || defined(_M_X64) || defined(__i386) || defined(_M_IX86) - // On x86 (which is little endian), unaligned loads are permitted - if (sizeof(T) == n_bytes) // GCC 4.9, VC++ 14.0, and probably others, elide this - // test and generate code only for the applicable return - // case since sizeof(T) and n_bytes are known at compile - // time. + // All major x86 compilers elide this test and optimize out memcpy + // (the x86 architecture allows unaligned loads, but -fsanitize=undefined does not) + if (sizeof(T) == n_bytes) { - // Avoids -fsanitize=undefined violations due to unaligned loads - // All major x86 compilers optimize a short-sized memcpy into a single instruction - - T t = 0; + T t; std::memcpy( &t, bytes, sizeof(T) ); - return big_to_native(t); + return endian::big_to_native(t); } # endif return unrolled_byte_loops::load_big @@ -307,18 +302,13 @@ namespace endian T load_little_endian(const void* bytes) BOOST_NOEXCEPT { # if defined(__x86_64__) || defined(_M_X64) || defined(__i386) || defined(_M_IX86) - // On x86 (which is little endian), unaligned loads are permitted - if (sizeof(T) == n_bytes) // GCC 4.9, VC++ 14.0, and probably others, elide this - // test and generate code only for the applicable return - // case since sizeof(T) and n_bytes are known at compile - // time. + // All major x86 compilers elide this test and optimize out memcpy + // (the x86 architecture allows unaligned loads, but -fsanitize=undefined does not) + if (sizeof(T) == n_bytes) { - // Avoids -fsanitize=undefined violations due to unaligned loads - // All major x86 compilers optimize a short-sized memcpy into a single instruction - T t; std::memcpy( &t, bytes, sizeof(T) ); - return t; + return t; // or endian::little_to_native(t) if we ever extend the #ifdef to non-x86 } # endif return unrolled_byte_loops::load_little @@ -330,16 +320,11 @@ namespace endian void store_big_endian(void* bytes, T value) BOOST_NOEXCEPT { # if defined(__x86_64__) || defined(_M_X64) || defined(__i386) || defined(_M_IX86) - // On x86 (which is little endian), unaligned stores are permitted - if (sizeof(T) == n_bytes) // GCC 4.9, VC++ 14.0, and probably others, elide this - // test and generate code only for the applicable return - // case since sizeof(T) and n_bytes are known at compile - // time. + // All major x86 compilers elide this test and optimize out memcpy + // (the x86 architecture allows unaligned loads, but -fsanitize=undefined does not) + if (sizeof(T) == n_bytes) { - // Avoids -fsanitize=undefined violations due to unaligned stores - // All major x86 compilers optimize a short-sized memcpy into a single instruction - - boost::endian::native_to_big_inplace(value); + endian::native_to_big_inplace(value); std::memcpy( bytes, &value, sizeof(T) ); return; } @@ -353,15 +338,12 @@ namespace endian void store_little_endian(void* bytes, T value) BOOST_NOEXCEPT { # if defined(__x86_64__) || defined(_M_X64) || defined(__i386) || defined(_M_IX86) - // On x86 (which is little endian), unaligned stores are permitted - if (sizeof(T) == n_bytes) // GCC 4.9, VC++ 14.0, and probably others, elide this - // test and generate code only for the applicable return - // case since sizeof(T) and n_bytes are known at compile - // time. + // All major x86 compilers elide this test and optimize out memcpy + // (the x86 architecture allows unaligned loads, but -fsanitize=undefined does not) + if (sizeof(T) == n_bytes) { - // Avoids -fsanitize=undefined violations due to unaligned stores - // All major x86 compilers optimize a short-sized memcpy into a single instruction - + // if we ever extend the #ifdef to non-x86: + // endian::native_to_little_inplace(value); std::memcpy( bytes, &value, sizeof(T) ); return; }