feat: asynchronized SATA IO
[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 // 除法 v/(2^k) 向上取整
7 #define CEIL(v, k) (((v) + (1 << (k)) - 1) >> (k))
8
9 #define ICEIL(x, y) ((x) / (y) + ((x) % (y) != 0))
10
11 // 除法 v/(2^k) 向下取整
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倍数 (k=2^m)
18 #define ROUNDDOWN(v, k) ((v) & ~((k)-1))
19
20 #define MIN(a, b) ((a) < (b) ? (a) : (b))
21 #define MAX(a, b) ((a) > (b) ? (a) : (b))
22
23 /**
24  * @brief Fast log base 2 for integer, utilizing constant unfolding.
25  * Adopted from
26  * https://elixir.bootlin.com/linux/v4.4/source/include/linux/log2.h#L85
27  *
28  */
29 #define ILOG2(x)                                                               \
30     __builtin_constant_p(x) ? ((x) == 0              ? 0                       \
31                                : ((x) & (1ul << 31)) ? 31                      \
32                                : ((x) & (1ul << 30)) ? 30                      \
33                                : ((x) & (1ul << 29)) ? 29                      \
34                                : ((x) & (1ul << 28)) ? 28                      \
35                                : ((x) & (1ul << 27)) ? 27                      \
36                                : ((x) & (1ul << 26)) ? 26                      \
37                                : ((x) & (1ul << 25)) ? 25                      \
38                                : ((x) & (1ul << 24)) ? 24                      \
39                                : ((x) & (1ul << 23)) ? 23                      \
40                                : ((x) & (1ul << 22)) ? 22                      \
41                                : ((x) & (1ul << 21)) ? 21                      \
42                                : ((x) & (1ul << 20)) ? 20                      \
43                                : ((x) & (1ul << 19)) ? 19                      \
44                                : ((x) & (1ul << 18)) ? 18                      \
45                                : ((x) & (1ul << 17)) ? 17                      \
46                                : ((x) & (1ul << 16)) ? 16                      \
47                                : ((x) & (1ul << 15)) ? 15                      \
48                                : ((x) & (1ul << 14)) ? 14                      \
49                                : ((x) & (1ul << 13)) ? 13                      \
50                                : ((x) & (1ul << 12)) ? 12                      \
51                                : ((x) & (1ul << 11)) ? 11                      \
52                                : ((x) & (1ul << 10)) ? 10                      \
53                                : ((x) & (1ul << 9))  ? 9                       \
54                                : ((x) & (1ul << 8))  ? 8                       \
55                                : ((x) & (1ul << 7))  ? 7                       \
56                                : ((x) & (1ul << 6))  ? 6                       \
57                                : ((x) & (1ul << 5))  ? 5                       \
58                                : ((x) & (1ul << 4))  ? 4                       \
59                                : ((x) & (1ul << 3))  ? 3                       \
60                                : ((x) & (1ul << 2))  ? 2                       \
61                                : ((x) & (1ul << 1))  ? 1                       \
62                                                      : 0)                       \
63                             : (31 - __builtin_clz(x))
64
65 #define __USER__ __attribute__((section(".usrtext")))
66
67 inline static void
68 spin()
69 {
70     while (1)
71         ;
72 }
73
74 #ifndef __LUNAIXOS_NASSERT__
75 #define assert(cond)                                                           \
76     if (!(cond)) {                                                             \
77         __assert_fail(#cond, __FILE__, __LINE__);                              \
78     }
79
80 #define assert_msg(cond, msg)                                                  \
81     if (!(cond)) {                                                             \
82         __assert_fail(msg, __FILE__, __LINE__);                                \
83     }
84 void
85 __assert_fail(const char* expr, const char* file, unsigned int line)
86   __attribute__((noinline, noreturn));
87 #else
88 #define assert(cond) (void)(cond);          // assert nothing
89 #define assert_msg(cond, msg) (void)(cond); // assert nothing
90
91 #endif // __LUNAIXOS_NASSERT__
92
93 void
94 panick(const char* msg);
95
96 void
97 panickf(const char* fmt, ...);
98
99 #define wait_until(cond)                                                       \
100     while (!(cond))                                                            \
101         ;
102 #define loop_until(cond)                                                       \
103     while (!(cond))                                                            \
104         ;
105
106 #define wait_until_expire(cond, max)                                           \
107     ({                                                                         \
108         unsigned int __wcounter__ = (max);                                     \
109         while (!(cond) && __wcounter__-- > 1)                                  \
110             ;                                                                  \
111         __wcounter__;                                                          \
112     })
113
114 #endif /* __LUNAIX_SPIKE_H */