Home | History | Annotate | Download | only in sys
      1 /*	$OpenBSD: endian.h,v 1.17 2006/01/06 18:53:05 millert 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 #include <machine/endian.h>
     41 #include <machine/_types.h>
     42 
     43 #define _LITTLE_ENDIAN	1234
     44 #define _BIG_ENDIAN	4321
     45 #define _PDP_ENDIAN	3412
     46 
     47 #if __BSD_VISIBLE
     48 #define LITTLE_ENDIAN	_LITTLE_ENDIAN
     49 #define BIG_ENDIAN	_BIG_ENDIAN
     50 #define PDP_ENDIAN	_PDP_ENDIAN
     51 #define BYTE_ORDER	_BYTE_ORDER
     52 #endif
     53 
     54 #ifdef __GNUC__
     55 
     56 #define __swap16gen(x) __statement({					\
     57 	__uint16_t __swap16gen_x = (x);					\
     58 									\
     59 	(__uint16_t)((__swap16gen_x & 0xff) << 8 |			\
     60 	    (__swap16gen_x & 0xff00) >> 8);				\
     61 })
     62 
     63 #define __swap32gen(x) __statement({					\
     64 	__uint32_t __swap32gen_x = (x);					\
     65 									\
     66 	(__uint32_t)((__swap32gen_x & 0xff) << 24 |			\
     67 	    (__swap32gen_x & 0xff00) << 8 |				\
     68 	    (__swap32gen_x & 0xff0000) >> 8 |				\
     69 	    (__swap32gen_x & 0xff000000) >> 24);			\
     70 })
     71 
     72 #define __swap64gen(x) __statement({					\
     73 	__uint64_t __swap64gen_x = (x);					\
     74 									\
     75 	(__uint64_t)((__swap64gen_x & 0xff) << 56 |			\
     76 	    (__swap64gen_x & 0xff00ULL) << 40 |				\
     77 	    (__swap64gen_x & 0xff0000ULL) << 24 |			\
     78 	    (__swap64gen_x & 0xff000000ULL) << 8 |			\
     79 	    (__swap64gen_x & 0xff00000000ULL) >> 8 |			\
     80 	    (__swap64gen_x & 0xff0000000000ULL) >> 24 |			\
     81 	    (__swap64gen_x & 0xff000000000000ULL) >> 40 |		\
     82 	    (__swap64gen_x & 0xff00000000000000ULL) >> 56);		\
     83 })
     84 
     85 #else /* __GNUC__ */
     86 
     87 /* Note that these macros evaluate their arguments several times.  */
     88 #define __swap16gen(x)							\
     89     (__uint16_t)(((__uint16_t)(x) & 0xff) << 8 | ((__uint16_t)(x) & 0xff00) >> 8)
     90 
     91 #define __swap32gen(x)							\
     92     (__uint32_t)(((__uint32_t)(x) & 0xff) << 24 |			\
     93     ((__uint32_t)(x) & 0xff00) << 8 | ((__uint32_t)(x) & 0xff0000) >> 8 |\
     94     ((__uint32_t)(x) & 0xff000000) >> 24)
     95 
     96 #define __swap64gen(x)							\
     97 	(__uint64_t)((((__uint64_t)(x) & 0xff) << 56) |			\
     98 	    ((__uint64_t)(x) & 0xff00ULL) << 40 |			\
     99 	    ((__uint64_t)(x) & 0xff0000ULL) << 24 |			\
    100 	    ((__uint64_t)(x) & 0xff000000ULL) << 8 |			\
    101 	    ((__uint64_t)(x) & 0xff00000000ULL) >> 8 |			\
    102 	    ((__uint64_t)(x) & 0xff0000000000ULL) >> 24 |		\
    103 	    ((__uint64_t)(x) & 0xff000000000000ULL) >> 40 |		\
    104 	    ((__uint64_t)(x) & 0xff00000000000000ULL) >> 56)
    105 
    106 #endif /* __GNUC__ */
    107 
    108 /*
    109  * Define MD_SWAP if you provide swap{16,32}md functions/macros that are
    110  * optimized for your architecture,  These will be used for swap{16,32}
    111  * unless the argument is a constant and we are using GCC, where we can
    112  * take advantage of the CSE phase much better by using the generic version.
    113  */
    114 #ifdef MD_SWAP
    115 #if __GNUC__
    116 
    117 #define __swap16(x) __statement({					\
    118 	__uint16_t __swap16_x = (x);					\
    119 									\
    120 	__builtin_constant_p(x) ? __swap16gen(__swap16_x) :		\
    121 	    __swap16md(__swap16_x);					\
    122 })
    123 
    124 #define __swap32(x) __statement({					\
    125 	__uint32_t __swap32_x = (x);					\
    126 									\
    127 	__builtin_constant_p(x) ? __swap32gen(__swap32_x) :		\
    128 	    __swap32md(__swap32_x);					\
    129 })
    130 
    131 #define __swap64(x) __statement({					\
    132 	__uint64_t __swap64_x = (x);					\
    133 									\
    134 	__builtin_constant_p(x) ? __swap64gen(__swap64_x) :		\
    135 	    __swap64md(__swap64_x);					\
    136 })
    137 
    138 #endif /* __GNUC__  */
    139 
    140 #else /* MD_SWAP */
    141 #define __swap16 __swap16gen
    142 #define __swap32 __swap32gen
    143 #define __swap64 __swap64gen
    144 #endif /* MD_SWAP */
    145 
    146 #define __swap16_multi(v, n) do {						\
    147 	__size_t __swap16_multi_n = (n);				\
    148 	__uint16_t *__swap16_multi_v = (v);				\
    149 									\
    150 	while (__swap16_multi_n) {					\
    151 		*__swap16_multi_v = swap16(*__swap16_multi_v);		\
    152 		__swap16_multi_v++;					\
    153 		__swap16_multi_n--;					\
    154 	}								\
    155 } while (0)
    156 
    157 #if __BSD_VISIBLE
    158 #define swap16 __swap16
    159 #define swap32 __swap32
    160 #define swap64 __swap64
    161 #define swap16_multi __swap16_multi
    162 
    163 __BEGIN_DECLS
    164 __uint64_t	htobe64(__uint64_t);
    165 __uint32_t	htobe32(__uint32_t);
    166 __uint16_t	htobe16(__uint16_t);
    167 __uint64_t	betoh64(__uint64_t);
    168 __uint32_t	betoh32(__uint32_t);
    169 __uint16_t	betoh16(__uint16_t);
    170 
    171 __uint64_t	htole64(__uint64_t);
    172 __uint32_t	htole32(__uint32_t);
    173 __uint16_t	htole16(__uint16_t);
    174 __uint64_t	letoh64(__uint64_t);
    175 __uint32_t	letoh32(__uint32_t);
    176 __uint16_t	letoh16(__uint16_t);
    177 __END_DECLS
    178 #endif /* __BSD_VISIBLE */
    179 
    180 #if _BYTE_ORDER == _LITTLE_ENDIAN
    181 
    182 /* Can be overridden by machine/endian.h before inclusion of this file.  */
    183 #ifndef _QUAD_HIGHWORD
    184 #define _QUAD_HIGHWORD 1
    185 #endif
    186 #ifndef _QUAD_LOWWORD
    187 #define _QUAD_LOWWORD 0
    188 #endif
    189 
    190 #if __BSD_VISIBLE
    191 #define htobe16 __swap16
    192 #define htobe32 __swap32
    193 #define htobe64 __swap64
    194 #define betoh16 __swap16
    195 #define betoh32 __swap32
    196 #define betoh64 __swap64
    197 
    198 #define htole16(x) (x)
    199 #define htole32(x) (x)
    200 #define htole64(x) (x)
    201 #define letoh16(x) (x)
    202 #define letoh32(x) (x)
    203 #define letoh64(x) (x)
    204 #endif /* __BSD_VISIBLE */
    205 
    206 #define htons(x) __swap16(x)
    207 #define htonl(x) __swap32(x)
    208 #define ntohs(x) __swap16(x)
    209 #define ntohl(x) __swap32(x)
    210 
    211 /* Bionic additions */
    212 #define ntohq(x) __swap64(x)
    213 #define htonq(x) __swap64(x)
    214 
    215 #define __LITTLE_ENDIAN_BITFIELD
    216 
    217 #endif /* _BYTE_ORDER */
    218 
    219 #if _BYTE_ORDER == _BIG_ENDIAN
    220 
    221 /* Can be overridden by machine/endian.h before inclusion of this file.  */
    222 #ifndef _QUAD_HIGHWORD
    223 #define _QUAD_HIGHWORD 0
    224 #endif
    225 #ifndef _QUAD_LOWWORD
    226 #define _QUAD_LOWWORD 1
    227 #endif
    228 
    229 #if __BSD_VISIBLE
    230 #define htole16 __swap16
    231 #define htole32 __swap32
    232 #define htole64 __swap64
    233 #define letoh16 __swap16
    234 #define letoh32 __swap32
    235 #define letoh64 __swap64
    236 
    237 #define htobe16(x) (x)
    238 #define htobe32(x) (x)
    239 #define htobe64(x) (x)
    240 #define betoh16(x) (x)
    241 #define betoh32(x) (x)
    242 #define betoh64(x) (x)
    243 #endif /* __BSD_VISIBLE */
    244 
    245 #define htons(x) (x)
    246 #define htonl(x) (x)
    247 #define ntohs(x) (x)
    248 #define ntohl(x) (x)
    249 
    250 /* Bionic additions */
    251 #define ntohq(x) (x)
    252 #define htonq(x) (x)
    253 
    254 #define __BIG_ENDIAN_BITFIELD
    255 
    256 #endif /* _BYTE_ORDER */
    257 
    258 #if __BSD_VISIBLE
    259 #define	NTOHL(x) (x) = ntohl((u_int32_t)(x))
    260 #define	NTOHS(x) (x) = ntohs((u_int16_t)(x))
    261 #define	HTONL(x) (x) = htonl((u_int32_t)(x))
    262 #define	HTONS(x) (x) = htons((u_int16_t)(x))
    263 #endif
    264 
    265 
    266 #define  __BYTE_ORDER       _BYTE_ORDER
    267 #ifndef  __LITTLE_ENDIAN
    268 #define  __LITTLE_ENDIAN    _LITTLE_ENDIAN
    269 #endif
    270 #ifndef  __BIG_ENDIAN
    271 #define  __BIG_ENDIAN       _BIG_ENDIAN
    272 #endif
    273 
    274 #endif /* _SYS_ENDIAN_H_ */
    275