Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:14

0001 /**
0002  * @file
0003  *
0004  * @ingroup RTEMSAPISystemLibrary
0005  *
0006  * @brief This header file provides interfaces of the system endianness
0007  *   support.
0008  */
0009 
0010 /*-
0011  * Copyright (c) 2002 Thomas Moestl <tmm@FreeBSD.org>
0012  * All rights reserved.
0013  *
0014  * Redistribution and use in source and binary forms, with or without
0015  * modification, are permitted provided that the following conditions
0016  * are met:
0017  * 1. Redistributions of source code must retain the above copyright
0018  *    notice, this list of conditions and the following disclaimer.
0019  * 2. Redistributions in binary form must reproduce the above copyright
0020  *    notice, this list of conditions and the following disclaimer in the
0021  *    documentation and/or other materials provided with the distribution.
0022  *
0023  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
0024  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0025  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0026  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
0027  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
0028  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
0029  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
0030  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
0031  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
0032  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
0033  * SUCH DAMAGE.
0034  *
0035  * $FreeBSD: head/sys/sys/endian.h 208331 2010-05-20 06:16:13Z phk $
0036  */
0037 
0038 #ifndef _SYS_ENDIAN_H_
0039 #define _SYS_ENDIAN_H_
0040 
0041 #include <sys/cdefs.h>
0042 #include <sys/_types.h>
0043 #include <machine/endian.h>
0044 
0045 #ifndef _UINT8_T_DECLARED
0046 typedef __uint8_t   uint8_t;
0047 #define _UINT8_T_DECLARED
0048 #endif
0049  
0050 #ifndef _UINT16_T_DECLARED
0051 typedef __uint16_t  uint16_t;
0052 #define _UINT16_T_DECLARED
0053 #endif
0054  
0055 #ifndef _UINT32_T_DECLARED
0056 typedef __uint32_t  uint32_t;
0057 #define _UINT32_T_DECLARED
0058 #endif
0059  
0060 #ifndef _UINT64_T_DECLARED
0061 typedef __uint64_t  uint64_t;
0062 #define _UINT64_T_DECLARED
0063 #endif
0064  
0065 /*
0066  * General byte order swapping functions.
0067  */
0068 #define bswap16(x)  __bswap16(x)
0069 #define bswap32(x)  __bswap32(x)
0070 #define bswap64(x)  __bswap64(x)
0071 
0072 /*
0073  * Host to big endian, host to little endian, big endian to host, and little
0074  * endian to host byte order functions as detailed in byteorder(9).
0075  */
0076 #if _BYTE_ORDER == _LITTLE_ENDIAN
0077 #define htobe16(x)  bswap16((x))
0078 #define htobe32(x)  bswap32((x))
0079 #define htobe64(x)  bswap64((x))
0080 #define htole16(x)  ((uint16_t)(x))
0081 #define htole32(x)  ((uint32_t)(x))
0082 #define htole64(x)  ((uint64_t)(x))
0083 
0084 #define be16toh(x)  bswap16((x))
0085 #define be32toh(x)  bswap32((x))
0086 #define be64toh(x)  bswap64((x))
0087 #define le16toh(x)  ((uint16_t)(x))
0088 #define le32toh(x)  ((uint32_t)(x))
0089 #define le64toh(x)  ((uint64_t)(x))
0090 #else /* _BYTE_ORDER != _LITTLE_ENDIAN */
0091 #define htobe16(x)  ((uint16_t)(x))
0092 #define htobe32(x)  ((uint32_t)(x))
0093 #define htobe64(x)  ((uint64_t)(x))
0094 #define htole16(x)  bswap16((x))
0095 #define htole32(x)  bswap32((x))
0096 #define htole64(x)  bswap64((x))
0097 
0098 #define be16toh(x)  ((uint16_t)(x))
0099 #define be32toh(x)  ((uint32_t)(x))
0100 #define be64toh(x)  ((uint64_t)(x))
0101 #define le16toh(x)  bswap16((x))
0102 #define le32toh(x)  bswap32((x))
0103 #define le64toh(x)  bswap64((x))
0104 #endif /* _BYTE_ORDER == _LITTLE_ENDIAN */
0105 
0106 /* Alignment-agnostic encode/decode bytestream to/from little/big endian. */
0107 
0108 static __inline uint16_t
0109 be16dec(const void *pp)
0110 {
0111     uint8_t const *p = (uint8_t const *)pp;
0112 
0113     return (((unsigned)p[0] << 8) | p[1]);
0114 }
0115 
0116 static __inline uint32_t
0117 be32dec(const void *pp)
0118 {
0119     uint8_t const *p = (uint8_t const *)pp;
0120 
0121     return (((uint32_t)p[0] << 24) | ((uint32_t)p[1] << 16) |
0122     ((uint32_t)p[2] << 8) | p[3]);
0123 }
0124 
0125 static __inline uint64_t
0126 be64dec(const void *pp)
0127 {
0128     uint8_t const *p = (uint8_t const *)pp;
0129 
0130     return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4));
0131 }
0132 
0133 static __inline uint16_t
0134 le16dec(const void *pp)
0135 {
0136     uint8_t const *p = (uint8_t const *)pp;
0137 
0138     return (((unsigned)p[1] << 8) | p[0]);
0139 }
0140 
0141 static __inline uint32_t
0142 le32dec(const void *pp)
0143 {
0144     uint8_t const *p = (uint8_t const *)pp;
0145 
0146     return (((uint32_t)p[3] << 24) | ((uint32_t)p[2] << 16) |
0147     ((uint32_t)p[1] << 8) | p[0]);
0148 }
0149 
0150 static __inline uint64_t
0151 le64dec(const void *pp)
0152 {
0153     uint8_t const *p = (uint8_t const *)pp;
0154 
0155     return (((uint64_t)le32dec(p + 4) << 32) | le32dec(p));
0156 }
0157 
0158 static __inline void
0159 be16enc(void *pp, uint16_t u)
0160 {
0161     uint8_t *p = (uint8_t *)pp;
0162 
0163     p[0] = (u >> 8) & 0xff;
0164     p[1] = u & 0xff;
0165 }
0166 
0167 static __inline void
0168 be32enc(void *pp, uint32_t u)
0169 {
0170     uint8_t *p = (uint8_t *)pp;
0171 
0172     p[0] = (u >> 24) & 0xff;
0173     p[1] = (u >> 16) & 0xff;
0174     p[2] = (u >> 8) & 0xff;
0175     p[3] = u & 0xff;
0176 }
0177 
0178 static __inline void
0179 be64enc(void *pp, uint64_t u)
0180 {
0181     uint8_t *p = (uint8_t *)pp;
0182 
0183     be32enc(p, (uint32_t)(u >> 32));
0184     be32enc(p + 4, (uint32_t)(u & 0xffffffffU));
0185 }
0186 
0187 static __inline void
0188 le16enc(void *pp, uint16_t u)
0189 {
0190     uint8_t *p = (uint8_t *)pp;
0191 
0192     p[0] = u & 0xff;
0193     p[1] = (u >> 8) & 0xff;
0194 }
0195 
0196 static __inline void
0197 le32enc(void *pp, uint32_t u)
0198 {
0199     uint8_t *p = (uint8_t *)pp;
0200 
0201     p[0] = u & 0xff;
0202     p[1] = (u >> 8) & 0xff;
0203     p[2] = (u >> 16) & 0xff;
0204     p[3] = (u >> 24) & 0xff;
0205 }
0206 
0207 static __inline void
0208 le64enc(void *pp, uint64_t u)
0209 {
0210     uint8_t *p = (uint8_t *)pp;
0211 
0212     le32enc(p, (uint32_t)(u & 0xffffffffU));
0213     le32enc(p + 4, (uint32_t)(u >> 32));
0214 }
0215 
0216 #endif  /* _SYS_ENDIAN_H_ */