2018-08-19 15:44:04 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* include/endian.h
|
|
|
|
*
|
2021-02-03 11:33:49 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2018-08-19 15:44:04 +02:00
|
|
|
*
|
2021-02-03 11:33:49 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-08-19 15:44:04 +02:00
|
|
|
*
|
2021-02-03 11:33:49 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2018-08-19 15:44:04 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __INCLUDE_ENDIAN_H
|
|
|
|
#define __INCLUDE_ENDIAN_H
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
#include <nuttx/compiler.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Preprocessor definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* The endian.h header must define at least the following macros for use in
|
|
|
|
* determining host byte order for integer types:
|
|
|
|
*
|
|
|
|
* BYTE_ORDER This macro will have a value equal to one of the *_ENDIAN
|
|
|
|
* macros in this header.
|
|
|
|
* LITTLE_ENDIAN If BYTE_ORDER == LITTLE_ENDIAN, the host byte order is
|
|
|
|
* from least significant to most significant.
|
|
|
|
* BIG_ENDIAN If BYTE_ORDER == BIG_ENDIAN, the host byte order is from
|
|
|
|
* most significant to least significant.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define LITTLE_ENDIAN 1234
|
2021-08-05 13:44:31 +02:00
|
|
|
#define __LITTLE_ENDIAN 1234
|
2018-08-19 15:44:04 +02:00
|
|
|
#define BIG_ENDIAN 4321
|
2021-08-05 13:44:31 +02:00
|
|
|
#define __BIG_ENDIAN 4321
|
2018-08-19 15:44:04 +02:00
|
|
|
|
|
|
|
/* Common byte swapping macros */
|
|
|
|
|
2022-06-19 22:24:55 +02:00
|
|
|
#ifdef CONFIG_HAVE_BUILTIN_BSWAP16
|
|
|
|
# define __swap_uint16 __builtin_bswap16
|
|
|
|
#else
|
2018-08-19 15:44:04 +02:00
|
|
|
# define __swap_uint16(n) \
|
|
|
|
(uint16_t)(((((uint16_t)(n)) & 0x00ff) << 8) | \
|
|
|
|
((((uint16_t)(n)) >> 8) & 0x00ff))
|
|
|
|
#endif
|
|
|
|
|
2022-06-19 22:24:55 +02:00
|
|
|
#ifdef CONFIG_HAVE_BUILTIN_BSWAP32
|
|
|
|
# define __swap_uint32 __builtin_bswap32
|
|
|
|
#else
|
2018-08-19 15:44:04 +02:00
|
|
|
# define __swap_uint32(n) \
|
|
|
|
(uint32_t)(((((uint32_t)(n)) & 0x000000ffUL) << 24) | \
|
|
|
|
((((uint32_t)(n)) & 0x0000ff00UL) << 8) | \
|
|
|
|
((((uint32_t)(n)) & 0x00ff0000UL) >> 8) | \
|
|
|
|
((((uint32_t)(n)) & 0xff000000UL) >> 24))
|
|
|
|
#endif
|
|
|
|
|
2022-06-19 22:24:55 +02:00
|
|
|
#ifdef CONFIG_HAVE_LONG_LONG
|
|
|
|
# ifdef CONFIG_HAVE_BUILTIN_BSWAP64
|
|
|
|
# define __swap_uint64 __builtin_bswap64
|
|
|
|
# else
|
|
|
|
# define __swap_uint64(n) \
|
|
|
|
(uint64_t)(((((uint64_t)(n)) & 0x00000000000000ffULL) << 56) | \
|
|
|
|
((((uint64_t)(n)) & 0x000000000000ff00ULL) << 40) | \
|
|
|
|
((((uint64_t)(n)) & 0x0000000000ff0000ULL) << 24) | \
|
|
|
|
((((uint64_t)(n)) & 0x00000000ff000000ULL) << 8) | \
|
|
|
|
((((uint64_t)(n)) & 0x000000ff00000000ULL) >> 8) | \
|
|
|
|
((((uint64_t)(n)) & 0x0000ff0000000000ULL) >> 24) | \
|
|
|
|
((((uint64_t)(n)) & 0x00ff000000000000ULL) >> 40) | \
|
|
|
|
((((uint64_t)(n)) & 0xff00000000000000ULL) >> 56))
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2018-08-19 15:44:04 +02:00
|
|
|
/* Endian-specific definitions */
|
|
|
|
|
|
|
|
#ifdef CONFIG_ENDIAN_BIG
|
|
|
|
/* Big-endian byte order */
|
|
|
|
|
|
|
|
# define BYTE_ORDER BIG_ENDIAN
|
2021-08-05 13:44:31 +02:00
|
|
|
# define __BYTE_ORDER BIG_ENDIAN
|
2018-08-19 15:44:04 +02:00
|
|
|
|
|
|
|
/* Big-endian byte order macros */
|
|
|
|
|
|
|
|
# define htobe16(n) (n)
|
|
|
|
# define htole16(n) __swap_uint16((uint16_t)n)
|
|
|
|
# define be16toh(n) (n)
|
|
|
|
# define le16toh(n) __swap_uint16((uint16_t)n)
|
|
|
|
|
|
|
|
# define htobe32(n) (n)
|
|
|
|
# define htole32(n) __swap_uint32((uint32_t)n)
|
|
|
|
# define be32toh(n) (n)
|
|
|
|
# define le32toh(n) __swap_uint32(n)
|
|
|
|
|
|
|
|
# ifdef CONFIG_HAVE_LONG_LONG
|
|
|
|
# define htobe64(n) (n)
|
|
|
|
# define htole64(n) __swap_uint64((uint64_t)n)
|
|
|
|
# define be64toh(n) (n)
|
2018-11-09 16:05:48 +01:00
|
|
|
# define le64toh(n) __swap_uint64((uint64_t)n)
|
2018-08-19 15:44:04 +02:00
|
|
|
# endif
|
|
|
|
|
|
|
|
#else
|
|
|
|
/* Little-endian byte order */
|
|
|
|
|
|
|
|
# define BYTE_ORDER LITTLE_ENDIAN
|
2021-08-05 13:44:31 +02:00
|
|
|
# define __BYTE_ORDER __LITTLE_ENDIAN
|
2018-08-19 15:44:04 +02:00
|
|
|
|
|
|
|
/* Little-endian byte order macros */
|
|
|
|
|
|
|
|
# define htobe16(n) __swap_uint16((uint16_t)n)
|
|
|
|
# define htole16(n) (n)
|
|
|
|
# define be16toh(n) __swap_uint16((uint16_t)n)
|
|
|
|
# define le16toh(n) (n)
|
|
|
|
|
|
|
|
# define htobe32(n) __swap_uint32((uint32_t)n)
|
|
|
|
# define htole32(n) (n)
|
|
|
|
# define be32toh(n) __swap_uint32((uint32_t)n)
|
|
|
|
# define le32toh(n) (n)
|
|
|
|
|
|
|
|
# ifdef CONFIG_HAVE_LONG_LONG
|
2018-11-09 16:05:48 +01:00
|
|
|
# define htobe64(n) __swap_uint64((uint64_t)n)
|
2018-08-19 15:44:04 +02:00
|
|
|
# define htole64(n) (n)
|
2018-11-09 16:05:48 +01:00
|
|
|
# define be64toh(n) __swap_uint64((uint64_t)n)
|
2018-08-19 15:44:04 +02:00
|
|
|
# define le64toh(n) (n)
|
|
|
|
# endif
|
|
|
|
#endif
|
2022-08-08 10:05:20 +02:00
|
|
|
|
|
|
|
/* OpenBSD style */
|
|
|
|
|
|
|
|
#define swap16 __swap_uint16
|
|
|
|
#define swap32 __swap_uint32
|
|
|
|
#define swap64 __swap_uint64
|
|
|
|
|
|
|
|
#define betoh16 be16toh
|
|
|
|
#define letoh16 le16toh
|
|
|
|
#define betoh32 be32toh
|
|
|
|
#define letoh32 le32toh
|
|
|
|
|
|
|
|
#ifdef CONFIG_HAVE_LONG_LONG
|
|
|
|
#define betoh64 be64toh
|
|
|
|
#define letoh64 le64toh
|
|
|
|
#endif
|
|
|
|
|
2018-08-19 15:44:04 +02:00
|
|
|
#endif /* __INCLUDE_ENDIAN_H */
|