[BACK]Return to endian.h CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / sys

Annotation of src/sys/sys/endian.h, Revision 1.24

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

CVSweb <webmaster@jp.NetBSD.org>