2016-07-21 13:18:14 +08:00
|
|
|
/** @file
|
|
|
|
* @brief Byte order helpers.
|
|
|
|
*/
|
2015-04-13 16:31:32 +08:00
|
|
|
|
|
|
|
/*
|
2016-07-21 13:18:14 +08:00
|
|
|
* Copyright (c) 2015-2016, Intel Corporation.
|
2015-04-13 16:31:32 +08:00
|
|
|
*
|
2017-01-19 09:01:01 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2015-04-13 16:31:32 +08:00
|
|
|
*/
|
|
|
|
|
2016-08-05 21:22:56 +08:00
|
|
|
#ifndef __BYTEORDER_H__
|
|
|
|
#define __BYTEORDER_H__
|
|
|
|
|
Introduce new sized integer typedefs
This is a start to move away from the C99 {u}int{8,16,32,64}_t types to
Zephyr defined u{8,16,32,64}_t and s{8,16,32,64}_t. This allows Zephyr
to define the sized types in a consistent manor across all the
architectures we support and not conflict with what various compilers
and libc might do with regards to the C99 types.
We introduce <zephyr/types.h> as part of this and have it include
<stdint.h> for now until we transition all the code away from the C99
types.
We go with u{8,16,32,64}_t and s{8,16,32,64}_t as there are some
existing variables defined u8 & u16 as well as to be consistent with
Zephyr naming conventions.
Jira: ZEP-2051
Change-Id: I451fed0623b029d65866622e478225dfab2c0ca8
Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2017-04-19 23:32:08 +08:00
|
|
|
#include <zephyr/types.h>
|
2016-10-10 21:40:44 +08:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <misc/__assert.h>
|
2016-07-21 13:18:14 +08:00
|
|
|
|
2016-07-22 01:59:07 +08:00
|
|
|
/* Internal helpers only used by the sys_* APIs further below */
|
|
|
|
#define __bswap_16(x) ((uint16_t) ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8)))
|
|
|
|
#define __bswap_32(x) ((uint32_t) ((((x) >> 24) & 0xff) | \
|
|
|
|
(((x) >> 8) & 0xff00) | \
|
|
|
|
(((x) & 0xff00) << 8) | \
|
|
|
|
(((x) & 0xff) << 24)))
|
2015-10-06 18:26:34 +08:00
|
|
|
|
2016-07-21 13:18:14 +08:00
|
|
|
/** @def sys_le16_to_cpu
|
|
|
|
* @brief Convert 16-bit integer from little-endian to host endianness.
|
|
|
|
*
|
|
|
|
* @param val 16-bit integer in little-endian format.
|
|
|
|
*
|
|
|
|
* @return 16-bit integer in host endianness.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @def sys_cpu_to_le16
|
|
|
|
* @brief Convert 16-bit integer from host endianness to little-endian.
|
|
|
|
*
|
|
|
|
* @param val 16-bit integer in host endianness.
|
|
|
|
*
|
|
|
|
* @return 16-bit integer in little-endian format.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @def sys_be16_to_cpu
|
|
|
|
* @brief Convert 16-bit integer from big-endian to host endianness.
|
|
|
|
*
|
|
|
|
* @param val 16-bit integer in big-endian format.
|
|
|
|
*
|
|
|
|
* @return 16-bit integer in host endianness.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @def sys_cpu_to_be16
|
|
|
|
* @brief Convert 16-bit integer from host endianness to big-endian.
|
|
|
|
*
|
|
|
|
* @param val 16-bit integer in host endianness.
|
|
|
|
*
|
|
|
|
* @return 16-bit integer in big-endian format.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @def sys_le32_to_cpu
|
|
|
|
* @brief Convert 32-bit integer from little-endian to host endianness.
|
|
|
|
*
|
|
|
|
* @param val 32-bit integer in little-endian format.
|
|
|
|
*
|
|
|
|
* @return 32-bit integer in host endianness.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @def sys_cpu_to_le32
|
|
|
|
* @brief Convert 32-bit integer from host endianness to little-endian.
|
|
|
|
*
|
|
|
|
* @param val 32-bit integer in host endianness.
|
|
|
|
*
|
|
|
|
* @return 32-bit integer in little-endian format.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @def sys_be32_to_cpu
|
|
|
|
* @brief Convert 32-bit integer from big-endian to host endianness.
|
|
|
|
*
|
|
|
|
* @param val 32-bit integer in big-endian format.
|
|
|
|
*
|
|
|
|
* @return 32-bit integer in host endianness.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @def sys_cpu_to_be32
|
|
|
|
* @brief Convert 32-bit integer from host endianness to big-endian.
|
|
|
|
*
|
|
|
|
* @param val 32-bit integer in host endianness.
|
|
|
|
*
|
|
|
|
* @return 32-bit integer in big-endian format.
|
|
|
|
*/
|
|
|
|
|
2015-04-13 16:31:32 +08:00
|
|
|
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
|
|
|
#define sys_le16_to_cpu(val) (val)
|
|
|
|
#define sys_cpu_to_le16(val) (val)
|
2016-07-22 01:59:07 +08:00
|
|
|
#define sys_be16_to_cpu(val) __bswap_16(val)
|
|
|
|
#define sys_cpu_to_be16(val) __bswap_16(val)
|
2015-04-29 16:54:43 +08:00
|
|
|
#define sys_le32_to_cpu(val) (val)
|
|
|
|
#define sys_cpu_to_le32(val) (val)
|
2016-07-22 01:59:07 +08:00
|
|
|
#define sys_be32_to_cpu(val) __bswap_32(val)
|
|
|
|
#define sys_cpu_to_be32(val) __bswap_32(val)
|
2015-04-13 16:31:32 +08:00
|
|
|
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
|
2016-07-22 01:59:07 +08:00
|
|
|
#define sys_le16_to_cpu(val) __bswap_16(val)
|
|
|
|
#define sys_cpu_to_le16(val) __bswap_16(val)
|
2015-10-06 18:26:34 +08:00
|
|
|
#define sys_be16_to_cpu(val) (val)
|
|
|
|
#define sys_cpu_to_be16(val) (val)
|
2016-07-22 01:59:07 +08:00
|
|
|
#define sys_le32_to_cpu(val) __bswap_32(val)
|
|
|
|
#define sys_cpu_to_le32(val) __bswap_32(val)
|
2015-10-06 18:26:34 +08:00
|
|
|
#define sys_be32_to_cpu(val) (val)
|
|
|
|
#define sys_cpu_to_be32(val) (val)
|
2015-04-13 16:31:32 +08:00
|
|
|
#else
|
|
|
|
#error "Unknown byte order"
|
|
|
|
#endif
|
2016-07-21 13:18:56 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Put a 16-bit intger as big-endian to arbitrary location.
|
|
|
|
*
|
|
|
|
* Put a 16-bit integer, originally in host endianness, to a
|
|
|
|
* potentially unaligned memory location in big-endian format.
|
|
|
|
*
|
|
|
|
* @param val 16-bit integer in host endianness.
|
|
|
|
* @param dst Destination memory address to store the result.
|
|
|
|
*/
|
|
|
|
static inline void sys_put_be16(uint16_t val, uint8_t dst[2])
|
|
|
|
{
|
|
|
|
dst[0] = val >> 8;
|
|
|
|
dst[1] = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Put a 32-bit intger as big-endian to arbitrary location.
|
|
|
|
*
|
|
|
|
* Put a 32-bit integer, originally in host endianness, to a
|
|
|
|
* potentially unaligned memory location in big-endian format.
|
|
|
|
*
|
|
|
|
* @param val 32-bit integer in host endianness.
|
|
|
|
* @param dst Destination memory address to store the result.
|
|
|
|
*/
|
|
|
|
static inline void sys_put_be32(uint32_t val, uint8_t dst[4])
|
|
|
|
{
|
|
|
|
sys_put_be16(val >> 16, dst);
|
|
|
|
sys_put_be16(val, &dst[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Put a 16-bit intger as little-endian to arbitrary location.
|
|
|
|
*
|
|
|
|
* Put a 16-bit integer, originally in host endianness, to a
|
|
|
|
* potentially unaligned memory location in little-endian format.
|
|
|
|
*
|
|
|
|
* @param val 16-bit integer in host endianness.
|
|
|
|
* @param dst Destination memory address to store the result.
|
|
|
|
*/
|
|
|
|
static inline void sys_put_le16(uint16_t val, uint8_t dst[2])
|
|
|
|
{
|
|
|
|
dst[0] = val;
|
|
|
|
dst[1] = val >> 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Put a 32-bit intger as little-endian to arbitrary location.
|
|
|
|
*
|
|
|
|
* Put a 32-bit integer, originally in host endianness, to a
|
|
|
|
* potentially unaligned memory location in little-endian format.
|
|
|
|
*
|
|
|
|
* @param val 32-bit integer in host endianness.
|
|
|
|
* @param dst Destination memory address to store the result.
|
|
|
|
*/
|
|
|
|
static inline void sys_put_le32(uint32_t val, uint8_t dst[4])
|
|
|
|
{
|
|
|
|
sys_put_le16(val, dst);
|
|
|
|
sys_put_le16(val >> 16, &dst[2]);
|
|
|
|
}
|
|
|
|
|
2016-09-08 21:42:09 +08:00
|
|
|
/**
|
|
|
|
* @brief Put a 64-bit integer as little-endian to arbitrary location.
|
|
|
|
*
|
|
|
|
* Put a 64-bit integer, originally in host endianness, to a
|
|
|
|
* potentially unaligned memory location in little-endian format.
|
|
|
|
*
|
|
|
|
* @param val 64-bit integer in host endianness.
|
|
|
|
* @param dst Destination memory address to store the result.
|
|
|
|
*/
|
|
|
|
static inline void sys_put_le64(uint64_t val, uint8_t dst[8])
|
|
|
|
{
|
|
|
|
sys_put_le32(val, dst);
|
|
|
|
sys_put_le32(val >> 32, &dst[4]);
|
|
|
|
}
|
|
|
|
|
2016-07-21 13:18:56 +08:00
|
|
|
/**
|
|
|
|
* @brief Get a 16-bit intger stored in big-endian format.
|
|
|
|
*
|
|
|
|
* Get a 16-bit integer, stored in big-endian format in a potentially
|
|
|
|
* unaligned memory location, and convert it to the host endianness.
|
|
|
|
*
|
|
|
|
* @param src Location of the big-endian 16-bit integer to get.
|
|
|
|
*
|
|
|
|
* @return 16-bit integer in host endianness.
|
|
|
|
*/
|
|
|
|
static inline uint16_t sys_get_be16(const uint8_t src[2])
|
|
|
|
{
|
|
|
|
return ((uint16_t)src[0] << 8) | src[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get a 32-bit intger stored in big-endian format.
|
|
|
|
*
|
|
|
|
* Get a 32-bit integer, stored in big-endian format in a potentially
|
|
|
|
* unaligned memory location, and convert it to the host endianness.
|
|
|
|
*
|
|
|
|
* @param src Location of the big-endian 32-bit integer to get.
|
|
|
|
*
|
|
|
|
* @return 32-bit integer in host endianness.
|
|
|
|
*/
|
|
|
|
static inline uint32_t sys_get_be32(const uint8_t src[4])
|
|
|
|
{
|
|
|
|
return ((uint32_t)sys_get_be16(&src[0]) << 16) | sys_get_be16(&src[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get a 16-bit intger stored in little-endian format.
|
|
|
|
*
|
|
|
|
* Get a 16-bit integer, stored in little-endian format in a potentially
|
|
|
|
* unaligned memory location, and convert it to the host endianness.
|
|
|
|
*
|
|
|
|
* @param src Location of the little-endian 16-bit integer to get.
|
|
|
|
*
|
|
|
|
* @return 16-bit integer in host endianness.
|
|
|
|
*/
|
|
|
|
static inline uint16_t sys_get_le16(const uint8_t src[2])
|
|
|
|
{
|
|
|
|
return ((uint16_t)src[1] << 8) | src[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get a 32-bit intger stored in little-endian format.
|
|
|
|
*
|
|
|
|
* Get a 32-bit integer, stored in little-endian format in a potentially
|
|
|
|
* unaligned memory location, and convert it to the host endianness.
|
|
|
|
*
|
|
|
|
* @param src Location of the little-endian 32-bit integer to get.
|
|
|
|
*
|
|
|
|
* @return 32-bit integer in host endianness.
|
|
|
|
*/
|
|
|
|
static inline uint32_t sys_get_le32(const uint8_t src[4])
|
|
|
|
{
|
|
|
|
return ((uint32_t)sys_get_le16(&src[2]) << 16) | sys_get_le16(&src[0]);
|
|
|
|
}
|
2016-08-05 21:22:56 +08:00
|
|
|
|
2016-08-30 14:47:47 +08:00
|
|
|
/**
|
|
|
|
* @brief Get a 64-bit integer stored in little-endian format.
|
|
|
|
*
|
|
|
|
* Get a 64-bit integer, stored in little-endian format in a potentially
|
|
|
|
* unaligned memory location, and convert it to the host endianness.
|
|
|
|
*
|
|
|
|
* @param src Location of the little-endian 64-bit integer to get.
|
|
|
|
*
|
|
|
|
* @return 64-bit integer in host endianness.
|
|
|
|
*/
|
|
|
|
static inline uint64_t sys_get_le64(const uint8_t src[8])
|
|
|
|
{
|
|
|
|
return ((uint64_t)sys_get_le32(&src[4]) << 32) | sys_get_le32(&src[0]);
|
|
|
|
}
|
|
|
|
|
2016-10-10 21:40:44 +08:00
|
|
|
/**
|
|
|
|
* @brief Swap one buffer content into another
|
|
|
|
*
|
|
|
|
* Copy the content of src buffer into dst buffer in reversed order,
|
|
|
|
* i.e.: src[n] will be put in dst[end-n]
|
|
|
|
* Where n is an index and 'end' the last index in both arrays.
|
|
|
|
* The 2 memory pointers must be pointing to different areas, and have
|
|
|
|
* a minimum size of given length.
|
|
|
|
*
|
|
|
|
* @param dst A valid pointer on a memory area where to copy the data in
|
|
|
|
* @param src A valid pointer on a memory area where to copy the data from
|
|
|
|
* @param length Size of both dst and src memory areas
|
|
|
|
*/
|
|
|
|
static inline void sys_memcpy_swap(void *dst, const void *src, size_t length)
|
|
|
|
{
|
2016-10-15 16:05:59 +08:00
|
|
|
__ASSERT(((src < dst && (src + length) <= dst) ||
|
|
|
|
(src > dst && (dst + length) <= src)),
|
|
|
|
"Source and destination buffers must not overlap");
|
2016-10-10 21:40:44 +08:00
|
|
|
|
|
|
|
src += length - 1;
|
|
|
|
|
|
|
|
for (; length > 0; length--) {
|
|
|
|
*((uint8_t *)dst++) = *((uint8_t *)src--);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Swap buffer content
|
|
|
|
*
|
|
|
|
* In-place memory swap, where final content will be reversed.
|
|
|
|
* I.e.: buf[n] will be put in buf[end-n]
|
|
|
|
* Where n is an index and 'end' the last index of buf.
|
|
|
|
*
|
|
|
|
* @param buf A valid pointer on a memory area to swap
|
|
|
|
* @param length Size of buf memory area
|
|
|
|
*/
|
|
|
|
static inline void sys_mem_swap(void *buf, size_t length)
|
|
|
|
{
|
2016-12-21 02:27:41 +08:00
|
|
|
size_t i;
|
2016-10-10 21:40:44 +08:00
|
|
|
|
|
|
|
for (i = 0; i < (length/2); i++) {
|
|
|
|
uint8_t tmp = ((uint8_t *)buf)[i];
|
|
|
|
|
|
|
|
((uint8_t *)buf)[i] = ((uint8_t *)buf)[length - 1 - i];
|
|
|
|
((uint8_t *)buf)[length - 1 - i] = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-05 21:22:56 +08:00
|
|
|
#endif /* __BYTEORDER_H__ */
|