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