Xenomai  3.1-devel
compiler.h
1 /*
2  * Copyright (C) 2013 Philippe Gerum <rpm@xenomai.org>.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13 
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18 #ifndef _BOILERPLATE_COMPILER_H
19 #define _BOILERPLATE_COMPILER_H
20 
21 #include <stddef.h>
22 
23 #define container_of(ptr, type, member) \
24  ({ \
25  const __typeof__(((type *)0)->member) *__mptr = (ptr); \
26  (type *)((char *)__mptr - offsetof(type, member)); \
27  })
28 
29 #define __stringify_1(x...) #x
30 #define __stringify(x...) __stringify_1(x)
31 
32 #ifndef likely
33 #define likely(x) __builtin_expect(!!(x), 1)
34 #define unlikely(x) __builtin_expect(!!(x), 0)
35 #endif
36 
37 #ifndef __noreturn
38 #define __noreturn __attribute__((__noreturn__))
39 #endif
40 
41 #ifndef __must_check
42 #define __must_check __attribute__((__warn_unused_result__))
43 #endif
44 
45 #ifndef __weak
46 #define __weak __attribute__((__weak__))
47 #endif
48 
49 #ifndef __const
50 #define __const __attribute__((__const__))
51 #endif
52 
53 #ifndef __pure
54 #define __pure __attribute__((__pure__))
55 #endif
56 
57 #ifndef __maybe_unused
58 #define __maybe_unused __attribute__((__unused__))
59 #endif
60 
61 #ifndef __aligned
62 #define __aligned(__n) __attribute__((aligned (__n)))
63 #endif
64 
65 #ifndef __deprecated
66 #define __deprecated __attribute__((__deprecated__))
67 #endif
68 
69 #ifndef __packed
70 #define __packed __attribute__((__packed__))
71 #endif
72 
73 #ifndef __alloc_size
74 #define __alloc_size(__args) __attribute__((__alloc_size__(__args)))
75 #endif
76 
77 #define __align_to(__size, __al) (((__size) + (__al) - 1) & (~((__al) - 1)))
78 
79 #ifdef __cplusplus
80 extern "C" {
81 #endif
82 
83 void __invalid_operand_size(void);
84 
85 #define __ctz(__v) \
86  ({ \
87  int __ret; \
88  if (!__v) \
89  __ret = sizeof(__v) * 8; \
90  else \
91  switch (sizeof(__v)) { \
92  case sizeof(int): \
93  __ret = __builtin_ctz((unsigned int)__v); \
94  break; \
95  case sizeof(long long): \
96  __ret = __builtin_ctzll(__v); \
97  break; \
98  default: \
99  __invalid_operand_size(); \
100  } \
101  __ret; \
102  })
103 
104 #define __clz(__v) \
105  ({ \
106  int __ret; \
107  if (!__v) \
108  __ret = sizeof(__v) * 8; \
109  else \
110  switch (sizeof(__v)) { \
111  case sizeof(int): \
112  __ret = __builtin_clz((unsigned int)__v); \
113  break; \
114  case sizeof(long long): \
115  __ret = __builtin_clzll(__v); \
116  break; \
117  default: \
118  __invalid_operand_size(); \
119  } \
120  __ret; \
121  })
122 
123 #ifdef __cplusplus
124 }
125 #endif
126 
127 #endif /* _BOILERPLATE_COMPILER_H */