Merge branch 'master' into sata-ahci-dev
[lunaix-os.git] / lunaix-os / includes / lunaix / spike.h
1 #ifndef __LUNAIX_SPIKE_H
2 #define __LUNAIX_SPIKE_H
3
4 // Some helper functions. As helpful as Spike the Dragon! :)
5
6 // 除法向上取整
7 #define CEIL(v, k) (((v) + (1 << (k)) - 1) >> (k))
8
9 #define ICEIL(x, y) ((x) / (y) + ((x) % (y) != 0))
10
11 // 除法向下取整
12 #define FLOOR(v, k) ((v) >> (k))
13
14 // 获取v最近的最大k倍数
15 #define ROUNDUP(v, k) (((v) + (k)-1) & ~((k)-1))
16
17 // 获取v最近的最小k倍数
18 #define ROUNDDOWN(v, k) ((v) & ~((k)-1))
19
20 #define __USER__ __attribute__((section(".usrtext")))
21
22 inline static void
23 spin()
24 {
25     while (1)
26         ;
27 }
28
29 #ifdef __LUNAIXOS_DEBUG__
30 #define assert(cond)                                                           \
31     if (!(cond)) {                                                             \
32         __assert_fail(#cond, __FILE__, __LINE__);                              \
33     }
34
35 #define assert_msg(cond, msg)                                                  \
36     if (!(cond)) {                                                             \
37         __assert_fail(msg, __FILE__, __LINE__);                                \
38     }
39 void
40 __assert_fail(const char* expr, const char* file, unsigned int line)
41   __attribute__((noinline, noreturn));
42 #else
43 #define assert(cond) (void)(cond);          // assert nothing
44 #define assert_msg(cond, msg) (void)(cond); // assert nothing
45 #endif
46
47 void
48 panick(const char* msg);
49
50 #define wait_until(cond)                                                       \
51     while (!(cond))                                                            \
52         ;
53 #define loop_until(cond)                                                       \
54     while (!(cond))                                                            \
55         ;
56
57 #define wait_until_expire(cond, max)                                           \
58     ({                                                                         \
59         unsigned int __wcounter__ = (max);                                     \
60         while (!(cond) && __wcounter__-- > 0)                                  \
61             ;                                                                  \
62         __wcounter__;                                                          \
63     })
64
65 #endif /* __LUNAIX_SPIKE_H */