endian.h revision 233684
1193326Sed/*-
2193326Sed * Copyright (c) 1987, 1991 Regents of the University of California.
3193326Sed * All rights reserved.
4193326Sed *
5193326Sed * Redistribution and use in source and binary forms, with or without
6193326Sed * modification, are permitted provided that the following conditions
7193326Sed * are met:
8193326Sed * 1. Redistributions of source code must retain the above copyright
9193326Sed *    notice, this list of conditions and the following disclaimer.
10193326Sed * 2. Redistributions in binary form must reproduce the above copyright
11193326Sed *    notice, this list of conditions and the following disclaimer in the
12193326Sed *    documentation and/or other materials provided with the distribution.
13193326Sed * 4. Neither the name of the University nor the names of its contributors
14193326Sed *    may be used to endorse or promote products derived from this software
15193326Sed *    without specific prior written permission.
16193326Sed *
17193326Sed * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18201361Srdivacky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19193326Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20193326Sed * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21193326Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22193326Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23199482Srdivacky * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24193326Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25193326Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26193326Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27193326Sed * SUCH DAMAGE.
28193326Sed *
29193326Sed *	@(#)endian.h	7.8 (Berkeley) 4/3/91
30193326Sed * $FreeBSD: head/sys/x86/include/endian.h 233684 2012-03-29 23:31:48Z dim $
31193326Sed */
32193326Sed
33193326Sed#ifndef _MACHINE_ENDIAN_H_
34207619Srdivacky#define	_MACHINE_ENDIAN_H_
35207619Srdivacky
36207619Srdivacky#include <sys/cdefs.h>
37207619Srdivacky#include <sys/_types.h>
38207619Srdivacky
39207619Srdivacky/*
40207619Srdivacky * Define the order of 32-bit words in 64-bit words.
41207619Srdivacky */
42207619Srdivacky#define	_QUAD_HIGHWORD 1
43207619Srdivacky#define	_QUAD_LOWWORD 0
44207619Srdivacky
45207619Srdivacky/*
46207619Srdivacky * Definitions for byte order, according to byte significance from low
47207619Srdivacky * address to high.
48207619Srdivacky */
49193326Sed#define	_LITTLE_ENDIAN	1234	/* LSB first: i386, vax */
50207619Srdivacky#define	_BIG_ENDIAN	4321	/* MSB first: 68000, ibm, net */
51207619Srdivacky#define	_PDP_ENDIAN	3412	/* LSB first in word, MSW first in long */
52207619Srdivacky
53207619Srdivacky#define	_BYTE_ORDER	_LITTLE_ENDIAN
54207619Srdivacky
55207619Srdivacky/*
56207619Srdivacky * Deprecated variants that don't have enough underscores to be useful in more
57207619Srdivacky * strict namespaces.
58207619Srdivacky */
59207619Srdivacky#if __BSD_VISIBLE
60207619Srdivacky#define	LITTLE_ENDIAN	_LITTLE_ENDIAN
61207619Srdivacky#define	BIG_ENDIAN	_BIG_ENDIAN
62207619Srdivacky#define	PDP_ENDIAN	_PDP_ENDIAN
63207619Srdivacky#define	BYTE_ORDER	_BYTE_ORDER
64207619Srdivacky#endif
65207619Srdivacky
66207619Srdivacky#define	__bswap16_gen(x)	(__uint16_t)((x) << 8 | (x) >> 8)
67207619Srdivacky#define	__bswap32_gen(x)		\
68207619Srdivacky	(((__uint32_t)__bswap16((x) & 0xffff) << 16) | __bswap16((x) >> 16))
69207619Srdivacky#define	__bswap64_gen(x)		\
70207619Srdivacky	(((__uint64_t)__bswap32((x) & 0xffffffff) << 32) | __bswap32((x) >> 32))
71207619Srdivacky
72193326Sed#ifdef __GNUCLIKE_BUILTIN_CONSTANT_P
73193326Sed#define	__bswap16(x)				\
74193326Sed	((__uint16_t)(__builtin_constant_p(x) ?	\
75198092Srdivacky	    __bswap16_gen((__uint16_t)(x)) : __bswap16_var(x)))
76193326Sed#define	__bswap32(x)			\
77199990Srdivacky	(__builtin_constant_p(x) ?	\
78199990Srdivacky	    __bswap32_gen((__uint32_t)(x)) : __bswap32_var(x))
79199990Srdivacky#define	__bswap64(x)			\
80207619Srdivacky	(__builtin_constant_p(x) ?	\
81200583Srdivacky	    __bswap64_gen((__uint64_t)(x)) : __bswap64_var(x))
82193326Sed#else
83193326Sed/* XXX these are broken for use in static initializers. */
84198092Srdivacky#define	__bswap16(x)	__bswap16_var(x)
85193326Sed#define	__bswap32(x)	__bswap32_var(x)
86193326Sed#define	__bswap64(x)	__bswap64_var(x)
87198092Srdivacky#endif
88193326Sed
89193326Sed/* These are defined as functions to avoid multiple evaluation of x. */
90193326Sed
91198092Srdivackystatic __inline __uint16_t
92193326Sed__bswap16_var(__uint16_t _x)
93193326Sed{
94193326Sed
95198092Srdivacky	return (__bswap16_gen(_x));
96193326Sed}
97193326Sed
98193326Sedstatic __inline __uint32_t
99193326Sed__bswap32_var(__uint32_t _x)
100193326Sed{
101193326Sed
102193326Sed#ifdef __GNUCLIKE_ASM
103193326Sed	__asm("bswap %0" : "+r" (_x));
104193326Sed	return (_x);
105195341Sed#else
106193326Sed	return (__bswap32_gen(_x));
107198092Srdivacky#endif
108193326Sed}
109193326Sed
110193326Sedstatic __inline __uint64_t
111193326Sed__bswap64_var(__uint64_t _x)
112193326Sed{
113203955Srdivacky
114203955Srdivacky#if defined(__amd64__) && defined(__GNUCLIKE_ASM)
115203955Srdivacky	__asm("bswap %0" : "+r" (_x));
116203955Srdivacky	return (_x);
117203955Srdivacky#else
118203955Srdivacky	/*
119203955Srdivacky	 * It is important for the optimizations that the following is not
120203955Srdivacky	 * really generic, but expands to 2 __bswap32_var()'s.
121203955Srdivacky	 */
122203955Srdivacky	return (__bswap64_gen(_x));
123203955Srdivacky#endif
124203955Srdivacky}
125203955Srdivacky
126203955Srdivacky#define	__htonl(x)	__bswap32(x)
127203955Srdivacky#define	__htons(x)	__bswap16(x)
128193326Sed#define	__ntohl(x)	__bswap32(x)
129193326Sed#define	__ntohs(x)	__bswap16(x)
130193326Sed
131193326Sed#endif /* !_MACHINE_ENDIAN_H_ */
132193326Sed