carl9170: Update to latest upstream
[linux-libre-firmware.git] / carl9170fw / include / linux / compiler.h
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License along
13  * with this program; if not, write to the Free Software Foundation, Inc.,
14  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15  */
16
17 #ifndef __SHARED_COMPILER_H
18 #define __SHARED_COMPILER_H
19
20 #define __noreturn      __attribute__((noreturn))
21 #define __inline        __attribute__((always_inline))
22 #define __hot           __attribute__((hot))
23 #define __cold          __attribute__((cold))
24 #define __force         __attribute__((force))
25 #define __in_section(s) __attribute__((section("." # s)))
26 #define __visible       __attribute__((externally_visible))
27
28 #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
29
30
31 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
32 #define BUILD_BUG_ON_ZERO(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
33
34 #define ALIGN(x, a)             __ALIGN_MASK(x, (typeof(x))(a) - 1)
35 #define __ALIGN_MASK(x, mask)   (((x) + (mask)) & ~(mask))
36
37 #define __roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
38
39 #define __must_be_array(a) \
40   BUILD_BUG_ON_ZERO(__builtin_types_compatible_p(typeof(a), typeof(&a[0])))
41 #define ARRAY_SIZE(arr) (sizeof((arr)) / sizeof((arr)[0]) + __must_be_array(arr))
42
43 #define BIT(b)                  (1 << (b))
44 #define MASK(w)                 (BIT(w) - 1)
45
46 #undef offsetof
47 #ifdef __compiler_offsetof
48 # define offsetof(TYPE, MEMBER) __compiler_offsetof(TYPE, MEMBER)
49 #else
50 # define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
51 #endif
52
53 #define likely(x)       __builtin_expect(!!(x), 1)
54 #define unlikely(x)     __builtin_expect(!!(x), 0)
55
56 #define min(x, y) ({                            \
57         typeof(x) _min1 = (x);                  \
58         typeof(y) _min2 = (y);                  \
59         (void) (&_min1 == &_min2);              \
60         _min1 < _min2 ? _min1 : _min2; })
61
62 #define max(x, y) ({                            \
63         typeof(x) _max1 = (x);                  \
64         typeof(y) _max2 = (y);                  \
65         (void) (&_max1 == &_max2);              \
66         _max1 > _max2 ? _max1 : _max2; })
67
68 #define min_t(type, x, y) ({                    \
69         type __min1 = (x);                      \
70         type __min2 = (y);                      \
71         __min1 < __min2 ? __min1 : __min2; })
72
73 #define max_t(type, x, y) ({                    \
74         type __max1 = (x);                      \
75         type __max2 = (y);                      \
76         __max1 > __max2 ? __max1 : __max2; })
77
78
79 #define container_of(ptr, type, member) ({                      \
80         const typeof(((type *)0)->member) * __mptr = (ptr);     \
81         (type *)(((unsigned long)__mptr - offsetof(type, member))); })
82
83 #define MAX_ERRNO       4095
84
85 #define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO)
86
87 static inline void *ERR_PTR(long errornr)
88 {
89         return (void *) errornr;
90 }
91
92 static inline long PTR_ERR(const void *ptr)
93 {
94         return (long) ptr;
95 }
96
97 static inline long IS_ERR(const void *ptr)
98 {
99         return IS_ERR_VALUE((unsigned long)ptr);
100 }
101
102 static inline long IS_ERR_OR_NULL(const void *ptr)
103 {
104         return !ptr || IS_ERR_VALUE((unsigned long)ptr);
105 }
106
107 static inline unsigned int hweight8(unsigned int w)
108 {
109         unsigned int res = w - ((w >> 1) & 0x55);
110         res = (res & 0x33) + ((res >> 2) & 0x33);
111         return (res + (res >> 4)) & 0x0F;
112 }
113
114 static inline unsigned int hweight16(unsigned int w)
115 {
116         unsigned int res = w - ((w >> 1) & 0x5555);
117         res = (res & 0x3333) + ((res >> 2) & 0x3333);
118         res = (res + (res >> 4)) & 0x0F0F;
119         return (res + (res >> 8)) & 0x00FF;
120 }
121
122 /**
123  * DECLARE_FLEX_ARRAY() - Declare a flexible array usable in a union
124  *
125  * @TYPE: The type of each flexible array element
126  * @NAME: The name of the flexible array member
127  *
128  * In order to have a flexible array member in a union or alone in a
129  * struct, it needs to be wrapped in an anonymous struct with at least 1
130  * named member, but that member can be empty.
131  */
132 #define DECLARE_FLEX_ARRAY(TYPE, NAME)  \
133         struct { \
134                 struct { } __empty_ ## NAME; \
135                 TYPE NAME[]; \
136         }
137
138 #define __bf_shf(x) (__builtin_ffsll(x) - 1)
139
140 #define FIELD_MAX(_mask)                                                \
141         ({                                                              \
142                 (typeof(_mask))((_mask) >> __bf_shf(_mask));            \
143         })
144
145 #endif /* __SHARED_COMPILER_H */