OpenOCD
types.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 /***************************************************************************
4  * Copyright (C) 2004, 2005 by Dominic Rath *
5  * Dominic.Rath@gmx.de *
6  * *
7  * Copyright (C) 2007,2008 Øyvind Harboe *
8  * oyvind.harboe@zylin.com *
9  ***************************************************************************/
10 
11 #ifndef OPENOCD_HELPER_TYPES_H
12 #define OPENOCD_HELPER_TYPES_H
13 
14 #ifdef HAVE_CONFIG_H
15 #include "config.h"
16 #endif
17 
18 #include <stddef.h>
19 #include <assert.h>
20 #ifdef HAVE_SYS_TYPES_H
21 #include <sys/types.h>
22 #endif
23 #ifdef HAVE_STDINT_H
24 #include <stdint.h>
25 #endif
26 #ifdef HAVE_INTTYPES_H
27 #include <inttypes.h>
28 #endif
29 
30 #ifdef HAVE_STDBOOL_H
31 #include <stdbool.h>
32 #else /* HAVE_STDBOOL_H */
33 #define __bool_true_false_are_defined 1
34 
35 #ifndef HAVE__BOOL
36 #ifndef __cplusplus
37 
38 #define false 0
39 #define true 1
40 
41 #endif /* __cplusplus */
42 #endif /* HAVE__BOOL */
43 #endif /* HAVE_STDBOOL_H */
44 
46 #define stringify(s) __stringify(s)
47 #define __stringify(s) #s
48 
49 
57 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
58 
59 
68 #define container_of(ptr, type, member) ({ \
69  const typeof( ((type *)0)->member ) *__mptr = (ptr); \
70  (type *)( (void *) ( (char *)__mptr - offsetof(type,member) ) );})
71 
72 
79 #define DIV_ROUND_UP(m, n) (((m) + (n) - 1) / (n))
80 
81 
82 /* DANGER!!!! here be dragons!
83  *
84  * Leave these fn's as byte accesses because it is safe
85  * across architectures. Clever usage of 32 bit access
86  * will create problems on some hosts.
87  *
88  * Note that the "buf" pointer in memory is probably unaligned.
89  *
90  * Were these functions to be re-written to take a 32 bit wide or 16 bit wide
91  * memory access shortcut, then on some CPU's, i.e. ARM7, the 2 lsbytes of the address are
92  * ignored for 32 bit access, whereas on other CPU's a 32 bit wide unaligned memory access
93  * will cause an exception, and lastly on x86, an unaligned "greater than bytewide"
94  * memory access works as if aligned. So what follows below will work for all
95  * platforms and gives the compiler leeway to do its own platform specific optimizations.
96  *
97  * Again, note that the "buf" pointer in memory is probably unaligned.
98  */
99 
100 static inline uint64_t le_to_h_u64(const uint8_t *buf)
101 {
102  return (uint64_t)((uint64_t)buf[0] |
103  (uint64_t)buf[1] << 8 |
104  (uint64_t)buf[2] << 16 |
105  (uint64_t)buf[3] << 24 |
106  (uint64_t)buf[4] << 32 |
107  (uint64_t)buf[5] << 40 |
108  (uint64_t)buf[6] << 48 |
109  (uint64_t)buf[7] << 56);
110 }
111 
112 static inline uint32_t le_to_h_u32(const uint8_t *buf)
113 {
114  return (uint32_t)((uint32_t)buf[0] | (uint32_t)buf[1] << 8 | (uint32_t)buf[2] << 16 | (uint32_t)buf[3] << 24);
115 }
116 
117 static inline uint32_t le_to_h_u24(const uint8_t *buf)
118 {
119  return (uint32_t)((uint32_t)buf[0] | (uint32_t)buf[1] << 8 | (uint32_t)buf[2] << 16);
120 }
121 
122 static inline uint16_t le_to_h_u16(const uint8_t *buf)
123 {
124  return (uint16_t)((uint16_t)buf[0] | (uint16_t)buf[1] << 8);
125 }
126 
127 static inline uint64_t be_to_h_u64(const uint8_t *buf)
128 {
129  return (uint64_t)((uint64_t)buf[7] |
130  (uint64_t)buf[6] << 8 |
131  (uint64_t)buf[5] << 16 |
132  (uint64_t)buf[4] << 24 |
133  (uint64_t)buf[3] << 32 |
134  (uint64_t)buf[2] << 40 |
135  (uint64_t)buf[1] << 48 |
136  (uint64_t)buf[0] << 56);
137 }
138 
139 static inline uint32_t be_to_h_u32(const uint8_t *buf)
140 {
141  return (uint32_t)((uint32_t)buf[3] | (uint32_t)buf[2] << 8 | (uint32_t)buf[1] << 16 | (uint32_t)buf[0] << 24);
142 }
143 
144 static inline uint32_t be_to_h_u24(const uint8_t *buf)
145 {
146  return (uint32_t)((uint32_t)buf[2] | (uint32_t)buf[1] << 8 | (uint32_t)buf[0] << 16);
147 }
148 
149 static inline uint16_t be_to_h_u16(const uint8_t *buf)
150 {
151  return (uint16_t)((uint16_t)buf[1] | (uint16_t)buf[0] << 8);
152 }
153 
154 static inline void h_u64_to_le(uint8_t *buf, uint64_t val)
155 {
156  buf[7] = (uint8_t) (val >> 56);
157  buf[6] = (uint8_t) (val >> 48);
158  buf[5] = (uint8_t) (val >> 40);
159  buf[4] = (uint8_t) (val >> 32);
160  buf[3] = (uint8_t) (val >> 24);
161  buf[2] = (uint8_t) (val >> 16);
162  buf[1] = (uint8_t) (val >> 8);
163  buf[0] = (uint8_t) (val >> 0);
164 }
165 
166 static inline void h_u64_to_be(uint8_t *buf, uint64_t val)
167 {
168  buf[0] = (uint8_t) (val >> 56);
169  buf[1] = (uint8_t) (val >> 48);
170  buf[2] = (uint8_t) (val >> 40);
171  buf[3] = (uint8_t) (val >> 32);
172  buf[4] = (uint8_t) (val >> 24);
173  buf[5] = (uint8_t) (val >> 16);
174  buf[6] = (uint8_t) (val >> 8);
175  buf[7] = (uint8_t) (val >> 0);
176 }
177 
178 static inline void h_u32_to_le(uint8_t *buf, uint32_t val)
179 {
180  buf[3] = (val >> 24) & 0xff;
181  buf[2] = (val >> 16) & 0xff;
182  buf[1] = (val >> 8) & 0xff;
183  buf[0] = (val >> 0) & 0xff;
184 }
185 
186 static inline void h_u32_to_be(uint8_t *buf, uint32_t val)
187 {
188  buf[0] = (val >> 24) & 0xff;
189  buf[1] = (val >> 16) & 0xff;
190  buf[2] = (val >> 8) & 0xff;
191  buf[3] = (val >> 0) & 0xff;
192 }
193 
194 static inline void h_u24_to_le(uint8_t *buf, unsigned int val)
195 {
196  buf[2] = (val >> 16) & 0xff;
197  buf[1] = (val >> 8) & 0xff;
198  buf[0] = (val >> 0) & 0xff;
199 }
200 
201 static inline void h_u24_to_be(uint8_t *buf, unsigned int val)
202 {
203  buf[0] = (val >> 16) & 0xff;
204  buf[1] = (val >> 8) & 0xff;
205  buf[2] = (val >> 0) & 0xff;
206 }
207 
208 static inline void h_u16_to_le(uint8_t *buf, uint16_t val)
209 {
210  buf[1] = (val >> 8) & 0xff;
211  buf[0] = (val >> 0) & 0xff;
212 }
213 
214 static inline void h_u16_to_be(uint8_t *buf, uint16_t val)
215 {
216  buf[0] = (val >> 8) & 0xff;
217  buf[1] = (val >> 0) & 0xff;
218 }
219 
229 static inline void buf_bswap16(uint8_t *dst, const uint8_t *src, size_t len)
230 {
231  assert(len % 2 == 0);
232  assert(dst == src || dst + len <= src || src + len <= dst);
233 
234  for (size_t n = 0; n < len; n += 2) {
235  uint16_t x = be_to_h_u16(src + n);
236  h_u16_to_le(dst + n, x);
237  }
238 }
239 
249 static inline void buf_bswap32(uint8_t *dst, const uint8_t *src, size_t len)
250 {
251  assert(len % 4 == 0);
252  assert(dst == src || dst + len <= src || src + len <= dst);
253 
254  for (size_t n = 0; n < len; n += 4) {
255  uint32_t x = be_to_h_u32(src + n);
256  h_u32_to_le(dst + n, x);
257  }
258 }
259 
265 static inline int parity_u32(uint32_t x)
266 {
267 #ifdef __GNUC__
268  return __builtin_parityl(x);
269 #else
270  x ^= x >> 16;
271  x ^= x >> 8;
272  x ^= x >> 4;
273  x ^= x >> 2;
274  x ^= x >> 1;
275  return x & 1;
276 #endif
277 }
278 
279 typedef uint64_t target_addr_t;
280 #define TARGET_ADDR_MAX UINT64_MAX
281 #define TARGET_PRIdADDR PRId64
282 #define TARGET_PRIuADDR PRIu64
283 #define TARGET_PRIoADDR PRIo64
284 #define TARGET_PRIxADDR PRIx64
285 #define TARGET_PRIXADDR PRIX64
286 #define TARGET_ADDR_FMT "0x%8.8" TARGET_PRIxADDR
287 
288 #endif /* OPENOCD_HELPER_TYPES_H */
static void h_u32_to_be(uint8_t *buf, uint32_t val)
Definition: types.h:186
static uint64_t le_to_h_u64(const uint8_t *buf)
Definition: types.h:100
static uint32_t be_to_h_u24(const uint8_t *buf)
Definition: types.h:144
static void h_u16_to_be(uint8_t *buf, uint16_t val)
Definition: types.h:214
static void buf_bswap16(uint8_t *dst, const uint8_t *src, size_t len)
Byte-swap buffer 16-bit.
Definition: types.h:229
static uint64_t be_to_h_u64(const uint8_t *buf)
Definition: types.h:127
static uint16_t le_to_h_u16(const uint8_t *buf)
Definition: types.h:122
static uint32_t le_to_h_u24(const uint8_t *buf)
Definition: types.h:117
static void h_u32_to_le(uint8_t *buf, uint32_t val)
Definition: types.h:178
static uint32_t be_to_h_u32(const uint8_t *buf)
Definition: types.h:139
uint64_t target_addr_t
Definition: types.h:279
static void h_u24_to_le(uint8_t *buf, unsigned int val)
Definition: types.h:194
static void h_u24_to_be(uint8_t *buf, unsigned int val)
Definition: types.h:201
static uint16_t be_to_h_u16(const uint8_t *buf)
Definition: types.h:149
static void h_u16_to_le(uint8_t *buf, uint16_t val)
Definition: types.h:208
static uint32_t le_to_h_u32(const uint8_t *buf)
Definition: types.h:112
static int parity_u32(uint32_t x)
Calculate the (even) parity of a 32-bit datum.
Definition: types.h:265
static void h_u64_to_be(uint8_t *buf, uint64_t val)
Definition: types.h:166
static void h_u64_to_le(uint8_t *buf, uint64_t val)
Definition: types.h:154
static void buf_bswap32(uint8_t *dst, const uint8_t *src, size_t len)
Byte-swap buffer 32-bit.
Definition: types.h:249