os/ossrv/genericopenlibs/openenvcore/include/machine/endian.h
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/*-
sl@0
     2
 * Copyright (c) 1987, 1991 Regents of the University of California.
sl@0
     3
 * All rights reserved.
sl@0
     4
 *
sl@0
     5
 * Redistribution and use in source and binary forms, with or without
sl@0
     6
 * modification, are permitted provided that the following conditions
sl@0
     7
 * are met:
sl@0
     8
 * 1. Redistributions of source code must retain the above copyright
sl@0
     9
 *    notice, this list of conditions and the following disclaimer.
sl@0
    10
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    11
 *    notice, this list of conditions and the following disclaimer in the
sl@0
    12
 *    documentation and/or other materials provided with the distribution.
sl@0
    13
 * 4. Neither the name of the University nor the names of its contributors
sl@0
    14
 *    may be used to endorse or promote products derived from this software
sl@0
    15
 *    without specific prior written permission.
sl@0
    16
 *
sl@0
    17
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
sl@0
    18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0
    19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
sl@0
    20
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
sl@0
    21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
sl@0
    22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
sl@0
    23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
    24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
sl@0
    25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
sl@0
    26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
sl@0
    27
 * SUCH DAMAGE.
sl@0
    28
 *
sl@0
    29
 *	@(#)endian.h	7.8 (Berkeley) 4/3/91
sl@0
    30
 * $FreeBSD: src/sys/i386/include/endian.h,v 1.41 2005/03/02 21:33:26 joerg Exp $
sl@0
    31
 */
sl@0
    32
sl@0
    33
#ifndef _MACHINE_ENDIAN_H_
sl@0
    34
#define	_MACHINE_ENDIAN_H_
sl@0
    35
sl@0
    36
#include <sys/cdefs.h>
sl@0
    37
#include <sys/_types.h>
sl@0
    38
sl@0
    39
#ifdef __cplusplus
sl@0
    40
extern "C" {
sl@0
    41
#endif
sl@0
    42
sl@0
    43
/*
sl@0
    44
 * Define the order of 32-bit words in 64-bit words.
sl@0
    45
 */
sl@0
    46
#define	_QUAD_HIGHWORD 1
sl@0
    47
#define	_QUAD_LOWWORD 0
sl@0
    48
sl@0
    49
/*
sl@0
    50
 * Definitions for byte order, according to byte significance from low
sl@0
    51
 * address to high.
sl@0
    52
 */
sl@0
    53
#define	_LITTLE_ENDIAN	1234	/* LSB first: i386, vax */
sl@0
    54
#define	_BIG_ENDIAN	4321	/* MSB first: 68000, ibm, net */
sl@0
    55
#define	_PDP_ENDIAN	3412	/* LSB first in word, MSW first in long */
sl@0
    56
sl@0
    57
#define	_BYTE_ORDER	_LITTLE_ENDIAN
sl@0
    58
sl@0
    59
/*
sl@0
    60
 * Deprecated variants that don't have enough underscores to be useful in more
sl@0
    61
 * strict namespaces.
sl@0
    62
 */
sl@0
    63
#if __BSD_VISIBLE
sl@0
    64
#define	LITTLE_ENDIAN	_LITTLE_ENDIAN
sl@0
    65
#define	BIG_ENDIAN	_BIG_ENDIAN
sl@0
    66
#define	PDP_ENDIAN	_PDP_ENDIAN
sl@0
    67
#define	BYTE_ORDER	_BYTE_ORDER
sl@0
    68
#endif
sl@0
    69
sl@0
    70
#ifdef __SYMBIAN32__
sl@0
    71
sl@0
    72
#define	_BYTEORDER_FUNC_DEFINED
sl@0
    73
sl@0
    74
#else /* __SYMBIAN32__ */
sl@0
    75
sl@0
    76
#if defined(__GNUCLIKE_ASM) && defined(__GNUCLIKE_BUILTIN_CONSTANT_P)
sl@0
    77
sl@0
    78
#define __word_swap_int_var(x) \
sl@0
    79
__extension__ ({ register __uint32_t __X = (x); \
sl@0
    80
   __asm ("rorl $16, %0" : "+r" (__X)); \
sl@0
    81
   __X; })
sl@0
    82
sl@0
    83
#ifdef __OPTIMIZE__
sl@0
    84
sl@0
    85
#define	__word_swap_int_const(x) \
sl@0
    86
	((((x) & 0xffff0000) >> 16) | \
sl@0
    87
	 (((x) & 0x0000ffff) << 16))
sl@0
    88
#define	__word_swap_int(x) (__builtin_constant_p(x) ? \
sl@0
    89
	__word_swap_int_const(x) : __word_swap_int_var(x))
sl@0
    90
sl@0
    91
#else	/* __OPTIMIZE__ */
sl@0
    92
sl@0
    93
#define	__word_swap_int(x) __word_swap_int_var(x)
sl@0
    94
sl@0
    95
#endif	/* __OPTIMIZE__ */
sl@0
    96
sl@0
    97
#define __byte_swap_int_var(x) \
sl@0
    98
__extension__ ({ register __uint32_t __X = (x); \
sl@0
    99
   __asm ("bswap %0" : "+r" (__X)); \
sl@0
   100
   __X; })
sl@0
   101
sl@0
   102
#ifdef __OPTIMIZE__
sl@0
   103
sl@0
   104
#define	__byte_swap_int_const(x) \
sl@0
   105
	((((x) & 0xff000000) >> 24) | \
sl@0
   106
	 (((x) & 0x00ff0000) >>  8) | \
sl@0
   107
	 (((x) & 0x0000ff00) <<  8) | \
sl@0
   108
	 (((x) & 0x000000ff) << 24))
sl@0
   109
#define	__byte_swap_int(x) (__builtin_constant_p(x) ? \
sl@0
   110
	__byte_swap_int_const(x) : __byte_swap_int_var(x))
sl@0
   111
sl@0
   112
#else	/* __OPTIMIZE__ */
sl@0
   113
sl@0
   114
#define	__byte_swap_int(x) __byte_swap_int_var(x)
sl@0
   115
sl@0
   116
#endif	/* __OPTIMIZE__ */
sl@0
   117
sl@0
   118
#define __byte_swap_word_var(x) \
sl@0
   119
__extension__ ({ register __uint16_t __X = (x); \
sl@0
   120
   __asm ("xchgb %h0, %b0" : "+q" (__X)); \
sl@0
   121
   __X; })
sl@0
   122
sl@0
   123
#ifdef __OPTIMIZE__
sl@0
   124
sl@0
   125
#define	__byte_swap_word_const(x) \
sl@0
   126
	((((x) & 0xff00) >> 8) | \
sl@0
   127
	 (((x) & 0x00ff) << 8))
sl@0
   128
sl@0
   129
#define	__byte_swap_word(x) (__builtin_constant_p(x) ? \
sl@0
   130
	__byte_swap_word_const(x) : __byte_swap_word_var(x))
sl@0
   131
sl@0
   132
#else	/* __OPTIMIZE__ */
sl@0
   133
sl@0
   134
#define	__byte_swap_word(x) __byte_swap_word_var(x)
sl@0
   135
sl@0
   136
#endif	/* __OPTIMIZE__ */
sl@0
   137
sl@0
   138
static __inline __uint64_t
sl@0
   139
__bswap64(__uint64_t _x)
sl@0
   140
{
sl@0
   141
sl@0
   142
	return ((_x >> 56) | ((_x >> 40) & 0xff00) | ((_x >> 24) & 0xff0000) |
sl@0
   143
	    ((_x >> 8) & 0xff000000) | ((_x << 8) & ((__uint64_t)0xff << 32)) |
sl@0
   144
	    ((_x << 24) & ((__uint64_t)0xff << 40)) |
sl@0
   145
	    ((_x << 40) & ((__uint64_t)0xff << 48)) | ((_x << 56)));
sl@0
   146
}
sl@0
   147
sl@0
   148
static __inline __uint32_t
sl@0
   149
__bswap32(__uint32_t _x)
sl@0
   150
{
sl@0
   151
sl@0
   152
	return (__byte_swap_int(_x));
sl@0
   153
}
sl@0
   154
sl@0
   155
static __inline __uint16_t
sl@0
   156
__bswap16(__uint16_t _x)
sl@0
   157
{
sl@0
   158
sl@0
   159
	return (__byte_swap_word(_x));
sl@0
   160
}
sl@0
   161
sl@0
   162
#define	__htonl(x)	__bswap32(x)
sl@0
   163
#define	__htons(x)	__bswap16(x)
sl@0
   164
#define	__ntohl(x)	__bswap32(x)
sl@0
   165
#define	__ntohs(x)	__bswap16(x)
sl@0
   166
sl@0
   167
#else /* !(__GNUCLIKE_ASM && __GNUCLIKE_BUILTIN_CONSTANT_P) */
sl@0
   168
sl@0
   169
/*
sl@0
   170
 * No optimizations are available for this compiler.  Fall back to
sl@0
   171
 * non-optimized functions by defining the constant usually used to prevent
sl@0
   172
 * redefinition.
sl@0
   173
 */
sl@0
   174
#define	_BYTEORDER_FUNC_DEFINED
sl@0
   175
sl@0
   176
#endif /* __GNUCLIKE_ASM && __GNUCLIKE_BUILTIN_CONSTANT_P */
sl@0
   177
#endif /* __SYMBIAN32__ */
sl@0
   178
sl@0
   179
#ifdef __cplusplus
sl@0
   180
}
sl@0
   181
#endif
sl@0
   182
sl@0
   183
#endif /* !_MACHINE_ENDIAN_H_ */