machine/endian.h

Go to the documentation of this file.
00001 /*-
00002  * Copyright (c) 1987, 1991 Regents of the University of California.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 4. Neither the name of the University nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  *
00029  *      @(#)endian.h    7.8 (Berkeley) 4/3/91
00030  * $FreeBSD: src/sys/i386/include/endian.h,v 1.41 2005/03/02 21:33:26 joerg Exp $
00031  */
00032 
00033 #ifndef _MACHINE_ENDIAN_H_
00034 #define _MACHINE_ENDIAN_H_
00035 
00036 #include <sys/cdefs.h>
00037 #include <sys/_types.h>
00038 
00039 #ifdef __cplusplus
00040 extern "C" {
00041 #endif
00042 
00043 /*
00044  * Define the order of 32-bit words in 64-bit words.
00045  */
00046 #define _QUAD_HIGHWORD 1
00047 #define _QUAD_LOWWORD 0
00048 
00049 /*
00050  * Definitions for byte order, according to byte significance from low
00051  * address to high.
00052  */
00053 #define _LITTLE_ENDIAN  1234    /* LSB first: i386, vax */
00054 #define _BIG_ENDIAN     4321    /* MSB first: 68000, ibm, net */
00055 #define _PDP_ENDIAN     3412    /* LSB first in word, MSW first in long */
00056 
00057 #define _BYTE_ORDER     _LITTLE_ENDIAN
00058 
00059 /*
00060  * Deprecated variants that don't have enough underscores to be useful in more
00061  * strict namespaces.
00062  */
00063 #if __BSD_VISIBLE
00064 #define LITTLE_ENDIAN   _LITTLE_ENDIAN
00065 #define BIG_ENDIAN      _BIG_ENDIAN
00066 #define PDP_ENDIAN      _PDP_ENDIAN
00067 #define BYTE_ORDER      _BYTE_ORDER
00068 #endif
00069 
00070 #ifdef __SYMBIAN32__
00071 
00072 #define _BYTEORDER_FUNC_DEFINED
00073 
00074 #else /* __SYMBIAN32__ */
00075 
00076 #if defined(__GNUCLIKE_ASM) && defined(__GNUCLIKE_BUILTIN_CONSTANT_P)
00077 
00078 #define __word_swap_int_var(x) \
00079 __extension__ ({ register __uint32_t __X = (x); \
00080    __asm ("rorl $16, %0" : "+r" (__X)); \
00081    __X; })
00082 
00083 #ifdef __OPTIMIZE__
00084 
00085 #define __word_swap_int_const(x) \
00086         ((((x) & 0xffff0000) >> 16) | \
00087          (((x) & 0x0000ffff) << 16))
00088 #define __word_swap_int(x) (__builtin_constant_p(x) ? \
00089         __word_swap_int_const(x) : __word_swap_int_var(x))
00090 
00091 #else   /* __OPTIMIZE__ */
00092 
00093 #define __word_swap_int(x) __word_swap_int_var(x)
00094 
00095 #endif  /* __OPTIMIZE__ */
00096 
00097 #define __byte_swap_int_var(x) \
00098 __extension__ ({ register __uint32_t __X = (x); \
00099    __asm ("bswap %0" : "+r" (__X)); \
00100    __X; })
00101 
00102 #ifdef __OPTIMIZE__
00103 
00104 #define __byte_swap_int_const(x) \
00105         ((((x) & 0xff000000) >> 24) | \
00106          (((x) & 0x00ff0000) >>  8) | \
00107          (((x) & 0x0000ff00) <<  8) | \
00108          (((x) & 0x000000ff) << 24))
00109 #define __byte_swap_int(x) (__builtin_constant_p(x) ? \
00110         __byte_swap_int_const(x) : __byte_swap_int_var(x))
00111 
00112 #else   /* __OPTIMIZE__ */
00113 
00114 #define __byte_swap_int(x) __byte_swap_int_var(x)
00115 
00116 #endif  /* __OPTIMIZE__ */
00117 
00118 #define __byte_swap_word_var(x) \
00119 __extension__ ({ register __uint16_t __X = (x); \
00120    __asm ("xchgb %h0, %b0" : "+q" (__X)); \
00121    __X; })
00122 
00123 #ifdef __OPTIMIZE__
00124 
00125 #define __byte_swap_word_const(x) \
00126         ((((x) & 0xff00) >> 8) | \
00127          (((x) & 0x00ff) << 8))
00128 
00129 #define __byte_swap_word(x) (__builtin_constant_p(x) ? \
00130         __byte_swap_word_const(x) : __byte_swap_word_var(x))
00131 
00132 #else   /* __OPTIMIZE__ */
00133 
00134 #define __byte_swap_word(x) __byte_swap_word_var(x)
00135 
00136 #endif  /* __OPTIMIZE__ */
00137 
00138 static __inline __uint64_t
00139 __bswap64(__uint64_t _x)
00140 {
00141 
00142         return ((_x >> 56) | ((_x >> 40) & 0xff00) | ((_x >> 24) & 0xff0000) |
00143             ((_x >> 8) & 0xff000000) | ((_x << 8) & ((__uint64_t)0xff << 32)) |
00144             ((_x << 24) & ((__uint64_t)0xff << 40)) |
00145             ((_x << 40) & ((__uint64_t)0xff << 48)) | ((_x << 56)));
00146 }
00147 
00148 static __inline __uint32_t
00149 __bswap32(__uint32_t _x)
00150 {
00151 
00152         return (__byte_swap_int(_x));
00153 }
00154 
00155 static __inline __uint16_t
00156 __bswap16(__uint16_t _x)
00157 {
00158 
00159         return (__byte_swap_word(_x));
00160 }
00161 
00162 #define __htonl(x)      __bswap32(x)
00163 #define __htons(x)      __bswap16(x)
00164 #define __ntohl(x)      __bswap32(x)
00165 #define __ntohs(x)      __bswap16(x)
00166 
00167 #else /* !(__GNUCLIKE_ASM && __GNUCLIKE_BUILTIN_CONSTANT_P) */
00168 
00169 /*
00170  * No optimizations are available for this compiler.  Fall back to
00171  * non-optimized functions by defining the constant usually used to prevent
00172  * redefinition.
00173  */
00174 #define _BYTEORDER_FUNC_DEFINED
00175 
00176 #endif /* __GNUCLIKE_ASM && __GNUCLIKE_BUILTIN_CONSTANT_P */
00177 #endif /* __SYMBIAN32__ */
00178 
00179 #ifdef __cplusplus
00180 }
00181 #endif
00182 
00183 #endif /* !_MACHINE_ENDIAN_H_ */

Copyright © Nokia Corporation 2001-2008
Back to top