2003-08-10 23:35:13 +02:00
|
|
|
#ifndef BSWAP_H
|
|
|
|
#define BSWAP_H
|
|
|
|
|
2009-07-27 16:13:01 +02:00
|
|
|
#ifdef CONFIG_MACHINE_BSWAP_H
|
2013-01-05 01:39:27 +01:00
|
|
|
# include <sys/endian.h>
|
|
|
|
# include <machine/bswap.h>
|
2014-04-02 16:06:38 +02:00
|
|
|
#elif defined(__FreeBSD__)
|
|
|
|
# include <sys/endian.h>
|
2020-07-13 15:36:10 +02:00
|
|
|
#elif defined(__HAIKU__)
|
|
|
|
# include <endian.h>
|
2013-01-05 01:39:27 +01:00
|
|
|
#elif defined(CONFIG_BYTESWAP_H)
|
|
|
|
# include <byteswap.h>
|
2021-04-16 15:55:42 +02:00
|
|
|
#define BSWAP_FROM_BYTESWAP
|
|
|
|
# else
|
|
|
|
#define BSWAP_FROM_FALLBACKS
|
|
|
|
#endif /* ! CONFIG_MACHINE_BSWAP_H */
|
2003-08-10 23:35:13 +02:00
|
|
|
|
2021-04-16 15:55:42 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef BSWAP_FROM_BYTESWAP
|
2003-08-10 23:35:13 +02:00
|
|
|
static inline uint16_t bswap16(uint16_t x)
|
|
|
|
{
|
|
|
|
return bswap_16(x);
|
|
|
|
}
|
|
|
|
|
2007-09-16 23:08:06 +02:00
|
|
|
static inline uint32_t bswap32(uint32_t x)
|
2003-08-10 23:35:13 +02:00
|
|
|
{
|
|
|
|
return bswap_32(x);
|
|
|
|
}
|
|
|
|
|
2007-09-16 23:08:06 +02:00
|
|
|
static inline uint64_t bswap64(uint64_t x)
|
2003-08-10 23:35:13 +02:00
|
|
|
{
|
|
|
|
return bswap_64(x);
|
|
|
|
}
|
2021-04-16 15:55:42 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef BSWAP_FROM_FALLBACKS
|
2013-01-05 01:39:27 +01:00
|
|
|
static inline uint16_t bswap16(uint16_t x)
|
|
|
|
{
|
|
|
|
return (((x & 0x00ff) << 8) |
|
|
|
|
((x & 0xff00) >> 8));
|
|
|
|
}
|
2003-08-10 23:35:13 +02:00
|
|
|
|
2013-01-05 01:39:27 +01:00
|
|
|
static inline uint32_t bswap32(uint32_t x)
|
|
|
|
{
|
|
|
|
return (((x & 0x000000ffU) << 24) |
|
|
|
|
((x & 0x0000ff00U) << 8) |
|
|
|
|
((x & 0x00ff0000U) >> 8) |
|
|
|
|
((x & 0xff000000U) >> 24));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint64_t bswap64(uint64_t x)
|
|
|
|
{
|
|
|
|
return (((x & 0x00000000000000ffULL) << 56) |
|
|
|
|
((x & 0x000000000000ff00ULL) << 40) |
|
|
|
|
((x & 0x0000000000ff0000ULL) << 24) |
|
|
|
|
((x & 0x00000000ff000000ULL) << 8) |
|
|
|
|
((x & 0x000000ff00000000ULL) >> 8) |
|
|
|
|
((x & 0x0000ff0000000000ULL) >> 24) |
|
|
|
|
((x & 0x00ff000000000000ULL) >> 40) |
|
|
|
|
((x & 0xff00000000000000ULL) >> 56));
|
|
|
|
}
|
2021-04-16 15:55:42 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#undef BSWAP_FROM_BYTESWAP
|
|
|
|
#undef BSWAP_FROM_FALLBACKS
|
2008-12-05 18:54:09 +01:00
|
|
|
|
2003-08-10 23:35:13 +02:00
|
|
|
static inline void bswap16s(uint16_t *s)
|
|
|
|
{
|
|
|
|
*s = bswap16(*s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void bswap32s(uint32_t *s)
|
|
|
|
{
|
|
|
|
*s = bswap32(*s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void bswap64s(uint64_t *s)
|
|
|
|
{
|
|
|
|
*s = bswap64(*s);
|
|
|
|
}
|
|
|
|
|
2022-03-23 16:57:17 +01:00
|
|
|
#if HOST_BIG_ENDIAN
|
2004-08-01 23:48:12 +02:00
|
|
|
#define be_bswap(v, size) (v)
|
2013-01-13 17:35:41 +01:00
|
|
|
#define le_bswap(v, size) glue(bswap, size)(v)
|
2004-08-01 23:48:12 +02:00
|
|
|
#define be_bswaps(v, size)
|
2013-01-13 17:35:41 +01:00
|
|
|
#define le_bswaps(p, size) do { *p = glue(bswap, size)(*p); } while(0)
|
2004-08-01 23:48:12 +02:00
|
|
|
#else
|
|
|
|
#define le_bswap(v, size) (v)
|
2013-01-13 17:35:41 +01:00
|
|
|
#define be_bswap(v, size) glue(bswap, size)(v)
|
2004-08-01 23:48:12 +02:00
|
|
|
#define le_bswaps(v, size)
|
2013-01-13 17:35:41 +01:00
|
|
|
#define be_bswaps(p, size) do { *p = glue(bswap, size)(*p); } while(0)
|
2004-08-01 23:48:12 +02:00
|
|
|
#endif
|
|
|
|
|
2016-07-07 18:21:00 +02:00
|
|
|
/**
|
|
|
|
* Endianness conversion functions between host cpu and specified endianness.
|
|
|
|
* (We list the complete set of prototypes produced by the macros below
|
|
|
|
* to assist people who search the headers to find their definitions.)
|
|
|
|
*
|
|
|
|
* uint16_t le16_to_cpu(uint16_t v);
|
|
|
|
* uint32_t le32_to_cpu(uint32_t v);
|
|
|
|
* uint64_t le64_to_cpu(uint64_t v);
|
|
|
|
* uint16_t be16_to_cpu(uint16_t v);
|
|
|
|
* uint32_t be32_to_cpu(uint32_t v);
|
|
|
|
* uint64_t be64_to_cpu(uint64_t v);
|
|
|
|
*
|
|
|
|
* Convert the value @v from the specified format to the native
|
|
|
|
* endianness of the host CPU by byteswapping if necessary, and
|
|
|
|
* return the converted value.
|
|
|
|
*
|
|
|
|
* uint16_t cpu_to_le16(uint16_t v);
|
|
|
|
* uint32_t cpu_to_le32(uint32_t v);
|
|
|
|
* uint64_t cpu_to_le64(uint64_t v);
|
|
|
|
* uint16_t cpu_to_be16(uint16_t v);
|
|
|
|
* uint32_t cpu_to_be32(uint32_t v);
|
|
|
|
* uint64_t cpu_to_be64(uint64_t v);
|
|
|
|
*
|
|
|
|
* Convert the value @v from the native endianness of the host CPU to
|
|
|
|
* the specified format by byteswapping if necessary, and return
|
|
|
|
* the converted value.
|
|
|
|
*
|
|
|
|
* void le16_to_cpus(uint16_t *v);
|
|
|
|
* void le32_to_cpus(uint32_t *v);
|
|
|
|
* void le64_to_cpus(uint64_t *v);
|
|
|
|
* void be16_to_cpus(uint16_t *v);
|
|
|
|
* void be32_to_cpus(uint32_t *v);
|
|
|
|
* void be64_to_cpus(uint64_t *v);
|
|
|
|
*
|
|
|
|
* Do an in-place conversion of the value pointed to by @v from the
|
|
|
|
* specified format to the native endianness of the host CPU.
|
|
|
|
*
|
|
|
|
* void cpu_to_le16s(uint16_t *v);
|
|
|
|
* void cpu_to_le32s(uint32_t *v);
|
|
|
|
* void cpu_to_le64s(uint64_t *v);
|
|
|
|
* void cpu_to_be16s(uint16_t *v);
|
|
|
|
* void cpu_to_be32s(uint32_t *v);
|
|
|
|
* void cpu_to_be64s(uint64_t *v);
|
|
|
|
*
|
|
|
|
* Do an in-place conversion of the value pointed to by @v from the
|
|
|
|
* native endianness of the host CPU to the specified format.
|
|
|
|
*
|
|
|
|
* Both X_to_cpu() and cpu_to_X() perform the same operation; you
|
|
|
|
* should use whichever one is better documenting of the function your
|
|
|
|
* code is performing.
|
|
|
|
*
|
|
|
|
* Do not use these functions for conversion of values which are in guest
|
|
|
|
* memory, since the data may not be sufficiently aligned for the host CPU's
|
|
|
|
* load and store instructions. Instead you should use the ld*_p() and
|
|
|
|
* st*_p() functions, which perform loads and stores of data of any
|
|
|
|
* required size and endianness and handle possible misalignment.
|
|
|
|
*/
|
|
|
|
|
2004-08-01 23:48:12 +02:00
|
|
|
#define CPU_CONVERT(endian, size, type)\
|
|
|
|
static inline type endian ## size ## _to_cpu(type v)\
|
|
|
|
{\
|
2013-01-13 17:35:41 +01:00
|
|
|
return glue(endian, _bswap)(v, size);\
|
2004-08-01 23:48:12 +02:00
|
|
|
}\
|
|
|
|
\
|
|
|
|
static inline type cpu_to_ ## endian ## size(type v)\
|
|
|
|
{\
|
2013-01-13 17:35:41 +01:00
|
|
|
return glue(endian, _bswap)(v, size);\
|
2004-08-01 23:48:12 +02:00
|
|
|
}\
|
|
|
|
\
|
|
|
|
static inline void endian ## size ## _to_cpus(type *p)\
|
|
|
|
{\
|
2013-01-13 17:35:41 +01:00
|
|
|
glue(endian, _bswaps)(p, size);\
|
2004-08-01 23:48:12 +02:00
|
|
|
}\
|
|
|
|
\
|
|
|
|
static inline void cpu_to_ ## endian ## size ## s(type *p)\
|
|
|
|
{\
|
2013-01-13 17:35:41 +01:00
|
|
|
glue(endian, _bswaps)(p, size);\
|
2004-08-01 23:48:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CPU_CONVERT(be, 16, uint16_t)
|
|
|
|
CPU_CONVERT(be, 32, uint32_t)
|
|
|
|
CPU_CONVERT(be, 64, uint64_t)
|
|
|
|
|
|
|
|
CPU_CONVERT(le, 16, uint16_t)
|
|
|
|
CPU_CONVERT(le, 32, uint32_t)
|
|
|
|
CPU_CONVERT(le, 64, uint64_t)
|
|
|
|
|
2016-04-12 08:07:19 +02:00
|
|
|
/*
|
2016-07-07 18:20:59 +02:00
|
|
|
* Same as cpu_to_le{16,32}, except that gcc will figure the result is
|
2016-04-12 08:07:19 +02:00
|
|
|
* a compile-time constant if you pass in a constant. So this can be
|
|
|
|
* used to initialize static variables.
|
|
|
|
*/
|
2022-03-23 16:57:17 +01:00
|
|
|
#if HOST_BIG_ENDIAN
|
2016-04-12 08:07:19 +02:00
|
|
|
# define const_le32(_x) \
|
|
|
|
((((_x) & 0x000000ffU) << 24) | \
|
|
|
|
(((_x) & 0x0000ff00U) << 8) | \
|
|
|
|
(((_x) & 0x00ff0000U) >> 8) | \
|
|
|
|
(((_x) & 0xff000000U) >> 24))
|
|
|
|
# define const_le16(_x) \
|
|
|
|
((((_x) & 0x00ff) << 8) | \
|
|
|
|
(((_x) & 0xff00) >> 8))
|
|
|
|
#else
|
|
|
|
# define const_le32(_x) (_x)
|
|
|
|
# define const_le16(_x) (_x)
|
|
|
|
#endif
|
|
|
|
|
2011-07-28 12:10:30 +02:00
|
|
|
/* unaligned/endian-independent pointer access */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* the generic syntax is:
|
|
|
|
*
|
2019-02-26 15:21:07 +01:00
|
|
|
* load: ld{type}{sign}{size}_{endian}_p(ptr)
|
2011-07-28 12:10:30 +02:00
|
|
|
*
|
2019-02-26 15:21:07 +01:00
|
|
|
* store: st{type}{size}_{endian}_p(ptr, val)
|
2011-07-28 12:10:30 +02:00
|
|
|
*
|
|
|
|
* Note there are small differences with the softmmu access API!
|
|
|
|
*
|
|
|
|
* type is:
|
|
|
|
* (empty): integer access
|
|
|
|
* f : float access
|
|
|
|
*
|
|
|
|
* sign is:
|
2015-01-20 16:19:35 +01:00
|
|
|
* (empty): for 32 or 64 bit sizes (including floats and doubles)
|
2011-07-28 12:10:30 +02:00
|
|
|
* u : unsigned
|
|
|
|
* s : signed
|
|
|
|
*
|
|
|
|
* size is:
|
|
|
|
* b: 8 bits
|
|
|
|
* w: 16 bits
|
|
|
|
* l: 32 bits
|
|
|
|
* q: 64 bits
|
|
|
|
*
|
|
|
|
* endian is:
|
2014-05-02 19:32:38 +02:00
|
|
|
* he : host endian
|
2011-07-28 12:10:30 +02:00
|
|
|
* be : big endian
|
|
|
|
* le : little endian
|
2015-01-20 16:19:35 +01:00
|
|
|
* te : target endian
|
2014-05-02 19:32:38 +02:00
|
|
|
* (except for byte accesses, which have no endian infix).
|
2015-01-20 16:19:35 +01:00
|
|
|
*
|
|
|
|
* The target endian accessors are obviously only available to source
|
|
|
|
* files which are built per-target; they are defined in cpu-all.h.
|
|
|
|
*
|
|
|
|
* In all cases these functions take a host pointer.
|
|
|
|
* For accessors that take a guest address rather than a
|
|
|
|
* host address, see the cpu_{ld,st}_* accessors defined in
|
|
|
|
* cpu_ldst.h.
|
2018-06-15 15:57:14 +02:00
|
|
|
*
|
|
|
|
* For cases where the size to be used is not fixed at compile time,
|
|
|
|
* there are
|
2019-02-26 15:21:07 +01:00
|
|
|
* stn_{endian}_p(ptr, sz, val)
|
2018-06-15 15:57:14 +02:00
|
|
|
* which stores @val to @ptr as an @endian-order number @sz bytes in size
|
|
|
|
* and
|
2019-02-26 15:21:07 +01:00
|
|
|
* ldn_{endian}_p(ptr, sz)
|
2018-06-15 15:57:14 +02:00
|
|
|
* which loads @sz bytes from @ptr as an unsigned @endian-order number
|
|
|
|
* and returns it in a uint64_t.
|
2011-07-28 12:10:30 +02:00
|
|
|
*/
|
2013-01-05 01:39:30 +01:00
|
|
|
|
2011-07-28 12:10:30 +02:00
|
|
|
static inline int ldub_p(const void *ptr)
|
|
|
|
{
|
|
|
|
return *(uint8_t *)ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int ldsb_p(const void *ptr)
|
|
|
|
{
|
|
|
|
return *(int8_t *)ptr;
|
|
|
|
}
|
|
|
|
|
2014-02-19 06:11:44 +01:00
|
|
|
static inline void stb_p(void *ptr, uint8_t v)
|
2011-07-28 12:10:30 +02:00
|
|
|
{
|
|
|
|
*(uint8_t *)ptr = v;
|
|
|
|
}
|
|
|
|
|
2019-03-18 12:29:38 +01:00
|
|
|
/*
|
|
|
|
* Any compiler worth its salt will turn these memcpy into native unaligned
|
|
|
|
* operations. Thus we don't need to play games with packed attributes, or
|
|
|
|
* inline byte-by-byte stores.
|
|
|
|
* Some compilation environments (eg some fortify-source implementations)
|
|
|
|
* may intercept memcpy() in a way that defeats the compiler optimization,
|
|
|
|
* though, so we use __builtin_memcpy() to give ourselves the best chance
|
|
|
|
* of good performance.
|
|
|
|
*/
|
2013-01-05 01:39:28 +01:00
|
|
|
|
2014-05-02 19:32:38 +02:00
|
|
|
static inline int lduw_he_p(const void *ptr)
|
2013-01-05 01:39:28 +01:00
|
|
|
{
|
|
|
|
uint16_t r;
|
2019-03-18 12:29:38 +01:00
|
|
|
__builtin_memcpy(&r, ptr, sizeof(r));
|
2013-01-05 01:39:28 +01:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-05-02 19:32:38 +02:00
|
|
|
static inline int ldsw_he_p(const void *ptr)
|
2013-01-05 01:39:28 +01:00
|
|
|
{
|
|
|
|
int16_t r;
|
2019-03-18 12:29:38 +01:00
|
|
|
__builtin_memcpy(&r, ptr, sizeof(r));
|
2013-01-05 01:39:28 +01:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-05-02 19:32:38 +02:00
|
|
|
static inline void stw_he_p(void *ptr, uint16_t v)
|
2013-01-05 01:39:28 +01:00
|
|
|
{
|
2019-03-18 12:29:38 +01:00
|
|
|
__builtin_memcpy(ptr, &v, sizeof(v));
|
2013-01-05 01:39:28 +01:00
|
|
|
}
|
|
|
|
|
2014-05-02 19:32:38 +02:00
|
|
|
static inline int ldl_he_p(const void *ptr)
|
2013-01-05 01:39:28 +01:00
|
|
|
{
|
|
|
|
int32_t r;
|
2019-03-18 12:29:38 +01:00
|
|
|
__builtin_memcpy(&r, ptr, sizeof(r));
|
2013-01-05 01:39:28 +01:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-05-02 19:32:38 +02:00
|
|
|
static inline void stl_he_p(void *ptr, uint32_t v)
|
2013-01-05 01:39:28 +01:00
|
|
|
{
|
2019-03-18 12:29:38 +01:00
|
|
|
__builtin_memcpy(ptr, &v, sizeof(v));
|
2013-01-05 01:39:28 +01:00
|
|
|
}
|
|
|
|
|
2014-05-02 19:32:38 +02:00
|
|
|
static inline uint64_t ldq_he_p(const void *ptr)
|
2013-01-05 01:39:28 +01:00
|
|
|
{
|
|
|
|
uint64_t r;
|
2019-03-18 12:29:38 +01:00
|
|
|
__builtin_memcpy(&r, ptr, sizeof(r));
|
2013-01-05 01:39:28 +01:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-05-02 19:32:38 +02:00
|
|
|
static inline void stq_he_p(void *ptr, uint64_t v)
|
2013-01-05 01:39:28 +01:00
|
|
|
{
|
2019-03-18 12:29:38 +01:00
|
|
|
__builtin_memcpy(ptr, &v, sizeof(v));
|
2013-01-05 01:39:28 +01:00
|
|
|
}
|
|
|
|
|
2011-07-28 12:10:30 +02:00
|
|
|
static inline int lduw_le_p(const void *ptr)
|
|
|
|
{
|
2014-05-02 19:32:38 +02:00
|
|
|
return (uint16_t)le_bswap(lduw_he_p(ptr), 16);
|
2011-07-28 12:10:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int ldsw_le_p(const void *ptr)
|
|
|
|
{
|
2014-05-02 19:32:38 +02:00
|
|
|
return (int16_t)le_bswap(lduw_he_p(ptr), 16);
|
2011-07-28 12:10:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int ldl_le_p(const void *ptr)
|
|
|
|
{
|
2014-05-02 19:32:38 +02:00
|
|
|
return le_bswap(ldl_he_p(ptr), 32);
|
2011-07-28 12:10:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint64_t ldq_le_p(const void *ptr)
|
|
|
|
{
|
2014-05-02 19:32:38 +02:00
|
|
|
return le_bswap(ldq_he_p(ptr), 64);
|
2011-07-28 12:10:30 +02:00
|
|
|
}
|
|
|
|
|
2014-02-19 06:11:45 +01:00
|
|
|
static inline void stw_le_p(void *ptr, uint16_t v)
|
2011-07-28 12:10:30 +02:00
|
|
|
{
|
2014-05-02 19:32:38 +02:00
|
|
|
stw_he_p(ptr, le_bswap(v, 16));
|
2011-07-28 12:10:30 +02:00
|
|
|
}
|
|
|
|
|
2014-02-19 06:11:45 +01:00
|
|
|
static inline void stl_le_p(void *ptr, uint32_t v)
|
2011-07-28 12:10:30 +02:00
|
|
|
{
|
2014-05-02 19:32:38 +02:00
|
|
|
stl_he_p(ptr, le_bswap(v, 32));
|
2011-07-28 12:10:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void stq_le_p(void *ptr, uint64_t v)
|
|
|
|
{
|
2014-05-02 19:32:38 +02:00
|
|
|
stq_he_p(ptr, le_bswap(v, 64));
|
2011-07-28 12:10:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int lduw_be_p(const void *ptr)
|
|
|
|
{
|
2014-05-02 19:32:38 +02:00
|
|
|
return (uint16_t)be_bswap(lduw_he_p(ptr), 16);
|
2011-07-28 12:10:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int ldsw_be_p(const void *ptr)
|
|
|
|
{
|
2014-05-02 19:32:38 +02:00
|
|
|
return (int16_t)be_bswap(lduw_he_p(ptr), 16);
|
2011-07-28 12:10:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int ldl_be_p(const void *ptr)
|
|
|
|
{
|
2014-05-02 19:32:38 +02:00
|
|
|
return be_bswap(ldl_he_p(ptr), 32);
|
2011-07-28 12:10:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint64_t ldq_be_p(const void *ptr)
|
|
|
|
{
|
2014-05-02 19:32:38 +02:00
|
|
|
return be_bswap(ldq_he_p(ptr), 64);
|
2011-07-28 12:10:30 +02:00
|
|
|
}
|
|
|
|
|
2014-02-19 06:11:45 +01:00
|
|
|
static inline void stw_be_p(void *ptr, uint16_t v)
|
2011-07-28 12:10:30 +02:00
|
|
|
{
|
2014-05-02 19:32:38 +02:00
|
|
|
stw_he_p(ptr, be_bswap(v, 16));
|
2011-07-28 12:10:30 +02:00
|
|
|
}
|
|
|
|
|
2014-02-19 06:11:45 +01:00
|
|
|
static inline void stl_be_p(void *ptr, uint32_t v)
|
2011-07-28 12:10:30 +02:00
|
|
|
{
|
2014-05-02 19:32:38 +02:00
|
|
|
stl_he_p(ptr, be_bswap(v, 32));
|
2011-07-28 12:10:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void stq_be_p(void *ptr, uint64_t v)
|
|
|
|
{
|
2014-05-02 19:32:38 +02:00
|
|
|
stq_he_p(ptr, be_bswap(v, 64));
|
2011-07-28 12:10:30 +02:00
|
|
|
}
|
|
|
|
|
2013-01-05 01:39:30 +01:00
|
|
|
static inline unsigned long leul_to_cpu(unsigned long v)
|
|
|
|
{
|
2016-03-11 13:41:13 +01:00
|
|
|
#if HOST_LONG_BITS == 32
|
2013-02-05 01:21:06 +01:00
|
|
|
return le_bswap(v, 32);
|
2016-03-11 13:41:13 +01:00
|
|
|
#elif HOST_LONG_BITS == 64
|
2013-02-05 01:21:06 +01:00
|
|
|
return le_bswap(v, 64);
|
|
|
|
#else
|
|
|
|
# error Unknown sizeof long
|
|
|
|
#endif
|
2013-01-05 01:39:30 +01:00
|
|
|
}
|
|
|
|
|
2018-06-15 15:57:14 +02:00
|
|
|
/* Store v to p as a sz byte value in host order */
|
|
|
|
#define DO_STN_LDN_P(END) \
|
|
|
|
static inline void stn_## END ## _p(void *ptr, int sz, uint64_t v) \
|
|
|
|
{ \
|
|
|
|
switch (sz) { \
|
|
|
|
case 1: \
|
|
|
|
stb_p(ptr, v); \
|
|
|
|
break; \
|
|
|
|
case 2: \
|
|
|
|
stw_ ## END ## _p(ptr, v); \
|
|
|
|
break; \
|
|
|
|
case 4: \
|
|
|
|
stl_ ## END ## _p(ptr, v); \
|
|
|
|
break; \
|
|
|
|
case 8: \
|
|
|
|
stq_ ## END ## _p(ptr, v); \
|
|
|
|
break; \
|
|
|
|
default: \
|
|
|
|
g_assert_not_reached(); \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
static inline uint64_t ldn_## END ## _p(const void *ptr, int sz) \
|
|
|
|
{ \
|
|
|
|
switch (sz) { \
|
|
|
|
case 1: \
|
|
|
|
return ldub_p(ptr); \
|
|
|
|
case 2: \
|
|
|
|
return lduw_ ## END ## _p(ptr); \
|
|
|
|
case 4: \
|
|
|
|
return (uint32_t)ldl_ ## END ## _p(ptr); \
|
|
|
|
case 8: \
|
|
|
|
return ldq_ ## END ## _p(ptr); \
|
|
|
|
default: \
|
|
|
|
g_assert_not_reached(); \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
DO_STN_LDN_P(he)
|
|
|
|
DO_STN_LDN_P(le)
|
|
|
|
DO_STN_LDN_P(be)
|
|
|
|
|
|
|
|
#undef DO_STN_LDN_P
|
|
|
|
|
2013-01-05 01:39:29 +01:00
|
|
|
#undef le_bswap
|
|
|
|
#undef be_bswap
|
|
|
|
#undef le_bswaps
|
|
|
|
#undef be_bswaps
|
2011-07-28 12:10:30 +02:00
|
|
|
|
2021-04-16 15:55:42 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-08-10 23:35:13 +02:00
|
|
|
#endif /* BSWAP_H */
|