src/blfwk/EndianUtilities.h
/*
* Copyright (c) 2013-14, Freescale Semiconductor, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice, this list
* of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* o Neither the name of Freescale Semiconductor, Inc. nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#if !defined(_EndianUtilities_h_)
#define _EndianUtilities_h_
#define _BYTESWAP16(value) (((((uint16_t)value) << 8) & 0xFF00) | ((((uint16_t)value) >> 8) & 0x00FF))
#define _BYTESWAP32(value) \
(((((uint32_t)value) << 24) & 0xFF000000) | ((((uint32_t)value) << 8) & 0x00FF0000) | \
((((uint32_t)value) >> 8) & 0x0000FF00) | ((((uint32_t)value) >> 24) & 0x000000FF))
#define _BYTESWAP64(value) \
(((((uint64_t)value) << 56) & 0xFF00000000000000ULL) | ((((uint64_t)value) << 40) & 0x00FF000000000000ULL) | \
((((uint64_t)value) << 24) & 0x0000FF0000000000ULL) | ((((uint64_t)value) << 8) & 0x000000FF00000000ULL) | \
((((uint64_t)value) >> 8) & 0x00000000FF000000ULL) | ((((uint64_t)value) >> 24) & 0x0000000000FF0000ULL) | \
((((uint64_t)value) >> 40) & 0x000000000000FF00ULL) | ((((uint64_t)value) >> 56) & 0x00000000000000FFULL))
inline uint16_t _swap_u16(uint16_t value)
{
return _BYTESWAP16(value);
}
inline int16_t _swap_s16(int16_t value)
{
return (int16_t)_BYTESWAP16((uint16_t)value);
}
inline uint32_t _swap_u32(uint32_t value)
{
return _BYTESWAP32(value);
}
inline int32_t _swap_s32(int32_t value)
{
return (int32_t)_BYTESWAP32((uint32_t)value);
}
inline uint64_t _swap_u64(uint64_t value)
{
return _BYTESWAP64(value);
}
inline int64_t _swap_s64(int64_t value)
{
return (uint64_t)_BYTESWAP64((uint64_t)value);
}
#if defined(__LITTLE_ENDIAN__)
/* little endian host */
#define ENDIAN_BIG_TO_HOST_U16(value) (_swap_u16(value))
#define ENDIAN_HOST_TO_BIG_U16(value) (_swap_u16(value))
#define ENDIAN_BIG_TO_HOST_S16(value) (_swap_s16(value))
#define ENDIAN_HOST_TO_BIG_S16(value) (_swap_s16(value))
#define ENDIAN_BIG_TO_HOST_U32(value) (_swap_u32(value))
#define ENDIAN_HOST_TO_BIG_U32(value) (_swap_u32(value))
#define ENDIAN_BIG_TO_HOST_S32(value) (_swap_s32(value))
#define ENDIAN_HOST_TO_BIG_S32(value) (_swap_s32(value))
#define ENDIAN_BIG_TO_HOST_U64(value) (_swap_u64(value))
#define ENDIAN_HOST_TO_BIG_U64(value) (_swap_u64(value))
#define ENDIAN_BIG_TO_HOST_S64(value) (_swap_s64(value))
#define ENDIAN_HOST_TO_BIG_S64(value) (_swap_s64(value))
/* no-ops */
#define ENDIAN_LITTLE_TO_HOST_U16(value) (value)
#define ENDIAN_HOST_TO_LITTLE_U16(value) (value)
#define ENDIAN_LITTLE_TO_HOST_S16(value) (value)
#define ENDIAN_HOST_TO_LITTLE_S16(value) (value)
#define ENDIAN_LITTLE_TO_HOST_U32(value) (value)
#define ENDIAN_HOST_TO_LITTLE_U32(value) (value)
#define ENDIAN_LITTLE_TO_HOST_S32(value) (value)
#define ENDIAN_HOST_TO_LITTLE_S32(value) (value)
#define ENDIAN_LITTLE_TO_HOST_U64(value) (value)
#define ENDIAN_HOST_TO_LITTLE_U64(value) (value)
#define ENDIAN_LITTLE_TO_HOST_S64(value) (value)
#define ENDIAN_HOST_TO_LITTLE_S64(value) (value)
#elif defined(__BIG_ENDIAN__)
/* big endian host */
#define ENDIAN_LITTLE_TO_HOST_U16(value) (_swap_u16(value))
#define ENDIAN_HOST_TO_LITTLE_U16(value) (_swap_u16(value))
#define ENDIAN_LITTLE_TO_HOST_S16(value) (_swap_s16(value))
#define ENDIAN_HOST_TO_LITTLE_S16(value) (_swap_s16(value))
#define ENDIAN_LITTLE_TO_HOST_U32(value) (_swap_u32(value))
#define ENDIAN_HOST_TO_LITTLE_U32(value) (_swap_u32(value))
#define ENDIAN_LITTLE_TO_HOST_S32(value) (_swap_s32(value))
#define ENDIAN_HOST_TO_LITTLE_S32(value) (_swap_s32(value))
#define ENDIAN_LITTLE_TO_HOST_U64(value) (_swap_u64(value))
#define ENDIAN_HOST_TO_LITTLE_U64(value) (_swap_u64(value))
#define ENDIAN_LITTLE_TO_HOST_S64(value) (_swap_s64(value))
#define ENDIAN_HOST_TO_LITTLE_S64(value) (_swap_s64(value))
/* no-ops */
#define ENDIAN_BIG_TO_HOST_U16(value) (value)
#define ENDIAN_HOST_TO_BIG_U16(value) (value)
#define ENDIAN_BIG_TO_HOST_S16(value) (value)
#define ENDIAN_HOST_TO_BIG_S16(value) (value)
#define ENDIAN_BIG_TO_HOST_U32(value) (value)
#define ENDIAN_HOST_TO_BIG_U32(value) (value)
#define ENDIAN_BIG_TO_HOST_S32(value) (value)
#define ENDIAN_HOST_TO_BIG_S32(value) (value)
#define ENDIAN_BIG_TO_HOST_U64(value) (value)
#define ENDIAN_HOST_TO_BIG_U64(value) (value)
#define ENDIAN_BIG_TO_HOST_S64(value) (value)
#define ENDIAN_HOST_TO_BIG_S64(value) (value)
#endif
#endif // _EndianUtilities_h_