00001
00038 #ifndef UTIL_H_INCLUDED
00039 #define UTIL_H_INCLUDED
00040
00041 #include <compiler.h>
00042 #include <stdint.h>
00043 #include <types.h>
00044
00058 #define xstr(s) str(s)
00059
00063 #define str(s) #s
00064
00068 #define ARRAY_LEN(a) (sizeof(a) / sizeof((a)[0]))
00069
00077 static inline int isdigit(int c)
00078 {
00079 return (c >= '0') && (c <= '9');
00080 }
00081
00090 static inline int iscntrl(int c)
00091 {
00092 return (c < 32) || (c >= 127);
00093 }
00094
00106 static inline int isspace(int c)
00107 {
00108 return c == ' ';
00109 }
00110
00120 #define container_of(ptr, type, member) \
00121 ((type *)((uintptr_t)(ptr) - offsetof(type, member)))
00122
00124
00125
00132 #define min_s(a, b) \
00133 ((sizeof(a) == 1) && (sizeof(b) == 1) ? compiler_min_s8(a, b) \
00134 : (sizeof(a) <= 2) && (sizeof(b) <= 2) ? compiler_min_s16(a, b) \
00135 : (sizeof(a) <= 4) && (sizeof(b) <= 4) ? compiler_min_s32(a, b) \
00136 : compiler_min_s64(a, b))
00137
00144 #define min_u(a, b) \
00145 ((sizeof(a) == 1) && (sizeof(b) == 1) ? compiler_min_u8(a, b) \
00146 : (sizeof(a) <= 2) && (sizeof(b) <= 2) ? compiler_min_u16(a, b) \
00147 : (sizeof(a) <= 4) && (sizeof(b) <= 4) ? compiler_min_u32(a, b) \
00148 : compiler_min_u64(a, b))
00149
00156 #define max_s(a, b) \
00157 ((sizeof(a) == 1) && (sizeof(b) == 1) ? compiler_max_s8(a, b) \
00158 : (sizeof(a) <= 2) && (sizeof(b) <= 2) ? compiler_max_s16(a, b) \
00159 : (sizeof(a) <= 4) && (sizeof(b) <= 4) ? compiler_max_s32(a, b) \
00160 : compiler_max_s64(a, b))
00161
00168 #define max_u(a, b) \
00169 ((sizeof(a) == 1) && (sizeof(b) == 1) ? compiler_max_u8(a, b) \
00170 : (sizeof(a) <= 2) && (sizeof(b) <= 2) ? compiler_max_u16(a, b) \
00171 : (sizeof(a) <= 4) && (sizeof(b) <= 4) ? compiler_max_u32(a, b) \
00172 : compiler_max_u64(a, b))
00173
00175
00181 int_fast8_t ilog2_undefined(void);
00182
00190 __always_inline static int_fast8_t ilog2(uint32_t x)
00191 {
00192 if (is_constant(x))
00193 return ((x) & (1ULL << 31) ? 31 :
00194 (x) & (1ULL << 30) ? 30 :
00195 (x) & (1ULL << 29) ? 29 :
00196 (x) & (1ULL << 28) ? 28 :
00197 (x) & (1ULL << 27) ? 27 :
00198 (x) & (1ULL << 26) ? 26 :
00199 (x) & (1ULL << 25) ? 25 :
00200 (x) & (1ULL << 24) ? 24 :
00201 (x) & (1ULL << 23) ? 23 :
00202 (x) & (1ULL << 22) ? 22 :
00203 (x) & (1ULL << 21) ? 21 :
00204 (x) & (1ULL << 20) ? 20 :
00205 (x) & (1ULL << 19) ? 19 :
00206 (x) & (1ULL << 18) ? 18 :
00207 (x) & (1ULL << 17) ? 17 :
00208 (x) & (1ULL << 16) ? 16 :
00209 (x) & (1ULL << 15) ? 15 :
00210 (x) & (1ULL << 14) ? 14 :
00211 (x) & (1ULL << 13) ? 13 :
00212 (x) & (1ULL << 12) ? 12 :
00213 (x) & (1ULL << 11) ? 11 :
00214 (x) & (1ULL << 10) ? 10 :
00215 (x) & (1ULL << 9) ? 9 :
00216 (x) & (1ULL << 8) ? 8 :
00217 (x) & (1ULL << 7) ? 7 :
00218 (x) & (1ULL << 6) ? 6 :
00219 (x) & (1ULL << 5) ? 5 :
00220 (x) & (1ULL << 4) ? 4 :
00221 (x) & (1ULL << 3) ? 3 :
00222 (x) & (1ULL << 2) ? 2 :
00223 (x) & (1ULL << 1) ? 1 :
00224 (x) & (1ULL << 0) ? 0 :
00225 ilog2_undefined());
00226
00227 return 31 - compiler_clz(x);
00228 }
00229
00236 __always_inline static bool is_power_of_two(unsigned long x)
00237 {
00238 return x && !(x & (x - 1));
00239 }
00240
00241 ERROR_FUNC(priv_round_down_bad_type, "Invalid type passed to round_down");
00242
00250 #define round_down(x, order) \
00251 (sizeof(x) == 4 ? round_down32((uint32_t)(x), (order)) : \
00252 sizeof(x) == 2 ? round_down16((uint16_t)(x), (order)) : \
00253 sizeof(x) == 1 ? round_down8 (( uint8_t)(x), (order)) : \
00254 (priv_round_down_bad_type(),1))
00255
00256 static inline uint8_t round_down8(uint8_t x, unsigned int order)
00257 {
00258 return (x & ~((1U << order) - 1));
00259 }
00260
00261 static inline uint16_t round_down16(uint16_t x, unsigned int order)
00262 {
00263 return (x & ~((1U << order) - 1));
00264 }
00265
00266 static inline uint32_t round_down32(uint32_t x, unsigned int order)
00267 {
00268 return (x & ~((1UL << order) - 1));
00269 }
00270
00271 ERROR_FUNC(priv_round_up_bad_type, "Invalid type passed to round_up");
00272
00280 #define round_up(x, order) \
00281 (sizeof(x) == 4 ? round_up32((uint32_t)(x), (order)) : \
00282 sizeof(x) == 2 ? round_up16((uint16_t)(x), (order)) : \
00283 sizeof(x) == 1 ? round_up8 (( uint8_t)(x), (order)) : \
00284 (priv_round_up_bad_type(),1))
00285
00286 static inline uint8_t round_up8(uint8_t x, unsigned int order)
00287 {
00288 return round_down8(x + (1U << order) - 1, order);
00289 }
00290
00291 static inline uint16_t round_up16(uint16_t x, unsigned int order)
00292 {
00293 return round_down16(x + (1U << order) - 1, order);
00294 }
00295
00296 static inline uint32_t round_up32(uint32_t x, unsigned int order)
00297 {
00298 return round_down32(x + (1UL << order) - 1, order);
00299 }
00300
00307 static inline unsigned long word_align(unsigned long x)
00308 {
00309 return round_up(x, 2);
00310 }
00311
00312 #ifdef CONFIG_PAGE_SIZE
00313
00320 static inline unsigned long page_align(unsigned long x)
00321 {
00322 return (x + CONFIG_PAGE_SIZE - 1) & ~(CONFIG_PAGE_SIZE - 1);
00323 }
00324 #endif
00325
00335 #define div_ceil(a, b) (((a) + (b) - 1) / (b))
00336
00338
00339 #endif