mirror of
https://github.com/boostorg/endian.git
synced 2025-08-02 05:54:31 +02:00
Rework intrinsic.hpp to handle GCC and Clang __builtin_bswap* if available. Add test program.
This commit is contained in:
@@ -1,27 +1,52 @@
|
|||||||
// Copyright (C) 2012 David Stone
|
// endian/detail/intrinsic.hpp -------------------------------------------------------//
|
||||||
// Distributed under the Boost Software License, Version 1.0.
|
|
||||||
// http://www.boost.org/LICENSE_1_0.txt
|
// Copyright (C) 2012 David Stone
|
||||||
//
|
// Copyright Beman Dawes 2013
|
||||||
// See http://www.boost.org/libs/endian/ for documentation.
|
|
||||||
|
// Distributed under the Boost Software License, Version 1.0.
|
||||||
|
// http://www.boost.org/LICENSE_1_0.txt
|
||||||
|
|
||||||
#ifndef BOOST_ENDIAN_INTRINSIC_HPP
|
#ifndef BOOST_ENDIAN_INTRINSIC_HPP
|
||||||
#define BOOST_ENDIAN_INTRINSIC_HPP
|
#define BOOST_ENDIAN_INTRINSIC_HPP
|
||||||
|
|
||||||
#if ((defined __GNUC__ && !defined(__MINGW32__)) || defined __clang__)
|
#ifndef __has_builtin // Optional of course
|
||||||
#include <byteswap.h>
|
#define __has_builtin(x) 0 // Compatibility with non-clang compilers
|
||||||
#define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_2 bswap_16
|
#endif
|
||||||
#define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_4 bswap_32
|
|
||||||
#define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_8 bswap_64
|
#if (defined(__clang__) && __has_builtin(__builtin_bswap16)) \
|
||||||
|
|| (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)
|
||||||
|
# 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."
|
||||||
|
# include <byteswap.h>
|
||||||
|
# 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
|
// 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).
|
||||||
#include <cstdlib>
|
# define BOOST_ENDIAN_INTRINSIC_MSG "_byteswap_ushort, etc."
|
||||||
#define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_2 _byteswap_ushort
|
# include <cstdlib>
|
||||||
#define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_4 _byteswap_ulong
|
# define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_2(x) _byteswap_ushort(x)
|
||||||
#define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_8 _byteswap_uint64
|
# define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_4(x) _byteswap_ulong(x)
|
||||||
|
# define BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_8(x) _byteswap_uint64(x)
|
||||||
#else
|
#else
|
||||||
#define BOOST_ENDIAN_NO_INTRINSICS
|
# define BOOST_ENDIAN_NO_INTRINSICS
|
||||||
|
# define BOOST_ENDIAN_INTRINSIC_MSG "no byte swap intrinsics"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif // BOOST_ENDIAN_INTRINSIC_HPP
|
#endif // BOOST_ENDIAN_INTRINSIC_HPP
|
||||||
|
@@ -239,6 +239,8 @@ namespace
|
|||||||
|
|
||||||
int cpp_main(int, char * [])
|
int cpp_main(int, char * [])
|
||||||
{
|
{
|
||||||
|
cout << "byte swap intrinsics: " BOOST_ENDIAN_INTRINSIC_MSG << endl;
|
||||||
|
|
||||||
//std::cerr << std::hex;
|
//std::cerr << std::hex;
|
||||||
|
|
||||||
cout << "int16_t" << endl;
|
cout << "int16_t" << endl;
|
||||||
|
24
test/intrinsic_test.cpp
Normal file
24
test/intrinsic_test.cpp
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
// Copyright Beman Dawes 2013
|
||||||
|
|
||||||
|
// Distributed under the Boost Software License, Version 1.0.
|
||||||
|
// http://www.boost.org/LICENSE_1_0.txt
|
||||||
|
|
||||||
|
#include "intrinsic.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
#include <cassert>
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
std::cout << "BOOST_ENDIAN_INTRINSIC_MSG: " BOOST_ENDIAN_INTRINSIC_MSG << std::endl;
|
||||||
|
|
||||||
|
#ifndef BOOST_ENDIAN_NO_INTRINSICS
|
||||||
|
uint16_t x2 = 0x1122U;
|
||||||
|
assert(BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_2(x2) == 0x2211U);
|
||||||
|
uint32_t x4 = 0x11223344UL;
|
||||||
|
assert(BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_4(x4) == 0x44332211UL);
|
||||||
|
uint64_t x8 = 0x1122334455667788U;
|
||||||
|
assert(BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_8(x8) == 0x8877665544332211ULL);
|
||||||
|
#endif
|
||||||
|
return 0;
|
||||||
|
}
|
Reference in New Issue
Block a user