intrinsic.hpp 3.2 KB

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