Add endian conversion functions

This commit is contained in:
Danny Robson 2011-08-29 14:36:03 +10:00
parent b23c0862cc
commit 73f27c7cb1
3 changed files with 139 additions and 41 deletions

View File

@ -7,49 +7,51 @@ AM_LDFLAGS = $(BOOST_LDFLAGS) $(BOOST_FILESYSTEM_LIB) $(COMMON_LDFLAGS)
SUBDIRS = test SUBDIRS = test
UTIL_INCLUDE = \ UTIL_INCLUDE = \
annotations.hpp \ annotations.hpp \
backtrace.hpp \ backtrace.hpp \
debug.hpp \ debug.hpp \
enable_if.hpp \ enable_if.hpp \
except.hpp \ endian.hpp \
fixed.hpp \ except.hpp \
float.hpp \ fixed.hpp \
io.hpp \ float.hpp \
ip.hpp \ io.hpp \
json.hpp \ ip.hpp \
maths.hpp \ json.hpp \
matrix.hpp \ maths.hpp \
nocopy.hpp \ matrix.hpp \
point.hpp \ nocopy.hpp \
pool.hpp \ point.hpp \
range.hpp \ pool.hpp \
region.hpp \ range.hpp \
signal.hpp \ region.hpp \
stream.hpp \ signal.hpp \
string.hpp \ stream.hpp \
types.hpp \ string.hpp \
vector.hpp \ types.hpp \
vector.hpp \
version.hpp version.hpp
UTIL_FILES = \ UTIL_FILES = \
debug.cpp \ debug.cpp \
except.cpp \ endian.cpp \
fixed.cpp \ except.cpp \
float.cpp \ fixed.cpp \
io.cpp \ float.cpp \
ip.cpp \ io.cpp \
json.cpp \ ip.cpp \
maths.cpp \ json.cpp \
matrix.cpp \ maths.cpp \
point.cpp \ matrix.cpp \
pool.cpp \ point.cpp \
range.cpp \ pool.cpp \
region.cpp \ range.cpp \
signal.cpp \ region.cpp \
stream.cpp \ signal.cpp \
string.cpp \ stream.cpp \
types.cpp \ string.cpp \
vector.cpp \ types.cpp \
vector.cpp \
version.cpp version.cpp

20
endian.cpp Normal file
View File

@ -0,0 +1,20 @@
/*
* This file is part of libgim.
*
* libgim is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* libgim is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2010 Danny Robson <danny@blubinc.net>
*/
#include "endian.hpp"

76
endian.hpp Normal file
View File

@ -0,0 +1,76 @@
/*
* This file is part of libgim.
*
* libgim is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* libgim is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2010 Danny Robson <danny@blubinc.net>
*/
#ifndef __UTIL_ENDIAN_HPP
#define __UTIL_ENDIAN_HPP
#include <cstdint>
enum {
ENDIAN_BIG,
ENDIAN_LITTLE
};
constexpr uint8_t hton (uint8_t h) { return h; }
constexpr uint8_t ntoh (uint8_t n) { return n; }
constexpr uint16_t hton (uint16_t h) {
return ((h & 0xFF00) >> 8) |
((h & 0x00FF) << 8);
}
constexpr uint16_t ntoh (uint16_t n)
{ return hton (n); }
constexpr uint32_t hton (uint32_t h) {
return __builtin_bswap32 (h);
//return (h & 0xFF000000U) >> 24 |
// (h & 0x00FF0000U) >> 8 |
// (h & 0x0000FF00U) << 8 |
// (h & 0x000000FFU) << 24;
}
constexpr uint32_t ntoh (uint32_t n)
{ return hton (n); }
constexpr uint64_t hton (uint64_t h) {
return __builtin_bswap64 (h);
// return (h & 0xFF00000000000000UL) >> 56 |
// (h & 0x00FF000000000000UL) >> 40 |
// (h & 0x0000FF0000000000UL) >> 24 |
// (h & 0x000000FF00000000UL) >> 8 |
// (h & 0x00000000FF000000UL) << 8 |
// (h & 0x0000000000FF0000UL) << 24 |
// (h & 0x000000000000FF00UL) << 40 |
// (h & 0x00000000000000FFUL) << 56;
}
constexpr uint64_t ntoh (uint64_t n)
{ return hton (n); }
#endif