os/ossrv/genericopenlibs/openenvcore/include/sys/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) 2002 Thomas Moestl <tmm@FreeBSD.org>
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
 *
sl@0
    14
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
sl@0
    15
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0
    16
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
sl@0
    17
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
sl@0
    18
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
sl@0
    19
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
sl@0
    20
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
    21
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
sl@0
    22
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
sl@0
    23
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
sl@0
    24
 * SUCH DAMAGE.
sl@0
    25
 *
sl@0
    26
 * $FreeBSD: src/sys/sys/endian.h,v 1.6 2003/10/15 20:05:57 obrien Exp $
sl@0
    27
 */
sl@0
    28
sl@0
    29
#ifndef _SYS_ENDIAN_H_
sl@0
    30
#define _SYS_ENDIAN_H_
sl@0
    31
sl@0
    32
#include <sys/cdefs.h>
sl@0
    33
#include <sys/_types.h>
sl@0
    34
#include <stdapis/machine/endian.h>
sl@0
    35
sl@0
    36
#ifndef _UINT16_T_DECLARED
sl@0
    37
typedef	__uint16_t	uint16_t;
sl@0
    38
#define	_UINT16_T_DECLARED
sl@0
    39
#endif
sl@0
    40
 
sl@0
    41
#ifndef _UINT32_T_DECLARED
sl@0
    42
typedef	__uint32_t	uint32_t;
sl@0
    43
#define	_UINT32_T_DECLARED
sl@0
    44
#endif
sl@0
    45
 
sl@0
    46
#ifndef _UINT64_T_DECLARED
sl@0
    47
typedef	__uint64_t	uint64_t;
sl@0
    48
#define	_UINT64_T_DECLARED
sl@0
    49
#endif
sl@0
    50
 
sl@0
    51
/*
sl@0
    52
 * General byte order swapping functions.
sl@0
    53
 */
sl@0
    54
#define	bswap16(x)	__bswap16(x)
sl@0
    55
#define	bswap32(x)	__bswap32(x)
sl@0
    56
#define	bswap64(x)	__bswap64(x)
sl@0
    57
sl@0
    58
/*
sl@0
    59
 * Host to big endian, host to little endian, big endian to host, and little
sl@0
    60
 * endian to host byte order functions as detailed in byteorder(9).
sl@0
    61
 */
sl@0
    62
#if _BYTE_ORDER == _LITTLE_ENDIAN
sl@0
    63
#define	htobe16(x)	bswap16((x))
sl@0
    64
#define	htobe32(x)	bswap32((x))
sl@0
    65
#define	htobe64(x)	bswap64((x))
sl@0
    66
#define	htole16(x)	((uint16_t)(x))
sl@0
    67
#define	htole32(x)	((uint32_t)(x))
sl@0
    68
#define	htole64(x)	((uint64_t)(x))
sl@0
    69
sl@0
    70
#define	be16toh(x)	bswap16((x))
sl@0
    71
#define	be32toh(x)	bswap32((x))
sl@0
    72
#define	be64toh(x)	bswap64((x))
sl@0
    73
#define	le16toh(x)	((uint16_t)(x))
sl@0
    74
#define	le32toh(x)	((uint32_t)(x))
sl@0
    75
#define	le64toh(x)	((uint64_t)(x))
sl@0
    76
#else /* _BYTE_ORDER != _LITTLE_ENDIAN */
sl@0
    77
#define	htobe16(x)	((uint16_t)(x))
sl@0
    78
#define	htobe32(x)	((uint32_t)(x))
sl@0
    79
#define	htobe64(x)	((uint64_t)(x))
sl@0
    80
#define	htole16(x)	bswap16((x))
sl@0
    81
#define	htole32(x)	bswap32((x))
sl@0
    82
#define	htole64(x)	bswap64((x))
sl@0
    83
sl@0
    84
#define	be16toh(x)	((uint16_t)(x))
sl@0
    85
#define	be32toh(x)	((uint32_t)(x))
sl@0
    86
#define	be64toh(x)	((uint64_t)(x))
sl@0
    87
#define	le16toh(x)	bswap16((x))
sl@0
    88
#define	le32toh(x)	bswap32((x))
sl@0
    89
#define	le64toh(x)	bswap64((x))
sl@0
    90
#endif /* _BYTE_ORDER == _LITTLE_ENDIAN */
sl@0
    91
sl@0
    92
/* Alignment-agnostic encode/decode bytestream to/from little/big endian. */
sl@0
    93
sl@0
    94
static __inline uint16_t
sl@0
    95
be16dec(const void *pp)
sl@0
    96
{
sl@0
    97
	unsigned char const *p = (unsigned char const *)pp;
sl@0
    98
sl@0
    99
	return ((p[0] << 8) | p[1]);
sl@0
   100
}
sl@0
   101
sl@0
   102
static __inline uint32_t
sl@0
   103
be32dec(const void *pp)
sl@0
   104
{
sl@0
   105
	unsigned char const *p = (unsigned char const *)pp;
sl@0
   106
sl@0
   107
	return ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
sl@0
   108
}
sl@0
   109
sl@0
   110
static __inline uint64_t
sl@0
   111
be64dec(const void *pp)
sl@0
   112
{
sl@0
   113
	unsigned char const *p = (unsigned char const *)pp;
sl@0
   114
sl@0
   115
	return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4));
sl@0
   116
}
sl@0
   117
sl@0
   118
static __inline uint16_t
sl@0
   119
le16dec(const void *pp)
sl@0
   120
{
sl@0
   121
	unsigned char const *p = (unsigned char const *)pp;
sl@0
   122
sl@0
   123
	return ((p[1] << 8) | p[0]);
sl@0
   124
}
sl@0
   125
sl@0
   126
static __inline uint32_t
sl@0
   127
le32dec(const void *pp)
sl@0
   128
{
sl@0
   129
	unsigned char const *p = (unsigned char const *)pp;
sl@0
   130
sl@0
   131
	return ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
sl@0
   132
}
sl@0
   133
sl@0
   134
static __inline uint64_t
sl@0
   135
le64dec(const void *pp)
sl@0
   136
{
sl@0
   137
	unsigned char const *p = (unsigned char const *)pp;
sl@0
   138
sl@0
   139
	return (((uint64_t)le32dec(p + 4) << 32) | le32dec(p));
sl@0
   140
}
sl@0
   141
sl@0
   142
static __inline void
sl@0
   143
be16enc(void *pp, uint16_t u)
sl@0
   144
{
sl@0
   145
	unsigned char *p = (unsigned char *)pp;
sl@0
   146
sl@0
   147
	p[0] = (u >> 8) & 0xff;
sl@0
   148
	p[1] = u & 0xff;
sl@0
   149
}
sl@0
   150
sl@0
   151
static __inline void
sl@0
   152
be32enc(void *pp, uint32_t u)
sl@0
   153
{
sl@0
   154
	unsigned char *p = (unsigned char *)pp;
sl@0
   155
sl@0
   156
	p[0] = (u >> 24) & 0xff;
sl@0
   157
	p[1] = (u >> 16) & 0xff;
sl@0
   158
	p[2] = (u >> 8) & 0xff;
sl@0
   159
	p[3] = u & 0xff;
sl@0
   160
}
sl@0
   161
sl@0
   162
static __inline void
sl@0
   163
be64enc(void *pp, uint64_t u)
sl@0
   164
{
sl@0
   165
	unsigned char *p = (unsigned char *)pp;
sl@0
   166
sl@0
   167
	be32enc(p, u >> 32);
sl@0
   168
	be32enc(p + 4, u & 0xffffffff);
sl@0
   169
}
sl@0
   170
sl@0
   171
static __inline void
sl@0
   172
le16enc(void *pp, uint16_t u)
sl@0
   173
{
sl@0
   174
	unsigned char *p = (unsigned char *)pp;
sl@0
   175
sl@0
   176
	p[0] = u & 0xff;
sl@0
   177
	p[1] = (u >> 8) & 0xff;
sl@0
   178
}
sl@0
   179
sl@0
   180
static __inline void
sl@0
   181
le32enc(void *pp, uint32_t u)
sl@0
   182
{
sl@0
   183
	unsigned char *p = (unsigned char *)pp;
sl@0
   184
sl@0
   185
	p[0] = u & 0xff;
sl@0
   186
	p[1] = (u >> 8) & 0xff;
sl@0
   187
	p[2] = (u >> 16) & 0xff;
sl@0
   188
	p[3] = (u >> 24) & 0xff;
sl@0
   189
}
sl@0
   190
sl@0
   191
static __inline void
sl@0
   192
le64enc(void *pp, uint64_t u)
sl@0
   193
{
sl@0
   194
	unsigned char *p = (unsigned char *)pp;
sl@0
   195
sl@0
   196
	le32enc(p, u & 0xffffffff);
sl@0
   197
	le32enc(p + 4, u >> 32);
sl@0
   198
}
sl@0
   199
sl@0
   200
#endif	/* _SYS_ENDIAN_H_ */