mirror of
https://github.com/boostorg/endian.git
synced 2025-08-01 21:44:31 +02:00
Use a more robust detection of __builtin_bswap funtions for Clang. Fix Clang lack of __builtin_bswap16 in some releases. To be conservative only, fallback to byteswap.h if __linux__ is defined. Fix intrinsic_test so that <cstdint> isn't required.
This commit is contained in:
@@ -9,37 +9,46 @@
|
|||||||
#ifndef BOOST_ENDIAN_INTRINSIC_HPP
|
#ifndef BOOST_ENDIAN_INTRINSIC_HPP
|
||||||
#define 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
|
#ifndef __has_builtin // Optional of course
|
||||||
#define __has_builtin(x) 0 // Compatibility with non-clang compilers
|
#define __has_builtin(x) 0 // Compatibility with non-clang compilers
|
||||||
#endif
|
#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__ ) && \
|
|| (defined(__GNUC__ ) && \
|
||||||
(__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)))
|
(__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)))
|
||||||
# define BOOST_ENDIAN_INTRINSIC_MSG "__builtin_bswap16, etc."
|
# 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
|
// 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
|
// 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)
|
# define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_2(x) __builtin_bswap16(x)
|
||||||
# else
|
# else
|
||||||
# define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_2(x) __builtin_bswap32((x) << 16)
|
# define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_2(x) __builtin_bswap32((x) << 16)
|
||||||
# endif
|
# endif
|
||||||
|
|
||||||
# define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_4(x) __builtin_bswap32(x)
|
# define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_4(x) __builtin_bswap32(x)
|
||||||
# define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_8(x) __builtin_bswap64(x)
|
# define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_8(x) __builtin_bswap64(x)
|
||||||
|
|
||||||
#elif ((defined __GNUC__ && !defined(__MINGW32__)) || defined(__clang__))
|
// Linux systems provide the byteswap.h header, with
|
||||||
# define BOOST_ENDIAN_INTRINSIC_MSG "__builtin_bswap_16, etc."
|
#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 <byteswap.h>
|
# include <byteswap.h>
|
||||||
# define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_2(x) bswap_16(x)
|
# 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_4(x) bswap_32(x)
|
||||||
# define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_8(x) bswap_64(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
|
// Microsoft documents these as being compatible since Windows 95 and specificly
|
||||||
// lists runtime library support since Visual Studio 2003 (aka 7.1).
|
// 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 <cstdlib>
|
# include <cstdlib>
|
||||||
# define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_2(x) _byteswap_ushort(x)
|
# define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_2(x) _byteswap_ushort(x)
|
||||||
# define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_4(x) _byteswap_ulong(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"
|
# define BOOST_ENDIAN_INTRINSIC_MSG "no byte swap intrinsics"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif // BOOST_ENDIAN_INTRINSIC_HPP
|
#endif // BOOST_ENDIAN_NO_INTRINSICS
|
||||||
|
#endif // BOOST_ENDIAN_INTRINSIC_HPP
|
||||||
|
@@ -3,10 +3,14 @@
|
|||||||
// Distributed under the Boost Software License, Version 1.0.
|
// Distributed under the Boost Software License, Version 1.0.
|
||||||
// http://www.boost.org/LICENSE_1_0.txt
|
// http://www.boost.org/LICENSE_1_0.txt
|
||||||
|
|
||||||
#include "intrinsic.hpp"
|
#include "test.hpp"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <cstdint>
|
//#include <cstdint>
|
||||||
|
|
||||||
|
typedef unsigned short uint16_t;
|
||||||
|
typedef unsigned int uint32_t;
|
||||||
|
typedef unsigned long long uint64_t;
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user