target/arm_adi_v5: fix sync CSW cache on apreg write
[openocd.git] / src / helper / types.h
blob5e35c13b796c520487fcb2b8f4e1a85bf7ed55ef
1 /***************************************************************************
2 * Copyright (C) 2004, 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
20 ***************************************************************************/
22 #ifndef OPENOCD_HELPER_TYPES_H
23 #define OPENOCD_HELPER_TYPES_H
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
29 #include <stddef.h>
30 #include <assert.h>
31 #ifdef HAVE_SYS_TYPES_H
32 #include <sys/types.h>
33 #endif
34 #ifdef HAVE_STDINT_H
35 #include <stdint.h>
36 #endif
37 #ifdef HAVE_INTTYPES_H
38 #include <inttypes.h>
39 #endif
41 #ifdef HAVE_STDBOOL_H
42 #include <stdbool.h>
43 #else /* HAVE_STDBOOL_H */
44 #define __bool_true_false_are_defined 1
46 #ifndef HAVE__BOOL
47 #ifndef __cplusplus
49 #define false 0
50 #define true 1
52 typedef int _Bool;
53 #else
54 typedef bool _Bool;
55 #endif /* __cplusplus */
56 #endif /* HAVE__BOOL */
58 #define bool _Bool
60 #endif /* HAVE_STDBOOL_H */
62 /// turns a macro argument into a string constant
63 #define stringify(s) __stringify(s)
64 #define __stringify(s) #s
67 /**
68 * Compute the number of elements of a variable length array.
69 * <code>
70 * const char *strs[] = { "a", "b", "c" };
71 * unsigned num_strs = ARRAY_SIZE(strs);
72 * </code>
74 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
77 /**
78 * Cast a member of a structure out to the containing structure.
79 * @param ptr The pointer to the member.
80 * @param type The type of the container struct this is embedded in.
81 * @param member The name of the member within the struct.
83 * This is a mechanism which is used throughout the Linux kernel.
85 #define container_of(ptr, type, member) ({ \
86 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
87 (type *)( (void *) ( (char *)__mptr - offsetof(type,member) ) );})
90 /**
91 * Rounds @c m up to the nearest multiple of @c n using division.
92 * @param m The value to round up to @c n.
93 * @param n Round @c m up to a multiple of this number.
94 * @returns The rounded integer value.
96 #define DIV_ROUND_UP(m, n) (((m) + (n) - 1) / (n))
99 /* DANGER!!!! here be dragons!
101 * Leave these fn's as byte accesses because it is safe
102 * across architectures. Clever usage of 32 bit access
103 * will create problems on some hosts.
105 * Note that the "buf" pointer in memory is probably unaligned.
107 * Were these functions to be re-written to take a 32 bit wide or 16 bit wide
108 * memory access shortcut, then on some CPU's, i.e. ARM7, the 2 lsbytes of the address are
109 * ignored for 32 bit access, whereas on other CPU's a 32 bit wide unaligned memory access
110 * will cause an exception, and lastly on x86, an unaligned "greater than bytewide"
111 * memory access works as if aligned. So what follows below will work for all
112 * platforms and gives the compiler leeway to do its own platform specific optimizations.
114 * Again, note that the "buf" pointer in memory is probably unaligned.
117 static inline uint64_t le_to_h_u64(const uint8_t *buf)
119 return (uint64_t)((uint64_t)buf[0] |
120 (uint64_t)buf[1] << 8 |
121 (uint64_t)buf[2] << 16 |
122 (uint64_t)buf[3] << 24 |
123 (uint64_t)buf[4] << 32 |
124 (uint64_t)buf[5] << 40 |
125 (uint64_t)buf[6] << 48 |
126 (uint64_t)buf[7] << 56);
129 static inline uint32_t le_to_h_u32(const uint8_t* buf)
131 return (uint32_t)((uint32_t)buf[0] | (uint32_t)buf[1] << 8 | (uint32_t)buf[2] << 16 | (uint32_t)buf[3] << 24);
134 static inline uint32_t le_to_h_u24(const uint8_t* buf)
136 return (uint32_t)((uint32_t)buf[0] | (uint32_t)buf[1] << 8 | (uint32_t)buf[2] << 16);
139 static inline uint16_t le_to_h_u16(const uint8_t* buf)
141 return (uint16_t)((uint16_t)buf[0] | (uint16_t)buf[1] << 8);
144 static inline uint64_t be_to_h_u64(const uint8_t *buf)
146 return (uint64_t)((uint64_t)buf[7] |
147 (uint64_t)buf[6] << 8 |
148 (uint64_t)buf[5] << 16 |
149 (uint64_t)buf[4] << 24 |
150 (uint64_t)buf[3] << 32 |
151 (uint64_t)buf[2] << 40 |
152 (uint64_t)buf[1] << 48 |
153 (uint64_t)buf[0] << 56);
156 static inline uint32_t be_to_h_u32(const uint8_t* buf)
158 return (uint32_t)((uint32_t)buf[3] | (uint32_t)buf[2] << 8 | (uint32_t)buf[1] << 16 | (uint32_t)buf[0] << 24);
161 static inline uint32_t be_to_h_u24(const uint8_t* buf)
163 return (uint32_t)((uint32_t)buf[2] | (uint32_t)buf[1] << 8 | (uint32_t)buf[0] << 16);
166 static inline uint16_t be_to_h_u16(const uint8_t* buf)
168 return (uint16_t)((uint16_t)buf[1] | (uint16_t)buf[0] << 8);
171 static inline void h_u64_to_le(uint8_t *buf, int64_t val)
173 buf[7] = (uint8_t) (val >> 56);
174 buf[6] = (uint8_t) (val >> 48);
175 buf[5] = (uint8_t) (val >> 40);
176 buf[4] = (uint8_t) (val >> 32);
177 buf[3] = (uint8_t) (val >> 24);
178 buf[2] = (uint8_t) (val >> 16);
179 buf[1] = (uint8_t) (val >> 8);
180 buf[0] = (uint8_t) (val >> 0);
183 static inline void h_u64_to_be(uint8_t *buf, int64_t val)
185 buf[0] = (uint8_t) (val >> 56);
186 buf[1] = (uint8_t) (val >> 48);
187 buf[2] = (uint8_t) (val >> 40);
188 buf[3] = (uint8_t) (val >> 32);
189 buf[4] = (uint8_t) (val >> 24);
190 buf[5] = (uint8_t) (val >> 16);
191 buf[6] = (uint8_t) (val >> 8);
192 buf[7] = (uint8_t) (val >> 0);
195 static inline void h_u32_to_le(uint8_t* buf, int val)
197 buf[3] = (uint8_t) (val >> 24);
198 buf[2] = (uint8_t) (val >> 16);
199 buf[1] = (uint8_t) (val >> 8);
200 buf[0] = (uint8_t) (val >> 0);
203 static inline void h_u32_to_be(uint8_t* buf, int val)
205 buf[0] = (uint8_t) (val >> 24);
206 buf[1] = (uint8_t) (val >> 16);
207 buf[2] = (uint8_t) (val >> 8);
208 buf[3] = (uint8_t) (val >> 0);
211 static inline void h_u24_to_le(uint8_t* buf, int val)
213 buf[2] = (uint8_t) (val >> 16);
214 buf[1] = (uint8_t) (val >> 8);
215 buf[0] = (uint8_t) (val >> 0);
218 static inline void h_u24_to_be(uint8_t* buf, int val)
220 buf[0] = (uint8_t) (val >> 16);
221 buf[1] = (uint8_t) (val >> 8);
222 buf[2] = (uint8_t) (val >> 0);
225 static inline void h_u16_to_le(uint8_t* buf, int val)
227 buf[1] = (uint8_t) (val >> 8);
228 buf[0] = (uint8_t) (val >> 0);
231 static inline void h_u16_to_be(uint8_t* buf, int val)
233 buf[0] = (uint8_t) (val >> 8);
234 buf[1] = (uint8_t) (val >> 0);
238 * Byte-swap buffer 16-bit.
240 * Len must be even, dst and src must be either the same or non-overlapping.
242 * @param dst Destination buffer.
243 * @param src Source buffer.
244 * @param len Length of source (and destination) buffer, in bytes.
246 static inline void buf_bswap16(uint8_t *dst, const uint8_t *src, size_t len)
248 assert(len % 2 == 0);
249 assert(dst == src || dst + len <= src || src + len <= dst);
251 for (size_t n = 0; n < len; n += 2) {
252 uint16_t x = be_to_h_u16(src + n);
253 h_u16_to_le(dst + n, x);
258 * Byte-swap buffer 32-bit.
260 * Len must be divisible by four, dst and src must be either the same or non-overlapping.
262 * @param dst Destination buffer.
263 * @param src Source buffer.
264 * @param len Length of source (and destination) buffer, in bytes.
266 static inline void buf_bswap32(uint8_t *dst, const uint8_t *src, size_t len)
268 assert(len % 4 == 0);
269 assert(dst == src || dst + len <= src || src + len <= dst);
271 for (size_t n = 0; n < len; n += 4) {
272 uint32_t x = be_to_h_u32(src + n);
273 h_u32_to_le(dst + n, x);
278 * Calculate the (even) parity of a 32-bit datum.
279 * @param x The datum.
280 * @return 1 if the number of set bits in x is odd, 0 if it is even.
282 static inline int parity_u32(uint32_t x)
284 #ifdef __GNUC__
285 return __builtin_parityl(x);
286 #else
287 x ^= x >> 16;
288 x ^= x >> 8;
289 x ^= x >> 4;
290 x ^= x >> 2;
291 x ^= x >> 1;
292 return x & 1;
293 #endif
296 #if defined(__ECOS)
298 /* eCos plain lacks these definition... A series of upstream patches
299 * could probably repair it, but it seems like too much work to be
300 * worth it.
303 #if !defined(_STDINT_H)
304 #define PRId32 "d"
305 #define PRIi32 "i"
306 #define PRIo32 "o"
307 #define PRIu32 "u"
308 #define PRIx32 "x"
309 #define PRIX32 "X"
310 #define SCNx32 "x"
311 #define PRId8 PRId32
312 #define SCNx64 "llx"
313 #define PRId64 "lld"
314 #define PRIi64 "lli"
315 #define PRIo64 "llo"
316 #define PRIu64 "llu"
317 #define PRIx64 "llx"
318 #define PRIX64 "llX"
320 typedef CYG_ADDRWORD intptr_t;
321 typedef int64_t intmax_t;
322 typedef uint64_t uintmax_t;
323 #define INT8_MAX 0x7f
324 #define INT8_MIN (-INT8_MAX - 1)
325 # define UINT8_MAX (255)
326 #define INT16_MAX 0x7fff
327 #define INT16_MIN (-INT16_MAX - 1)
328 # define UINT16_MAX (65535)
329 #define INT32_MAX 0x7fffffffL
330 #define INT32_MIN (-INT32_MAX - 1L)
331 # define UINT32_MAX (4294967295U)
332 #define INT64_MAX 0x7fffffffffffffffLL
333 #define INT64_MIN (-INT64_MAX - 1LL)
334 #define UINT64_MAX (__CONCAT(INT64_MAX, U) * 2ULL + 1ULL)
335 #endif
337 #ifndef LLONG_MAX
338 #define ULLONG_MAX UINT64_C(0xFFFFFFFFFFFFFFFF)
339 #define LLONG_MAX INT64_C(0x7FFFFFFFFFFFFFFF)
340 #define LLONG_MIN ULLONG_MAX
341 #endif
344 #define ULLONG_MAX 18446744073709551615
346 /* C99, eCos is C90 compliant (with bits of C99) */
347 #define isblank(c) ((c) == ' ' || (c) == '\t')
350 #endif
352 #if BUILD_TARGET64
353 typedef uint64_t target_addr_t;
354 #define TARGET_ADDR_MAX UINT64_MAX
355 #define TARGET_PRIdADDR PRId64
356 #define TARGET_PRIuADDR PRIu64
357 #define TARGET_PRIoADDR PRIo64
358 #define TARGET_PRIxADDR PRIx64
359 #define TARGET_PRIXADDR PRIX64
360 #else
361 typedef uint32_t target_addr_t;
362 #define TARGET_ADDR_MAX UINT32_MAX
363 #define TARGET_PRIdADDR PRId32
364 #define TARGET_PRIuADDR PRIu32
365 #define TARGET_PRIoADDR PRIo32
366 #define TARGET_PRIxADDR PRIx32
367 #define TARGET_PRIXADDR PRIX32
368 #endif
369 #define TARGET_ADDR_FMT "0x%8.8" TARGET_PRIxADDR
371 #endif /* OPENOCD_HELPER_TYPES_H */