Moves KiB and friends from common.h (XCC)
[akaros.git] / kern / include / ros / common.h
1 #ifndef ROS_COMMON_H
2 #define ROS_COMMON_H
3
4 #ifndef __ASSEMBLER__
5
6 #include <stddef.h>
7 #include <stdint.h>
8 #include <sys/types.h>
9 #include <stdbool.h>
10
11 typedef uintptr_t physaddr_t;
12 typedef long intreg_t;
13 typedef unsigned long uintreg_t;
14
15 #ifndef NULL
16 #define NULL ((void*) 0)
17 #endif
18
19 #ifndef TRUE
20 #define TRUE    1
21 #endif
22
23 #ifndef FALSE
24 #define FALSE   0
25 #endif
26
27 #define FOR_CIRC_BUFFER(next, size, var) \
28         for (int _var = 0, var = (next); _var < (size); _var++, var = (var + 1) % (size))
29
30 #define STRINGIFY(s) __STRINGIFY(s)
31 #define __STRINGIFY(s) #s
32
33 /* A macro for testing if another macro has been #defined or not.  Can be used
34  * wherever you need a boolean defined.  Returns 0 or 1. */
35 #define is_defined(macro) is_defined_(macro)
36 #define is_defined_test_1 ,
37 #define is_defined_(value) is_defined__(is_defined_test_##value, value)
38 #define is_defined__(comma, value) is_defined___(comma 1, 0)
39 #define is_defined___(_, v, ...) v
40
41 // Efficient min and max operations
42 #ifdef ROS_KERNEL /* Glibc or other user libs have their own */
43 #define MIN(_a, _b)                                             \
44 ({                                                              \
45         typeof(_a) __a = (_a);                                  \
46         typeof(_b) __b = (_b);                                  \
47         __a <= __b ? __a : __b;                                 \
48 })
49 #define MAX(_a, _b)                                             \
50 ({                                                              \
51         typeof(_a) __a = (_a);                                  \
52         typeof(_b) __b = (_b);                                  \
53         __a >= __b ? __a : __b;                                 \
54 })
55
56 /* Other kernel-only includes */
57
58 /* Test for alignment, e.g. 2^6 */
59 #define ALIGNED(p, a)   (!(((uintptr_t)(p)) & ((a)-1)))
60 /* Aligns x up to the mask, e.g. (2^6 - 1) (round up if any mask bits are set)*/
61 #define __ALIGN_MASK(x, mask) (((uintptr_t)(x) + (mask)) & ~(mask))
62 /* Aligns x up to the alignment, e.g. 2^6. */
63 #define ALIGN(x, a) ((typeof(x)) __ALIGN_MASK(x, (a) - 1))
64 /* Will return false for 0.  Debatable, based on what you want. */
65 #define IS_PWR2(x) ((x) && !((x) & (x - 1)))
66
67 #define ARRAY_SIZE(x) (sizeof((x))/sizeof((x)[0]))
68
69 #endif
70
71 /* Rounding operations (efficient when n is a power of 2)
72  * Round down to the nearest multiple of n.
73  * The compiler should compile out the branch.  This is needed for 32 bit, so
74  * that we can round down uint64_t, without chopping off the top 32 bits. */
75 #define ROUNDDOWN(a, n)                                                        \
76 ({                                                                             \
77         typeof(a) __b;                                                             \
78         if (sizeof(a) == 8) {                                                      \
79                 uint64_t __a = (uint64_t) (a);                                         \
80                 __b = (typeof(a)) (__a - __a % (n));                                   \
81         } else {                                                                   \
82                 uintptr_t __a = (uintptr_t) (a);                                       \
83                 __b = (typeof(a)) (__a - __a % (n));                                   \
84         }                                                                          \
85         __b;                                                                       \
86 })
87
88 /* Round up to the nearest multiple of n */
89 #define ROUNDUP(a, n)                                                          \
90 ({                                                                             \
91         typeof(a) __b;                                                             \
92         if (sizeof(a) == 8) {                                                      \
93                 uint64_t __n = (uint64_t) (n);                                         \
94                 __b = (typeof(a)) (ROUNDDOWN((uint64_t) (a) + __n - 1, __n));          \
95         } else {                                                                   \
96                 uintptr_t __n = (uintptr_t) (n);                                       \
97                 __b = (typeof(a)) (ROUNDDOWN((uintptr_t) (a) + __n - 1, __n));         \
98         }                                                                          \
99         __b;                                                                       \
100 })
101
102 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
103
104 // Return the integer logarithm of the value provided rounded down
105 static inline uintptr_t LOG2_DOWN(uintptr_t value)
106 {
107         value |= 1;  // clz(0) is undefined, just or in a 1 bit and define it
108         // intrinsic __builtin_clz supported by both > gcc4.6 and LLVM > 1.5
109         return (sizeof(value) == 8) ? 63 - __builtin_clzll(value)
110                                     : 31 - __builtin_clz(value);
111 }
112
113 // Return the integer logarithm of the value provided rounded up
114 static inline uintptr_t LOG2_UP(uintptr_t value)
115 {
116         uintptr_t ret = LOG2_DOWN(value);
117         ret += 0 != (value ^ ((uintptr_t) 1 << ret));  // Add 1 if a lower bit set
118         return ret;
119 }
120
121 static inline uintptr_t ROUNDUPPWR2(uintptr_t value)
122 {
123         return 1 << LOG2_UP(value);
124 }
125
126 static inline uintptr_t ROUNDDOWNPWR2(uintptr_t value)
127 {
128         return 1 << LOG2_DOWN(value);
129 }
130
131 /* We wraparound if UINT_MAX < a * b, which is also UINT_MAX / a < b. */
132 static inline bool mult_will_overflow_u64(uint64_t a, uint64_t b)
133 {
134         if (!a)
135                 return FALSE;
136         return (uint64_t)(-1) / a < b;
137 }
138
139 // Return the offset of 'member' relative to the beginning of a struct type
140 #ifndef offsetof
141 #define offsetof(type, member)  ((size_t) (&((type*)0)->member))
142 #endif
143
144 /* Return the container/struct holding the object 'ptr' points to */
145 #define container_of(ptr, type, member) ({                                     \
146         (type*)((char*)ptr - offsetof(type, member));                             \
147 })
148
149 /* Force the reading exactly once of x.  You may still need mbs().  See
150  * http://lwn.net/Articles/508991/ for more info. */
151 #define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))
152
153 /* Makes sure func is run exactly once.  Can handle concurrent callers, and
154  * other callers spin til the func is complete. */
155 #define run_once(func)                                                         \
156 {                                                                              \
157         static bool ran_once = FALSE;                                              \
158         static atomic_t is_running = FALSE;                                        \
159         if (!ran_once) {                                                           \
160                 if (!atomic_swap(&is_running, TRUE)) {                                 \
161                         /* we won the race and get to run the func */                      \
162                         func;                                                              \
163                         wmb();  /* don't let the ran_once write pass previous writes */    \
164                         ran_once = TRUE;                                                   \
165                 } else {                                                               \
166                         /* someone else won, wait til they are done to break out */        \
167                         while (!ran_once)                                                  \
168                                 cpu_relax();                                                   \
169                                                                                \
170                 }                                                                      \
171         }                                                                          \
172 }
173
174 /* Unprotected, single-threaded version, makes sure func is run exactly once */
175 #define run_once_racy(func)                                                    \
176 {                                                                              \
177         static bool ran_once = FALSE;                                              \
178         if (!ran_once) {                                                           \
179                 func;                                                                  \
180                 ran_once = TRUE;                                                       \
181         }                                                                          \
182 }
183
184 /* Aborts with 'retcmd' if this function has already been called.  Compared to
185  * run_once, this is put at the top of a function that can be called from
186  * multiple sources but should only execute once. */
187 #define init_once_racy(retcmd)                                                 \
188 {                                                                              \
189         static bool initialized = FALSE;                                           \
190         if (initialized) {                                                         \
191                 retcmd;                                                                \
192         }                                                                          \
193         initialized = TRUE;                                                        \
194 }
195
196 #endif /* __ASSEMBLER__ */
197
198 #endif /* ROS_COMMON_H */