diff --git a/include/boost/endian/detail/intrinsic.hpp b/include/boost/endian/detail/intrinsic.hpp index c98aec0..8971085 100644 --- a/include/boost/endian/detail/intrinsic.hpp +++ b/include/boost/endian/detail/intrinsic.hpp @@ -9,37 +9,46 @@ #ifndef BOOST_ENDIAN_INTRINSIC_HPP #define BOOST_ENDIAN_INTRINSIC_HPP +// Allow user to force BOOST_ENDIAN_NO_INTRINSICS in case they aren't available for a +// particular platform/compiler combination. Please report such platform/compiler +// combinations to the Boost mailing list. +#ifndef BOOST_ENDIAN_NO_INTRINSICS + #ifndef __has_builtin // Optional of course #define __has_builtin(x) 0 // Compatibility with non-clang compilers #endif -#if (defined(__clang__) && __has_builtin(__builtin_bswap16)) \ +// GCC and Clang recent versions provide intrinsic byte swaps via builtins +#if (defined(__clang__) && __has_builtin(__builtin_bswap32) && __has_builtin(__builtin_bswap64)) \ || (defined(__GNUC__ ) && \ (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))) # define BOOST_ENDIAN_INTRINSIC_MSG "__builtin_bswap16, etc." - // prior to 4.8, gcc did not provide __builtin_bswap16 on some platforms so we emulate it // see http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52624 -# if defined(__clang__) || __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8) +// Clang has a similar problem, but their feature test macros make it easier to detect +# if (defined(__clang__) && __has_builtin(__builtin_bswap16)) \ + || (defined(__GNUC__) &&(__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8))) # define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_2(x) __builtin_bswap16(x) # else # define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_2(x) __builtin_bswap32((x) << 16) # endif - # define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_4(x) __builtin_bswap32(x) # define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_8(x) __builtin_bswap64(x) -#elif ((defined __GNUC__ && !defined(__MINGW32__)) || defined(__clang__)) -# define BOOST_ENDIAN_INTRINSIC_MSG "__builtin_bswap_16, etc." +// Linux systems provide the byteswap.h header, with +#elif defined(__linux__) +// don't check for obsolete forms defined(linux) and defined(__linux) on the theory that +// compilers that predefine only these are so old that byteswap.h probably isn't present. +# define BOOST_ENDIAN_INTRINSIC_MSG "byteswap.h bswap_16, etc." # include # define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_2(x) bswap_16(x) # define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_4(x) bswap_32(x) # define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_8(x) bswap_64(x) -#elif defined _MSC_VER +#elif defined(_MSC_VER) // Microsoft documents these as being compatible since Windows 95 and specificly // lists runtime library support since Visual Studio 2003 (aka 7.1). -# define BOOST_ENDIAN_INTRINSIC_MSG "_byteswap_ushort, etc." +# define BOOST_ENDIAN_INTRINSIC_MSG "cstdlib _byteswap_ushort, etc." # include # define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_2(x) _byteswap_ushort(x) # define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_4(x) _byteswap_ulong(x) @@ -49,4 +58,5 @@ # define BOOST_ENDIAN_INTRINSIC_MSG "no byte swap intrinsics" #endif -#endif // BOOST_ENDIAN_INTRINSIC_HPP +#endif // BOOST_ENDIAN_NO_INTRINSICS +#endif // BOOST_ENDIAN_INTRINSIC_HPP diff --git a/test/intrinsic_test.cpp b/test/intrinsic_test.cpp index 90a32fb..fc22e12 100644 --- a/test/intrinsic_test.cpp +++ b/test/intrinsic_test.cpp @@ -3,10 +3,14 @@ // Distributed under the Boost Software License, Version 1.0. // http://www.boost.org/LICENSE_1_0.txt -#include "intrinsic.hpp" +#include "test.hpp" #include #include -#include +//#include + +typedef unsigned short uint16_t; +typedef unsigned int uint32_t; +typedef unsigned long long uint64_t; int main() {