| 1 |
// endian/detail/intrinsic.hpp -------------------------------------------------------// |
| 2 |
|
| 3 |
// Copyright (C) 2012 David Stone |
| 4 |
// Copyright Beman Dawes 2013 |
| 5 |
|
| 6 |
// Distributed under the Boost Software License, Version 1.0. |
| 7 |
// http://www.boost.org/LICENSE_1_0.txt |
| 8 |
|
| 9 |
#ifndef BOOST_ENDIAN_INTRINSIC_HPP |
| 10 |
#define BOOST_ENDIAN_INTRINSIC_HPP |
| 11 |
|
| 12 |
// Allow user to force BOOST_ENDIAN_NO_INTRINSICS in case they aren't available for a |
| 13 |
// particular platform/compiler combination. Please report such platform/compiler |
| 14 |
// combinations to the Boost mailing list. |
| 15 |
#ifndef BOOST_ENDIAN_NO_INTRINSICS |
| 16 |
|
| 17 |
#ifndef __has_builtin // Optional of course |
| 18 |
#define __has_builtin(x) 0 // Compatibility with non-clang compilers |
| 19 |
#endif |
| 20 |
|
| 21 |
// GCC and Clang recent versions provide intrinsic byte swaps via builtins |
| 22 |
#if (defined(__clang__) && __has_builtin(__builtin_bswap32) && __has_builtin(__builtin_bswap64)) \ |
| 23 |
|| (defined(__GNUC__ ) && \ |
| 24 |
(__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))) |
| 25 |
# define BOOST_ENDIAN_INTRINSIC_MSG "__builtin_bswap16, etc." |
| 26 |
// prior to 4.8, gcc did not provide __builtin_bswap16 on some platforms so we emulate it |
| 27 |
// see http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52624 |
| 28 |
// Clang has a similar problem, but their feature test macros make it easier to detect |
| 29 |
# if (defined(__clang__) && __has_builtin(__builtin_bswap16)) \ |
| 30 |
|| (defined(__GNUC__) &&(__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8))) |
| 31 |
# define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_2(x) __builtin_bswap16(x) |
| 32 |
# else |
| 33 |
# define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_2(x) __builtin_bswap32((x) << 16) |
| 34 |
# endif |
| 35 |
# define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_4(x) __builtin_bswap32(x) |
| 36 |
# define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_8(x) __builtin_bswap64(x) |
| 37 |
|
| 38 |
// Linux systems provide the byteswap.h header, with |
| 39 |
#elif defined(__linux__) |
| 40 |
// don't check for obsolete forms defined(linux) and defined(__linux) on the theory that |
| 41 |
// compilers that predefine only these are so old that byteswap.h probably isn't present. |
| 42 |
# define BOOST_ENDIAN_INTRINSIC_MSG "byteswap.h bswap_16, etc." |
| 43 |
# include <byteswap.h> |
| 44 |
# define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_2(x) bswap_16(x) |
| 45 |
# define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_4(x) bswap_32(x) |
| 46 |
# define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_8(x) bswap_64(x) |
| 47 |
|
| 48 |
#elif defined(_MSC_VER) |
| 49 |
// Microsoft documents these as being compatible since Windows 95 and specificly |
| 50 |
// lists runtime library support since Visual Studio 2003 (aka 7.1). |
| 51 |
# define BOOST_ENDIAN_INTRINSIC_MSG "cstdlib _byteswap_ushort, etc." |
| 52 |
# include <cstdlib> |
| 53 |
# define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_2(x) _byteswap_ushort(x) |
| 54 |
# define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_4(x) _byteswap_ulong(x) |
| 55 |
# define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_8(x) _byteswap_uint64(x) |
| 56 |
#else |
| 57 |
# define BOOST_ENDIAN_NO_INTRINSICS |
| 58 |
# define BOOST_ENDIAN_INTRINSIC_MSG "no byte swap intrinsics" |
| 59 |
#endif |
| 60 |
|
| 61 |
#elif !defined(BOOST_ENDIAN_INTRINSIC_MSG) |
| 62 |
# define BOOST_ENDIAN_INTRINSIC_MSG "no byte swap intrinsics" |
| 63 |
#endif // BOOST_ENDIAN_NO_INTRINSICS |
| 64 |
#endif // BOOST_ENDIAN_INTRINSIC_HPP |