Xenomai  3.0.8
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 __maybe_unused
50 #define __maybe_unused __attribute__((__unused__))
51 #endif
52 
53 #ifndef __aligned
54 #define __aligned(__n) __attribute__((aligned (__n)))
55 #endif
56 
57 #ifndef __deprecated
58 #define __deprecated __attribute__((__deprecated__))
59 #endif
60 
61 #ifdef __cplusplus
62 extern "C" {
63 #endif
64 
65 void __invalid_operand_size(void);
66 
67 #define __ctz(__v) \
68  ({ \
69  int __ret; \
70  if (!__v) \
71  __ret = sizeof(__v) * 8; \
72  else \
73  switch (sizeof(__v)) { \
74  case sizeof(int): \
75  __ret = __builtin_ctz((unsigned int)__v); \
76  break; \
77  case sizeof(long long): \
78  __ret = __builtin_ctzll(__v); \
79  break; \
80  default: \
81  __invalid_operand_size(); \
82  } \
83  __ret; \
84  })
85 
86 #define xenomai_count_leading_zeros(__v) \
87  ({ \
88  int __ret; \
89  if (!__v) \
90  __ret = sizeof(__v) * 8; \
91  else \
92  switch (sizeof(__v)) { \
93  case sizeof(int): \
94  __ret = __builtin_clz((unsigned int)__v); \
95  break; \
96  case sizeof(long long): \
97  __ret = __builtin_clzll(__v); \
98  break; \
99  default: \
100  __invalid_operand_size(); \
101  } \
102  __ret; \
103  })
104 
105 #ifdef __cplusplus
106 }
107 #endif
108 
109 #endif /* _BOILERPLATE_COMPILER_H */