Home | History | Annotate | Line # | Download | only in machine
endian.h revision 1.2
      1  1.2  uwe /*	$NetBSD: endian.h,v 1.2 2005/05/11 16:44:45 uwe Exp $	*/
      2  1.1  uch 
      3  1.1  uch /* Windows CE architecture */
      4  1.1  uch 
      5  1.2  uwe /*
      6  1.2  uwe  * This file should be just:
      7  1.2  uwe 
      8  1.1  uch #include <sys/endian.h>
      9  1.2  uwe 
     10  1.2  uwe  * but our <sys/endian.h> is no longer compilable with eVC3 and
     11  1.2  uwe  * probably other old WinCE compilers because they don't grok ULL
     12  1.2  uwe  * constant suffix.
     13  1.2  uwe  *
     14  1.2  uwe  * Instead of polluting sys/endian.h with WinCE compatibility
     15  1.2  uwe  * ugliness, pull a copy here, so that we can hack it privately.
     16  1.2  uwe  */
     17  1.2  uwe 
     18  1.2  uwe /*	From: NetBSD: endian.h,v 1.15 2005/02/03 19:16:10 perry Exp	*/
     19  1.2  uwe 
     20  1.2  uwe /*
     21  1.2  uwe  * Copyright (c) 1987, 1991, 1993
     22  1.2  uwe  *	The Regents of the University of California.  All rights reserved.
     23  1.2  uwe  *
     24  1.2  uwe  * Redistribution and use in source and binary forms, with or without
     25  1.2  uwe  * modification, are permitted provided that the following conditions
     26  1.2  uwe  * are met:
     27  1.2  uwe  * 1. Redistributions of source code must retain the above copyright
     28  1.2  uwe  *    notice, this list of conditions and the following disclaimer.
     29  1.2  uwe  * 2. Redistributions in binary form must reproduce the above copyright
     30  1.2  uwe  *    notice, this list of conditions and the following disclaimer in the
     31  1.2  uwe  *    documentation and/or other materials provided with the distribution.
     32  1.2  uwe  * 3. Neither the name of the University nor the names of its contributors
     33  1.2  uwe  *    may be used to endorse or promote products derived from this software
     34  1.2  uwe  *    without specific prior written permission.
     35  1.2  uwe  *
     36  1.2  uwe  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     37  1.2  uwe  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     38  1.2  uwe  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     39  1.2  uwe  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     40  1.2  uwe  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     41  1.2  uwe  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     42  1.2  uwe  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     43  1.2  uwe  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     44  1.2  uwe  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     45  1.2  uwe  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     46  1.2  uwe  * SUCH DAMAGE.
     47  1.2  uwe  *
     48  1.2  uwe  *	@(#)endian.h	8.1 (Berkeley) 6/11/93
     49  1.2  uwe  */
     50  1.2  uwe 
     51  1.2  uwe #ifndef _SYS_ENDIAN_H_
     52  1.2  uwe #define _SYS_ENDIAN_H_
     53  1.2  uwe 
     54  1.2  uwe #include <sys/featuretest.h>
     55  1.2  uwe 
     56  1.2  uwe /*
     57  1.2  uwe  * Definitions for byte order, according to byte significance from low
     58  1.2  uwe  * address to high.
     59  1.2  uwe  */
     60  1.2  uwe #define	_LITTLE_ENDIAN	1234	/* LSB first: i386, vax */
     61  1.2  uwe #define	_BIG_ENDIAN	4321	/* MSB first: 68000, ibm, net */
     62  1.2  uwe #define	_PDP_ENDIAN	3412	/* LSB first in word, MSW first in long */
     63  1.2  uwe 
     64  1.2  uwe 
     65  1.2  uwe #if defined(_XOPEN_SOURCE) || defined(_NETBSD_SOURCE)
     66  1.2  uwe #ifndef _LOCORE
     67  1.2  uwe 
     68  1.2  uwe /* C-family endian-ness definitions */
     69  1.2  uwe 
     70  1.2  uwe #include <sys/ansi.h>
     71  1.2  uwe #include <sys/cdefs.h>
     72  1.2  uwe #include <sys/types.h>
     73  1.2  uwe 
     74  1.2  uwe #ifndef in_addr_t
     75  1.2  uwe typedef __in_addr_t	in_addr_t;
     76  1.2  uwe #define	in_addr_t	__in_addr_t
     77  1.2  uwe #endif
     78  1.2  uwe 
     79  1.2  uwe #ifndef in_port_t
     80  1.2  uwe typedef __in_port_t	in_port_t;
     81  1.2  uwe #define	in_port_t	__in_port_t
     82  1.2  uwe #endif
     83  1.2  uwe 
     84  1.2  uwe __BEGIN_DECLS
     85  1.2  uwe uint32_t htonl(uint32_t) __attribute__((__const__));
     86  1.2  uwe uint16_t htons(uint16_t) __attribute__((__const__));
     87  1.2  uwe uint32_t ntohl(uint32_t) __attribute__((__const__));
     88  1.2  uwe uint16_t ntohs(uint16_t) __attribute__((__const__));
     89  1.2  uwe __END_DECLS
     90  1.2  uwe 
     91  1.2  uwe #endif /* !_LOCORE */
     92  1.2  uwe #endif /* _XOPEN_SOURCE || _NETBSD_SOURCE */
     93  1.2  uwe 
     94  1.2  uwe 
     95  1.2  uwe #include <machine/endian_machdep.h>
     96  1.2  uwe 
     97  1.2  uwe /*
     98  1.2  uwe  * Define the order of 32-bit words in 64-bit words.
     99  1.2  uwe  */
    100  1.2  uwe #if _BYTE_ORDER == _LITTLE_ENDIAN
    101  1.2  uwe #define _QUAD_HIGHWORD 1
    102  1.2  uwe #define _QUAD_LOWWORD 0
    103  1.2  uwe #endif
    104  1.2  uwe 
    105  1.2  uwe #if _BYTE_ORDER == _BIG_ENDIAN
    106  1.2  uwe #define _QUAD_HIGHWORD 0
    107  1.2  uwe #define _QUAD_LOWWORD 1
    108  1.2  uwe #endif
    109  1.2  uwe 
    110  1.2  uwe 
    111  1.2  uwe #if defined(_XOPEN_SOURCE) || defined(_NETBSD_SOURCE)
    112  1.2  uwe /*
    113  1.2  uwe  *  Traditional names for byteorder.  These are defined as the numeric
    114  1.2  uwe  *  sequences so that third party code can "#define XXX_ENDIAN" and not
    115  1.2  uwe  *  cause errors.
    116  1.2  uwe  */
    117  1.2  uwe #define	LITTLE_ENDIAN	1234		/* LSB first: i386, vax */
    118  1.2  uwe #define	BIG_ENDIAN	4321		/* MSB first: 68000, ibm, net */
    119  1.2  uwe #define	PDP_ENDIAN	3412		/* LSB first in word, MSW first in long */
    120  1.2  uwe #define BYTE_ORDER	_BYTE_ORDER
    121  1.2  uwe 
    122  1.2  uwe #ifndef _LOCORE
    123  1.2  uwe 
    124  1.2  uwe /*
    125  1.2  uwe  * Macros for network/external number representation conversion.
    126  1.2  uwe  */
    127  1.2  uwe #if BYTE_ORDER == BIG_ENDIAN && !defined(__lint__)
    128  1.2  uwe #define	ntohl(x)	(x)
    129  1.2  uwe #define	ntohs(x)	(x)
    130  1.2  uwe #define	htonl(x)	(x)
    131  1.2  uwe #define	htons(x)	(x)
    132  1.2  uwe 
    133  1.2  uwe #define	NTOHL(x)	(void) (x)
    134  1.2  uwe #define	NTOHS(x)	(void) (x)
    135  1.2  uwe #define	HTONL(x)	(void) (x)
    136  1.2  uwe #define	HTONS(x)	(void) (x)
    137  1.2  uwe 
    138  1.2  uwe #else	/* LITTLE_ENDIAN || !defined(__lint__) */
    139  1.2  uwe 
    140  1.2  uwe #define	NTOHL(x)	(x) = ntohl((uint32_t)(x))
    141  1.2  uwe #define	NTOHS(x)	(x) = ntohs((uint16_t)(x))
    142  1.2  uwe #define	HTONL(x)	(x) = htonl((uint32_t)(x))
    143  1.2  uwe #define	HTONS(x)	(x) = htons((uint16_t)(x))
    144  1.2  uwe #endif	/* LITTLE_ENDIAN || !defined(__lint__) */
    145  1.2  uwe 
    146  1.2  uwe /*
    147  1.2  uwe  * Macros to convert to a specific endianness.
    148  1.2  uwe  */
    149  1.2  uwe 
    150  1.2  uwe #include <machine/bswap.h>
    151  1.2  uwe 
    152  1.2  uwe #if BYTE_ORDER == BIG_ENDIAN
    153  1.2  uwe 
    154  1.2  uwe #define htobe16(x)	(x)
    155  1.2  uwe #define htobe32(x)	(x)
    156  1.2  uwe #define htobe64(x)	(x)
    157  1.2  uwe #define htole16(x)	bswap16((u_int16_t)(x))
    158  1.2  uwe #define htole32(x)	bswap32((u_int32_t)(x))
    159  1.2  uwe #define htole64(x)	bswap64((u_int64_t)(x))
    160  1.2  uwe 
    161  1.2  uwe #define HTOBE16(x)	(void) (x)
    162  1.2  uwe #define HTOBE32(x)	(void) (x)
    163  1.2  uwe #define HTOBE64(x)	(void) (x)
    164  1.2  uwe #define HTOLE16(x)	(x) = bswap16((u_int16_t)(x))
    165  1.2  uwe #define HTOLE32(x)	(x) = bswap32((u_int32_t)(x))
    166  1.2  uwe #define HTOLE64(x)	(x) = bswap64((u_int64_t)(x))
    167  1.2  uwe 
    168  1.2  uwe #else	/* LITTLE_ENDIAN */
    169  1.2  uwe 
    170  1.2  uwe #define htobe16(x)	bswap16((u_int16_t)(x))
    171  1.2  uwe #define htobe32(x)	bswap32((u_int32_t)(x))
    172  1.2  uwe #define htobe64(x)	bswap64((u_int64_t)(x))
    173  1.2  uwe #define htole16(x)	(x)
    174  1.2  uwe #define htole32(x)	(x)
    175  1.2  uwe #define htole64(x)	(x)
    176  1.2  uwe 
    177  1.2  uwe #define HTOBE16(x)	(x) = bswap16((u_int16_t)(x))
    178  1.2  uwe #define HTOBE32(x)	(x) = bswap32((u_int32_t)(x))
    179  1.2  uwe #define HTOBE64(x)	(x) = bswap64((u_int64_t)(x))
    180  1.2  uwe #define HTOLE16(x)	(void) (x)
    181  1.2  uwe #define HTOLE32(x)	(void) (x)
    182  1.2  uwe #define HTOLE64(x)	(void) (x)
    183  1.2  uwe 
    184  1.2  uwe #endif	/* LITTLE_ENDIAN */
    185  1.2  uwe 
    186  1.2  uwe #define be16toh(x)	htobe16(x)
    187  1.2  uwe #define be32toh(x)	htobe32(x)
    188  1.2  uwe #define be64toh(x)	htobe64(x)
    189  1.2  uwe #define le16toh(x)	htole16(x)
    190  1.2  uwe #define le32toh(x)	htole32(x)
    191  1.2  uwe #define le64toh(x)	htole64(x)
    192  1.2  uwe 
    193  1.2  uwe #define BE16TOH(x)	HTOBE16(x)
    194  1.2  uwe #define BE32TOH(x)	HTOBE32(x)
    195  1.2  uwe #define BE64TOH(x)	HTOBE64(x)
    196  1.2  uwe #define LE16TOH(x)	HTOLE16(x)
    197  1.2  uwe #define LE32TOH(x)	HTOLE32(x)
    198  1.2  uwe #define LE64TOH(x)	HTOLE64(x)
    199  1.2  uwe 
    200  1.2  uwe /*
    201  1.2  uwe  * Routines to encode/decode big- and little-endian multi-octet values
    202  1.2  uwe  * to/from an octet stream.
    203  1.2  uwe  */
    204  1.2  uwe 
    205  1.2  uwe static __inline void __unused
    206  1.2  uwe be16enc(void *buf, uint16_t u)
    207  1.2  uwe {
    208  1.2  uwe 	uint8_t *p = (uint8_t *)buf;
    209  1.2  uwe 
    210  1.2  uwe 	p[0] = ((unsigned)u >> 8) & 0xff;
    211  1.2  uwe 	p[1] = u & 0xff;
    212  1.2  uwe }
    213  1.2  uwe 
    214  1.2  uwe static __inline void __unused
    215  1.2  uwe le16enc(void *buf, uint16_t u)
    216  1.2  uwe {
    217  1.2  uwe 	uint8_t *p = (uint8_t *)buf;
    218  1.2  uwe 
    219  1.2  uwe 	p[0] = u & 0xff;
    220  1.2  uwe 	p[1] = ((unsigned)u >> 8) & 0xff;
    221  1.2  uwe }
    222  1.2  uwe 
    223  1.2  uwe static __inline uint16_t __unused
    224  1.2  uwe be16dec(const void *buf)
    225  1.2  uwe {
    226  1.2  uwe 	const uint8_t *p = (const uint8_t *)buf;
    227  1.2  uwe 
    228  1.2  uwe 	return ((p[0] << 8) | p[1]);
    229  1.2  uwe }
    230  1.2  uwe 
    231  1.2  uwe static __inline uint16_t __unused
    232  1.2  uwe le16dec(const void *buf)
    233  1.2  uwe {
    234  1.2  uwe 	const uint8_t *p = (const uint8_t *)buf;
    235  1.2  uwe 
    236  1.2  uwe 	return ((p[1] << 8) | p[0]);
    237  1.2  uwe }
    238  1.2  uwe 
    239  1.2  uwe static __inline void __unused
    240  1.2  uwe be32enc(void *buf, uint32_t u)
    241  1.2  uwe {
    242  1.2  uwe 	uint8_t *p = (uint8_t *)buf;
    243  1.2  uwe 
    244  1.2  uwe 	p[0] = (u >> 24) & 0xff;
    245  1.2  uwe 	p[1] = (u >> 16) & 0xff;
    246  1.2  uwe 	p[2] = (u >> 8) & 0xff;
    247  1.2  uwe 	p[3] = u & 0xff;
    248  1.2  uwe }
    249  1.2  uwe 
    250  1.2  uwe static __inline void __unused
    251  1.2  uwe le32enc(void *buf, uint32_t u)
    252  1.2  uwe {
    253  1.2  uwe 	uint8_t *p = (uint8_t *)buf;
    254  1.2  uwe 
    255  1.2  uwe 	p[0] = u & 0xff;
    256  1.2  uwe 	p[1] = (u >> 8) & 0xff;
    257  1.2  uwe 	p[2] = (u >> 16) & 0xff;
    258  1.2  uwe 	p[3] = (u >> 24) & 0xff;
    259  1.2  uwe }
    260  1.2  uwe 
    261  1.2  uwe static __inline uint32_t __unused
    262  1.2  uwe be32dec(const void *buf)
    263  1.2  uwe {
    264  1.2  uwe 	const uint8_t *p = (const uint8_t *)buf;
    265  1.2  uwe 
    266  1.2  uwe 	return ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
    267  1.2  uwe }
    268  1.2  uwe 
    269  1.2  uwe static __inline uint32_t __unused
    270  1.2  uwe le32dec(const void *buf)
    271  1.2  uwe {
    272  1.2  uwe 	const uint8_t *p = (const uint8_t *)buf;
    273  1.2  uwe 
    274  1.2  uwe 	return ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
    275  1.2  uwe }
    276  1.2  uwe 
    277  1.2  uwe 
    278  1.2  uwe /*
    279  1.2  uwe  * XXX: uwe: ULL suffix makes eVC unhappy.  Since nothing in hpcboot
    280  1.2  uwe  * needs 64-bit enc/dec routines - just ifdef them out for now.
    281  1.2  uwe  */
    282  1.2  uwe #ifndef _WIN32
    283  1.2  uwe 
    284  1.2  uwe static __inline void __unused
    285  1.2  uwe be64enc(void *buf, uint64_t u)
    286  1.2  uwe {
    287  1.2  uwe 	uint8_t *p = (uint8_t *)buf;
    288  1.2  uwe 
    289  1.2  uwe 	be32enc(p, (uint32_t)(u >> 32));
    290  1.2  uwe 	be32enc(p + 4, (uint32_t)(u & 0xffffffffULL));
    291  1.2  uwe }
    292  1.2  uwe 
    293  1.2  uwe static __inline void __unused
    294  1.2  uwe le64enc(void *buf, uint64_t u)
    295  1.2  uwe {
    296  1.2  uwe 	uint8_t *p = (uint8_t *)buf;
    297  1.2  uwe 
    298  1.2  uwe 	le32enc(p, (uint32_t)(u & 0xffffffffULL));
    299  1.2  uwe 	le32enc(p + 4, (uint32_t)(u >> 32));
    300  1.2  uwe }
    301  1.2  uwe 
    302  1.2  uwe static __inline uint64_t __unused
    303  1.2  uwe be64dec(const void *buf)
    304  1.2  uwe {
    305  1.2  uwe 	const uint8_t *p = (const uint8_t *)buf;
    306  1.2  uwe 
    307  1.2  uwe 	return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4));
    308  1.2  uwe }
    309  1.2  uwe 
    310  1.2  uwe static __inline uint64_t __unused
    311  1.2  uwe le64dec(const void *buf)
    312  1.2  uwe {
    313  1.2  uwe 	const uint8_t *p = (const uint8_t *)buf;
    314  1.2  uwe 
    315  1.2  uwe 	return (le32dec(p) | ((uint64_t)le32dec(p + 4) << 32));
    316  1.2  uwe }
    317  1.2  uwe #endif /* !_WIN32 */
    318  1.2  uwe 
    319  1.2  uwe #endif /* !_LOCORE */
    320  1.2  uwe #endif /* _XOPEN_SOURCE || _NETBSD_SOURCE */
    321  1.2  uwe #endif /* !_SYS_ENDIAN_H_ */
    322