-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.h
117 lines (88 loc) · 2.33 KB
/
utils.h
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#ifndef H_UTILS_H
#include <stdint.h>
#include <stddef.h>
typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef int32_t b32; // NOTE: For bool.
typedef size_t memory_size;
typedef float f32;
typedef double f64;
#undef TRUE
#undef FALSE
#define TRUE (1)
#define FALSE (0)
void platform_throw_error(const u8* text);
#define ASSERT(x) do { if (!(x)) { platform_throw_error((u8*)#x); *(volatile int*)0; } } while (0)
#define ARRAY_COUNT(x) (sizeof(x) / sizeof(*(x)))
#define SWAP(a, b, t) \
{ \
t temp = a; \
a = b; \
b = temp; \
}
#define DOUBLY_LINKED_LIST_INIT(sentinel) \
(sentinel)->prev = (sentinel); \
(sentinel)->next = (sentinel);
#define DOUBLY_LINKED_LIST_INSERT(sentinel, element) \
(element)->next = (sentinel)->next; \
(element)->prev = (sentinel); \
(element)->next->prev = (element); \
(element)->prev->next = (element);
static inline f32 lerp(f32 min, f32 value, f32 max)
{
f32 result = min + value * (max - min);
return result;
}
static inline i32 modulo(i32 a, i32 b)
{
return (a % b + b) % b;
}
static inline u32 string_length(const u8* string)
{
u32 length = 0;
while (string && *string)
{
++length;
++string;
}
return length;
}
static void u32_to_string(u8* string, u32 value)
{
u32 number = value;
i32 digit_count = 0;
while (number)
{
number /= 10;
++digit_count;
}
number = value;
if (!number)
{
++digit_count;
}
string[digit_count] = 0;
while (digit_count > 0)
{
string[digit_count - 1] = (u8)(number % 10) + '0';
number /= 10;
--digit_count;
}
}
static u32 lfsr_rand(void)
{
static u16 start_value = 0xACE1U;
u16 lfsr = start_value;
u32 bit = ((lfsr >> 0) ^ (lfsr >> 2) ^ (lfsr >> 3) ^ (lfsr >> 5) ) & 1;
lfsr = (lfsr >> 1) | (bit << 15);
++start_value;
return lfsr;
}
#define H_UTILS_H
#endif