Home | History | Annotate | Line # | Download | only in include
endian.h revision 1.20
      1 /*	$NetBSD: endian.h,v 1.20 1997/07/17 18:44:08 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 __END_DECLS
     71 
     72 
     73 #ifdef __GNUC__
     74 
     75 #if defined(_KERNEL) && !defined(I386_CPU)
     76 #define	__byte_swap_long_variable(x) \
     77 ({ register in_addr_t __x = (x); \
     78    __asm ("bswap %1" \
     79 	: "=r" (__x) \
     80 	: "0" (__x)); \
     81    __x; })
     82 #else
     83 #define	__byte_swap_long_variable(x) \
     84 ({ register in_addr_t __x = (x); \
     85    __asm ("rorw $8, %w1\n\trorl $16, %1\n\trorw $8, %w1" \
     86 	: "=r" (__x) \
     87 	: "0" (__x)); \
     88    __x; })
     89 #endif	/* _KERNEL && ... */
     90 
     91 #define	__byte_swap_word_variable(x) \
     92 ({ register in_port_t __x = (x); \
     93    __asm ("rorw $8, %w1" \
     94 	: "=r" (__x) \
     95 	: "0" (__x)); \
     96    __x; })
     97 
     98 #ifdef __OPTIMIZE__
     99 
    100 #define	__byte_swap_long_constant(x) \
    101 	((((x) & 0xff000000) >> 24) | \
    102 	 (((x) & 0x00ff0000) >>  8) | \
    103 	 (((x) & 0x0000ff00) <<  8) | \
    104 	 (((x) & 0x000000ff) << 24))
    105 #define	__byte_swap_word_constant(x) \
    106 	((((x) & 0xff00) >> 8) | \
    107 	 (((x) & 0x00ff) << 8))
    108 #define	__byte_swap_long(x) \
    109 	(__builtin_constant_p((x)) ? \
    110 	 __byte_swap_long_constant(x) : __byte_swap_long_variable(x))
    111 #define	__byte_swap_word(x) \
    112 	(__builtin_constant_p((x)) ? \
    113 	 __byte_swap_word_constant(x) : __byte_swap_word_variable(x))
    114 
    115 #else /* __OPTIMIZE__ */
    116 
    117 #define	__byte_swap_long(x)	__byte_swap_long_variable(x)
    118 #define	__byte_swap_word(x)	__byte_swap_word_variable(x)
    119 
    120 #endif /* __OPTIMIZE__ */
    121 
    122 #define	ntohl(x)	((in_addr_t)__byte_swap_long((in_addr_t)(x)))
    123 #define	ntohs(x)	((in_port_t)__byte_swap_word((in_port_t)(x)))
    124 #define	htonl(x)	((in_addr_t)__byte_swap_long((in_addr_t)(x)))
    125 #define	htons(x)	((in_port_t)__byte_swap_word((in_port_t)(x)))
    126 
    127 #endif	/* __GNUC__ */
    128 
    129 
    130 /*
    131  * Macros for network/external number representation conversion.
    132  */
    133 #define	NTOHL(x)	(x) = ntohl((in_addr_t)(x))
    134 #define	NTOHS(x)	(x) = ntohs((in_port_t)(x))
    135 #define	HTONL(x)	(x) = htonl((in_addr_t)(x))
    136 #define	HTONS(x)	(x) = htons((in_port_t)(x))
    137 
    138 #endif /* _POSIX_SOURCE */
    139 
    140 #endif /* !_I386_ENDIAN_H_ */
    141