b81bf1472800404745d444e0412f2e8304884a5f
[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 __noinline      __attribute__((noinline))
21 #define __noreturn      __attribute__((noreturn))
22 #define __inline        __attribute__((always_inline))
23 #define __hot           __attribute__((hot))
24 #define __cold          __attribute__((cold))
25 #define __unused        __attribute__((unused))
26 #define __force         __attribute__((force))
27 #define __section(s)    __attribute__((section("." # s)))
28 #define __aligned(a)    __attribute__((aligned(a)))
29 #define __packed        __attribute__((packed))
30 #define __visible       __attribute__((externally_visible))
31
32 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
33 #define BUILD_BUG_ON_ZERO(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
34
35 #define ALIGN(x, a)             __ALIGN_MASK(x, (typeof(x))(a) - 1)
36 #define __ALIGN_MASK(x, mask)   (((x) + (mask)) & ~(mask))
37
38 #define __roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
39
40 #define __must_be_array(a) \
41   BUILD_BUG_ON_ZERO(__builtin_types_compatible_p(typeof(a), typeof(&a[0])))
42 #define ARRAY_SIZE(arr) (sizeof((arr)) / sizeof((arr)[0]) + __must_be_array(arr))
43
44 #define BIT(b)                  (1 << (b))
45 #define MASK(w)                 (BIT(w) - 1)
46
47 #undef offsetof
48 #ifdef __compiler_offsetof
49 # define offsetof(TYPE, MEMBER) __compiler_offsetof(TYPE, MEMBER)
50 #else
51 # define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
52 #endif
53
54 #define likely(x)       __builtin_expect(!!(x), 1)
55 #define unlikely(x)     __builtin_expect(!!(x), 0)
56
57 #define min(x, y) ({                            \
58         typeof(x) _min1 = (x);                  \
59         typeof(y) _min2 = (y);                  \
60         (void) (&_min1 == &_min2);              \
61         _min1 < _min2 ? _min1 : _min2; })
62
63 #define max(x, y) ({                            \
64         typeof(x) _max1 = (x);                  \
65         typeof(y) _max2 = (y);                  \
66         (void) (&_max1 == &_max2);              \
67         _max1 > _max2 ? _max1 : _max2; })
68
69 #define min_t(type, x, y) ({                    \
70         type __min1 = (x);                      \
71         type __min2 = (y);                      \
72         __min1 < __min2 ? __min1 : __min2; })
73
74 #define max_t(type, x, y) ({                    \
75         type __max1 = (x);                      \
76         type __max2 = (y);                      \
77         __max1 > __max2 ? __max1 : __max2; })
78
79
80 #define container_of(ptr, type, member) ({                      \
81         const typeof(((type *)0)->member) * __mptr = (ptr);     \
82         (type *)(((unsigned long)__mptr - offsetof(type, member))); })
83
84 #define MAX_ERRNO       4095
85
86 #define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO)
87
88 static inline void *ERR_PTR(long errornr)
89 {
90         return (void *) errornr;
91 }
92
93 static inline long PTR_ERR(const void *ptr)
94 {
95         return (long) ptr;
96 }
97
98 static inline long IS_ERR(const void *ptr)
99 {
100         return IS_ERR_VALUE((unsigned long)ptr);
101 }
102
103 static inline long IS_ERR_OR_NULL(const void *ptr)
104 {
105         return !ptr || IS_ERR_VALUE((unsigned long)ptr);
106 }
107
108 #endif /* __SHARED_COMPILER_H */