190868Smike/*-
270576Sobrien * Copyright (c) 1987, 1991, 1993
370576Sobrien *	The Regents of the University of California.  All rights reserved.
470576Sobrien *
570576Sobrien * Redistribution and use in source and binary forms, with or without
670576Sobrien * modification, are permitted provided that the following conditions
770576Sobrien * are met:
870576Sobrien * 1. Redistributions of source code must retain the above copyright
970576Sobrien *    notice, this list of conditions and the following disclaimer.
1070576Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1170576Sobrien *    notice, this list of conditions and the following disclaimer in the
1270576Sobrien *    documentation and/or other materials provided with the distribution.
1370576Sobrien * 4. Neither the name of the University nor the names of its contributors
1470576Sobrien *    may be used to endorse or promote products derived from this software
1570576Sobrien *    without specific prior written permission.
1670576Sobrien *
1770576Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1870576Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1970576Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2070576Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2170576Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2270576Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2370576Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2470576Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2570576Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2670576Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2770576Sobrien * SUCH DAMAGE.
2870576Sobrien *
2970576Sobrien *	@(#)endian.h	8.1 (Berkeley) 6/10/93
3070576Sobrien * $FreeBSD$
3170576Sobrien */
3270576Sobrien
3390868Smike#ifndef _MACHINE_ENDIAN_H_
3490868Smike#define	_MACHINE_ENDIAN_H_
3570576Sobrien
3694512Smike#include <sys/cdefs.h>
37102227Smike#include <sys/_types.h>
3890868Smike
3970576Sobrien/*
4070576Sobrien * Define the order of 32-bit words in 64-bit words.
4170576Sobrien */
4270576Sobrien#define	_QUAD_HIGHWORD 0
4370576Sobrien#define	_QUAD_LOWWORD 1
4470576Sobrien
4570576Sobrien/*
46184460Smarcel * GCC defines _BIG_ENDIAN and _LITTLE_ENDIAN equal to __BIG_ENDIAN__
47184460Smarcel * and __LITTLE_ENDIAN__ (resp).
48184460Smarcel */
49184460Smarcel#ifdef _BIG_ENDIAN
50184460Smarcel#undef _BIG_ENDIAN
51184460Smarcel#endif
52184460Smarcel#ifdef _LITTLE_ENDIAN
53184460Smarcel#undef _LITTLE_ENDIAN
54184460Smarcel#endif
55184460Smarcel
56184460Smarcel/*
5770576Sobrien * Definitions for byte order, according to byte significance from low
5870576Sobrien * address to high.
5970576Sobrien */
6094362Smike#define	_LITTLE_ENDIAN	1234	/* LSB first: i386, vax */
6194362Smike#define	_BIG_ENDIAN	4321	/* MSB first: 68000, ibm, net */
6294362Smike#define	_PDP_ENDIAN	3412	/* LSB first in word, MSW first in long */
6370576Sobrien
64184460Smarcel#ifdef __LITTLE_ENDIAN__
65184460Smarcel#define	_BYTE_ORDER	_LITTLE_ENDIAN
66184460Smarcel#else
6794362Smike#define	_BYTE_ORDER	_BIG_ENDIAN
68184460Smarcel#endif
6970576Sobrien
7094362Smike/*
7194362Smike * Deprecated variants that don't have enough underscores to be useful in more
7294362Smike * strict namespaces.
7394362Smike */
7494362Smike#if __BSD_VISIBLE
7594362Smike#define	LITTLE_ENDIAN	_LITTLE_ENDIAN
7694362Smike#define	BIG_ENDIAN	_BIG_ENDIAN
7794362Smike#define	PDP_ENDIAN	_PDP_ENDIAN
7894362Smike#define	BYTE_ORDER	_BYTE_ORDER
7994362Smike#endif
8094362Smike
81216122Snwhitehorn#if defined(__GNUCLIKE_BUILTIN_CONSTANT_P)
82216122Snwhitehorn#define	__is_constant(x)	__builtin_constant_p(x)
83216122Snwhitehorn#else
84216122Snwhitehorn#define	__is_constant(x)	0
85216122Snwhitehorn#endif
8670576Sobrien
87216122Snwhitehorn#define	__bswap16_const(x)	((((__uint16_t)(x) >> 8) & 0xff) |	\
88216122Snwhitehorn	(((__uint16_t)(x) << 8) & 0xff00))
89216122Snwhitehorn#define	__bswap32_const(x)	((((__uint32_t)(x) >> 24) & 0xff) |	\
90216122Snwhitehorn	(((__uint32_t)(x) >> 8) & 0xff00) |				\
91216122Snwhitehorn	(((__uint32_t)(x)<< 8) & 0xff0000) |				\
92216122Snwhitehorn	(((__uint32_t)(x) << 24) & 0xff000000))
93216122Snwhitehorn#define	__bswap64_const(x)	((((__uint64_t)(x) >> 56) & 0xff) |	\
94216122Snwhitehorn	(((__uint64_t)(x) >> 40) & 0xff00) |				\
95216122Snwhitehorn	(((__uint64_t)(x) >> 24) & 0xff0000) |				\
96216122Snwhitehorn	(((__uint64_t)(x) >> 8) & 0xff000000) |				\
97216122Snwhitehorn	(((__uint64_t)(x) << 8) & ((__uint64_t)0xff << 32)) |		\
98216122Snwhitehorn	(((__uint64_t)(x) << 24) & ((__uint64_t)0xff << 40)) |		\
99216122Snwhitehorn	(((__uint64_t)(x) << 40) & ((__uint64_t)0xff << 48)) |		\
100216122Snwhitehorn	(((__uint64_t)(x) << 56) & ((__uint64_t)0xff << 56)))
101216122Snwhitehorn
10291959Smikestatic __inline __uint16_t
103216122Snwhitehorn__bswap16_var(__uint16_t _x)
10491959Smike{
10591959Smike
10691959Smike	return ((_x >> 8) | ((_x << 8) & 0xff00));
10791959Smike}
10891959Smike
10991959Smikestatic __inline __uint32_t
110216122Snwhitehorn__bswap32_var(__uint32_t _x)
11191959Smike{
11291959Smike
11391959Smike	return ((_x >> 24) | ((_x >> 8) & 0xff00) | ((_x << 8) & 0xff0000) |
11491959Smike	    ((_x << 24) & 0xff000000));
11591959Smike}
11691959Smike
11791959Smikestatic __inline __uint64_t
118216122Snwhitehorn__bswap64_var(__uint64_t _x)
11991959Smike{
12091959Smike
12191959Smike	return ((_x >> 56) | ((_x >> 40) & 0xff00) | ((_x >> 24) & 0xff0000) |
12291959Smike	    ((_x >> 8) & 0xff000000) | ((_x << 8) & ((__uint64_t)0xff << 32)) |
12391959Smike	    ((_x << 24) & ((__uint64_t)0xff << 40)) |
12491959Smike	    ((_x << 40) & ((__uint64_t)0xff << 48)) | ((_x << 56)));
12591959Smike}
12691959Smike
127233349Sdim#define	__bswap16(x)	((__uint16_t)(__is_constant(x) ? __bswap16_const(x) : \
128233349Sdim	__bswap16_var(x)))
129216122Snwhitehorn#define	__bswap32(x)	(__is_constant(x) ? __bswap32_const(x) : \
130216122Snwhitehorn	__bswap32_var(x))
131216122Snwhitehorn#define	__bswap64(x)	(__is_constant(x) ? __bswap64_const(x) : \
132216122Snwhitehorn	__bswap64_var(x))
133216122Snwhitehorn
13491959Smike#define	__htonl(x)	((__uint32_t)(x))
13591959Smike#define	__htons(x)	((__uint16_t)(x))
13691959Smike#define	__ntohl(x)	((__uint32_t)(x))
13791959Smike#define	__ntohs(x)	((__uint16_t)(x))
13891959Smike
13990868Smike#endif /* !_MACHINE_ENDIAN_H_ */
140