Home | History | Annotate | Line # | Download | only in include
endian.h revision 1.25
      1 /*	$NetBSD: endian.h,v 1.25 1999/01/15 13:31:24 bouyer 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 __END_DECLS
    106 
    107 
    108 #ifdef __GNUC__
    109 
    110 #include <machine/byte_swap.h>
    111 
    112 #define	ntohl(x)	((in_addr_t)__byte_swap_long((in_addr_t)(x)))
    113 #define	ntohs(x)	((in_port_t)__byte_swap_word((in_port_t)(x)))
    114 #define	htonl(x)	((in_addr_t)__byte_swap_long((in_addr_t)(x)))
    115 #define	htons(x)	((in_port_t)__byte_swap_word((in_port_t)(x)))
    116 
    117 #endif	/* __GNUC__ */
    118 
    119 
    120 /*
    121  * Macros for network/external number representation conversion.
    122  */
    123 #define	NTOHL(x)	(x) = ntohl((in_addr_t)(x))
    124 #define	NTOHS(x)	(x) = ntohs((in_port_t)(x))
    125 #define	HTONL(x)	(x) = htonl((in_addr_t)(x))
    126 #define	HTONS(x)	(x) = htons((in_port_t)(x))
    127 
    128 #endif /* _POSIX_SOURCE */
    129 
    130 #endif /* !_I386_ENDIAN_H_ */
    131