RetroArch/libretro-common/include/retro_endianness.h

164 lines
4.4 KiB
C
Raw Normal View History

2015-01-07 17:31:19 +00:00
/* Copyright (C) 2010-2015 The RetroArch team
2014-10-21 06:29:05 +00:00
*
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (retro_endianness.h).
2014-10-21 06:29:05 +00:00
* ---------------------------------------------------------------------------------------
*
* Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef __LIBRETRO_SDK_ENDIANNESS_H
#define __LIBRETRO_SDK_ENDIANNESS_H
2014-10-21 23:00:59 +00:00
#include <retro_inline.h>
2014-10-21 06:29:05 +00:00
#include <stdint.h>
#include <stdlib.h>
2014-10-21 06:29:05 +00:00
#if defined(_MSC_VER)
2015-06-09 22:56:59 +00:00
#define SWAP16 _byteswap_ushort
#define SWAP32 _byteswap_ulong
#else
#define SWAP16(x) ((uint16_t)( \
(((uint16_t)(x) & 0x00ff) << 8) | \
2014-12-30 20:49:45 +00:00
(((uint16_t)(x) & 0xff00) >> 8) \
))
2014-10-21 06:29:05 +00:00
#define SWAP32(x) ((uint32_t)( \
(((uint32_t)(x) & 0x000000ff) << 24) | \
(((uint32_t)(x) & 0x0000ff00) << 8) | \
(((uint32_t)(x) & 0x00ff0000) >> 8) | \
(((uint32_t)(x) & 0xff000000) >> 24) \
))
2015-06-09 22:56:59 +00:00
#endif
2014-10-21 06:29:05 +00:00
/**
* is_little_endian:
*
* Checks if the system is little endian or big-endian.
*
* Returns: greater than 0 if little-endian,
* otherwise big-endian.
**/
2014-10-21 23:00:59 +00:00
static INLINE uint8_t is_little_endian(void)
2014-10-21 06:29:05 +00:00
{
2015-06-09 22:56:59 +00:00
#if defined(__x86_64) || defined(__i386) || defined(_M_IX86) || defined(_M_X64)
return 1;
#elif defined(MSB_FIRST)
return 0;
#else
2014-10-21 06:29:05 +00:00
union
{
uint16_t x;
uint8_t y[2];
} u;
u.x = 1;
return u.y[0];
2015-06-09 22:56:59 +00:00
#endif
2015-04-02 19:00:30 +00:00
}
/**
* swap_if_big32:
* @val : unsigned 32-bit value
*
* Byteswap unsigned 32-bit value if system is big-endian.
*
* Returns: Byteswapped value in case system is big-endian,
* otherwise returns same value.
**/
2014-10-21 23:00:59 +00:00
static INLINE uint32_t swap_if_big32(uint32_t val)
2014-10-21 06:29:05 +00:00
{
if (is_little_endian())
return val;
2015-06-09 22:56:59 +00:00
return SWAP32(val);
}
/**
* swap_if_little32:
* @val : unsigned 32-bit value
*
* Byteswap unsigned 32-bit value if system is little-endian.
*
* Returns: Byteswapped value in case system is little-endian,
* otherwise returns same value.
**/
2014-10-21 23:00:59 +00:00
static INLINE uint32_t swap_if_little32(uint32_t val)
2014-10-21 06:29:05 +00:00
{
if (is_little_endian())
2015-06-09 22:56:59 +00:00
return SWAP32(val);
2014-10-21 06:29:05 +00:00
return val;
}
/**
* swap_if_big16:
* @val : unsigned 16-bit value
*
* Byteswap unsigned 16-bit value if system is big-endian.
*
* Returns: Byteswapped value in case system is big-endian,
* otherwise returns same value.
**/
2014-10-21 23:00:59 +00:00
static INLINE uint16_t swap_if_big16(uint16_t val)
2014-10-21 06:29:05 +00:00
{
if (is_little_endian())
return val;
2015-06-09 22:56:59 +00:00
return SWAP16(val);
}
2014-10-21 06:29:05 +00:00
/**
* swap_if_little16:
* @val : unsigned 16-bit value
*
* Byteswap unsigned 16-bit value if system is little-endian.
*
* Returns: Byteswapped value in case system is little-endian,
* otherwise returns same value.
**/
2014-10-21 23:00:59 +00:00
static INLINE uint16_t swap_if_little16(uint16_t val)
2014-10-21 06:29:05 +00:00
{
if (is_little_endian())
2015-06-09 22:56:59 +00:00
return SWAP16(val);
2014-10-21 06:29:05 +00:00
return val;
}
/**
* store32be:
* @addr : pointer to unsigned 32-bit buffer
* @data : unsigned 32-bit value to write
*
* Write data to address. Endian-safe. Byteswaps the data
* first if necessary before storing it.
**/
2014-10-21 23:00:59 +00:00
static INLINE void store32be(uint32_t *addr, uint32_t data)
2014-10-21 06:29:05 +00:00
{
2015-06-09 22:56:59 +00:00
*addr = swap_if_little32(data);
2014-10-21 06:29:05 +00:00
}
/**
* load32be:
* @addr : pointer to unsigned 32-bit buffer
*
* Load value from address. Endian-safe.
*
* Returns: value from address, byte-swapped if necessary.
**/
2014-10-21 23:00:59 +00:00
static INLINE uint32_t load32be(const uint32_t *addr)
2014-10-21 06:29:05 +00:00
{
2015-06-09 22:56:59 +00:00
return swap_if_little32(*addr);
2014-10-21 06:29:05 +00:00
}
#endif