199129Sobrien/*-
299129Sobrien * Copyright (c) 1987, 1991 Regents of the University of California.
399129Sobrien * All rights reserved.
499129Sobrien *
599129Sobrien * Redistribution and use in source and binary forms, with or without
699129Sobrien * modification, are permitted provided that the following conditions
799129Sobrien * are met:
899129Sobrien * 1. Redistributions of source code must retain the above copyright
999129Sobrien *    notice, this list of conditions and the following disclaimer.
1099129Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1199129Sobrien *    notice, this list of conditions and the following disclaimer in the
1299129Sobrien *    documentation and/or other materials provided with the distribution.
1399129Sobrien * 4. Neither the name of the University nor the names of its contributors
1499129Sobrien *    may be used to endorse or promote products derived from this software
1599129Sobrien *    without specific prior written permission.
1699129Sobrien *
1799129Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1899129Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1999129Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2099129Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2199129Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2299129Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2399129Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2499129Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2599129Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2699129Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2799129Sobrien * SUCH DAMAGE.
2899129Sobrien *
2999129Sobrien *	@(#)endian.h	7.8 (Berkeley) 4/3/91
3099129Sobrien * $FreeBSD$
3199129Sobrien */
3299129Sobrien
3399129Sobrien#ifndef _MACHINE_ENDIAN_H_
3499129Sobrien#define	_MACHINE_ENDIAN_H_
3599129Sobrien
3699129Sobrien#include <sys/cdefs.h>
37102227Smike#include <sys/_types.h>
3899129Sobrien
3999129Sobrien/*
4099129Sobrien * Define the order of 32-bit words in 64-bit words.
4199129Sobrien */
4299129Sobrien#define	_QUAD_HIGHWORD 1
4399129Sobrien#define	_QUAD_LOWWORD 0
4499129Sobrien
4599129Sobrien/*
4699129Sobrien * Definitions for byte order, according to byte significance from low
4799129Sobrien * address to high.
4899129Sobrien */
4999129Sobrien#define	_LITTLE_ENDIAN	1234	/* LSB first: i386, vax */
5099129Sobrien#define	_BIG_ENDIAN	4321	/* MSB first: 68000, ibm, net */
5199129Sobrien#define	_PDP_ENDIAN	3412	/* LSB first in word, MSW first in long */
5299129Sobrien
5399129Sobrien#define	_BYTE_ORDER	_LITTLE_ENDIAN
5499129Sobrien
5599129Sobrien/*
5699129Sobrien * Deprecated variants that don't have enough underscores to be useful in more
5799129Sobrien * strict namespaces.
5899129Sobrien */
5999129Sobrien#if __BSD_VISIBLE
6099129Sobrien#define	LITTLE_ENDIAN	_LITTLE_ENDIAN
6199129Sobrien#define	BIG_ENDIAN	_BIG_ENDIAN
6299129Sobrien#define	PDP_ENDIAN	_PDP_ENDIAN
6399129Sobrien#define	BYTE_ORDER	_BYTE_ORDER
6499129Sobrien#endif
6599129Sobrien
66233683Sdim#define	__bswap16_gen(x)	(__uint16_t)((x) << 8 | (x) >> 8)
67232721Stijl#define	__bswap32_gen(x)		\
68233684Sdim	(((__uint32_t)__bswap16((x) & 0xffff) << 16) | __bswap16((x) >> 16))
69232721Stijl#define	__bswap64_gen(x)		\
70233684Sdim	(((__uint64_t)__bswap32((x) & 0xffffffff) << 32) | __bswap32((x) >> 32))
7199129Sobrien
72232721Stijl#ifdef __GNUCLIKE_BUILTIN_CONSTANT_P
73232730Stijl#define	__bswap16(x)				\
74232745Sdim	((__uint16_t)(__builtin_constant_p(x) ?	\
75232745Sdim	    __bswap16_gen((__uint16_t)(x)) : __bswap16_var(x)))
76232721Stijl#define	__bswap32(x)			\
77232721Stijl	(__builtin_constant_p(x) ?	\
78232721Stijl	    __bswap32_gen((__uint32_t)(x)) : __bswap32_var(x))
79232721Stijl#define	__bswap64(x)			\
80232721Stijl	(__builtin_constant_p(x) ?	\
81232721Stijl	    __bswap64_gen((__uint64_t)(x)) : __bswap64_var(x))
82232721Stijl#else
83232721Stijl/* XXX these are broken for use in static initializers. */
84232721Stijl#define	__bswap16(x)	__bswap16_var(x)
85232721Stijl#define	__bswap32(x)	__bswap32_var(x)
86232721Stijl#define	__bswap64(x)	__bswap64_var(x)
87232721Stijl#endif
8899129Sobrien
89232721Stijl/* These are defined as functions to avoid multiple evaluation of x. */
90120350Speter
91232266Stijlstatic __inline __uint16_t
92232266Stijl__bswap16_var(__uint16_t _x)
9399129Sobrien{
94114349Speter
95232721Stijl	return (__bswap16_gen(_x));
9699129Sobrien}
9799129Sobrien
9899129Sobrienstatic __inline __uint32_t
99219819Sjeff__bswap32_var(__uint32_t _x)
10099129Sobrien{
10199129Sobrien
102232721Stijl#ifdef __GNUCLIKE_ASM
103232721Stijl	__asm("bswap %0" : "+r" (_x));
104219819Sjeff	return (_x);
105232721Stijl#else
106232721Stijl	return (__bswap32_gen(_x));
107232721Stijl#endif
10899129Sobrien}
10999129Sobrien
110232266Stijlstatic __inline __uint64_t
111232266Stijl__bswap64_var(__uint64_t _x)
11299129Sobrien{
113232721Stijl
114232721Stijl#if defined(__amd64__) && defined(__GNUCLIKE_ASM)
115232721Stijl	__asm("bswap %0" : "+r" (_x));
116232266Stijl	return (_x);
117232266Stijl#else
118232721Stijl	/*
119232721Stijl	 * It is important for the optimizations that the following is not
120232721Stijl	 * really generic, but expands to 2 __bswap32_var()'s.
121232721Stijl	 */
122232721Stijl	return (__bswap64_gen(_x));
123232266Stijl#endif
12499129Sobrien}
12599129Sobrien
12699129Sobrien#define	__htonl(x)	__bswap32(x)
12799129Sobrien#define	__htons(x)	__bswap16(x)
12899129Sobrien#define	__ntohl(x)	__bswap32(x)
12999129Sobrien#define	__ntohs(x)	__bswap16(x)
13099129Sobrien
13199129Sobrien#endif /* !_MACHINE_ENDIAN_H_ */
132