Home | History | Annotate | Line # | Download | only in include
endian.h revision 1.20.2.1
      1 /*	$NetBSD: endian.h,v 1.20.2.1 1997/10/14 09:10:01 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995 Charles M. Hannum.  All rights reserved.
      5  * Copyright (c) 1987, 1991 Regents of the University of California.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the University of
     19  *	California, Berkeley and its contributors.
     20  * 4. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  *	@(#)endian.h	7.8 (Berkeley) 4/3/91
     37  */
     38 
     39 #ifndef _I386_ENDIAN_H_
     40 #define	_I386_ENDIAN_H_
     41 
     42 /*
     43  * Define the order of 32-bit words in 64-bit words.
     44  */
     45 #define _QUAD_HIGHWORD 1
     46 #define _QUAD_LOWWORD 0
     47 
     48 
     49 #ifndef _POSIX_SOURCE
     50 /*
     51  * Definitions for byte order, according to byte significance from low
     52  * address to high.
     53  */
     54 #define	LITTLE_ENDIAN	1234	/* LSB first: i386, vax */
     55 #define	BIG_ENDIAN	4321	/* MSB first: 68000, ibm, net */
     56 #define	PDP_ENDIAN	3412	/* LSB first in word, MSW first in long */
     57 
     58 #define	BYTE_ORDER	LITTLE_ENDIAN
     59 
     60 #include <sys/cdefs.h>
     61 
     62 typedef u_int32_t	in_addr_t;
     63 typedef u_int16_t	in_port_t;
     64 
     65 __BEGIN_DECLS
     66 in_addr_t htonl __P((in_addr_t));
     67 in_port_t htons __P((in_port_t));
     68 in_addr_t ntohl __P((in_addr_t));
     69 in_port_t ntohs __P((in_port_t));
     70 u_int16_t bswap16 __P((u_int16_t));
     71 u_int32_t bswap32 __P((u_int32_t));
     72 u_int64_t bswap64 __P((u_int64_t));
     73 __END_DECLS
     74 
     75 
     76 #ifdef __GNUC__
     77 
     78 #if defined(_KERNEL) && !defined(I386_CPU)
     79 #define	__byte_swap_long_variable(x) \
     80 ({ register in_addr_t __x = (x); \
     81    __asm ("bswap %1" \
     82 	: "=r" (__x) \
     83 	: "0" (__x)); \
     84    __x; })
     85 #else
     86 #define	__byte_swap_long_variable(x) \
     87 ({ register in_addr_t __x = (x); \
     88    __asm ("rorw $8, %w1\n\trorl $16, %1\n\trorw $8, %w1" \
     89 	: "=r" (__x) \
     90 	: "0" (__x)); \
     91    __x; })
     92 #endif	/* _KERNEL && ... */
     93 
     94 #define	__byte_swap_word_variable(x) \
     95 ({ register in_port_t __x = (x); \
     96    __asm ("rorw $8, %w1" \
     97 	: "=r" (__x) \
     98 	: "0" (__x)); \
     99    __x; })
    100 
    101 #ifdef __OPTIMIZE__
    102 
    103 #define	__byte_swap_long_constant(x) \
    104 	((((x) & 0xff000000) >> 24) | \
    105 	 (((x) & 0x00ff0000) >>  8) | \
    106 	 (((x) & 0x0000ff00) <<  8) | \
    107 	 (((x) & 0x000000ff) << 24))
    108 #define	__byte_swap_word_constant(x) \
    109 	((((x) & 0xff00) >> 8) | \
    110 	 (((x) & 0x00ff) << 8))
    111 #define	__byte_swap_long(x) \
    112 	(__builtin_constant_p((x)) ? \
    113 	 __byte_swap_long_constant(x) : __byte_swap_long_variable(x))
    114 #define	__byte_swap_word(x) \
    115 	(__builtin_constant_p((x)) ? \
    116 	 __byte_swap_word_constant(x) : __byte_swap_word_variable(x))
    117 
    118 #else /* __OPTIMIZE__ */
    119 
    120 #define	__byte_swap_long(x)	__byte_swap_long_variable(x)
    121 #define	__byte_swap_word(x)	__byte_swap_word_variable(x)
    122 
    123 #endif /* __OPTIMIZE__ */
    124 
    125 #define	ntohl(x)	((in_addr_t)__byte_swap_long((in_addr_t)(x)))
    126 #define	ntohs(x)	((in_port_t)__byte_swap_word((in_port_t)(x)))
    127 #define	htonl(x)	((in_addr_t)__byte_swap_long((in_addr_t)(x)))
    128 #define	htons(x)	((in_port_t)__byte_swap_word((in_port_t)(x)))
    129 
    130 #endif	/* __GNUC__ */
    131 
    132 
    133 /*
    134  * Macros for network/external number representation conversion.
    135  */
    136 #define	NTOHL(x)	(x) = ntohl((in_addr_t)(x))
    137 #define	NTOHS(x)	(x) = ntohs((in_port_t)(x))
    138 #define	HTONL(x)	(x) = htonl((in_addr_t)(x))
    139 #define	HTONS(x)	(x) = htons((in_port_t)(x))
    140 
    141 #endif /* _POSIX_SOURCE */
    142 
    143 #endif /* !_I386_ENDIAN_H_ */
    144