Skip to content

Commit

Permalink
Poco::ByteOrder now uses intrinsics, if available
Browse files Browse the repository at this point in the history
  • Loading branch information
obiltschnig committed Feb 4, 2014
1 parent a005d58 commit 66e096a
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions Foundation/include/Poco/ByteOrder.h
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -40,6 +40,9 @@

#include "Poco/Foundation.h"
#include "Poco/Types.h"
#if defined(_MSC_VER)
#include <stdlib.h> // builtins
#endif


namespace Poco {
Expand Down Expand Up @@ -116,12 +119,29 @@ class Foundation_API ByteOrder
};


#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
}


Expand All @@ -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
}


Expand All @@ -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
}


Expand Down

0 comments on commit 66e096a

Please sign in to comment.