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