endian.h revision 4
14Srgrimes/* 24Srgrimes * Copyright (c) 1987, 1991 Regents of the University of California. 34Srgrimes * All rights reserved. 44Srgrimes * 54Srgrimes * Redistribution and use in source and binary forms, with or without 64Srgrimes * modification, are permitted provided that the following conditions 74Srgrimes * are met: 84Srgrimes * 1. Redistributions of source code must retain the above copyright 94Srgrimes * notice, this list of conditions and the following disclaimer. 104Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 114Srgrimes * notice, this list of conditions and the following disclaimer in the 124Srgrimes * documentation and/or other materials provided with the distribution. 134Srgrimes * 3. All advertising materials mentioning features or use of this software 144Srgrimes * must display the following acknowledgement: 154Srgrimes * This product includes software developed by the University of 164Srgrimes * California, Berkeley and its contributors. 174Srgrimes * 4. Neither the name of the University nor the names of its contributors 184Srgrimes * may be used to endorse or promote products derived from this software 194Srgrimes * without specific prior written permission. 204Srgrimes * 214Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 224Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 234Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 244Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 254Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 264Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 274Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 284Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 294Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 304Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 314Srgrimes * SUCH DAMAGE. 324Srgrimes * 334Srgrimes * @(#)endian.h 7.8 (Berkeley) 4/3/91 344Srgrimes * 354Srgrimes * PATCHES MAGIC LEVEL PATCH THAT GOT US HERE 364Srgrimes * -------------------- ----- ---------------------- 374Srgrimes * CURRENT PATCH LEVEL: 1 00093 384Srgrimes * -------------------- ----- ---------------------- 394Srgrimes * 404Srgrimes * 27 Feb 93 Charles Hannum Better byte-swapping macros for 414Srgrimes * i386/i486. 424Srgrimes */ 434Srgrimes 444Srgrimes/* 454Srgrimes * Definitions for byte order, according to byte significance from low 464Srgrimes * address to high. 474Srgrimes */ 484Srgrimes#define LITTLE_ENDIAN 1234 /* LSB first: i386, vax */ 494Srgrimes#define BIG_ENDIAN 4321 /* MSB first: 68000, ibm, net */ 504Srgrimes#define PDP_ENDIAN 3412 /* LSB first in word, MSW first in long */ 514Srgrimes 524Srgrimes#define BYTE_ORDER LITTLE_ENDIAN 534Srgrimes 544Srgrimes#ifndef KERNEL 554Srgrimes#include <sys/cdefs.h> 564Srgrimes#endif 574Srgrimes 584Srgrimes#define __word_swap_long(x) \ 594Srgrimes({ register u_long X = (x); \ 604Srgrimes asm ("rorl $16, %1" \ 614Srgrimes : "=r" (X) \ 624Srgrimes : "0" (X)); \ 634Srgrimes X; }) 644Srgrimes#if __GNUC__ >= 2 654Srgrimes#define __byte_swap_long(x) \ 664Srgrimes({ register u_long X = (x); \ 674Srgrimes asm ("xchgb %h1, %b1\n\trorl $16, %1\n\txchgb %h1, %b1" \ 684Srgrimes : "=q" (X) \ 694Srgrimes : "0" (X)); \ 704Srgrimes X; }) 714Srgrimes#define __byte_swap_word(x) \ 724Srgrimes({ register u_short X = (x); \ 734Srgrimes asm ("xchgb %h1, %b1" \ 744Srgrimes : "=q" (X) \ 754Srgrimes : "0" (X)); \ 764Srgrimes X; }) 774Srgrimes#else /* __GNUC__ >= 2 */ 784Srgrimes#define __byte_swap_long(x) \ 794Srgrimes({ register u_long X = (x); \ 804Srgrimes asm ("rorw $8, %w1\n\trorl $16, %1\n\trorw $8, %w1" \ 814Srgrimes : "=r" (X) \ 824Srgrimes : "0" (X)); \ 834Srgrimes X; }) 844Srgrimes#define __byte_swap_word(x) \ 854Srgrimes({ register u_short X = (x); \ 864Srgrimes asm ("rorw $8, %w1" \ 874Srgrimes : "=r" (X) \ 884Srgrimes : "0" (X)); \ 894Srgrimes X; }) 904Srgrimes#endif /* __GNUC__ >= 2 */ 914Srgrimes 924Srgrimes/* 934Srgrimes * Macros for network/external number representation conversion. 944Srgrimes */ 954Srgrimes#if BYTE_ORDER == BIG_ENDIAN && !defined(lint) 964Srgrimes#define ntohl(x) (x) 974Srgrimes#define ntohs(x) (x) 984Srgrimes#define htonl(x) (x) 994Srgrimes#define htons(x) (x) 1004Srgrimes 1014Srgrimes#define NTOHL(x) (x) 1024Srgrimes#define NTOHS(x) (x) 1034Srgrimes#define HTONL(x) (x) 1044Srgrimes#define HTONS(x) (x) 1054Srgrimes 1064Srgrimes#else 1074Srgrimes 1084Srgrimes#define ntohl __byte_swap_long 1094Srgrimes#define ntohs __byte_swap_word 1104Srgrimes#define htonl __byte_swap_long 1114Srgrimes#define htons __byte_swap_word 1124Srgrimes 1134Srgrimes#define NTOHL(x) (x) = ntohl((u_long)x) 1144Srgrimes#define NTOHS(x) (x) = ntohs((u_short)x) 1154Srgrimes#define HTONL(x) (x) = htonl((u_long)x) 1164Srgrimes#define HTONS(x) (x) = htons((u_short)x) 1174Srgrimes#endif 118