endian.h revision 4
1219019Sgabor/*
2219019Sgabor * Copyright (c) 1987, 1991 Regents of the University of California.
3219019Sgabor * All rights reserved.
4219019Sgabor *
5219019Sgabor * Redistribution and use in source and binary forms, with or without
6219019Sgabor * modification, are permitted provided that the following conditions
7219019Sgabor * are met:
8219019Sgabor * 1. Redistributions of source code must retain the above copyright
9219019Sgabor *    notice, this list of conditions and the following disclaimer.
10219019Sgabor * 2. Redistributions in binary form must reproduce the above copyright
11219019Sgabor *    notice, this list of conditions and the following disclaimer in the
12219019Sgabor *    documentation and/or other materials provided with the distribution.
13219019Sgabor * 3. All advertising materials mentioning features or use of this software
14219019Sgabor *    must display the following acknowledgement:
15219019Sgabor *	This product includes software developed by the University of
16219019Sgabor *	California, Berkeley and its contributors.
17219019Sgabor * 4. Neither the name of the University nor the names of its contributors
18219019Sgabor *    may be used to endorse or promote products derived from this software
19219019Sgabor *    without specific prior written permission.
20219019Sgabor *
21219019Sgabor * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22219019Sgabor * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23219019Sgabor * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24219019Sgabor * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25219019Sgabor * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26219019Sgabor * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27219019Sgabor * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28219019Sgabor * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29219019Sgabor * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30219019Sgabor * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31219019Sgabor * SUCH DAMAGE.
32219019Sgabor *
33219019Sgabor *	@(#)endian.h	7.8 (Berkeley) 4/3/91
34219019Sgabor *
35219019Sgabor * PATCHES MAGIC                LEVEL   PATCH THAT GOT US HERE
36219019Sgabor * --------------------         -----   ----------------------
37219019Sgabor * CURRENT PATCH LEVEL:         1       00093
38219019Sgabor * --------------------         -----   ----------------------
39219019Sgabor *
40219019Sgabor * 27 Feb 93    Charles Hannum		Better byte-swapping macros for
41219019Sgabor *					i386/i486.
42219019Sgabor */
43219019Sgabor
44219019Sgabor/*
45219019Sgabor * Definitions for byte order, according to byte significance from low
46219019Sgabor * address to high.
47219019Sgabor */
48219019Sgabor#define	LITTLE_ENDIAN	1234	/* LSB first: i386, vax */
49219019Sgabor#define	BIG_ENDIAN	4321	/* MSB first: 68000, ibm, net */
50219019Sgabor#define	PDP_ENDIAN	3412	/* LSB first in word, MSW first in long */
51219019Sgabor
52219019Sgabor#define	BYTE_ORDER	LITTLE_ENDIAN
53219019Sgabor
54219019Sgabor#ifndef KERNEL
55219019Sgabor#include <sys/cdefs.h>
56219019Sgabor#endif
57219019Sgabor
58219019Sgabor#define __word_swap_long(x) \
59219019Sgabor({ register u_long X = (x); \
60219019Sgabor   asm ("rorl $16, %1" \
61219019Sgabor	: "=r" (X) \
62219019Sgabor	: "0" (X)); \
63219019Sgabor   X; })
64219019Sgabor#if __GNUC__ >= 2
65219019Sgabor#define __byte_swap_long(x) \
66219019Sgabor({ register u_long X = (x); \
67219019Sgabor   asm ("xchgb %h1, %b1\n\trorl $16, %1\n\txchgb %h1, %b1" \
68219019Sgabor	: "=q" (X) \
69219019Sgabor	: "0" (X)); \
70219019Sgabor   X; })
71219019Sgabor#define __byte_swap_word(x) \
72219019Sgabor({ register u_short X = (x); \
73219019Sgabor   asm ("xchgb %h1, %b1" \
74219019Sgabor	: "=q" (X) \
75219019Sgabor	: "0" (X)); \
76219019Sgabor   X; })
77219019Sgabor#else /* __GNUC__ >= 2 */
78219019Sgabor#define __byte_swap_long(x) \
79219019Sgabor({ register u_long X = (x); \
80219019Sgabor   asm ("rorw $8, %w1\n\trorl $16, %1\n\trorw $8, %w1" \
81219019Sgabor	: "=r" (X) \
82219019Sgabor	: "0" (X)); \
83219019Sgabor   X; })
84219019Sgabor#define __byte_swap_word(x) \
85219019Sgabor({ register u_short X = (x); \
86219019Sgabor   asm ("rorw $8, %w1" \
87219019Sgabor	: "=r" (X) \
88219019Sgabor	: "0" (X)); \
89219019Sgabor   X; })
90219019Sgabor#endif /* __GNUC__ >= 2 */
91219019Sgabor
92219019Sgabor/*
93219019Sgabor * Macros for network/external number representation conversion.
94219019Sgabor */
95219019Sgabor#if BYTE_ORDER == BIG_ENDIAN && !defined(lint)
96219019Sgabor#define	ntohl(x)	(x)
97219019Sgabor#define	ntohs(x)	(x)
98219019Sgabor#define	htonl(x)	(x)
99219019Sgabor#define	htons(x)	(x)
100219019Sgabor
101219019Sgabor#define	NTOHL(x)	(x)
102219019Sgabor#define	NTOHS(x)	(x)
103219019Sgabor#define	HTONL(x)	(x)
104219019Sgabor#define	HTONS(x)	(x)
105219019Sgabor
106219019Sgabor#else
107219019Sgabor
108219019Sgabor#define	ntohl	__byte_swap_long
109219019Sgabor#define	ntohs	__byte_swap_word
110219019Sgabor#define	htonl	__byte_swap_long
111219019Sgabor#define	htons	__byte_swap_word
112219019Sgabor
113219019Sgabor#define	NTOHL(x)	(x) = ntohl((u_long)x)
114219019Sgabor#define	NTOHS(x)	(x) = ntohs((u_short)x)
115219019Sgabor#define	HTONL(x)	(x) = htonl((u_long)x)
116219019Sgabor#define	HTONS(x)	(x) = htons((u_short)x)
117250984Sed#endif
118219019Sgabor