summaryrefslogtreecommitdiff
path: root/vendors/_.c
blob: fc7ce077dbd132be8921fa482be1ddbd41fc6047 (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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include "_.h"
#include <math.h>
#undef free
#undef malloc
#undef calloc
#undef realloc

static void **_alloced = nullptr;
static size_t _alloced_size = 0;
static size_t _alloced_capacity = 0;

void
wdeinit(void)
{
	if (_alloced != nullptr) {
		rfor (i, 0u, _alloced_size)
			free(_alloced[i]);
		free(_alloced);
	}
	_alloced = nullptr;
	_alloced_size = 0;
	_alloced_capacity = 0;
}

void *
alloc(size_t size)
{
	_alloced_size += 1;
	if (_alloced == nullptr) {
		_alloced = calloc(sizeof(void *), 16);
		assert(_alloced != nullptr);
		_alloced_capacity = 16;
	}
	if (_alloced_size > _alloced_capacity) {
		size_t new_alloced_capacity = 16;
		while (new_alloced_capacity <= _alloced_size)
			new_alloced_capacity *= 2;
		void *new_alloced =
		    realloc(_alloced, new_alloced_capacity * sizeof(void *));
		_alloced_size -= (new_alloced == nullptr);
		assert(new_alloced != nullptr);
		_alloced = new_alloced;
		_alloced_capacity = new_alloced_capacity;
	}

	_alloced[_alloced_size - 1] = calloc(1, size);
	assert(_alloced[_alloced_size - 1] != nullptr);
	return _alloced[_alloced_size - 1];
}

void
_free(void *ptr)
{
	rfor (i, 0u, _alloced_size) {
		if (_alloced[i] == ptr) {
			free(_alloced[i]);
			_alloced[i] = nullptr;
			rfor (k, i, _alloced_size - 1)
				_alloced[k] = _alloced[k + 1];
			_alloced_size -= 1;
			return;
		}
	}
	pwrn("double free of %p", ptr);
}

void *
_realloc(void *ptr, size_t size)
{
	rfor (i, 0u, _alloced_size) {
		if (_alloced[i] == ptr) {
			void *new_ptr = realloc(ptr, size);
			assert(new_ptr != nullptr);
			_alloced[i] = new_ptr;
			return new_ptr;
		}
	}
	panic("fucked up realloc, have fun debugging");
	__builtin_unreachable();
}

// mathematical vectors

float2
tofloat2(int2 a)
{
	return F2(a.x, a.y);
}

int2
toint2(float2 a)
{
	return I2(a.x, a.y);
}

float2
float2_sub(float2 a, float2 b)
{
	return F2(a.x - b.x, a.y - b.y);
}

float2
float2_add(float2 a, float2 b)
{
	return F2(a.x + b.x, a.y + b.y);
}

float2
float2_mul(float2 a, float x)
{
	return F2(a.x * x, a.y * x);
}

float2
float2_div(float2 a, float x)
{
	return F2(a.x / x, a.y / x);
}

float
float2_length(float2 a)
{
	return sqrtf(a.x * a.x + a.y * a.y);
}

float2
float2_normalize(float2 a)
{
	const auto len = float2_length(a);
	if (len < 1.0)
		return a;
	return F2(a.x / len, a.y / len);
}

float
float2_angle(float2 a)
{
	return atanf(a.y / a.x) / 3.125 / 2 + 0.5 * (a.x < 0);
}

int2
float2_round(float2 a)
{
	return I2(roundf(a.x), roundf(a.y));
}

float2
float2_clamp(float2 min, float2 a, float2 max)
{
	rfor (i, 0, 2) {
		if (a.a[i] < min.a[i])
			a.a[i] = min.a[i];
		if (a.a[i] > max.a[i])
			a.a[i] = max.a[i];
	}
	return a;
}

float2
float2_swp(float2 a)
{
	return F2(a.y, a.x);
}

float2
float2_lerp(float2 a, float2 b, float x)
{
	return float2_add(a, float2_mul(float2_sub(b, a), x));
}

int2
int2_sub(int2 a, int2 b)
{
	return I2(a.x - b.x, a.y - b.y);
}

int2
int2_add(int2 a, int2 b)
{
	return I2(a.x + b.x, a.y + b.y);
}

int2
int2_mul(int2 a, float x)
{
	return I2(a.x * x, a.y * x);
}

int2
int2_div(int2 a, float x)
{
	return I2(a.x / x, a.y / x);
}

static int
___abs(int a)
{
	return (a < 0) ? (-a) : (a);
}

int2
int2_abs(int2 a)
{
	return I2(___abs(a.x), ___abs(a.y));
}

int2
int2_swp(int2 a)
{
	return I2(a.y, a.x);
}

int2
int2_inv(int2 a)
{
	return I2(-a.x, -a.y);
}

int2
int4_xy(int4 a)
{
	return I2(a.x, a.y);
}

int2
int4_zw(int4 a)
{
	return I2(a.z, a.w);
}