[BACK]Return to endian.h CVS log [TXT][DIR] Up to [local] / sys / sys

Annotation of sys/sys/endian.h, Revision 1.1

1.1     ! nbrk        1: /*     $OpenBSD: endian.h,v 1.18 2006/03/27 07:09:24 otto Exp $        */
        !             2:
        !             3: /*-
        !             4:  * Copyright (c) 1997 Niklas Hallqvist.  All rights reserved.
        !             5:  *
        !             6:  * Redistribution and use in source and binary forms, with or without
        !             7:  * modification, are permitted provided that the following conditions
        !             8:  * are met:
        !             9:  * 1. Redistributions of source code must retain the above copyright
        !            10:  *    notice, this list of conditions and the following disclaimer.
        !            11:  * 2. Redistributions in binary form must reproduce the above copyright
        !            12:  *    notice, this list of conditions and the following disclaimer in the
        !            13:  *    documentation and/or other materials provided with the distribution.
        !            14:  *
        !            15:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
        !            16:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
        !            17:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
        !            18:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
        !            19:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
        !            20:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
        !            21:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
        !            22:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
        !            23:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
        !            24:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
        !            25:  */
        !            26:
        !            27: /*
        !            28:  * Generic definitions for little- and big-endian systems.  Other endianesses
        !            29:  * has to be dealt with in the specific machine/endian.h file for that port.
        !            30:  *
        !            31:  * This file is meant to be included from a little- or big-endian port's
        !            32:  * machine/endian.h after setting _BYTE_ORDER to either 1234 for little endian
        !            33:  * or 4321 for big..
        !            34:  */
        !            35:
        !            36: #ifndef _SYS_ENDIAN_H_
        !            37: #define _SYS_ENDIAN_H_
        !            38:
        !            39: #include <sys/cdefs.h>
        !            40:
        !            41: #define _LITTLE_ENDIAN 1234
        !            42: #define _BIG_ENDIAN    4321
        !            43: #define _PDP_ENDIAN    3412
        !            44:
        !            45: #if __BSD_VISIBLE
        !            46: #define LITTLE_ENDIAN  _LITTLE_ENDIAN
        !            47: #define BIG_ENDIAN     _BIG_ENDIAN
        !            48: #define PDP_ENDIAN     _PDP_ENDIAN
        !            49: #define BYTE_ORDER     _BYTE_ORDER
        !            50: #endif
        !            51:
        !            52: #ifdef __GNUC__
        !            53:
        !            54: #define __swap16gen(x) __statement({                                   \
        !            55:        __uint16_t __swap16gen_x = (x);                                 \
        !            56:                                                                        \
        !            57:        (__uint16_t)((__swap16gen_x & 0xff) << 8 |                      \
        !            58:            (__swap16gen_x & 0xff00) >> 8);                             \
        !            59: })
        !            60:
        !            61: #define __swap32gen(x) __statement({                                   \
        !            62:        __uint32_t __swap32gen_x = (x);                                 \
        !            63:                                                                        \
        !            64:        (__uint32_t)((__swap32gen_x & 0xff) << 24 |                     \
        !            65:            (__swap32gen_x & 0xff00) << 8 |                             \
        !            66:            (__swap32gen_x & 0xff0000) >> 8 |                           \
        !            67:            (__swap32gen_x & 0xff000000) >> 24);                        \
        !            68: })
        !            69:
        !            70: #define __swap64gen(x) __statement({                                   \
        !            71:        __uint64_t __swap64gen_x = (x);                                 \
        !            72:                                                                        \
        !            73:        (__uint64_t)((__swap64gen_x & 0xff) << 56 |                     \
        !            74:            (__swap64gen_x & 0xff00ULL) << 40 |                         \
        !            75:            (__swap64gen_x & 0xff0000ULL) << 24 |                       \
        !            76:            (__swap64gen_x & 0xff000000ULL) << 8 |                      \
        !            77:            (__swap64gen_x & 0xff00000000ULL) >> 8 |                    \
        !            78:            (__swap64gen_x & 0xff0000000000ULL) >> 24 |                 \
        !            79:            (__swap64gen_x & 0xff000000000000ULL) >> 40 |               \
        !            80:            (__swap64gen_x & 0xff00000000000000ULL) >> 56);             \
        !            81: })
        !            82:
        !            83: #else /* __GNUC__ */
        !            84:
        !            85: /* Note that these macros evaluate their arguments several times.  */
        !            86: #define __swap16gen(x)                                                 \
        !            87:     (__uint16_t)(((__uint16_t)(x) & 0xffU) << 8 | ((__uint16_t)(x) & 0xff00U) >> 8)
        !            88:
        !            89: #define __swap32gen(x)                                                 \
        !            90:     (__uint32_t)(((__uint32_t)(x) & 0xff) << 24 |                      \
        !            91:     ((__uint32_t)(x) & 0xff00) << 8 | ((__uint32_t)(x) & 0xff0000) >> 8 |\
        !            92:     ((__uint32_t)(x) & 0xff000000) >> 24)
        !            93:
        !            94: #define __swap64gen(x)                                                 \
        !            95:        (__uint64_t)((((__uint64_t)(x) & 0xff) << 56) |                 \
        !            96:            ((__uint64_t)(x) & 0xff00ULL) << 40 |                       \
        !            97:            ((__uint64_t)(x) & 0xff0000ULL) << 24 |                     \
        !            98:            ((__uint64_t)(x) & 0xff000000ULL) << 8 |                    \
        !            99:            ((__uint64_t)(x) & 0xff00000000ULL) >> 8 |                  \
        !           100:            ((__uint64_t)(x) & 0xff0000000000ULL) >> 24 |               \
        !           101:            ((__uint64_t)(x) & 0xff000000000000ULL) >> 40 |             \
        !           102:            ((__uint64_t)(x) & 0xff00000000000000ULL) >> 56)
        !           103:
        !           104: #endif /* __GNUC__ */
        !           105:
        !           106: /*
        !           107:  * Define MD_SWAP if you provide swap{16,32}md functions/macros that are
        !           108:  * optimized for your architecture,  These will be used for swap{16,32}
        !           109:  * unless the argument is a constant and we are using GCC, where we can
        !           110:  * take advantage of the CSE phase much better by using the generic version.
        !           111:  */
        !           112: #ifdef MD_SWAP
        !           113: #if __GNUC__
        !           114:
        !           115: #define __swap16(x) __statement({                                      \
        !           116:        __uint16_t __swap16_x = (x);                                    \
        !           117:                                                                        \
        !           118:        __builtin_constant_p(x) ? __swap16gen(__swap16_x) :             \
        !           119:            __swap16md(__swap16_x);                                     \
        !           120: })
        !           121:
        !           122: #define __swap32(x) __statement({                                      \
        !           123:        __uint32_t __swap32_x = (x);                                    \
        !           124:                                                                        \
        !           125:        __builtin_constant_p(x) ? __swap32gen(__swap32_x) :             \
        !           126:            __swap32md(__swap32_x);                                     \
        !           127: })
        !           128:
        !           129: #define __swap64(x) __statement({                                      \
        !           130:        __uint64_t __swap64_x = (x);                                    \
        !           131:                                                                        \
        !           132:        __builtin_constant_p(x) ? __swap64gen(__swap64_x) :             \
        !           133:            __swap64md(__swap64_x);                                     \
        !           134: })
        !           135:
        !           136: #endif /* __GNUC__  */
        !           137:
        !           138: #else /* MD_SWAP */
        !           139: #define __swap16 __swap16gen
        !           140: #define __swap32 __swap32gen
        !           141: #define __swap64 __swap64gen
        !           142: #endif /* MD_SWAP */
        !           143:
        !           144: #define __swap16_multi(v, n) do {                                              \
        !           145:        __size_t __swap16_multi_n = (n);                                \
        !           146:        __uint16_t *__swap16_multi_v = (v);                             \
        !           147:                                                                        \
        !           148:        while (__swap16_multi_n) {                                      \
        !           149:                *__swap16_multi_v = swap16(*__swap16_multi_v);          \
        !           150:                __swap16_multi_v++;                                     \
        !           151:                __swap16_multi_n--;                                     \
        !           152:        }                                                               \
        !           153: } while (0)
        !           154:
        !           155: #if __BSD_VISIBLE
        !           156: #define swap16 __swap16
        !           157: #define swap32 __swap32
        !           158: #define swap64 __swap64
        !           159: #define swap16_multi __swap16_multi
        !           160:
        !           161: __BEGIN_DECLS
        !           162: __uint64_t     htobe64(__uint64_t);
        !           163: __uint32_t     htobe32(__uint32_t);
        !           164: __uint16_t     htobe16(__uint16_t);
        !           165: __uint64_t     betoh64(__uint64_t);
        !           166: __uint32_t     betoh32(__uint32_t);
        !           167: __uint16_t     betoh16(__uint16_t);
        !           168:
        !           169: __uint64_t     htole64(__uint64_t);
        !           170: __uint32_t     htole32(__uint32_t);
        !           171: __uint16_t     htole16(__uint16_t);
        !           172: __uint64_t     letoh64(__uint64_t);
        !           173: __uint32_t     letoh32(__uint32_t);
        !           174: __uint16_t     letoh16(__uint16_t);
        !           175: __END_DECLS
        !           176: #endif /* __BSD_VISIBLE */
        !           177:
        !           178: #if _BYTE_ORDER == _LITTLE_ENDIAN
        !           179:
        !           180: /* Can be overridden by machine/endian.h before inclusion of this file.  */
        !           181: #ifndef _QUAD_HIGHWORD
        !           182: #define _QUAD_HIGHWORD 1
        !           183: #endif
        !           184: #ifndef _QUAD_LOWWORD
        !           185: #define _QUAD_LOWWORD 0
        !           186: #endif
        !           187:
        !           188: #if __BSD_VISIBLE
        !           189: #define htobe16 __swap16
        !           190: #define htobe32 __swap32
        !           191: #define htobe64 __swap64
        !           192: #define betoh16 __swap16
        !           193: #define betoh32 __swap32
        !           194: #define betoh64 __swap64
        !           195:
        !           196: #define htole16(x) (x)
        !           197: #define htole32(x) (x)
        !           198: #define htole64(x) (x)
        !           199: #define letoh16(x) (x)
        !           200: #define letoh32(x) (x)
        !           201: #define letoh64(x) (x)
        !           202: #endif /* __BSD_VISIBLE */
        !           203:
        !           204: #define htons(x) __swap16(x)
        !           205: #define htonl(x) __swap32(x)
        !           206: #define ntohs(x) __swap16(x)
        !           207: #define ntohl(x) __swap32(x)
        !           208:
        !           209: #endif /* _BYTE_ORDER */
        !           210:
        !           211: #if _BYTE_ORDER == _BIG_ENDIAN
        !           212:
        !           213: /* Can be overridden by machine/endian.h before inclusion of this file.  */
        !           214: #ifndef _QUAD_HIGHWORD
        !           215: #define _QUAD_HIGHWORD 0
        !           216: #endif
        !           217: #ifndef _QUAD_LOWWORD
        !           218: #define _QUAD_LOWWORD 1
        !           219: #endif
        !           220:
        !           221: #if __BSD_VISIBLE
        !           222: #define htole16 __swap16
        !           223: #define htole32 __swap32
        !           224: #define htole64 __swap64
        !           225: #define letoh16 __swap16
        !           226: #define letoh32 __swap32
        !           227: #define letoh64 __swap64
        !           228:
        !           229: #define htobe16(x) (x)
        !           230: #define htobe32(x) (x)
        !           231: #define htobe64(x) (x)
        !           232: #define betoh16(x) (x)
        !           233: #define betoh32(x) (x)
        !           234: #define betoh64(x) (x)
        !           235: #endif /* __BSD_VISIBLE */
        !           236:
        !           237: #define htons(x) (x)
        !           238: #define htonl(x) (x)
        !           239: #define ntohs(x) (x)
        !           240: #define ntohl(x) (x)
        !           241:
        !           242: #endif /* _BYTE_ORDER */
        !           243:
        !           244: #if __BSD_VISIBLE
        !           245: #define        NTOHL(x) (x) = ntohl((u_int32_t)(x))
        !           246: #define        NTOHS(x) (x) = ntohs((u_int16_t)(x))
        !           247: #define        HTONL(x) (x) = htonl((u_int32_t)(x))
        !           248: #define        HTONS(x) (x) = htons((u_int16_t)(x))
        !           249: #endif
        !           250:
        !           251: #endif /* _SYS_ENDIAN_H_ */

CVSweb