xref: /freebsd/sys/i386/include/endian.h (revision 5b81b6b301437eb9a6df491c829475bd29ae5d6c)
15b81b6b3SRodney W. Grimes /*
25b81b6b3SRodney W. Grimes  * Copyright (c) 1987, 1991 Regents of the University of California.
35b81b6b3SRodney W. Grimes  * All rights reserved.
45b81b6b3SRodney W. Grimes  *
55b81b6b3SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
65b81b6b3SRodney W. Grimes  * modification, are permitted provided that the following conditions
75b81b6b3SRodney W. Grimes  * are met:
85b81b6b3SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
95b81b6b3SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
105b81b6b3SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
115b81b6b3SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
125b81b6b3SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
135b81b6b3SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
145b81b6b3SRodney W. Grimes  *    must display the following acknowledgement:
155b81b6b3SRodney W. Grimes  *	This product includes software developed by the University of
165b81b6b3SRodney W. Grimes  *	California, Berkeley and its contributors.
175b81b6b3SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
185b81b6b3SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
195b81b6b3SRodney W. Grimes  *    without specific prior written permission.
205b81b6b3SRodney W. Grimes  *
215b81b6b3SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
225b81b6b3SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
235b81b6b3SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
245b81b6b3SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
255b81b6b3SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
265b81b6b3SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
275b81b6b3SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
285b81b6b3SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
295b81b6b3SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
305b81b6b3SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
315b81b6b3SRodney W. Grimes  * SUCH DAMAGE.
325b81b6b3SRodney W. Grimes  *
335b81b6b3SRodney W. Grimes  *	@(#)endian.h	7.8 (Berkeley) 4/3/91
345b81b6b3SRodney W. Grimes  *
355b81b6b3SRodney W. Grimes  * PATCHES MAGIC                LEVEL   PATCH THAT GOT US HERE
365b81b6b3SRodney W. Grimes  * --------------------         -----   ----------------------
375b81b6b3SRodney W. Grimes  * CURRENT PATCH LEVEL:         1       00093
385b81b6b3SRodney W. Grimes  * --------------------         -----   ----------------------
395b81b6b3SRodney W. Grimes  *
405b81b6b3SRodney W. Grimes  * 27 Feb 93    Charles Hannum		Better byte-swapping macros for
415b81b6b3SRodney W. Grimes  *					i386/i486.
425b81b6b3SRodney W. Grimes  */
435b81b6b3SRodney W. Grimes 
445b81b6b3SRodney W. Grimes /*
455b81b6b3SRodney W. Grimes  * Definitions for byte order, according to byte significance from low
465b81b6b3SRodney W. Grimes  * address to high.
475b81b6b3SRodney W. Grimes  */
485b81b6b3SRodney W. Grimes #define	LITTLE_ENDIAN	1234	/* LSB first: i386, vax */
495b81b6b3SRodney W. Grimes #define	BIG_ENDIAN	4321	/* MSB first: 68000, ibm, net */
505b81b6b3SRodney W. Grimes #define	PDP_ENDIAN	3412	/* LSB first in word, MSW first in long */
515b81b6b3SRodney W. Grimes 
525b81b6b3SRodney W. Grimes #define	BYTE_ORDER	LITTLE_ENDIAN
535b81b6b3SRodney W. Grimes 
545b81b6b3SRodney W. Grimes #ifndef KERNEL
555b81b6b3SRodney W. Grimes #include <sys/cdefs.h>
565b81b6b3SRodney W. Grimes #endif
575b81b6b3SRodney W. Grimes 
585b81b6b3SRodney W. Grimes #define __word_swap_long(x) \
595b81b6b3SRodney W. Grimes ({ register u_long X = (x); \
605b81b6b3SRodney W. Grimes    asm ("rorl $16, %1" \
615b81b6b3SRodney W. Grimes 	: "=r" (X) \
625b81b6b3SRodney W. Grimes 	: "0" (X)); \
635b81b6b3SRodney W. Grimes    X; })
645b81b6b3SRodney W. Grimes #if __GNUC__ >= 2
655b81b6b3SRodney W. Grimes #define __byte_swap_long(x) \
665b81b6b3SRodney W. Grimes ({ register u_long X = (x); \
675b81b6b3SRodney W. Grimes    asm ("xchgb %h1, %b1\n\trorl $16, %1\n\txchgb %h1, %b1" \
685b81b6b3SRodney W. Grimes 	: "=q" (X) \
695b81b6b3SRodney W. Grimes 	: "0" (X)); \
705b81b6b3SRodney W. Grimes    X; })
715b81b6b3SRodney W. Grimes #define __byte_swap_word(x) \
725b81b6b3SRodney W. Grimes ({ register u_short X = (x); \
735b81b6b3SRodney W. Grimes    asm ("xchgb %h1, %b1" \
745b81b6b3SRodney W. Grimes 	: "=q" (X) \
755b81b6b3SRodney W. Grimes 	: "0" (X)); \
765b81b6b3SRodney W. Grimes    X; })
775b81b6b3SRodney W. Grimes #else /* __GNUC__ >= 2 */
785b81b6b3SRodney W. Grimes #define __byte_swap_long(x) \
795b81b6b3SRodney W. Grimes ({ register u_long X = (x); \
805b81b6b3SRodney W. Grimes    asm ("rorw $8, %w1\n\trorl $16, %1\n\trorw $8, %w1" \
815b81b6b3SRodney W. Grimes 	: "=r" (X) \
825b81b6b3SRodney W. Grimes 	: "0" (X)); \
835b81b6b3SRodney W. Grimes    X; })
845b81b6b3SRodney W. Grimes #define __byte_swap_word(x) \
855b81b6b3SRodney W. Grimes ({ register u_short X = (x); \
865b81b6b3SRodney W. Grimes    asm ("rorw $8, %w1" \
875b81b6b3SRodney W. Grimes 	: "=r" (X) \
885b81b6b3SRodney W. Grimes 	: "0" (X)); \
895b81b6b3SRodney W. Grimes    X; })
905b81b6b3SRodney W. Grimes #endif /* __GNUC__ >= 2 */
915b81b6b3SRodney W. Grimes 
925b81b6b3SRodney W. Grimes /*
935b81b6b3SRodney W. Grimes  * Macros for network/external number representation conversion.
945b81b6b3SRodney W. Grimes  */
955b81b6b3SRodney W. Grimes #if BYTE_ORDER == BIG_ENDIAN && !defined(lint)
965b81b6b3SRodney W. Grimes #define	ntohl(x)	(x)
975b81b6b3SRodney W. Grimes #define	ntohs(x)	(x)
985b81b6b3SRodney W. Grimes #define	htonl(x)	(x)
995b81b6b3SRodney W. Grimes #define	htons(x)	(x)
1005b81b6b3SRodney W. Grimes 
1015b81b6b3SRodney W. Grimes #define	NTOHL(x)	(x)
1025b81b6b3SRodney W. Grimes #define	NTOHS(x)	(x)
1035b81b6b3SRodney W. Grimes #define	HTONL(x)	(x)
1045b81b6b3SRodney W. Grimes #define	HTONS(x)	(x)
1055b81b6b3SRodney W. Grimes 
1065b81b6b3SRodney W. Grimes #else
1075b81b6b3SRodney W. Grimes 
1085b81b6b3SRodney W. Grimes #define	ntohl	__byte_swap_long
1095b81b6b3SRodney W. Grimes #define	ntohs	__byte_swap_word
1105b81b6b3SRodney W. Grimes #define	htonl	__byte_swap_long
1115b81b6b3SRodney W. Grimes #define	htons	__byte_swap_word
1125b81b6b3SRodney W. Grimes 
1135b81b6b3SRodney W. Grimes #define	NTOHL(x)	(x) = ntohl((u_long)x)
1145b81b6b3SRodney W. Grimes #define	NTOHS(x)	(x) = ntohs((u_short)x)
1155b81b6b3SRodney W. Grimes #define	HTONL(x)	(x) = htonl((u_long)x)
1165b81b6b3SRodney W. Grimes #define	HTONS(x)	(x) = htons((u_short)x)
1175b81b6b3SRodney W. Grimes #endif
118