190868Smike/*-
266458Sdfr * Copyright (c) 1987, 1991, 1993
366458Sdfr *	The Regents of the University of California.  All rights reserved.
466458Sdfr *
566458Sdfr * Redistribution and use in source and binary forms, with or without
666458Sdfr * modification, are permitted provided that the following conditions
766458Sdfr * are met:
866458Sdfr * 1. Redistributions of source code must retain the above copyright
966458Sdfr *    notice, this list of conditions and the following disclaimer.
1066458Sdfr * 2. Redistributions in binary form must reproduce the above copyright
1166458Sdfr *    notice, this list of conditions and the following disclaimer in the
1266458Sdfr *    documentation and/or other materials provided with the distribution.
1366458Sdfr * 4. Neither the name of the University nor the names of its contributors
1466458Sdfr *    may be used to endorse or promote products derived from this software
1566458Sdfr *    without specific prior written permission.
1666458Sdfr *
1766458Sdfr * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1866458Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1966458Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2066458Sdfr * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2166458Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2266458Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2366458Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2466458Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2566458Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2666458Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2766458Sdfr * SUCH DAMAGE.
2866458Sdfr *
2966458Sdfr *	@(#)endian.h	8.1 (Berkeley) 6/10/93
3090868Smike *	$NetBSD: endian.h,v 1.5 1997/10/09 15:42:19 bouyer Exp $
3190868Smike * $FreeBSD$
3266458Sdfr */
3366458Sdfr
3490868Smike#ifndef _MACHINE_ENDIAN_H_
3590868Smike#define	_MACHINE_ENDIAN_H_
3666458Sdfr
3790868Smike#include <sys/cdefs.h>
38102227Smike#include <sys/_types.h>
3990868Smike
4066458Sdfr/*
4166458Sdfr * Define the order of 32-bit words in 64-bit words.
4266458Sdfr */
43118382Sobrien#define	_QUAD_HIGHWORD 1
44118382Sobrien#define	_QUAD_LOWWORD 0
4566458Sdfr
4666458Sdfr/*
4766458Sdfr * Definitions for byte order, according to byte significance from low
4866458Sdfr * address to high.
4966458Sdfr */
5094362Smike#define	_LITTLE_ENDIAN	1234	/* LSB first: i386, vax */
5194362Smike#define	_BIG_ENDIAN	4321	/* MSB first: 68000, ibm, net */
5294362Smike#define	_PDP_ENDIAN	3412	/* LSB first in word, MSW first in long */
5366458Sdfr
5494362Smike#define	_BYTE_ORDER	_LITTLE_ENDIAN
5566458Sdfr
5694362Smike/*
5794362Smike * Deprecated variants that don't have enough underscores to be useful in more
5894362Smike * strict namespaces.
5994362Smike */
6094362Smike#if __BSD_VISIBLE
6194362Smike#define	LITTLE_ENDIAN	_LITTLE_ENDIAN
6294362Smike#define	BIG_ENDIAN	_BIG_ENDIAN
6394362Smike#define	PDP_ENDIAN	_PDP_ENDIAN
6494362Smike#define	BYTE_ORDER	_BYTE_ORDER
6594362Smike#endif
6694362Smike
67143063Sjoerg#if defined(__CC_SUPPORTS___INLINE) && defined(__GNUCLIKE_ASM)
6884638Sdfr
6984638Sdfrstatic __inline __uint64_t
70118382Sobrien__bswap64(__uint64_t _x)
7184638Sdfr{
7284638Sdfr	__uint64_t __r;
73118382Sobrien
7484638Sdfr	__asm __volatile("mux1 %0=%1,@rev"
75118382Sobrien			 : "=r" (__r) : "r"(_x));
7684638Sdfr	return __r;
7784638Sdfr}
7884638Sdfr
7984638Sdfrstatic __inline __uint32_t
80118382Sobrien__bswap32(__uint32_t _x)
8184638Sdfr{
8290868Smike
83118382Sobrien	return (__bswap64(_x) >> 32);
8484638Sdfr}
8584638Sdfr
8684638Sdfrstatic __inline __uint16_t
87118382Sobrien__bswap16(__uint16_t _x)
8884638Sdfr{
8990868Smike
90118382Sobrien	return (__bswap64(_x) >> 48);
9184638Sdfr}
9284638Sdfr
9391959Smike#define	__htonl(x)	__bswap32(x)
9491959Smike#define	__htons(x)	__bswap16(x)
9591959Smike#define	__ntohl(x)	__bswap32(x)
9691959Smike#define	__ntohs(x)	__bswap16(x)
9784638Sdfr
98143063Sjoerg#else /* !(__CC_SUPPORTS___INLINE && __GNUCLIKE_ASM) */
99103814Smike
100103814Smike/*
101103814Smike * No optimizations are available for this compiler.  Fall back to
102103814Smike * non-optimized functions by defining the constant usually used to prevent
103103814Smike * redefinition.
104103814Smike */
105103814Smike#define	_BYTEORDER_FUNC_DEFINED
106103814Smike
107143063Sjoerg#endif /* __CC_SUPPORTS___INLINE && __GNUCLIKE_ASM */
108103814Smike
10990868Smike#endif /* !_MACHINE_ENDIAN_H_ */
110