Commit 9d40b7f3 authored by Axel Auweter's avatar Axel Auweter
Browse files

Fixes ticket #23 (endianess support).

parent a4b30e7f
......@@ -2,7 +2,8 @@ include ../config.mk
# C++ Compiler Flags (use fPIC for our dynamic library)
CXXFLAGS = -O0 -g -Wall -Werror -Wno-unused-local-typedefs -Wno-unknown-warning-option\
-fPIC --std=c++11 -I$(DCDBDEPLOYPATH)/include -I./include -I./include_internal -fmessage-length=0
-fPIC --std=c++11 -I$(DCDBDEPLOYPATH)/include -I./include -I./include_internal\
-I$(DCDBBASEPATH)/include/ -fmessage-length=0
# List of object files to build and the derived list of corresponding source files
OBJS = src/sensordatastore.o \
......
......@@ -6,20 +6,21 @@
*/
#include <boost/lexical_cast.hpp>
#include <endian.h>
#include "sensordatastore_internal.h"
string SensorDataStoreImpl::sidConvert(SensorId *sid)
{
uint64_t ll[2];
ll[0] = __builtin_bswap64(sid->raw[0]);
ll[1]= __builtin_bswap64(sid->raw[1]);
ll[0] = Endian::hostToBE(sid->raw[0]);
ll[1] = Endian::hostToBE(sid->raw[1]);
return string((char*)ll, 16);
}
string SensorDataStoreImpl::int64Convert(uint64_t n)
{
n = __builtin_bswap64(n);
n = Endian::hostToBE(n);
return string((char*)&n, 8);
}
......
/*
* endian.h
*
* Created on: Apr 6, 2014
* Author: Axel Auweter
*/
#ifndef ENDIAN_H
#define ENDIAN_H
#include <boost/predef/detail/endian_compat.h>
#include <stdint.h>
/* Check if we can use any optimized byte swap implementation */
#if defined(__llvm__) || (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)) && !defined(__ICC)
# define OPTIMIZED_BSWAP_16(x) __builtin_bswap16(x)
# define OPTIMIZED_BSWAP_32(x) __builtin_bswap32(x)
# define OPTIMIZED_BSWAP_64(x) __builtin_bswap64(x)
#elif defined(_MSC_VER) && !defined(_DEBUG)
# define OPTIMIZED_BSWAP_16(x) _byteswap_ushort(x)
# define OPTIMIZED_BSWAP_32(x) _byteswap_ulong(x)
# define OPTIMIZED_BSWAP_64(x) _byteswap_uint64(x)
#endif
class Endian
{
public:
static inline uint16_t swap(uint16_t x) {
#if defined OPTIMIZED_BSWAP_16
return OPTIMIZED_BSWAP_16(x);
#else
return (x>>8) | (x<<8);
#endif
}
static inline uint32_t swap(uint32_t x) {
#if defined OPTIMIZED_BSWAP_32
return OPTIMIZED_BSWAP_32(x);
#else
return ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) |
(((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24));
#endif
}
static inline uint64_t swap(uint64_t x) {
#if defined OPTIMIZED_BSWAP_64
return OPTIMIZED_BSWAP_64(x);
#else
return (((uint64_t)swap32((uint32_t)x))<<32) |
(swap32((uint32_t)(x>>32)));
#endif
}
static inline uint16_t hostToBE(uint16_t x) {
#if defined BOOST_BIG_ENDIAN
return x;
#elif defined BOOST_LITTLE_ENDIAN
return swap(x);
#else
# error Endianess of host is undefined!
#endif
}
static inline uint32_t hostToBE(uint32_t x) {
#if defined BOOST_BIG_ENDIAN
return x;
#elif defined BOOST_LITTLE_ENDIAN
return swap(x);
#else
# error Endianess of host is undefined!
#endif
}
static inline uint64_t hostToBE(uint64_t x) {
#if defined BOOST_BIG_ENDIAN
return x;
#elif defined BOOST_LITTLE_ENDIAN
return swap(x);
#else
# error Endianess of host is undefined!
#endif
}
static inline uint16_t hostToLE(uint16_t x) {
#if defined BOOST_BIG_ENDIAN
return swap(x);
#elif defined BOOST_LITTLE_ENDIAN
return x;
#else
# error Endianess of host is undefined!
#endif
}
static inline uint32_t hostToLE(uint32_t x) {
#if defined BOOST_BIG_ENDIAN
return swap(x);
#elif defined BOOST_LITTLE_ENDIAN
return x;
#else
# error Endianess of host is undefined!
#endif
}
static inline uint64_t hostToLE(uint64_t x) {
#if defined BOOST_BIG_ENDIAN
return swap(x);
#elif defined BOOST_LITTLE_ENDIAN
return x;
#else
# error Endianess of host is undefined!
#endif
}
static inline uint16_t BEToHost(uint16_t x) { return hostToBE(x); }
static inline uint32_t BEToHost(uint32_t x) { return hostToBE(x); }
static inline uint64_t BEToHost(uint64_t x) { return hostToBE(x); }
};
#endif
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment