rsys

Basic data structures and low-level features
git clone git://git.meso-star.fr/rsys.git
Log | Files | Refs | README | LICENSE

commit b38dbe74d038c9fabec0827af23e562cc6558b28
parent 0abee385cd516bb0cbcd784affddb0d445911683
Author: vaplv <vaplv@free.fr>
Date:   Thu, 10 Sep 2020 10:51:58 +0200

Add endianness macros and functions

Diffstat:
Mcmake/CMakeLists.txt | 1+
Asrc/endianness.h | 126+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 127 insertions(+), 0 deletions(-)

diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -90,6 +90,7 @@ set(RSYS_FILES_INC_API dynamic_array_uint.h dynamic_array_size_t.h dynamic_array_str.h + endianness.h float2.h float3.h float4.h diff --git a/src/endianness.h b/src/endianness.h @@ -0,0 +1,126 @@ +/* Copyright (C) 2013-2020 Vincent Forest (vaplv@free.fr) + * + * The RSys 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 RSys 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 RSys library. If not, see <http://www.gnu.org/licenses/>. */ + +#ifndef ENDIANNESS_H +#define ENDIANNESS_H + +#include "rsys.h" + +#if defined(COMPILER_GCC) + #define BYTE_ORDER __BYTE_ORDER__ + #define LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__ + #define BIG_ENDIAN __ORDER_BIG_ENDIAN__ + #include <byteswap.h> +#elif + #include <Windows.h> + #define BYTE_ORDER REG_DWORD + #define LITTLE_ENDIAN REG_DWORD_LITTLE_ENDIAN + #define BIG_ENDIAN REG_DWORD_BIG_ENDIAN +#else + #error "Undefined byte ordering macros" +#endif + +#ifdef COMPILER_GCC +static FINLINE uint16_t byte_swap_16(const uint16_t ui) { return bswap_16(ui); } +static FINLINE uint32_t byte_swap_32(const uint32_t ui) { return bswap_32(ui); } +static FINLINE uint64_t byte_swap_64(const uint64_t ui) { return bswap_64(ui); } + +#elif defined COMPILER_CL +static FINLINE uint16_t +byte_swap_16(const uint16_t ui) +{ + STATIC_ASSERT(sizeof(unsigned short) == sizeof(uint16_t), + Unexpected_sizeof_ushort); + return _byteswap_ushort(ui); +} + +static FINLINE uint32_t +byte_swap_32(const uint32_t ui) +{ + STATIC_ASSERT(sizeof(unsigned long) == sizeof(uint32_t), + Unexpected_sizeof_ushort); + return _byteswap_ulong(ui); +} + +static FINLINE uint64_t +byte_swap_64(const uint64_t ui) +{ + STATIC_ASSERT(sizeof(unsigned long) == sizeof(uint64_t), + Unexpected_sizeof_ulong); + return _byteswap_uint64(ui); +} +#endif + +static FINLINE uint16_t +little_endian_16(const uint16_t ui) +{ +#if BYTE_ORDER == LITTLE_ENDIAN + return ui; +#elif BYTE_ORDER == BIG_ENDIAN + return byte_swap_16(ui); +#endif +} + +static FINLINE uint32_t +little_endian_32(const uint32_t ui) +{ +#if BYTE_ORDER == LITTLE_ENDIAN + return ui; +#elif BYTE_ORDER == BIG_ENDIAN + return byte_swap_32(ui); +#endif +} + +static FINLINE uint64_t +little_endian_64(const uint64_t ui) +{ +#if BYTE_ORDER == LITTLE_ENDIAN + return ui; +#elif BYTE_ORDER == BIG_ENDIAN + return byte_swap_64(ui); +#endif +} + +static FINLINE uint16_t +big_endian_16(const uint16_t ui) +{ +#if BYTE_ORDER == LITTLE_ENDIAN + return byte_swap_16(ui); +#elif BYTE_ORDER == BIG_ENDIAN + return ui; +#endif +} + +static FINLINE uint32_t +big_endian_32(const uint32_t ui) +{ +#if BYTE_ORDER == LITTLE_ENDIAN + return byte_swap_32(ui); +#elif BYTE_ORDER == BIG_ENDIAN + return ui; +#endif +} + +static FINLINE uint64_t +big_endian_64(const uint64_t ui) +{ +#if BYTE_ORDER == LITTLE_ENDIAN + return byte_swap_64(ui); +#elif BYTE_ORDER == BIG_ENDIAN + return ui; +#endif +} + +#endif /* ENDIANNESS_H */