sl@0: /*-
sl@0:  * Copyright (c) 2002 Thomas Moestl <tmm@FreeBSD.org>
sl@0:  * All rights reserved.
sl@0:  *
sl@0:  * Redistribution and use in source and binary forms, with or without
sl@0:  * modification, are permitted provided that the following conditions
sl@0:  * are met:
sl@0:  * 1. Redistributions of source code must retain the above copyright
sl@0:  *    notice, this list of conditions and the following disclaimer.
sl@0:  * 2. Redistributions in binary form must reproduce the above copyright
sl@0:  *    notice, this list of conditions and the following disclaimer in the
sl@0:  *    documentation and/or other materials provided with the distribution.
sl@0:  *
sl@0:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
sl@0:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
sl@0:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
sl@0:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
sl@0:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
sl@0:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
sl@0:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
sl@0:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
sl@0:  * SUCH DAMAGE.
sl@0:  *
sl@0:  * $FreeBSD: src/sys/sys/endian.h,v 1.6 2003/10/15 20:05:57 obrien Exp $
sl@0:  */
sl@0: 
sl@0: #ifndef _SYS_ENDIAN_H_
sl@0: #define _SYS_ENDIAN_H_
sl@0: 
sl@0: #include <sys/cdefs.h>
sl@0: #include <sys/_types.h>
sl@0: #include <stdapis/machine/endian.h>
sl@0: 
sl@0: #ifndef _UINT16_T_DECLARED
sl@0: typedef	__uint16_t	uint16_t;
sl@0: #define	_UINT16_T_DECLARED
sl@0: #endif
sl@0:  
sl@0: #ifndef _UINT32_T_DECLARED
sl@0: typedef	__uint32_t	uint32_t;
sl@0: #define	_UINT32_T_DECLARED
sl@0: #endif
sl@0:  
sl@0: #ifndef _UINT64_T_DECLARED
sl@0: typedef	__uint64_t	uint64_t;
sl@0: #define	_UINT64_T_DECLARED
sl@0: #endif
sl@0:  
sl@0: /*
sl@0:  * General byte order swapping functions.
sl@0:  */
sl@0: #define	bswap16(x)	__bswap16(x)
sl@0: #define	bswap32(x)	__bswap32(x)
sl@0: #define	bswap64(x)	__bswap64(x)
sl@0: 
sl@0: /*
sl@0:  * Host to big endian, host to little endian, big endian to host, and little
sl@0:  * endian to host byte order functions as detailed in byteorder(9).
sl@0:  */
sl@0: #if _BYTE_ORDER == _LITTLE_ENDIAN
sl@0: #define	htobe16(x)	bswap16((x))
sl@0: #define	htobe32(x)	bswap32((x))
sl@0: #define	htobe64(x)	bswap64((x))
sl@0: #define	htole16(x)	((uint16_t)(x))
sl@0: #define	htole32(x)	((uint32_t)(x))
sl@0: #define	htole64(x)	((uint64_t)(x))
sl@0: 
sl@0: #define	be16toh(x)	bswap16((x))
sl@0: #define	be32toh(x)	bswap32((x))
sl@0: #define	be64toh(x)	bswap64((x))
sl@0: #define	le16toh(x)	((uint16_t)(x))
sl@0: #define	le32toh(x)	((uint32_t)(x))
sl@0: #define	le64toh(x)	((uint64_t)(x))
sl@0: #else /* _BYTE_ORDER != _LITTLE_ENDIAN */
sl@0: #define	htobe16(x)	((uint16_t)(x))
sl@0: #define	htobe32(x)	((uint32_t)(x))
sl@0: #define	htobe64(x)	((uint64_t)(x))
sl@0: #define	htole16(x)	bswap16((x))
sl@0: #define	htole32(x)	bswap32((x))
sl@0: #define	htole64(x)	bswap64((x))
sl@0: 
sl@0: #define	be16toh(x)	((uint16_t)(x))
sl@0: #define	be32toh(x)	((uint32_t)(x))
sl@0: #define	be64toh(x)	((uint64_t)(x))
sl@0: #define	le16toh(x)	bswap16((x))
sl@0: #define	le32toh(x)	bswap32((x))
sl@0: #define	le64toh(x)	bswap64((x))
sl@0: #endif /* _BYTE_ORDER == _LITTLE_ENDIAN */
sl@0: 
sl@0: /* Alignment-agnostic encode/decode bytestream to/from little/big endian. */
sl@0: 
sl@0: static __inline uint16_t
sl@0: be16dec(const void *pp)
sl@0: {
sl@0: 	unsigned char const *p = (unsigned char const *)pp;
sl@0: 
sl@0: 	return ((p[0] << 8) | p[1]);
sl@0: }
sl@0: 
sl@0: static __inline uint32_t
sl@0: be32dec(const void *pp)
sl@0: {
sl@0: 	unsigned char const *p = (unsigned char const *)pp;
sl@0: 
sl@0: 	return ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
sl@0: }
sl@0: 
sl@0: static __inline uint64_t
sl@0: be64dec(const void *pp)
sl@0: {
sl@0: 	unsigned char const *p = (unsigned char const *)pp;
sl@0: 
sl@0: 	return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4));
sl@0: }
sl@0: 
sl@0: static __inline uint16_t
sl@0: le16dec(const void *pp)
sl@0: {
sl@0: 	unsigned char const *p = (unsigned char const *)pp;
sl@0: 
sl@0: 	return ((p[1] << 8) | p[0]);
sl@0: }
sl@0: 
sl@0: static __inline uint32_t
sl@0: le32dec(const void *pp)
sl@0: {
sl@0: 	unsigned char const *p = (unsigned char const *)pp;
sl@0: 
sl@0: 	return ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
sl@0: }
sl@0: 
sl@0: static __inline uint64_t
sl@0: le64dec(const void *pp)
sl@0: {
sl@0: 	unsigned char const *p = (unsigned char const *)pp;
sl@0: 
sl@0: 	return (((uint64_t)le32dec(p + 4) << 32) | le32dec(p));
sl@0: }
sl@0: 
sl@0: static __inline void
sl@0: be16enc(void *pp, uint16_t u)
sl@0: {
sl@0: 	unsigned char *p = (unsigned char *)pp;
sl@0: 
sl@0: 	p[0] = (u >> 8) & 0xff;
sl@0: 	p[1] = u & 0xff;
sl@0: }
sl@0: 
sl@0: static __inline void
sl@0: be32enc(void *pp, uint32_t u)
sl@0: {
sl@0: 	unsigned char *p = (unsigned char *)pp;
sl@0: 
sl@0: 	p[0] = (u >> 24) & 0xff;
sl@0: 	p[1] = (u >> 16) & 0xff;
sl@0: 	p[2] = (u >> 8) & 0xff;
sl@0: 	p[3] = u & 0xff;
sl@0: }
sl@0: 
sl@0: static __inline void
sl@0: be64enc(void *pp, uint64_t u)
sl@0: {
sl@0: 	unsigned char *p = (unsigned char *)pp;
sl@0: 
sl@0: 	be32enc(p, u >> 32);
sl@0: 	be32enc(p + 4, u & 0xffffffff);
sl@0: }
sl@0: 
sl@0: static __inline void
sl@0: le16enc(void *pp, uint16_t u)
sl@0: {
sl@0: 	unsigned char *p = (unsigned char *)pp;
sl@0: 
sl@0: 	p[0] = u & 0xff;
sl@0: 	p[1] = (u >> 8) & 0xff;
sl@0: }
sl@0: 
sl@0: static __inline void
sl@0: le32enc(void *pp, uint32_t u)
sl@0: {
sl@0: 	unsigned char *p = (unsigned char *)pp;
sl@0: 
sl@0: 	p[0] = u & 0xff;
sl@0: 	p[1] = (u >> 8) & 0xff;
sl@0: 	p[2] = (u >> 16) & 0xff;
sl@0: 	p[3] = (u >> 24) & 0xff;
sl@0: }
sl@0: 
sl@0: static __inline void
sl@0: le64enc(void *pp, uint64_t u)
sl@0: {
sl@0: 	unsigned char *p = (unsigned char *)pp;
sl@0: 
sl@0: 	le32enc(p, u & 0xffffffff);
sl@0: 	le32enc(p + 4, u >> 32);
sl@0: }
sl@0: 
sl@0: #endif	/* _SYS_ENDIAN_H_ */