1.1 --- a/epoc32/include/stdapis/machine/endian.h Tue Nov 24 13:55:44 2009 +0000
1.2 +++ b/epoc32/include/stdapis/machine/endian.h Tue Mar 16 16:12:26 2010 +0000
1.3 @@ -1,1 +1,183 @@
1.4 -endian.h
1.5 +/*-
1.6 + * Copyright (c) 1987, 1991 Regents of the University of California.
1.7 + * All rights reserved.
1.8 + *
1.9 + * Redistribution and use in source and binary forms, with or without
1.10 + * modification, are permitted provided that the following conditions
1.11 + * are met:
1.12 + * 1. Redistributions of source code must retain the above copyright
1.13 + * notice, this list of conditions and the following disclaimer.
1.14 + * 2. Redistributions in binary form must reproduce the above copyright
1.15 + * notice, this list of conditions and the following disclaimer in the
1.16 + * documentation and/or other materials provided with the distribution.
1.17 + * 4. Neither the name of the University nor the names of its contributors
1.18 + * may be used to endorse or promote products derived from this software
1.19 + * without specific prior written permission.
1.20 + *
1.21 + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1.22 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1.23 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1.24 + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1.25 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1.26 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1.27 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1.28 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1.29 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1.30 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1.31 + * SUCH DAMAGE.
1.32 + *
1.33 + * @(#)endian.h 7.8 (Berkeley) 4/3/91
1.34 + * $FreeBSD: src/sys/i386/include/endian.h,v 1.41 2005/03/02 21:33:26 joerg Exp $
1.35 + */
1.36 +
1.37 +#ifndef _MACHINE_ENDIAN_H_
1.38 +#define _MACHINE_ENDIAN_H_
1.39 +
1.40 +#include <sys/cdefs.h>
1.41 +#include <sys/_types.h>
1.42 +
1.43 +#ifdef __cplusplus
1.44 +extern "C" {
1.45 +#endif
1.46 +
1.47 +/*
1.48 + * Define the order of 32-bit words in 64-bit words.
1.49 + */
1.50 +#define _QUAD_HIGHWORD 1
1.51 +#define _QUAD_LOWWORD 0
1.52 +
1.53 +/*
1.54 + * Definitions for byte order, according to byte significance from low
1.55 + * address to high.
1.56 + */
1.57 +#define _LITTLE_ENDIAN 1234 /* LSB first: i386, vax */
1.58 +#define _BIG_ENDIAN 4321 /* MSB first: 68000, ibm, net */
1.59 +#define _PDP_ENDIAN 3412 /* LSB first in word, MSW first in long */
1.60 +
1.61 +#define _BYTE_ORDER _LITTLE_ENDIAN
1.62 +
1.63 +/*
1.64 + * Deprecated variants that don't have enough underscores to be useful in more
1.65 + * strict namespaces.
1.66 + */
1.67 +#if __BSD_VISIBLE
1.68 +#define LITTLE_ENDIAN _LITTLE_ENDIAN
1.69 +#define BIG_ENDIAN _BIG_ENDIAN
1.70 +#define PDP_ENDIAN _PDP_ENDIAN
1.71 +#define BYTE_ORDER _BYTE_ORDER
1.72 +#endif
1.73 +
1.74 +#ifdef __SYMBIAN32__
1.75 +
1.76 +#define _BYTEORDER_FUNC_DEFINED
1.77 +
1.78 +#else /* __SYMBIAN32__ */
1.79 +
1.80 +#if defined(__GNUCLIKE_ASM) && defined(__GNUCLIKE_BUILTIN_CONSTANT_P)
1.81 +
1.82 +#define __word_swap_int_var(x) \
1.83 +__extension__ ({ register __uint32_t __X = (x); \
1.84 + __asm ("rorl $16, %0" : "+r" (__X)); \
1.85 + __X; })
1.86 +
1.87 +#ifdef __OPTIMIZE__
1.88 +
1.89 +#define __word_swap_int_const(x) \
1.90 + ((((x) & 0xffff0000) >> 16) | \
1.91 + (((x) & 0x0000ffff) << 16))
1.92 +#define __word_swap_int(x) (__builtin_constant_p(x) ? \
1.93 + __word_swap_int_const(x) : __word_swap_int_var(x))
1.94 +
1.95 +#else /* __OPTIMIZE__ */
1.96 +
1.97 +#define __word_swap_int(x) __word_swap_int_var(x)
1.98 +
1.99 +#endif /* __OPTIMIZE__ */
1.100 +
1.101 +#define __byte_swap_int_var(x) \
1.102 +__extension__ ({ register __uint32_t __X = (x); \
1.103 + __asm ("bswap %0" : "+r" (__X)); \
1.104 + __X; })
1.105 +
1.106 +#ifdef __OPTIMIZE__
1.107 +
1.108 +#define __byte_swap_int_const(x) \
1.109 + ((((x) & 0xff000000) >> 24) | \
1.110 + (((x) & 0x00ff0000) >> 8) | \
1.111 + (((x) & 0x0000ff00) << 8) | \
1.112 + (((x) & 0x000000ff) << 24))
1.113 +#define __byte_swap_int(x) (__builtin_constant_p(x) ? \
1.114 + __byte_swap_int_const(x) : __byte_swap_int_var(x))
1.115 +
1.116 +#else /* __OPTIMIZE__ */
1.117 +
1.118 +#define __byte_swap_int(x) __byte_swap_int_var(x)
1.119 +
1.120 +#endif /* __OPTIMIZE__ */
1.121 +
1.122 +#define __byte_swap_word_var(x) \
1.123 +__extension__ ({ register __uint16_t __X = (x); \
1.124 + __asm ("xchgb %h0, %b0" : "+q" (__X)); \
1.125 + __X; })
1.126 +
1.127 +#ifdef __OPTIMIZE__
1.128 +
1.129 +#define __byte_swap_word_const(x) \
1.130 + ((((x) & 0xff00) >> 8) | \
1.131 + (((x) & 0x00ff) << 8))
1.132 +
1.133 +#define __byte_swap_word(x) (__builtin_constant_p(x) ? \
1.134 + __byte_swap_word_const(x) : __byte_swap_word_var(x))
1.135 +
1.136 +#else /* __OPTIMIZE__ */
1.137 +
1.138 +#define __byte_swap_word(x) __byte_swap_word_var(x)
1.139 +
1.140 +#endif /* __OPTIMIZE__ */
1.141 +
1.142 +static __inline __uint64_t
1.143 +__bswap64(__uint64_t _x)
1.144 +{
1.145 +
1.146 + return ((_x >> 56) | ((_x >> 40) & 0xff00) | ((_x >> 24) & 0xff0000) |
1.147 + ((_x >> 8) & 0xff000000) | ((_x << 8) & ((__uint64_t)0xff << 32)) |
1.148 + ((_x << 24) & ((__uint64_t)0xff << 40)) |
1.149 + ((_x << 40) & ((__uint64_t)0xff << 48)) | ((_x << 56)));
1.150 +}
1.151 +
1.152 +static __inline __uint32_t
1.153 +__bswap32(__uint32_t _x)
1.154 +{
1.155 +
1.156 + return (__byte_swap_int(_x));
1.157 +}
1.158 +
1.159 +static __inline __uint16_t
1.160 +__bswap16(__uint16_t _x)
1.161 +{
1.162 +
1.163 + return (__byte_swap_word(_x));
1.164 +}
1.165 +
1.166 +#define __htonl(x) __bswap32(x)
1.167 +#define __htons(x) __bswap16(x)
1.168 +#define __ntohl(x) __bswap32(x)
1.169 +#define __ntohs(x) __bswap16(x)
1.170 +
1.171 +#else /* !(__GNUCLIKE_ASM && __GNUCLIKE_BUILTIN_CONSTANT_P) */
1.172 +
1.173 +/*
1.174 + * No optimizations are available for this compiler. Fall back to
1.175 + * non-optimized functions by defining the constant usually used to prevent
1.176 + * redefinition.
1.177 + */
1.178 +#define _BYTEORDER_FUNC_DEFINED
1.179 +
1.180 +#endif /* __GNUCLIKE_ASM && __GNUCLIKE_BUILTIN_CONSTANT_P */
1.181 +#endif /* __SYMBIAN32__ */
1.182 +
1.183 +#ifdef __cplusplus
1.184 +}
1.185 +#endif
1.186 +
1.187 +#endif /* !_MACHINE_ENDIAN_H_ */