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
UTIL_INCLUDE = \
annotations.hpp \
backtrace.hpp \
debug.hpp \
enable_if.hpp \
except.hpp \
fixed.hpp \
float.hpp \
io.hpp \
ip.hpp \
json.hpp \
maths.hpp \
matrix.hpp \
nocopy.hpp \
point.hpp \
pool.hpp \
range.hpp \
region.hpp \
signal.hpp \
stream.hpp \
string.hpp \
types.hpp \
vector.hpp \
annotations.hpp \
backtrace.hpp \
debug.hpp \
enable_if.hpp \
endian.hpp \
except.hpp \
fixed.hpp \
float.hpp \
io.hpp \
ip.hpp \
json.hpp \
maths.hpp \
matrix.hpp \
nocopy.hpp \
point.hpp \
pool.hpp \
range.hpp \
region.hpp \
signal.hpp \
stream.hpp \
string.hpp \
types.hpp \
vector.hpp \
version.hpp
UTIL_FILES = \
debug.cpp \
except.cpp \
fixed.cpp \
float.cpp \
io.cpp \
ip.cpp \
json.cpp \
maths.cpp \
matrix.cpp \
point.cpp \
pool.cpp \
range.cpp \
region.cpp \
signal.cpp \
stream.cpp \
string.cpp \
types.cpp \
vector.cpp \
UTIL_FILES = \
debug.cpp \
endian.cpp \
except.cpp \
fixed.cpp \
float.cpp \
io.cpp \
ip.cpp \
json.cpp \
maths.cpp \
matrix.cpp \
point.cpp \
pool.cpp \
range.cpp \
region.cpp \
signal.cpp \
stream.cpp \
string.cpp \
types.cpp \
vector.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