Home | History | Annotate | Line # | Download | only in machine
endian.h revision 1.8.14.1
      1  1.8.14.1   ad /*	$NetBSD: endian.h,v 1.8.14.1 2006/11/18 21:29:15 ad 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.7  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.8  uwe /*	From: NetBSD: endian.h,v 1.23 2006/02/04 01:07:20 uwe 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.8.14.1   ad uint32_t htonl(uint32_t) __attribute__((__const__));
     86  1.8.14.1   ad uint16_t htons(uint16_t) __attribute__((__const__));
     87  1.8.14.1   ad uint32_t ntohl(uint32_t) __attribute__((__const__));
     88  1.8.14.1   ad 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.8  uwe #include <machine/bswap.h>
    125       1.8  uwe 
    126       1.2  uwe /*
    127       1.2  uwe  * Macros for network/external number representation conversion.
    128       1.2  uwe  */
    129       1.2  uwe #if BYTE_ORDER == BIG_ENDIAN && !defined(__lint__)
    130       1.2  uwe #define	ntohl(x)	(x)
    131       1.2  uwe #define	ntohs(x)	(x)
    132       1.2  uwe #define	htonl(x)	(x)
    133       1.2  uwe #define	htons(x)	(x)
    134       1.2  uwe 
    135       1.2  uwe #define	NTOHL(x)	(void) (x)
    136       1.2  uwe #define	NTOHS(x)	(void) (x)
    137       1.2  uwe #define	HTONL(x)	(void) (x)
    138       1.2  uwe #define	HTONS(x)	(void) (x)
    139       1.2  uwe 
    140       1.2  uwe #else	/* LITTLE_ENDIAN || !defined(__lint__) */
    141       1.2  uwe 
    142       1.8  uwe /* XXX: uwe: Use ntohl &co supplied by WinCE. */
    143       1.8  uwe #if 0
    144       1.8  uwe #define	ntohl(x)	bswap32((uint32_t)(x))
    145       1.8  uwe #define	ntohs(x)	bswap16((uint16_t)(x))
    146       1.8  uwe #define	htonl(x)	bswap32((uint32_t)(x))
    147       1.8  uwe #define	htons(x)	bswap16((uint16_t)(x))
    148       1.8  uwe #endif
    149       1.8  uwe 
    150       1.2  uwe #define	NTOHL(x)	(x) = ntohl((uint32_t)(x))
    151       1.2  uwe #define	NTOHS(x)	(x) = ntohs((uint16_t)(x))
    152       1.2  uwe #define	HTONL(x)	(x) = htonl((uint32_t)(x))
    153       1.2  uwe #define	HTONS(x)	(x) = htons((uint16_t)(x))
    154       1.2  uwe #endif	/* LITTLE_ENDIAN || !defined(__lint__) */
    155       1.2  uwe 
    156       1.2  uwe /*
    157       1.2  uwe  * Macros to convert to a specific endianness.
    158       1.2  uwe  */
    159       1.2  uwe 
    160       1.2  uwe #if BYTE_ORDER == BIG_ENDIAN
    161       1.2  uwe 
    162       1.2  uwe #define htobe16(x)	(x)
    163       1.2  uwe #define htobe32(x)	(x)
    164       1.2  uwe #define htobe64(x)	(x)
    165       1.6  uwe #define htole16(x)	bswap16((uint16_t)(x))
    166       1.6  uwe #define htole32(x)	bswap32((uint32_t)(x))
    167       1.6  uwe #define htole64(x)	bswap64((uint64_t)(x))
    168       1.2  uwe 
    169       1.2  uwe #define HTOBE16(x)	(void) (x)
    170       1.2  uwe #define HTOBE32(x)	(void) (x)
    171       1.2  uwe #define HTOBE64(x)	(void) (x)
    172       1.6  uwe #define HTOLE16(x)	(x) = bswap16((uint16_t)(x))
    173       1.6  uwe #define HTOLE32(x)	(x) = bswap32((uint32_t)(x))
    174       1.6  uwe #define HTOLE64(x)	(x) = bswap64((uint64_t)(x))
    175       1.2  uwe 
    176       1.2  uwe #else	/* LITTLE_ENDIAN */
    177       1.2  uwe 
    178       1.6  uwe #define htobe16(x)	bswap16((uint16_t)(x))
    179       1.6  uwe #define htobe32(x)	bswap32((uint32_t)(x))
    180       1.6  uwe #define htobe64(x)	bswap64((uint64_t)(x))
    181       1.2  uwe #define htole16(x)	(x)
    182       1.2  uwe #define htole32(x)	(x)
    183       1.2  uwe #define htole64(x)	(x)
    184       1.2  uwe 
    185       1.6  uwe #define HTOBE16(x)	(x) = bswap16((uint16_t)(x))
    186       1.6  uwe #define HTOBE32(x)	(x) = bswap32((uint32_t)(x))
    187       1.6  uwe #define HTOBE64(x)	(x) = bswap64((uint64_t)(x))
    188       1.2  uwe #define HTOLE16(x)	(void) (x)
    189       1.2  uwe #define HTOLE32(x)	(void) (x)
    190       1.2  uwe #define HTOLE64(x)	(void) (x)
    191       1.2  uwe 
    192       1.2  uwe #endif	/* LITTLE_ENDIAN */
    193       1.2  uwe 
    194       1.2  uwe #define be16toh(x)	htobe16(x)
    195       1.2  uwe #define be32toh(x)	htobe32(x)
    196       1.2  uwe #define be64toh(x)	htobe64(x)
    197       1.2  uwe #define le16toh(x)	htole16(x)
    198       1.2  uwe #define le32toh(x)	htole32(x)
    199       1.2  uwe #define le64toh(x)	htole64(x)
    200       1.2  uwe 
    201       1.2  uwe #define BE16TOH(x)	HTOBE16(x)
    202       1.2  uwe #define BE32TOH(x)	HTOBE32(x)
    203       1.2  uwe #define BE64TOH(x)	HTOBE64(x)
    204       1.2  uwe #define LE16TOH(x)	HTOLE16(x)
    205       1.2  uwe #define LE32TOH(x)	HTOLE32(x)
    206       1.2  uwe #define LE64TOH(x)	HTOLE64(x)
    207       1.2  uwe 
    208       1.2  uwe /*
    209       1.2  uwe  * Routines to encode/decode big- and little-endian multi-octet values
    210       1.2  uwe  * to/from an octet stream.
    211       1.2  uwe  */
    212       1.2  uwe 
    213       1.6  uwe static __inline void __unused
    214       1.2  uwe be16enc(void *buf, uint16_t u)
    215       1.2  uwe {
    216       1.2  uwe 	uint8_t *p = (uint8_t *)buf;
    217       1.2  uwe 
    218       1.2  uwe 	p[0] = ((unsigned)u >> 8) & 0xff;
    219       1.2  uwe 	p[1] = u & 0xff;
    220       1.2  uwe }
    221       1.2  uwe 
    222       1.6  uwe static __inline void __unused
    223       1.2  uwe le16enc(void *buf, uint16_t u)
    224       1.2  uwe {
    225       1.2  uwe 	uint8_t *p = (uint8_t *)buf;
    226       1.2  uwe 
    227       1.2  uwe 	p[0] = u & 0xff;
    228       1.2  uwe 	p[1] = ((unsigned)u >> 8) & 0xff;
    229       1.2  uwe }
    230       1.2  uwe 
    231       1.6  uwe static __inline uint16_t __unused
    232       1.2  uwe be16dec(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[0] << 8) | p[1]);
    237       1.2  uwe }
    238       1.2  uwe 
    239       1.6  uwe static __inline uint16_t __unused
    240       1.2  uwe le16dec(const void *buf)
    241       1.2  uwe {
    242       1.2  uwe 	const uint8_t *p = (const uint8_t *)buf;
    243       1.2  uwe 
    244       1.2  uwe 	return ((p[1] << 8) | p[0]);
    245       1.2  uwe }
    246       1.2  uwe 
    247       1.6  uwe static __inline void __unused
    248       1.2  uwe be32enc(void *buf, uint32_t u)
    249       1.2  uwe {
    250       1.2  uwe 	uint8_t *p = (uint8_t *)buf;
    251       1.2  uwe 
    252       1.2  uwe 	p[0] = (u >> 24) & 0xff;
    253       1.2  uwe 	p[1] = (u >> 16) & 0xff;
    254       1.2  uwe 	p[2] = (u >> 8) & 0xff;
    255       1.2  uwe 	p[3] = u & 0xff;
    256       1.2  uwe }
    257       1.2  uwe 
    258       1.6  uwe static __inline void __unused
    259       1.2  uwe le32enc(void *buf, uint32_t u)
    260       1.2  uwe {
    261       1.2  uwe 	uint8_t *p = (uint8_t *)buf;
    262       1.2  uwe 
    263       1.2  uwe 	p[0] = u & 0xff;
    264       1.2  uwe 	p[1] = (u >> 8) & 0xff;
    265       1.2  uwe 	p[2] = (u >> 16) & 0xff;
    266       1.2  uwe 	p[3] = (u >> 24) & 0xff;
    267       1.2  uwe }
    268       1.2  uwe 
    269       1.6  uwe static __inline uint32_t __unused
    270       1.2  uwe be32dec(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[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
    275       1.2  uwe }
    276       1.2  uwe 
    277       1.6  uwe static __inline uint32_t __unused
    278       1.2  uwe le32dec(const void *buf)
    279       1.2  uwe {
    280       1.2  uwe 	const uint8_t *p = (const uint8_t *)buf;
    281       1.2  uwe 
    282       1.2  uwe 	return ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
    283       1.2  uwe }
    284       1.2  uwe 
    285       1.2  uwe 
    286       1.2  uwe /*
    287       1.2  uwe  * XXX: uwe: ULL suffix makes eVC unhappy.  Since nothing in hpcboot
    288       1.2  uwe  * needs 64-bit enc/dec routines - just ifdef them out for now.
    289       1.2  uwe  */
    290       1.2  uwe #ifndef _WIN32
    291       1.2  uwe 
    292       1.6  uwe static __inline void __unused
    293       1.2  uwe be64enc(void *buf, uint64_t u)
    294       1.2  uwe {
    295       1.2  uwe 	uint8_t *p = (uint8_t *)buf;
    296       1.2  uwe 
    297       1.2  uwe 	be32enc(p, (uint32_t)(u >> 32));
    298       1.2  uwe 	be32enc(p + 4, (uint32_t)(u & 0xffffffffULL));
    299       1.2  uwe }
    300       1.2  uwe 
    301       1.6  uwe static __inline void __unused
    302       1.2  uwe le64enc(void *buf, uint64_t u)
    303       1.2  uwe {
    304       1.2  uwe 	uint8_t *p = (uint8_t *)buf;
    305       1.2  uwe 
    306       1.2  uwe 	le32enc(p, (uint32_t)(u & 0xffffffffULL));
    307       1.2  uwe 	le32enc(p + 4, (uint32_t)(u >> 32));
    308       1.2  uwe }
    309       1.2  uwe 
    310       1.6  uwe static __inline uint64_t __unused
    311       1.2  uwe be64dec(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 (((uint64_t)be32dec(p) << 32) | be32dec(p + 4));
    316       1.2  uwe }
    317       1.2  uwe 
    318       1.6  uwe static __inline uint64_t __unused
    319       1.2  uwe le64dec(const void *buf)
    320       1.2  uwe {
    321       1.2  uwe 	const uint8_t *p = (const uint8_t *)buf;
    322       1.2  uwe 
    323       1.2  uwe 	return (le32dec(p) | ((uint64_t)le32dec(p + 4) << 32));
    324       1.2  uwe }
    325       1.2  uwe #endif /* !_WIN32 */
    326       1.2  uwe 
    327       1.2  uwe #endif /* !_LOCORE */
    328       1.2  uwe #endif /* _XOPEN_SOURCE || _NETBSD_SOURCE */
    329       1.2  uwe #endif /* !_SYS_ENDIAN_H_ */
    330