Support to multi-threading and pthread interface (POSIX.1-2008) (#23)
[lunaix-os.git] / lunaix-os / includes / lunaix / spike.h
index 6f91924dc19a174d280cb3bb7c98905f8cd91837..da901fc39e76f59f6a3995dd86fddf759ca890ba 100644 (file)
 #ifndef __LUNAIX_SPIKE_H
 #define __LUNAIX_SPIKE_H
 
-// Some helper functions. As helpful as Spike the Dragon! :)
+#include <lunaix/compiler.h>
 
-// 除法向上取整
+/** Some helper functions. As helpful as Spike the Dragon! :) **/
+
+// 除法 v/(2^k) 向上取整
 #define CEIL(v, k) (((v) + (1 << (k)) - 1) >> (k))
 
 #define ICEIL(x, y) ((x) / (y) + ((x) % (y) != 0))
 
-// 除法向下取整
+// 除法 v/(2^k) 向下取整
 #define FLOOR(v, k) ((v) >> (k))
 
-// 获取v最近的最大k倍数
+// 获取v最近的最大k倍数 (k=2^n)
 #define ROUNDUP(v, k) (((v) + (k)-1) & ~((k)-1))
 
-// 获取v最近的最小k倍数
+// 获取v最近的最小k倍数 (k=2^m)
 #define ROUNDDOWN(v, k) ((v) & ~((k)-1))
 
-#define __USER__ __attribute__((section(".usrtext")))
+#define MIN(a, b) ((a) < (b) ? (a) : (b))
+#define MAX(a, b) ((a) > (b) ? (a) : (b))
 
-inline static void
-spin()
-{
-    while (1)
-        ;
-}
+#define is_pot(val) (((val) != 0) || ((val) & ((val)-1)) == 0)
+
+/**
+ * @brief Fast log base 2 for integer, utilizing constant unfolding.
+ * Adopted from
+ * https://elixir.bootlin.com/linux/v4.4/source/include/linux/log2.h#L85
+ *
+ */
+#define ILOG2(x)                                                               \
+    (__builtin_constant_p(x) ? ((x) == 0              ? 0                      \
+                                : ((x) & (1ul << 31)) ? 31                     \
+                                : ((x) & (1ul << 30)) ? 30                     \
+                                : ((x) & (1ul << 29)) ? 29                     \
+                                : ((x) & (1ul << 28)) ? 28                     \
+                                : ((x) & (1ul << 27)) ? 27                     \
+                                : ((x) & (1ul << 26)) ? 26                     \
+                                : ((x) & (1ul << 25)) ? 25                     \
+                                : ((x) & (1ul << 24)) ? 24                     \
+                                : ((x) & (1ul << 23)) ? 23                     \
+                                : ((x) & (1ul << 22)) ? 22                     \
+                                : ((x) & (1ul << 21)) ? 21                     \
+                                : ((x) & (1ul << 20)) ? 20                     \
+                                : ((x) & (1ul << 19)) ? 19                     \
+                                : ((x) & (1ul << 18)) ? 18                     \
+                                : ((x) & (1ul << 17)) ? 17                     \
+                                : ((x) & (1ul << 16)) ? 16                     \
+                                : ((x) & (1ul << 15)) ? 15                     \
+                                : ((x) & (1ul << 14)) ? 14                     \
+                                : ((x) & (1ul << 13)) ? 13                     \
+                                : ((x) & (1ul << 12)) ? 12                     \
+                                : ((x) & (1ul << 11)) ? 11                     \
+                                : ((x) & (1ul << 10)) ? 10                     \
+                                : ((x) & (1ul << 9))  ? 9                      \
+                                : ((x) & (1ul << 8))  ? 8                      \
+                                : ((x) & (1ul << 7))  ? 7                      \
+                                : ((x) & (1ul << 6))  ? 6                      \
+                                : ((x) & (1ul << 5))  ? 5                      \
+                                : ((x) & (1ul << 4))  ? 4                      \
+                                : ((x) & (1ul << 3))  ? 3                      \
+                                : ((x) & (1ul << 2))  ? 2                      \
+                                : ((x) & (1ul << 1))  ? 1                      \
+                                                      : 0)                      \
+                             : (31 - clz(x)))
 
-#ifdef __LUNAIXOS_DEBUG__
+#ifndef __LUNAIXOS_NASSERT__
 #define assert(cond)                                                           \
-    if (!(cond)) {                                                             \
-        __assert_fail(#cond, __FILE__, __LINE__);                              \
-    }
+    do {                                                                       \
+        if (unlikely(!(cond))) {                                                         \
+            __assert_fail(#cond, __FILE__, __LINE__);                          \
+        }                                                                      \
+    } while(0)
 
 #define assert_msg(cond, msg)                                                  \
-    if (!(cond)) {                                                             \
-        __assert_fail(msg, __FILE__, __LINE__);                                \
-    }
+    do {                                                                       \
+        if (unlikely(!(cond))) {                                                         \
+            __assert_fail(msg, __FILE__, __LINE__);                            \
+        }                                                                      \
+    } while(0)
+
+#define must_success(statement)                                                \
+    do {                                                                       \
+        int err = (statement);                                                 \
+        if (err) panickf(#statement "failed with errcode=%d", err);           \
+    } while(0)
+
+#define fail(msg) __assert_fail(msg, __FILE__, __LINE__);
+
 void
 __assert_fail(const char* expr, const char* file, unsigned int line)
-  __attribute__((noinline, noreturn));
+    __attribute__((noinline, noreturn));
 #else
 #define assert(cond) (void)(cond);          // assert nothing
 #define assert_msg(cond, msg) (void)(cond); // assert nothing
-#endif
 
-void
+#endif // __LUNAIXOS_NASSERT__
+
+void noret
 panick(const char* msg);
 
+void noret
+panickf(const char* fmt, ...);
+
 #define wait_until(cond)                                                       \
     while (!(cond))                                                            \
         ;
@@ -57,7 +114,7 @@ panick(const char* msg);
 #define wait_until_expire(cond, max)                                           \
     ({                                                                         \
         unsigned int __wcounter__ = (max);                                     \
-        while (!(cond) && __wcounter__-- > 0)                                  \
+        while (!(cond) && __wcounter__-- > 1)                                  \
             ;                                                                  \
         __wcounter__;                                                          \
     })