summaryrefslogtreecommitdiff
path: root/sources/bits.c
diff options
context:
space:
mode:
Diffstat (limited to 'sources/bits.c')
-rw-r--r--sources/bits.c72
1 files changed, 60 insertions, 12 deletions
diff --git a/sources/bits.c b/sources/bits.c
index 0197436..de0aa0d 100644
--- a/sources/bits.c
+++ b/sources/bits.c
@@ -5,22 +5,70 @@
* See LICENSE for details
*/
+typedef unsigned char u8;
+typedef unsigned short u16;
+typedef unsigned int u32;
+typedef unsigned long long u64; /* XXX: GCC extension */
+typedef signed char s8;
+typedef signed short s16;
+typedef signed int s32;
+typedef signed long long s64; /* XXX: GCC extension */
+
+typedef s8 bool;
+
+typedef u64 usize;
+typedef s64 ssize;
+
+typedef float f32;
+typedef double f64;
+
+
+#define min_s8 ((s8) (0x80))
+#define min_s16 ((s16)(0x8000))
+#define min_s32 ((s32)(0x80000000L))
+#define min_s64 ((s64)(0x8000000000000000LL))
+#define min_ssize min_s64
+
+#define max_s8 ((s8) (0x7F))
+#define max_s16 ((s16)(0x7FFF))
+#define max_s32 ((s32)(0x7FFFFFFFL))
+#define max_s64 ((s64)(0x7FFFFFFFFFFFFFFFLL))
+#define max_ssize max_s64
+
+#define max_u8 ((u8) (0xFF))
+#define max_u16 ((u16)(0xFFFF))
+#define max_u32 ((u32)(0xFFFFFFFFUL))
+#define max_u64 ((u64)(0xFFFFFFFFFFFFFFFFULL))
+#define max_usize max_u64
+
+#ifndef true
+ #define true 1
+#endif
+
+#ifndef false
+ #define false 0
+#endif
#define iota __COUNTER__
+#define nil {0}
-#define lengthof(x) (sizeof(x) / sizeof((x)[0]))
+#define KB bit(10LL)
+#define MB bit(20LL)
+#define GB bit(30LL)
+#define TB bit(40LL)
-#define bit(x) (1 << (x))
+#define packed __attribute__((packed))
-#define KB bit(10)
-#define MB bit(11)
-#define GB bit(12)
+#define bit(n) (1LL << (n))
+#define align(what, to) (((what) + ((to) - 1)) & ~((to) - 1)) /* NOTE: `to` *must* be pow. of 2 */
-typedef unsigned char u8;
-typedef unsigned short u16;
-typedef unsigned long long u64;
-typedef u64 usize;
+#define lengthof(arr) (sizeof(arr) / sizeof(arr[0]))
+#ifndef offsetof
+ #define offsetof(strct, memb) ((ssize)(&((strct*)(0)->memb)))
+#endif
+
+#define min(a, b) (((a) <= (b)) ? (a) : (b))
+#define max(a, b) (((a) >= (b)) ? (a) : (b))
+#define clamp(x, mn, mx) (min((mn), max((x), (mx))))
-typedef u8 bool;
-#define true 1
-#define false 0
+#define unused(expr) ((void)(expr))