Poco::ByteOrder now uses intrinsics, if available

This commit is contained in:
Guenter Obiltschnig 2014-02-04 14:20:51 +01:00
parent a005d582bc
commit 66e096a1c8

View File

@ -1,13 +1,13 @@
//
// ByteOrder.h
//
// $Id: //poco/1.4/Foundation/include/Poco/ByteOrder.h#1 $
// $Id: //poco/1.4/Foundation/include/Poco/ByteOrder.h#2 $
//
// Library: Foundation
// Package: Core
// Module: ByteOrder
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// Copyright (c) 2004-2014, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
@ -40,6 +40,9 @@
#include "Poco/Foundation.h"
#include "Poco/Types.h"
#if defined(_MSC_VER)
#include <stdlib.h> // builtins
#endif
namespace Poco {
@ -116,12 +119,29 @@ public:
};
#if !defined(POCO_NO_BYTESWAP_BUILTINS)
#if defined(_MSC_VER)
#define POCO_HAVE_MSC_BYTESWAP 1
#elif defined(__clang__) && __has_builtin(__builtin_bswap32)
#define POCO_HAVE_GCC_BYTESWAP 1
#elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
#define POCO_HAVE_GCC_BYTESWAP 1
#endif
#endif
//
// inlines
//
inline UInt16 ByteOrder::flipBytes(UInt16 value)
{
#if defined(POCO_HAVE_MSC_BYTESWAP)
return _byteswap_ushort(value);
#elif defined(POCO_HAVE_GCC_BYTESWAP)
return __builtin_bswap16(value);
#else
return ((value >> 8) & 0x00FF) | ((value << 8) & 0xFF00);
#endif
}
@ -133,8 +153,14 @@ inline Int16 ByteOrder::flipBytes(Int16 value)
inline UInt32 ByteOrder::flipBytes(UInt32 value)
{
#if defined(POCO_HAVE_MSC_BYTESWAP)
return _byteswap_ulong(value)
#elif defined(POCO_HAVE_GCC_BYTESWAP)
return __builtin_bswap32(value);
#else
return ((value >> 24) & 0x000000FF) | ((value >> 8) & 0x0000FF00)
| ((value << 8) & 0x00FF0000) | ((value << 24) & 0xFF000000);
#endif
}
@ -147,9 +173,15 @@ inline Int32 ByteOrder::flipBytes(Int32 value)
#if defined(POCO_HAVE_INT64)
inline UInt64 ByteOrder::flipBytes(UInt64 value)
{
#if defined(POCO_HAVE_MSC_BYTESWAP)
return _byteswap_uint64(value)
#elif defined(POCO_HAVE_GCC_BYTESWAP)
return __builtin_bswap64(value);
#else
UInt32 hi = UInt32(value >> 32);
UInt32 lo = UInt32(value & 0xFFFFFFFF);
return UInt64(flipBytes(hi)) | (UInt64(flipBytes(lo)) << 32);
#endif
}