summaryrefslogtreecommitdiff
path: root/sources/bits.c
blob: de0aa0d3511fdfbe771ac110c472452cb5e46723 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
 * Copyright (C) 2025 dwlr <dweller@cabin.digital>
 *
 * BSD 3-Clause License (BSD-3-Clause)
 * 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 KB bit(10LL)
#define MB bit(20LL)
#define GB bit(30LL)
#define TB bit(40LL)

#define packed __attribute__((packed))

#define bit(n) (1LL << (n))
#define align(what, to) (((what) + ((to) - 1)) & ~((to) - 1)) /* NOTE: `to` *must* be pow. of 2 */

#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))))

#define unused(expr) ((void)(expr))