00001
00041 #ifndef UTIL_H_INCLUDED
00042 #define UTIL_H_INCLUDED
00043
00044 #include <types.h>
00045
00049 #define xstr(s) str(s)
00050
00054 #define str(s) #s
00055
00059 #define ARRAY_LEN(a) (sizeof(a) / sizeof((a)[0]))
00060
00068 static inline int isdigit(int c)
00069 {
00070 return (c >= '0') && (c <= '9');
00071 }
00072
00081 static inline int iscntrl(int c)
00082 {
00083 return (c < 32) || (c >= 127);
00084 }
00085
00097 static inline int isspace(int c)
00098 {
00099 return c == ' ';
00100 }
00101
00111 #define container_of(ptr, type, member) \
00112 ((type *)((unsigned long)(ptr) - offsetof(type, member)))
00113
00119 int ilog2_undefined(void);
00120
00128 static inline int ilog2(unsigned int x)
00129 {
00130 if (is_constant(x))
00131 return ((x) & (1ULL << 31) ? 31 :
00132 (x) & (1ULL << 30) ? 30 :
00133 (x) & (1ULL << 29) ? 29 :
00134 (x) & (1ULL << 28) ? 28 :
00135 (x) & (1ULL << 27) ? 27 :
00136 (x) & (1ULL << 26) ? 26 :
00137 (x) & (1ULL << 25) ? 25 :
00138 (x) & (1ULL << 24) ? 24 :
00139 (x) & (1ULL << 23) ? 23 :
00140 (x) & (1ULL << 22) ? 22 :
00141 (x) & (1ULL << 21) ? 21 :
00142 (x) & (1ULL << 20) ? 20 :
00143 (x) & (1ULL << 19) ? 19 :
00144 (x) & (1ULL << 18) ? 18 :
00145 (x) & (1ULL << 17) ? 17 :
00146 (x) & (1ULL << 16) ? 16 :
00147 (x) & (1ULL << 15) ? 15 :
00148 (x) & (1ULL << 14) ? 14 :
00149 (x) & (1ULL << 13) ? 13 :
00150 (x) & (1ULL << 12) ? 12 :
00151 (x) & (1ULL << 11) ? 11 :
00152 (x) & (1ULL << 10) ? 10 :
00153 (x) & (1ULL << 9) ? 9 :
00154 (x) & (1ULL << 8) ? 8 :
00155 (x) & (1ULL << 7) ? 7 :
00156 (x) & (1ULL << 6) ? 6 :
00157 (x) & (1ULL << 5) ? 5 :
00158 (x) & (1ULL << 4) ? 4 :
00159 (x) & (1ULL << 3) ? 3 :
00160 (x) & (1ULL << 2) ? 2 :
00161 (x) & (1ULL << 1) ? 1 :
00162 (x) & (1ULL << 0) ? 0 :
00163 ilog2_undefined());
00164
00165 return 31 - count_leading_zeroes(x);
00166 }
00167
00174 static inline bool is_power_of_two(unsigned long x)
00175 {
00176 return x && !(x & (x - 1));
00177 }
00178
00186 static inline unsigned long round_down(unsigned long x, unsigned int order)
00187 {
00188 return x & ~((1UL << order) - 1);
00189 }
00190
00198 static inline unsigned long round_up(unsigned long x, unsigned int order)
00199 {
00200 return round_down(x + (1UL << order) - 1, order);
00201 }
00202
00209 static inline unsigned long word_align(unsigned long x)
00210 {
00211 return round_up(x, 2);
00212 }
00213
00214 #ifdef CONFIG_PAGE_SIZE
00215
00222 static inline unsigned long page_align(unsigned long x)
00223 {
00224 return (x + CONFIG_PAGE_SIZE - 1) & ~(CONFIG_PAGE_SIZE - 1);
00225 }
00226 #endif
00227
00237 #define div_ceil(a, b) (((a) + (b) - 1) / (b))
00238
00239 #endif