diff options
author | Luo Jinghua <sunmoon1997@gmail.com> | 2009-10-28 23:41:22 +0800 |
---|---|---|
committer | Luo Jinghua <sunmoon1997@gmail.com> | 2009-10-28 23:41:22 +0800 |
commit | b10d18dbdee8f8762a79e8e7b6706d7326217798 (patch) | |
tree | 282318a781e61e9e08acbd6cb01a0c636d013531 | |
parent | 6275c356bcfac4fbb2ea1f49cb86df26a5410431 (diff) |
milkway: add mw-byteorder.h & mw-config.h
-rw-r--r-- | milkway/Makefile.am | 2 | ||||
-rw-r--r-- | milkway/milkway.h | 2 | ||||
-rw-r--r-- | milkway/mw-byteorder.h | 209 | ||||
-rw-r--r-- | milkway/mw-compiler.h | 30 | ||||
-rw-r--r-- | milkway/mw-config.h | 58 |
5 files changed, 301 insertions, 0 deletions
diff --git a/milkway/Makefile.am b/milkway/Makefile.am index 2b83b8c..11265ba 100644 --- a/milkway/Makefile.am +++ b/milkway/Makefile.am @@ -21,6 +21,8 @@ libmilkwayincludedir = $(includedir)/milkway/milkway libmilkwayinclude_HEADER = \ milkway.h \ mw-assert.h \ + mw-byteorder.h \ + mw-config.h \ mw-types.h \ mw-compiler.h \ mw-crypt.h diff --git a/milkway/milkway.h b/milkway/milkway.h index fcf9932..e384937 100644 --- a/milkway/milkway.h +++ b/milkway/milkway.h @@ -20,9 +20,11 @@ #endif #include <milkway/mw-compiler.h> +#include <milkway/mw-config.h> #include <milkway/mw-types.h> #include <milkway/mw-error.h> #include <milkway/mw-assert.h> +#include <milkway/mw-byteorder.h> #include <milkway/mw-crypt.h> #include <milkway/mw-checksum.h> #include <milkway/mw-hexlify.h> diff --git a/milkway/mw-byteorder.h b/milkway/mw-byteorder.h new file mode 100644 index 0000000..d934276 --- /dev/null +++ b/milkway/mw-byteorder.h @@ -0,0 +1,209 @@ +/* Milkway + * + * Copyright (C) 2009- Luo Jinghua <sunmoon1997@gmail.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ +#ifndef FBYTEORDER_H +#define FBYTEORDER_H + +#include <milkway/mw-types.h> +#include <milkway/mw-config.h> +#include <milkway/mw-compiler.h> + +/** + * @file mw-byteorder.h + * @brief convert values between little endian and big endian. + * + * flib only supports little endian and big endian. + */ + +MW_BEGIN_DECLS + +/** + * @brief little edian value + */ +#define MW_BYTEORDER_LITTLE_ENDIAN 1 +/** + * @brief big edian value + */ +#define MW_BYTEORDER_BIG_ENDIAN 2 + +/** + * @brief the byteoder of current platform + * + * if the endian is little endian, then + * MW_BYTEORDER is defined as MW_BYTEORDER_LITTLE_ENDIAN, + * otherwise it's defined as MW_BYTEORDER_BIG_ENDIAN. + * + * Any endian other then little endian and bit endian is + * unsupported by flib. + */ +#if defined(__PPC__) || defined(__PPC64__) || defined(__sparc__) || defined(WORDS_BIGENDIAN) +#define MW_BYTEORDER MW_BYTEORDER_BIG_ENDIAN +#else +#define MW_BYTEORDER MW_BYTEORDER_LITTLE_ENDIAN +#endif + +/** + * @brief Swaps a 16bit unsigned integer + * + * @param u the integer + * @return the swaped value of integer + */ +static mw_inline mw_uint16_t +mw_swap16 (mw_uint16_t u) +{ + return + (u & 0xff00) >> 8 | + (u & 0x00ff) << 8; +} + +/** + * @brief Swaps a 32bit unsigned integer + * + * @param u the integer + * @return the swaped value of integer + */ +static mw_inline mw_uint32_t +mw_swap32 (mw_uint32_t u) +{ + return + (u & 0xff000000) >> 24 | + (u & 0x00ff0000) >> 16 | + (u & 0x0000ff00) << 16 | + (u & 0x000000ff) << 24; +} + +/** + * @brief Swaps a 64bit unsigned integer + * + * @param u the integer + * @return the swaped value of integer + */ +static mw_inline mw_uint64_t +mw_swap64 (mw_uint64_t u) +{ + return + (u & MW_U64_CONST(0xff00000000000000)) >> 56 | + (u & MW_U64_CONST(0x00ff000000000000)) >> 48 | + (u & MW_U64_CONST(0x0000ff0000000000)) >> 32 | + (u & MW_U64_CONST(0x000000ff00000000)) >> 24 | + (u & MW_U64_CONST(0x00000000ff000000)) << 24 | + (u & MW_U64_CONST(0x0000000000ff0000)) << 32 | + (u & MW_U64_CONST(0x000000000000ff00)) << 48 | + (u & MW_U64_CONST(0x00000000000000ff)) << 56; +} + +/** + * @brief convert a value from big endien to host byte order + * + * @param u the integer in big endian + * @return the value of integer in host byte order + */ +static mw_inline mw_uint16_t +mw_betoh16 (mw_uint16_t u) +{ +#ifdef MW_WORDS_BIGENDIAN + return u; +#else + return mw_swap16 (u); +#endif +} + +/** + * @brief convert a value from big endien to host byte order + * + * @param u the integer in big endian + * @return the value of integer in host byte order + */ +static mw_inline mw_uint32_t +mw_betoh32 (mw_uint32_t u) +{ +#ifdef MW_WORDS_BIGENDIAN + return u; +#else + return mw_swap32 (u); +#endif +} + +/** + * @brief convert a value from big endien to host byte order + * + * @param u the integer in big endian + * @return the value of integer in host byte order + */ +static mw_inline mw_uint64_t +mw_betoh64 (mw_uint64_t u) +{ +#ifdef MW_WORDS_BIGENDIAN + return u; +#else + return mw_swap64 (u); +#endif +} + +/** + * @brief convert a value from little endien to host byte order + * + * @param u the integer in big endian + * @return the value of integer in host byte order + */ +static mw_inline mw_uint16_t +mw_letoh16 (mw_uint16_t u) +{ +#ifdef MW_WORDS_BIGENDIAN + return mw_swap16 (u); +#else + return u; +#endif +} + +/** + * @brief convert a value from little endien to host byte order + * + * @param u the integer in big endian + * @return the value of integer in host byte order + */ +static mw_inline mw_uint32_t +mw_letoh32 (mw_uint32_t u) +{ +#ifdef MW_WORDS_BIGENDIAN + return mw_swap32 (u); +#else + return u; +#endif +} + +/** + * @brief convert values from little endien to host byte order + * + * @param u the integer in little endian + * @return the value of integer in host byte order + */ +static mw_inline mw_uint64_t +mw_letoh64 (mw_uint64_t u) +{ +#ifdef MW_WORDS_BIGENDIAN + return mw_swap64 (u); +#else + return u; +#endif +} + +MW_END_DECLS + +#endif diff --git a/milkway/mw-compiler.h b/milkway/mw-compiler.h index 9ed8424..8cab8e2 100644 --- a/milkway/mw-compiler.h +++ b/milkway/mw-compiler.h @@ -37,4 +37,34 @@ #define mw_export #endif +#if defined(__GNUC__) +#define mw_inline __inline__ +#define MW_I64_CONST(c) (c##LL) +#define MW_U64_CONST(c) (c##ULL) +#elif defined(_MSC_VER) +#define mw_inline __forceinline +#define MW_I64_CONST(c) (c##i64) +#define MW_U64_CONST(c) (c##ui64) +#endif + +#ifndef MW_I64_CONST +#define MW_I64_CONST(c) c +#define MW_U64_CONST(c) c +#endif + +/** + * @brief a marker for a inline function + */ +#ifndef mw_inline +#define mw_inline +#endif + +#if defined(__cplusplus) || defined(cplusplus) +#define MW_BEGIN_DECLS extern "C" { +#define MW_END_DECLS } +#else +#define MW_BEGIN_DECLS +#define MW_END_DECLS +#endif + #endif diff --git a/milkway/mw-config.h b/milkway/mw-config.h new file mode 100644 index 0000000..efae810 --- /dev/null +++ b/milkway/mw-config.h @@ -0,0 +1,58 @@ +/* Milkway + + * Copyright (C) 2009- Luo Jinghua <sunmoon1997@gmail.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more + */ +#ifndef MW_CONFIG_H +#define MW_CONFIG_H + +#if defined(__i386__) /* gcc */ || defined(_M_IX86) /* msvc */ || defined(_X86_) || defined(__386__) || defined(i386) || defined(__i386) /* Sun cc */ +#define MW_ARCH_X86 +#endif + +#if defined(__x86_64__) /* gcc */ || defined(_M_X64) /* msvc */ || defined(_M_AMD64) /* msvc */ || defined(__x86_64) /* Sun cc */ +#define MW_ARCH_X86_64 +#endif + +#if defined(__PPC__) +#define MW_ARCH_PPC +#if defined(__PPC64__) +#define MW_ARCH_PPC_64 +#endif +#endif + +#if defined(__mips__) || defined(__mipse__) +#define MW_ARCH_MIPS +#endif + +#if defined(__linux__) +#define MW_OS_LINUX +#endif + +#if defined(__FreeBSD__) +#define MW_OS_BSD +#endif + +#if defined(__sun) +#define MW_OS_SOLARIS +#endif + +#if defined(__APPLE__) +#define MW_OS_APPLE +#endif + +#if defined(_WIN32) || defined(WIN32) +#define MW_OS_WINDOWS +#endif + + +#endif |