endian.h revision 128019
1193323Sed/*-
2193323Sed * Copyright (c) 1987, 1991, 1993
3193323Sed *	The Regents of the University of California.  All rights reserved.
4193323Sed *
5193323Sed * Redistribution and use in source and binary forms, with or without
6193323Sed * modification, are permitted provided that the following conditions
7193323Sed * are met:
8193323Sed * 1. Redistributions of source code must retain the above copyright
9193323Sed *    notice, this list of conditions and the following disclaimer.
10193323Sed * 2. Redistributions in binary form must reproduce the above copyright
11193323Sed *    notice, this list of conditions and the following disclaimer in the
12193323Sed *    documentation and/or other materials provided with the distribution.
13193323Sed * 4. Neither the name of the University nor the names of its contributors
14193323Sed *    may be used to endorse or promote products derived from this software
15193323Sed *    without specific prior written permission.
16193323Sed *
17193323Sed * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18193323Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19193323Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20193323Sed * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21193323Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22193323Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23193323Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24249423Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25224145Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26193323Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27249423Sdim * SUCH DAMAGE.
28207618Srdivacky *
29249423Sdim *	@(#)endian.h	8.1 (Berkeley) 6/10/93
30193323Sed *	$NetBSD: endian.h,v 1.5 1997/10/09 15:42:19 bouyer Exp $
31249423Sdim * $FreeBSD: head/sys/ia64/include/endian.h 128019 2004-04-07 20:46:16Z imp $
32249423Sdim */
33218893Sdim
34194612Sed#ifndef _MACHINE_ENDIAN_H_
35249423Sdim#define	_MACHINE_ENDIAN_H_
36193323Sed
37193323Sed#include <sys/cdefs.h>
38193323Sed#include <sys/_types.h>
39193323Sed
40193323Sed/*
41193323Sed * Define the order of 32-bit words in 64-bit words.
42193323Sed */
43193323Sed#define	_QUAD_HIGHWORD 1
44193323Sed#define	_QUAD_LOWWORD 0
45193323Sed
46205218Srdivacky/*
47193323Sed * Definitions for byte order, according to byte significance from low
48193323Sed * address to high.
49193323Sed */
50193323Sed#define	_LITTLE_ENDIAN	1234	/* LSB first: i386, vax */
51202878Srdivacky#define	_BIG_ENDIAN	4321	/* MSB first: 68000, ibm, net */
52249423Sdim#define	_PDP_ENDIAN	3412	/* LSB first in word, MSW first in long */
53193323Sed
54193323Sed#define	_BYTE_ORDER	_LITTLE_ENDIAN
55193323Sed
56193323Sed/*
57193323Sed * Deprecated variants that don't have enough underscores to be useful in more
58198090Srdivacky * strict namespaces.
59193323Sed */
60193323Sed#if __BSD_VISIBLE
61193323Sed#define	LITTLE_ENDIAN	_LITTLE_ENDIAN
62193323Sed#define	BIG_ENDIAN	_BIG_ENDIAN
63193323Sed#define	PDP_ENDIAN	_PDP_ENDIAN
64193323Sed#define	BYTE_ORDER	_BYTE_ORDER
65193323Sed#endif
66193323Sed
67193323Sed#ifdef __GNUC__
68193323Sed
69193323Sedstatic __inline __uint64_t
70193323Sed__bswap64(__uint64_t _x)
71193323Sed{
72193323Sed	__uint64_t __r;
73193323Sed
74193323Sed	__asm __volatile("mux1 %0=%1,@rev"
75193323Sed			 : "=r" (__r) : "r"(_x));
76193323Sed	return __r;
77239462Sdim}
78239462Sdim
79239462Sdimstatic __inline __uint32_t
80239462Sdim__bswap32(__uint32_t _x)
81207618Srdivacky{
82193323Sed
83193323Sed	return (__bswap64(_x) >> 32);
84193323Sed}
85193323Sed
86193323Sedstatic __inline __uint16_t
87193323Sed__bswap16(__uint16_t _x)
88193323Sed{
89193323Sed
90193323Sed	return (__bswap64(_x) >> 48);
91193323Sed}
92193323Sed
93193323Sed#define	__htonl(x)	__bswap32(x)
94193323Sed#define	__htons(x)	__bswap16(x)
95193323Sed#define	__ntohl(x)	__bswap32(x)
96193323Sed#define	__ntohs(x)	__bswap16(x)
97193323Sed
98193323Sed#else /* !__GNUC__ */
99193323Sed
100193323Sed/*
101193323Sed * No optimizations are available for this compiler.  Fall back to
102193323Sed * non-optimized functions by defining the constant usually used to prevent
103193323Sed * redefinition.
104193323Sed */
105193323Sed#define	_BYTEORDER_FUNC_DEFINED
106193323Sed
107193323Sed#endif /* __GNUC__ */
108193323Sed
109193323Sed#endif /* !_MACHINE_ENDIAN_H_ */
110203954Srdivacky