source: rtems/cpukit/include/sys/endian.h @ b2ee119f

4.115
Last change on this file since b2ee119f was b2ee119f, checked in by Sebastian Huber <sebastian.huber@…>, on 12/15/14 at 07:01:59

sys/endian.h: Fix 16-bit int problems

  • Property mode set to 100644
File size: 4.9 KB
Line 
1/*-
2 * Copyright (c) 2002 Thomas Moestl <tmm@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD$
27 */
28
29#ifndef _SYS_ENDIAN_H_
30#define _SYS_ENDIAN_H_
31
32#include <sys/cdefs.h>
33#include <rtems/endian.h>
34 
35/*
36 * General byte order swapping functions.
37 */
38#define bswap16(x)      CPU_swap_u16(x)
39#define bswap32(x)      CPU_swap_u32(x)
40static __inline uint64_t
41bswap64(uint64_t v)
42{
43#ifdef __GNUC__
44        return __builtin_bswap64(v);
45#else
46        return ((v >> 56) | ((v >> 40) & 0xff00) | ((v >> 24) & 0xff0000) |
47            ((v >> 8) & 0xff000000) | ((v << 8) & ((uint64_t)0xff << 32)) |
48            ((v << 24) & ((uint64_t)0xff << 40)) |
49            ((v << 40) & ((uint64_t)0xff << 48)) | ((v << 56)));
50#endif
51}
52
53/*
54 * Host to big endian, host to little endian, big endian to host, and little
55 * endian to host byte order functions as detailed in byteorder(9).
56 */
57#if BYTE_ORDER == LITTLE_ENDIAN
58#define htobe16(x)      bswap16((x))
59#define htobe32(x)      bswap32((x))
60#define htobe64(x)      bswap64((x))
61#define htole16(x)      ((uint16_t)(x))
62#define htole32(x)      ((uint32_t)(x))
63#define htole64(x)      ((uint64_t)(x))
64
65#define be16toh(x)      bswap16((x))
66#define be32toh(x)      bswap32((x))
67#define be64toh(x)      bswap64((x))
68#define le16toh(x)      ((uint16_t)(x))
69#define le32toh(x)      ((uint32_t)(x))
70#define le64toh(x)      ((uint64_t)(x))
71#else /* BYTE_ORDER != LITTLE_ENDIAN */
72#define htobe16(x)      ((uint16_t)(x))
73#define htobe32(x)      ((uint32_t)(x))
74#define htobe64(x)      ((uint64_t)(x))
75#define htole16(x)      bswap16((x))
76#define htole32(x)      bswap32((x))
77#define htole64(x)      bswap64((x))
78
79#define be16toh(x)      ((uint16_t)(x))
80#define be32toh(x)      ((uint32_t)(x))
81#define be64toh(x)      ((uint64_t)(x))
82#define le16toh(x)      bswap16((x))
83#define le32toh(x)      bswap32((x))
84#define le64toh(x)      bswap64((x))
85#endif /* BYTE_ORDER == LITTLE_ENDIAN */
86
87/* Alignment-agnostic encode/decode bytestream to/from little/big endian. */
88
89static __inline uint16_t
90be16dec(const void *pp)
91{
92        uint8_t const *p = (uint8_t const *)pp;
93
94        return (((unsigned)p[0] << 8) | p[1]);
95}
96
97static __inline uint32_t
98be32dec(const void *pp)
99{
100        uint8_t const *p = (uint8_t const *)pp;
101
102        return (((uint32_t)p[0] << 24) | ((uint32_t)p[1] << 16) |
103    ((uint32_t)p[2] << 8) | p[3]);
104}
105
106static __inline uint64_t
107be64dec(const void *pp)
108{
109        uint8_t const *p = (uint8_t const *)pp;
110
111        return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4));
112}
113
114static __inline uint16_t
115le16dec(const void *pp)
116{
117        uint8_t const *p = (uint8_t const *)pp;
118
119        return (((unsigned)p[1] << 8) | p[0]);
120}
121
122static __inline uint32_t
123le32dec(const void *pp)
124{
125        uint8_t const *p = (uint8_t const *)pp;
126
127        return (((uint32_t)p[3] << 24) | ((uint32_t)p[2] << 16) |
128    ((uint32_t)p[1] << 8) | p[0]);
129}
130
131static __inline uint64_t
132le64dec(const void *pp)
133{
134        uint8_t const *p = (uint8_t const *)pp;
135
136        return (((uint64_t)le32dec(p + 4) << 32) | le32dec(p));
137}
138
139static __inline void
140be16enc(void *pp, uint16_t u)
141{
142        uint8_t *p = (uint8_t *)pp;
143
144        p[0] = (u >> 8) & 0xff;
145        p[1] = u & 0xff;
146}
147
148static __inline void
149be32enc(void *pp, uint32_t u)
150{
151        uint8_t *p = (uint8_t *)pp;
152
153        p[0] = (u >> 24) & 0xff;
154        p[1] = (u >> 16) & 0xff;
155        p[2] = (u >> 8) & 0xff;
156        p[3] = u & 0xff;
157}
158
159static __inline void
160be64enc(void *pp, uint64_t u)
161{
162        uint8_t *p = (uint8_t *)pp;
163
164        be32enc(p, (uint32_t)(u >> 32));
165        be32enc(p + 4, (uint32_t)(u & 0xffffffffU));
166}
167
168static __inline void
169le16enc(void *pp, uint16_t u)
170{
171        uint8_t *p = (uint8_t *)pp;
172
173        p[0] = u & 0xff;
174        p[1] = (u >> 8) & 0xff;
175}
176
177static __inline void
178le32enc(void *pp, uint32_t u)
179{
180        uint8_t *p = (uint8_t *)pp;
181
182        p[0] = u & 0xff;
183        p[1] = (u >> 8) & 0xff;
184        p[2] = (u >> 16) & 0xff;
185        p[3] = (u >> 24) & 0xff;
186}
187
188static __inline void
189le64enc(void *pp, uint64_t u)
190{
191        uint8_t *p = (uint8_t *)pp;
192
193        le32enc(p, (uint32_t)(u & 0xffffffffU));
194        le32enc(p + 4, (uint32_t)(u >> 32));
195}
196
197#endif  /* _SYS_ENDIAN_H_ */
Note: See TracBrowser for help on using the repository browser.