From e70887cf3d57f9d12415c60075e8b2f289de99f0 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Thu, 14 Feb 2013 14:24:14 +0100 Subject: [PATCH] Add endian.h. --- libc/include/__/endian.h | 86 ++++++++++++++++++++++++++++++++++++++++ libc/include/endian.h | 62 +++++++++++++++++++++++++++++ 2 files changed, 148 insertions(+) create mode 100644 libc/include/__/endian.h create mode 100644 libc/include/endian.h diff --git a/libc/include/__/endian.h b/libc/include/__/endian.h new file mode 100644 index 00000000..f63335b4 --- /dev/null +++ b/libc/include/__/endian.h @@ -0,0 +1,86 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2013. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C 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 Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + __/endian.h + Convert byte ordering of integers. + +*******************************************************************************/ + +#ifndef INCLUDE____ENDIAN_H +#define INCLUDE____ENDIAN_H + +#include +#include <__/byteswap.h> + +__BEGIN_DECLS + +/* The compiler provides the type constants. */ +#define __LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__ +#define __PDP_ENDIAN __ORDER_PDP_ENDIAN__ +#define __BIG_ENDIAN __ORDER_BIG_ENDIAN__ + +/* The compiler also provides the local endianness as one of the above. */ +#define __BYTE_ORDER __BYTE_ORDER__ +#define __FLOAT_WORD_ORDER __FLOAT_WORD_ORDER__ + +/* Declare conversion macros to allow easy endian conversion. */ + +#if __BYTE_ORDER == __LITTLE_ENDIAN + + #define __htobe16(x) __bswap_16(x) + #define __htole16(x) (x) + #define __be16toh(x) __bswap_16(x) + #define __le16toh(x) (x) + + #define __htobe32(x) __bswap_32(x) + #define __htole32(x) (x) + #define __be32toh(x) __bswap_32(x) + #define __le32toh(x) (x) + + #define __htobe64(x) __bswap_64(x) + #define __htole64(x) (x) + #define __be64toh(x) __bswap_64(x) + #define __le64toh(x) (x) + +#elif __BYTE_ORDER == __BIG_ENDIAN + + #define __htobe16(x) (x) + #define __htole16(x) __bswap_16(x) + #define __be16toh(x) (x) + #define __le16toh(x) __bswap_16(x) + + #define __htobe32(x) (x) + #define __htole32(x) __bswap_32(x) + #define __be32toh(x) (x) + #define __le32toh(x) __bswap_32(x) + + #define __htobe64(x) (x) + #define __htole64(x) __bswap_64(x) + #define __be64toh(x) (x) + #define __le64toh(x) __bswap_64(x) + +#else + + #error Your processor uses a weird endian, please add support. + +#endif + +__END_DECLS + +#endif diff --git a/libc/include/endian.h b/libc/include/endian.h new file mode 100644 index 00000000..b6c74ecd --- /dev/null +++ b/libc/include/endian.h @@ -0,0 +1,62 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2013. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C 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 Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + endian.h + Convert byte ordering of integers. + +*******************************************************************************/ + +#ifndef _ENDIAN_H +#define _ENDIAN_H 1 + +#include +#include <__/endian.h> + +__BEGIN_DECLS + +/* Constans for each kind of known endian. */ +#define LITTLE_ENDIAN __LITTLE_ENDIAN +#define PDP_ENDIAN __PDP_ENDIAN +#define BIG_ENDIAN __BIG_ENDIAN + +/* Easy access to the current endian. */ +#define BYTE_ORDER __BYTE_ORDER +#define FLOAT_WORD_ORDER __FLOAT_WORD_ORDER + +/* Easy conversion of 16-bit integers. */ +#define htobe16(x) __htobe16(x) +#define htole16(x) __htole16(x) +#define be16toh(x) __be16toh(x) +#define le16toh(x) __le16toh(x) + +/* Easy conversion of 32-bit integers. */ +#define htobe32(x) __htobe32(x) +#define htole32(x) __htole32(x) +#define be32toh(x) __be32toh(x) +#define le32toh(x) __le32toh(x) + +/* Easy conversion of 64-bit integers. */ +#define htobe64(x) __htobe64(x) +#define htole64(x) __htole64(x) +#define be64toh(x) __be64toh(x) +#define le64toh(x) __le64toh(x) + +__END_DECLS + +#endif