aboutsummaryrefslogtreecommitdiff
path: root/src/common.hff
diff options
context:
space:
mode:
authorlemon <lsof@mailbox.org>2022-08-14 11:16:03 +0200
committerlemon <lsof@mailbox.org>2022-08-14 11:16:03 +0200
commit0d1e125832d0fd8ca31c5f782e7c3db774ae5a02 (patch)
treee4622f75a8307d8ee1970f8bd6cc92766582f0ba /src/common.hff
parentc129f77ad724aa940b53a125de0e1e4de0ca7240 (diff)
woa
Diffstat (limited to 'src/common.hff')
-rw-r--r--src/common.hff45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/common.hff b/src/common.hff
new file mode 100644
index 0000000..aac733e
--- /dev/null
+++ b/src/common.hff
@@ -0,0 +1,45 @@
+import "libc.hff";
+
+/// Macros
+
+defmacro assert {
+(ex, s, ...args) [
+ (do
+ if not (ex) {
+ fprintf(stderr, "%s:%d: assertion failed: ", #FILE, #LINE);
+ fprintf(stderr, s, args);
+ fprintf(stderr, "\n");
+ abort();
+ }
+ )
+]
+}
+
+defmacro foreach(x, i, a, ...body) [
+ {
+ let $a = a;
+ for let i = 0; i < $a.#len; ++i {
+ let x = $a[i];
+ { body }
+ }
+ }
+]
+
+defmacro streq(a,b) [ (strcmp(a,b) == 0) ]
+
+// Inline functions
+fn bswap32(x u32) u32 {
+ return (x >> 24)
+ | ((x >> 8) & 0x00FF00)
+ | ((x << 8) & 0xFF0000)
+ | (x << 24);
+}
+
+fn bswap64(x u64) u64 {
+ return (as(u64)bswap32(x) << 32)
+ | (bswap32(x >> 32));
+}
+
+fn spanz(x *const u8) [#]const u8 {
+ return x[0::strlen(x)];
+}