190868Smike/*-
280708Sjake * Copyright (c) 1987, 1991, 1993
380708Sjake *	The Regents of the University of California.  All rights reserved.
480708Sjake *
580708Sjake * Redistribution and use in source and binary forms, with or without
680708Sjake * modification, are permitted provided that the following conditions
780708Sjake * are met:
880708Sjake * 1. Redistributions of source code must retain the above copyright
980708Sjake *    notice, this list of conditions and the following disclaimer.
1080708Sjake * 2. Redistributions in binary form must reproduce the above copyright
1180708Sjake *    notice, this list of conditions and the following disclaimer in the
1280708Sjake *    documentation and/or other materials provided with the distribution.
1390868Smike * 4. Neither the name of the University nor the names of its contributors
1490868Smike *    may be used to endorse or promote products derived from this software
1590868Smike *    without specific prior written permission.
1680708Sjake *
1780708Sjake * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1880708Sjake * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1980708Sjake * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2080708Sjake * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2180708Sjake * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2280708Sjake * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2380708Sjake * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2480708Sjake * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2580708Sjake * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2680708Sjake * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2780708Sjake * SUCH DAMAGE.
2880708Sjake *
2980708Sjake *	@(#)endian.h	8.1 (Berkeley) 6/10/93
3080708Sjake * $FreeBSD$
3180708Sjake */
3280708Sjake
3380708Sjake#ifndef _MACHINE_ENDIAN_H_
3480708Sjake#define	_MACHINE_ENDIAN_H_
3580708Sjake
3694512Smike#include <sys/cdefs.h>
37102227Smike#include <sys/_types.h>
3890868Smike
3980708Sjake/*
4080708Sjake * Define the order of 32-bit words in 64-bit words.
4180708Sjake */
4296672Sobrien#define	_QUAD_HIGHWORD 0
4396672Sobrien#define	_QUAD_LOWWORD 1
4480708Sjake
4580708Sjake/*
4680708Sjake * Definitions for byte order, according to byte significance from low
4780708Sjake * address to high.
4880708Sjake */
4994362Smike#define	_LITTLE_ENDIAN	1234	/* LSB first: i386, vax */
5094362Smike#define	_BIG_ENDIAN	4321	/* MSB first: 68000, ibm, net */
5194362Smike#define	_PDP_ENDIAN	3412	/* LSB first in word, MSW first in long */
5280708Sjake
5394362Smike#define	_BYTE_ORDER	_BIG_ENDIAN
5480708Sjake
5594362Smike/*
5694362Smike * Deprecated variants that don't have enough underscores to be useful in more
5794362Smike * strict namespaces.
5894362Smike */
5994362Smike#if __BSD_VISIBLE
6094362Smike#define	LITTLE_ENDIAN	_LITTLE_ENDIAN
6194362Smike#define	BIG_ENDIAN	_BIG_ENDIAN
6294362Smike#define	PDP_ENDIAN	_PDP_ENDIAN
6394362Smike#define	BYTE_ORDER	_BYTE_ORDER
6494362Smike#endif
6594362Smike
66143063Sjoerg#if defined(__GNUCLIKE_BUILTIN_CONSTANT_P) && defined(__OPTIMIZE__)
67120611Smux#define	__is_constant(x)	__builtin_constant_p(x)
68120611Smux#else
69120611Smux#define	__is_constant(x)	0
70120611Smux#endif
7191959Smike
72213578Smarius#define	__bswap16_const(x)	((((__uint16_t)(x) >> 8) & 0xff) |	\
73213578Smarius	(((__uint16_t)(x) << 8) & 0xff00))
74213578Smarius#define	__bswap32_const(x)	((((__uint32_t)(x) >> 24) & 0xff) |	\
75213578Smarius	(((__uint32_t)(x) >> 8) & 0xff00) |				\
76213578Smarius	(((__uint32_t)(x)<< 8) & 0xff0000) |				\
77213578Smarius	(((__uint32_t)(x) << 24) & 0xff000000))
78213578Smarius#define	__bswap64_const(x)	((((__uint64_t)(x) >> 56) & 0xff) |	\
79213578Smarius	(((__uint64_t)(x) >> 40) & 0xff00) |				\
80213578Smarius	(((__uint64_t)(x) >> 24) & 0xff0000) |				\
81213578Smarius	(((__uint64_t)(x) >> 8) & 0xff000000) |				\
82213578Smarius	(((__uint64_t)(x) << 8) & ((__uint64_t)0xff << 32)) |		\
83213578Smarius	(((__uint64_t)(x) << 24) & ((__uint64_t)0xff << 40)) |		\
84213578Smarius	(((__uint64_t)(x) << 40) & ((__uint64_t)0xff << 48)) |		\
85213578Smarius	(((__uint64_t)(x) << 56) & ((__uint64_t)0xff << 56)))
86120611Smux
8791959Smikestatic __inline __uint16_t
88120611Smux__bswap16_var(__uint16_t _x)
8991959Smike{
9091959Smike
9191959Smike	return ((_x >> 8) | ((_x << 8) & 0xff00));
9291959Smike}
9391959Smike
9491959Smikestatic __inline __uint32_t
95120611Smux__bswap32_var(__uint32_t _x)
9691959Smike{
9791959Smike
9891959Smike	return ((_x >> 24) | ((_x >> 8) & 0xff00) | ((_x << 8) & 0xff0000) |
9991959Smike	    ((_x << 24) & 0xff000000));
10091959Smike}
10191959Smike
10291959Smikestatic __inline __uint64_t
103120611Smux__bswap64_var(__uint64_t _x)
10491959Smike{
10591959Smike
10691959Smike	return ((_x >> 56) | ((_x >> 40) & 0xff00) | ((_x >> 24) & 0xff0000) |
10791959Smike	    ((_x >> 8) & 0xff000000) | ((_x << 8) & ((__uint64_t)0xff << 32)) |
10891959Smike	    ((_x << 24) & ((__uint64_t)0xff << 40)) |
10991959Smike	    ((_x << 40) & ((__uint64_t)0xff << 48)) | ((_x << 56)));
11091959Smike}
11191959Smike
112233349Sdim#define	__bswap16(x)	((__uint16_t)(__is_constant(x) ? __bswap16_const(x) : \
113233349Sdim	__bswap16_var(x)))
114120611Smux#define	__bswap32(x)	(__is_constant(x) ? __bswap32_const(x) : \
115120611Smux	__bswap32_var(x))
116120611Smux#define	__bswap64(x)	(__is_constant(x) ? __bswap64_const(x) : \
117120611Smux	__bswap64_var(x))
118120611Smux
11991959Smike#define	__htonl(x)	((__uint32_t)(x))
12091959Smike#define	__htons(x)	((__uint16_t)(x))
12191959Smike#define	__ntohl(x)	((__uint32_t)(x))
12291959Smike#define	__ntohs(x)	((__uint16_t)(x))
12391959Smike
12480708Sjake#endif /* !_MACHINE_ENDIAN_H_ */
125