1292407Sbr/*-
2292407Sbr * Copyright (c) 2001 David E. O'Brien
3292407Sbr *
4292407Sbr * Redistribution and use in source and binary forms, with or without
5292407Sbr * modification, are permitted provided that the following conditions
6292407Sbr * are met:
7292407Sbr * 1. Redistributions of source code must retain the above copyright
8292407Sbr *    notice, this list of conditions and the following disclaimer.
9292407Sbr * 2. Redistributions in binary form must reproduce the above copyright
10292407Sbr *    notice, this list of conditions and the following disclaimer in the
11292407Sbr *    documentation and/or other materials provided with the distribution.
12292407Sbr * 3. Neither the name of the University nor the names of its contributors
13292407Sbr *    may be used to endorse or promote products derived from this software
14292407Sbr *    without specific prior written permission.
15292407Sbr *
16292407Sbr * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17292407Sbr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18292407Sbr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19292407Sbr * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20292407Sbr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21292407Sbr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22292407Sbr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23292407Sbr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24292407Sbr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25292407Sbr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26292407Sbr * SUCH DAMAGE.
27292407Sbr *
28292407Sbr *	@(#)endian.h	8.1 (Berkeley) 6/10/93
29292407Sbr * $NetBSD: endian.h,v 1.7 1999/08/21 05:53:51 simonb Exp $
30292407Sbr * $FreeBSD$
31292407Sbr */
32292407Sbr
33292407Sbr#ifndef _MACHINE_ENDIAN_H_
34292407Sbr#define	_MACHINE_ENDIAN_H_
35292407Sbr
36292407Sbr#include <sys/_types.h>
37292407Sbr
38292407Sbr/*
39292407Sbr * Definitions for byte order, according to byte significance from low
40292407Sbr * address to high.
41292407Sbr */
42292407Sbr#define	_LITTLE_ENDIAN  1234    /* LSB first: i386, vax */
43292407Sbr#define	_BIG_ENDIAN     4321    /* MSB first: 68000, ibm, net */
44292407Sbr#define	_PDP_ENDIAN     3412    /* LSB first in word, MSW first in long */
45292407Sbr
46292407Sbr#define	_BYTE_ORDER	_LITTLE_ENDIAN
47292407Sbr
48292407Sbr#if __BSD_VISIBLE
49292407Sbr#define	LITTLE_ENDIAN   _LITTLE_ENDIAN
50292407Sbr#define	BIG_ENDIAN      _BIG_ENDIAN
51292407Sbr#define	PDP_ENDIAN      _PDP_ENDIAN
52292407Sbr#define	BYTE_ORDER      _BYTE_ORDER
53292407Sbr#endif
54292407Sbr
55292407Sbr#define	_QUAD_HIGHWORD  1
56292407Sbr#define	_QUAD_LOWWORD 0
57292407Sbr#define	__ntohl(x)        (__bswap32(x))
58292407Sbr#define	__ntohs(x)        (__bswap16(x))
59292407Sbr#define	__htonl(x)        (__bswap32(x))
60292407Sbr#define	__htons(x)        (__bswap16(x))
61292407Sbr
62292407Sbrstatic __inline __uint64_t
63292407Sbr__bswap64(__uint64_t _x)
64292407Sbr{
65292407Sbr	__uint64_t ret;
66292407Sbr
67292407Sbr	ret = (_x >> 56);
68292407Sbr	ret |= ((_x >> 40) & 0xff00);
69292407Sbr	ret |= ((_x >> 24) & 0xff0000);
70292407Sbr	ret |= ((_x >>  8) & 0xff000000);
71292407Sbr	ret |= ((_x <<  8) & ((__uint64_t)0xff << 32));
72292407Sbr	ret |= ((_x << 24) & ((__uint64_t)0xff << 40));
73292407Sbr	ret |= ((_x << 40) & ((__uint64_t)0xff << 48));
74292407Sbr	ret |= (_x << 56);
75292407Sbr
76292407Sbr	return (ret);
77292407Sbr}
78292407Sbr
79292407Sbrstatic __inline __uint32_t
80292407Sbr__bswap32_var(__uint32_t _x)
81292407Sbr{
82292407Sbr
83292407Sbr	return ((_x >> 24) | ((_x >> 8) & 0xff00) | ((_x << 8) & 0xff0000) |
84292407Sbr	    ((_x << 24) & 0xff000000));
85292407Sbr}
86292407Sbr
87292407Sbrstatic __inline __uint16_t
88292407Sbr__bswap16_var(__uint16_t _x)
89292407Sbr{
90292407Sbr	__uint32_t ret;
91292407Sbr
92292407Sbr	ret = ((_x >> 8) | ((_x << 8) & 0xff00));
93292407Sbr
94292407Sbr	return ((__uint16_t)ret);
95292407Sbr}
96292407Sbr
97292407Sbr#ifdef __OPTIMIZE__
98292407Sbr
99292407Sbr#define	__bswap32_constant(x)	\
100292407Sbr    ((((x) & 0xff000000U) >> 24) |	\
101292407Sbr     (((x) & 0x00ff0000U) >>  8) |	\
102292407Sbr     (((x) & 0x0000ff00U) <<  8) |	\
103292407Sbr     (((x) & 0x000000ffU) << 24))
104292407Sbr
105292407Sbr#define	__bswap16_constant(x)	\
106292407Sbr    ((((x) & 0xff00) >> 8) |		\
107292407Sbr     (((x) & 0x00ff) << 8))
108292407Sbr
109292407Sbr#define	__bswap16(x)	\
110292407Sbr    ((__uint16_t)(__builtin_constant_p(x) ?	\
111292407Sbr     __bswap16_constant(x) :			\
112292407Sbr     __bswap16_var(x)))
113292407Sbr
114292407Sbr#define	__bswap32(x)	\
115292407Sbr    ((__uint32_t)(__builtin_constant_p(x) ? 	\
116292407Sbr     __bswap32_constant(x) :			\
117292407Sbr     __bswap32_var(x)))
118292407Sbr
119292407Sbr#else
120292407Sbr#define	__bswap16(x)	__bswap16_var(x)
121292407Sbr#define	__bswap32(x)	__bswap32_var(x)
122292407Sbr
123292407Sbr#endif /* __OPTIMIZE__ */
124292407Sbr#endif /* !_MACHINE_ENDIAN_H_ */
125