-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsol.h
3859 lines (3250 loc) · 106 KB
/
sol.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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef SOL_H
#define SOL_H
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdarg.h>
#include <string.h>
#include <emmintrin.h>
#include <assert.h>
#include <math.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/sysinfo.h>
#include <sys/stat.h>
#include <dlfcn.h>
#include <time.h>
#endif
#define SCB_OVERRIDE_STDLIB
#ifdef SCB_OVERRIDE_STDLIB
#undef assert
#define assert scb_assert
#define snprintf scb_snprintf
#define strcpy scb_strcpy
#endif
#define local_persist static
#define internal static
#define inline_fn static inline
#define dll_export __declspec(dllexport)
typedef int8_t s8;
typedef int16_t s16;
typedef int32_t s32;
typedef int64_t s64;
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef float f32;
typedef double f64;
typedef u32 b32;
typedef void (*voidpfn)(void);
#define Max_s8 0x7f
#define Max_s16 0x7fff
#define Max_s32 0x7fffffff
#define Max_s64 0x7fffffffffffffff
#define Max_u8 0xff
#define Max_u16 0xffff
#define Max_u32 0xffffffff
#define Max_u64 0xffffffffffffffff
#ifdef _WIN32
#define ctz(x) _tzcnt_u64(x)
#define ctz64(x) _tzcnt_u64(x)
#define ctz32(x) _tzcnt_u32(x)
#define ctz16(x) _tzcnt_u16(x)
#define clz(x) __lzcnt64(x)
#define clz64(x) __lzcnt64(x)
#define clz32(x) __lzcnt(x)
#define clz16(x) __lzcnt16(x)
#define popcnt(x) __popcnt64(x)
#define popcnt64(x) __popcnt64(x)
#define popcnt32(x) __popcnt32(x)
#define popcnt16(x) __popcnt16(x)
#else
#define ffs(x) __builtin_ffsl(x)
#define ffs32(x) __builtin_ffs(x)
#define ctz(x) __builtin_ctzl(x)
#define ctz64(x) __builtin_ctzl(x)
#define ctz32(x) __builtin_ctz(x)
#define ctz16(x) __builtin_ctzs(x)
#define clz(x) __builtin_clzl(x)
#define clz64(x) __builtin_clzl(x)
#define clz32(x) __builtin_clz(x)
#define clz16(x) __builtin_clzs(x)
#define popcnt(x) __builtin_popcountl(x)
#define popcnt64(x) __builtin_popcountl(x)
#define popcnt32(x) __builtin_popcount(x)
#define popcnt16(x) __builtin_popcounts(x)
#endif
#define typeof(x) __typeof__(x)
#define maxif(x) ((u64)0 - (bool)(x))
#define cl_array_size(x) (sizeof(x)/sizeof(x[0]))
#if _WIN32
#define gcc_align(x)
#define msvc_align(x) __declspec(align(x))
#else
#define maybe_unused __attribute__((unused))
#define gcc_align(x) __attribute__((aligned(x)))
#define msvc_align(x)
#define offsetof(type, memb) __builtin_offsetof(type, memb)
#endif
#define memb_to_struct(memb, memb_of, memb_name) \
((typeof(memb_of))((u8*)memb - offsetof(typeof(*memb_of), memb_name)))
#define memb_size(type, memb) sizeof(((type*)0)->memb)
#define struct_memb(type, memb) (((type*)0)->memb)
#define swap(a, b) \
do { \
typeof(a) m__swap_tmp = a; \
a = b; \
b = m__swap_tmp; \
} while(0);
#define for_bits(pos, count, mask) \
for(count = 0, pos = (typeof(pos))ctz(mask); \
count < (u64)popcnt(mask); \
pos = (typeof(pos))ctz((u64)mask & (Max_u64 << (pos + 1))), ++count)
static inline u64 trunc_copy(void *to, u64 to_sz, void *from, u64 from_sz)
{
if (to_sz < from_sz) {
memcpy(to, from, to_sz);
return to_sz;
}
memcpy(to, from, from_sz);
return from_sz;
}
static inline bool is_whitechar(char c)
{
return c == ' ' || c == '\n' || c == '\t';
}
enum type {
TYPE_LINEAR,
TYPE_ARENA,
};
// preproc.h
#define glue_(a, b) a##b
#define glue(a, b) glue_(a, b)
#define paste_(...) __VA_ARGS__
#define paste(...) paste_(__VA_ARGS__)
#define stringify_(...) #__VA_ARGS__
#define stringify(...) stringify_(__VA_ARGS__)
/*
* I thought that the below type checker macros would be superior to the def_wrapper_fn method, but now I
* think that the former is actually a really nice solution: it is certainly more effort intensive to
* use than typecheck macros, but not as annoying as I previously thought, (after just formatting the dict
* macro so each bit is laid out clearly, I do not think that it would be particularly difficult to write
* again).
*
* Furthermore, the frontend of the def_wrapper solution is basically perfect because you are just calling
* actual, properly declared functions, as if you had just written them yourself: the compiler errors about
* misuse are exactly what to expect, they show up as function calls in LSP function docs so you can see
* the types of the arguments, unlike a macro call.
*
* So yeah, after testing out other solutions which make writing the backend a bit less cumbersome, I actually
* think that I have a pretty good solution in the def_wrapper_fn macro.
*/
// does not work with lvalues
#define typecheck(a, b, c) (b = (typeof(a))b, c)
// necessary for lvalue references
#define ptypecheck(a, b, c) (*b = *(typeof(a))b, c)
#define def_wrapper_fn(wrapper_name, wrapped_name, ret_type, wrapper_args, wrapped_args) \
static inline ret_type wrapper_name(wrapper_args) { return (ret_type)wrapped_name(wrapped_args); }
// os.h
// NOTE(SollyCB) os_handle will be 4 bytes larger on Windows which will mess with alignment,
// but this
#ifdef _WIN32
#define OS_INVALID_HANDLE INVALID_HANDLE_VALUE
#else
#define OS_INVALID_HANDLE ((void*)-1)
#endif
typedef union os_handle {
void *handle;
#ifndef _WIN32
void *dl;
pid_t pid;
int fd;
#endif
} os_handle;
#define os_handle_is_valid(handle) (handle.handle != OS_INVALID_HANDLE)
#define os_fd_to_handle(fd) ((os_handle)(void*)(s64)fd)
extern struct os {
bool is_valid;
u32 page_size;
u32 thread_count;
os_handle stdin_handle;
os_handle stdout_handle;
os_handle stderr_handle;
} os;
struct os_process {
os_handle p,t; // process and thread handle
};
#define def_create_os(name) void name(void)
def_create_os(create_os);
#define def_os_error_string(name) void name(char *buf, u32 size)
def_os_error_string(os_error_string);
#define def_os_allocate(name) void* name(u64 size)
def_os_allocate(os_allocate);
#define def_os_deallocate(name) void name(void *p, u64 size)
def_os_deallocate(os_deallocate);
#define def_os_page_size(name) u32 name(void)
def_os_page_size(os_page_size);
#define def_os_stdout(name) os_handle name(void)
def_os_stdout(os_stdout);
#define def_os_create_lib(name) os_handle name(char *uri)
def_os_create_lib(os_create_lib);
#define def_os_libproc(name) voidpfn name(os_handle lib, char *proc)
def_os_libproc(os_libproc);
#define def_os_destroy_lib(name) void name(os_handle lib)
def_os_destroy_lib(os_destroy_lib);
#define def_os_create_process(name) int name(char *cmdline, struct os_process *p)
def_os_create_process(os_create_process);
#define def_os_await_process(name) int name(struct os_process *p)
def_os_await_process(os_await_process);
#define def_os_destroy_process(name) void name(struct os_process *p)
def_os_destroy_process(os_destroy_process);
#define def_os_sleep_ms(name) void name(u32 ms)
def_os_sleep_ms(os_sleep_ms);
// rc.h
typedef struct rc rc_t;
struct rc {
u32 count;
};
static inline void rc_init(rc_t *rc)
{
memset(rc, 0, sizeof(*rc));
}
static inline void rc_inc(rc_t *rc)
{
rc->count += 1;
}
static inline bool rc_dec(rc_t *rc)
{
rc->count -= rc->count > 0;
return rc->count;
}
// file.h
#define FILE_ERROR Max_u64
#define def_write_stdout(name) void name(char *buf, u64 size)
def_write_stdout(write_stdout);
#define def_write_file(name) u64 name(char *uri, void *buf, u64 size, u64 ofs)
def_write_file(write_file);
#define def_read_file(name) u64 name(char *uri, void *buf, u64 size, u64 ofs)
def_read_file(read_file);
enum create_fd_flags {
CREATE_FD_READ = 0x01,
CREATE_FD_WRITE = 0x02,
};
#define def_create_fd(name) os_handle name(char *uri, u32 flags)
def_create_fd(create_fd);
#define def_destroy_fd(name) void name(os_handle fd)
def_destroy_fd(destroy_fd);
#define def_write_fd(name) u64 name(os_handle fd, void *buf, u64 size, u64 ofs)
def_write_fd(write_fd);
#define def_read_fd(name) u64 name(os_handle fd, void *buf, u64 size, u64 ofs)
def_read_fd(read_fd);
#define def_copy_file(name) int name(char *fnew, char *fold)
def_copy_file(copy_file);
#define def_trunc_file(name) int name(char *uri, u64 sz)
def_trunc_file(trunc_file);
#define def_getftim(name) u64 name(char *uri)
def_getftim(getftim);
enum {
FTIM_MOD,
};
#define def_cmpftim(name) int name(u32 opt, char *x, char *y)
def_cmpftim(cmpftim);
// print.h
#define def_snprintf(name) u32 name(char *buf, u32 size, const char *fmt, ...)
def_snprintf(scb_snprintf);
#define print(fmt, ...) \
do { \
char m__print_buf[2046]; \
u32 m__print_size = scb_snprintf(m__print_buf, sizeof(m__print_buf), fmt, ##__VA_ARGS__); \
write_stdout(m__print_buf, m__print_size); \
} while(0);
#define println(fmt, ...) \
do { \
char m__println_buf[2046]; \
u32 m__println_size = scb_snprintf(m__println_buf, sizeof(m__println_buf), fmt, ##__VA_ARGS__); \
m__println_buf[m__println_size++] = '\n'; \
write_stdout(m__println_buf, m__println_size); \
} while(0);
#define print_err(fmt, ...) \
do { \
char m__print_buf[2046]; \
u32 m__print_size = scb_snprintf(m__print_buf, sizeof(m__print_buf), fmt, ##__VA_ARGS__); \
write_stderr(m__print_buf, m__print_size); \
} while(0);
#define println_err(fmt, ...) \
do { \
char m__println_buf[2046]; \
u32 m__println_size = scb_snprintf(m__println_buf, sizeof(m__println_buf), fmt, ##__VA_ARGS__); \
m__println_buf[m__println_size++] = '\n'; \
write_stderr(m__println_buf, m__println_size); \
} while(0);
// assert.h
#ifdef _WIN32
#define scb_debugbreak() __debugbreak()
#else
#define scb_debugbreak() __builtin_trap()
#endif
#define scb_assert(x) \
if (!(x)) { \
println("[%s, %s, %u] ASSERT : %s", __FILE__, __FUNCTION__, __LINE__, #x); \
scb_debugbreak(); \
}
#if DEBUG
#define log_break scb_debugbreak()
#define log_error(...) \
do { \
print("[%s, %s, %u] LOG ERROR : ", __FILE__, __FUNCTION__, __LINE__); \
println(__VA_ARGS__); \
log_break; \
} while(0);
#define log_os_error(...) \
do { \
char m__log_os_error_buf[512]; \
os_error_string(m__log_os_error_buf, sizeof(m__log_os_error_buf)); \
print("[%s, %s, %u] LOG OS ERROR : ", __FILE__, __FUNCTION__, __LINE__); \
print(__VA_ARGS__); \
println(" : %s", m__log_os_error_buf); \
log_break; \
} while(0);
#else
#define log_error(...)
#define log_os_error(...)
#endif
#define log_error_if(prec, ...) \
do { if (prec) {log_error(__VA_ARGS__);} } while(0);
#define log_os_error_if(prec, ...) \
do { if (prec) {log_os_error(__VA_ARGS__);} } while(0);
#define invalid_default_case log_error("Invalid default case")
// math.h
#define kb(x) ((x) * (u64)1024)
#define mb(x) (kb(x) * (u64)1024UL)
#define gb(x) (mb(x) * (u64)1024UL)
#define secs_to_ms(x) ((u64)(x) * (u64)1000)
#define secs_to_ns(x) ((u64)(x) * (u64)1e9)
#define ms_to_secs(x) ((u64)(x) / 1000)
#define ms_to_ns(x) ((u64)(x) * 1000000)
struct offset_u32 { u32 x,y; };
struct extent_u32 { u32 w,h; };
struct rect_u32 {
struct offset_u32 ofs;
struct extent_u32 ext;
};
struct offset_s32 { s32 x,y; };
struct extent_s32 { s32 w,h; };
struct rect_s32 {
struct offset_s32 ofs;
struct extent_s32 ext;
};
struct offset_u16 { u16 x,y; };
struct extent_u16 { u16 w,h; };
struct rect_u16 {
struct offset_u16 ofs;
struct extent_u16 ext;
};
struct offset_s16 { s16 x,y; };
struct extent_s16 { s16 w,h; };
struct rect_s16 {
struct offset_s16 ofs;
struct extent_s16 ext;
};
struct offset_f32 { float x,y; };
struct extent_f32 { float w,h; };
struct rect_f32 {
struct offset_f32 ofs;
struct extent_f32 ext;
};
#define OFFSET(ofs_x, ofs_y, type) ((struct offset_ ## type) {.x = (type)(ofs_x), .y = (type)(ofs_y)})
#define EXTENT(ext_w, ext_h, type) ((struct extent_ ## type) {.w = (type)(ext_w), .h = (type)(ext_h)})
#define CAST_OFFSET(ofs, type) ((struct offset_ ## type) {.x = (type)(ofs.x), .y = (type)(ofs.y)})
#define CAST_EXTENT(ext, type) ((struct extent_ ## type) {.w = (type)(ext.w), .h = (type)(ext.h)})
#define RECT(o, e, type) ((struct rect_ ## type) {.ofs = o, .ext = e})
#define OFFSET_OP(p1, p2, op, type) OFFSET(p1.x op p2.x, p1.y op p2.y, type)
#define rect_clamp(rect, lim) \
do { \
if (rect.ofs.x < lim.ofs.x) rect.ofs.x = lim.ofs.x; \
if (rect.ofs.y < lim.ofs.y) rect.ofs.y = lim.ofs.y; \
if (rect.ext.w > lim.ext.w) rect.ext.w = lim.ext.w; \
if (rect.ext.h > lim.ext.h) rect.ext.h = lim.ext.h; \
} while(0)
struct rgba { u8 r,g,b,a; };
#define RGBA(red,green,blue,alpha) ((struct rgba) {.r = red, .g = green, .b = blue, .a = alpha})
static inline void rgb_copy(struct rgba *to, struct rgba *from) {
to->r = from->r;
to->g = from->g;
to->b = from->b;
}
static inline bool is_pow2(u64 x)
{
// NOTE(SollyCB): I know that zero is not a power of two, but this function
// acts on integers, so any code using it is doing so for alignment and offset purposes,
// and zero being valid is therefore useful.
return (x & (x - 1)) == 0;
}
static inline u64 mod_pow2(u64 l, u64 r)
{
log_error_if(!is_pow2(r), "Trying to align to a value which is not a power of 2");
return l & (r - 1);
}
static inline u64 next_pow2(u64 x) // TODO(SollyCB): There must be a better implementation...
{
if (x == 0)
return 1;
return is_pow2(x) ? x : (u64)1 << clz(x);
}
static inline u64 inc_and_wrap(u64 inc, u64 wrap)
{
log_error_if(!is_pow2(wrap), "Wrapping value must be a power of 2");
return (inc + 1) & (wrap - 1);
}
static inline u64 align(u64 size, u64 alignment) {
log_error_if(!is_pow2(alignment), "Trying to align to a value which is not a power of 2");
u64 alignment_mask = alignment - 1;
return (size + alignment_mask) & ~alignment_mask;
}
static inline u32 log2_u32(u32 x)
{
u32 c = 0;
while(x /= 2)
c += 1;
return c;
}
static inline f32 circle(f32 x, f32 r, f32 h, f32 k)
{
return sqrtf(r*r - (x-h)*(x-h)) + k;
}
static inline u32 fill_rect_simple(struct rect_u32 r, struct offset_u32 *ret)
{
u32 cnt = 0;
for(u32 j=r.ofs.y; j < r.ofs.y + r.ext.h; ++j) {
for(u32 i = r.ofs.x; i < r.ofs.x + r.ext.w; ++i)
ret[cnt++] = OFFSET(i, j, u32);
}
return cnt;
}
// @Todo - This is pretty close, but not quite working correctly...
// the rectangle is drawn around the vector pos_1 - pos_2
static inline u32 fill_rect(struct offset_u32 pos_1, struct offset_u32 pos_2, u32 width, struct offset_u32 *ret)
{
if (pos_1.x == pos_2.x &&
pos_1.y == pos_2.y)
{
ret[0] = pos_1;
return 1;
}
if (pos_1.y > pos_2.y)
swap(pos_1, pos_2);
if (pos_1.x == pos_2.x) {
struct rect_u32 rect = RECT(OFFSET(pos_1.x, pos_1.y, u32), EXTENT(width, pos_2.y - pos_1.y, u32), u32);
return fill_rect_simple(rect, ret);
} else if (pos_1.y == pos_2.y) {
u32 x1 = pos_1.x, x2 = pos_2.x;
if (x1 > x2) swap(x1, x2);
struct rect_u32 rect = RECT(OFFSET(x1, pos_1.y, u32), EXTENT(x2-x1, width, u32), u32);
return fill_rect_simple(rect, ret);
}
struct offset_s32 vec = OFFSET(pos_2.x - pos_1.x, pos_2.y - pos_1.y, s32);
float f = (f32)(width * width) / (vec.x*vec.x + vec.y*vec.y);
s32 sh_x = (s32)(vec.y * f);
s32 sh_y = (s32)(vec.x * f);
struct offset_u32 p1,p2,p3,p4;
if (vec.x < 0) { // sh_y will be negative
p1 = OFFSET(pos_1.x - sh_x, pos_1.y + sh_y, u32);
p2 = OFFSET(pos_1.x + sh_x, pos_1.y - sh_y, u32);
p3 = OFFSET(pos_2.x - sh_x, pos_2.y + sh_y, u32);
p4 = OFFSET(pos_2.x + sh_x, pos_2.y - sh_y, u32);
} else { // sh_y will be positive
p1 = OFFSET(pos_1.x + sh_x, pos_1.y - sh_y, u32);
p2 = OFFSET(pos_2.x + sh_x, pos_2.y - sh_y, u32);
p3 = OFFSET(pos_1.x - sh_x, pos_1.y + sh_y, u32);
p4 = OFFSET(pos_2.x - sh_x, pos_2.y + sh_y, u32);
}
if ((vec.x < 0 && p2.y > p3.y) ||
(vec.x > 0 && p2.y < p3.y))
{
swap(p2, p3);
}
u32 h_top = vec.x < 0 ? p2.y - p1.y : p3.y - p1.y;
u32 h_mid = vec.x < 0 ? p3.y - p2.y : p2.y - p3.y;
u32 h_bot = vec.x < 0 ? p4.y - p3.y : p4.y - p2.y;
u32 cnt = 0;
{
f32 d12 = ((f32)p2.x - p1.x) / (p2.y - p1.y);
f32 d13 = ((f32)p3.x - p1.x) / (p3.y - p1.y);
u32 h = h_top;
f32 xbeg = d13;
f32 xend = d12;
for(u32 y = 0; y < h; ++y, xbeg += d13, xend += d12) {
for(s32 x = (s32)xbeg; x < (s32)xend; ++x)
ret[cnt++] = OFFSET((u32)(x + p1.x), y + p1.y, u32);
}
} {
f32 d = vec.x < 0 ? ((f32)p3.x - p1.x) / (p3.y - p1.y) : ((f32)p2.x - p1.x) / (p2.y - p1.y);
u32 h = h_mid;
f32 xbeg = d;
f32 xend = d;
for(u32 y = h_top; y < h + h_top; ++y, xbeg += d, xend += d) {
for(s32 x = (s32)xbeg; x < (s32)xend; ++x)
ret[cnt++] = OFFSET((u32)(x + p1.x), y + p1.y, u32);
}
} {
f32 d24 = ((f32)p4.x - p2.x) / (p4.y - p2.y);
f32 d34 = ((f32)p4.x - p3.x) / (p4.y - p3.y);
u32 h = h_bot;
f32 xbeg = d34;
f32 xend = d24;
for(u32 y = h_mid + h_top; y < h + h_mid + h_top; ++y, xbeg += d34, xend += d24) {
for(s32 x = (s32)xbeg; x < (s32)xend; ++x)
ret[cnt++] = OFFSET((u32)(x + p1.x), y + p1.y, u32);
}
}
return cnt;
}
static inline u32 fill_circle(s32 r, u32 h, u32 k, struct offset_u32 *ret)
{
u32 cnt = 0;
for(s32 j = -r; j < r; ++j) {
s32 l = (s32)roundf(sqrtf((f32)r*r - j*j));
for(s32 i = -l; i < l; ++i)
ret[cnt++] = OFFSET(i + h, j + k, u32);
}
return cnt;
}
static inline u64 set_add(u64 set, u64 i)
{
log_error_if(i > 64, "Trying to add %u to a set which only holds 64", i);
return set | ((u64)1 << i);
}
static inline bool set_test(u64 set, u64 i)
{
return set & ((u64)1 << i);
}
// vec.h
// @Todo Idk if this is too big/too small. Same magnitude as used in test.c.
#define FLOAT_ERROR 0.000001
static inline bool feq(float a, float b)
{
return fabsf(a - b) < FLOAT_ERROR;
}
static inline float lerp(float a, float b, float c)
{
return a + c * (b - a);
}
static inline float clamp(float num, float min, float max)
{
if (num > max)
num = max;
else if (num < min)
num = min;
return num;
}
#define PI 3.1415926f
#define PI_OVER_180 0.01745329f
static inline float radf(float x) {
return x * PI_OVER_180;
}
typedef msvc_align(16) struct {
float x,y,z,w;
} vector gcc_align(16);
// should be constructed as:
// bottom left near, bottom left far, bottom right far, bottom right near,
// top left near, top left far, top right far, top right near,
struct box {
vector p[8];
};
static inline void get_box(vector bln, vector blf, vector brf, vector brn,
vector tln, vector tlf, vector trf, vector trn, struct box *b)
{
b->p[0] = bln; b->p[1] = blf; b->p[2] = brf; b->p[3] = brn;
b->p[4] = tln; b->p[5] = tlf; b->p[6] = trf; b->p[7] = trn;
}
struct trs {
vector t;
vector r;
vector s;
};
typedef msvc_align(16) struct matrix {
float m[16];
} matrix gcc_align(16);
struct triangle_f32 {
vector p[3];
};
// glsl compatibility for cpu interactive structs
#define vec4 vector
#define mat4 matrix
static inline vector get_vector(float x, float y, float z, float w)
{
return (vector) {.x = x, .y = y, .z = z, .w = w};
}
#define vector4(x, y, z, w) get_vector(x, y, z, w)
#define vector3(x, y, z) get_vector(x, y, z, 0)
#define vector2(x, y) get_vector(x, y, 0, 0)
static inline void get_matrix(vector colx, vector coly, vector colz, vector colw, matrix *m)
{
__m128 a = _mm_load_ps(&colx.x);
__m128 b = _mm_load_ps(&coly.x);
__m128 c = _mm_load_ps(&colz.x);
__m128 d = _mm_load_ps(&colw.x);
_mm_store_ps(&m->m[ 0], a);
_mm_store_ps(&m->m[ 4], b);
_mm_store_ps(&m->m[ 8], c);
_mm_store_ps(&m->m[12], d);
}
#define matrix4(x, y, z, w, m) get_matrix(x, y, z, w, m)
#define matrix3(x, y, z, m) get_matrix(x, y, z, (vector){0}, m)
static inline void load_count_matrices_ua(u32 count, float *from, matrix *to)
{
for(u32 i=0; i < count; ++i)
matrix4(vector4(from[i*16+ 0], from[i*16+ 1], from[i*16+ 2], from[i*16+ 3]),
vector4(from[i*16+ 4], from[i*16+ 5], from[i*16+ 6], from[i*16+ 7]),
vector4(from[i*16+ 8], from[i*16+ 9], from[i*16+10], from[i*16+11]),
vector4(from[i*16+12], from[i*16+13], from[i*16+14], from[i*16+15]), &to[i]);
}
static inline vector vector3_w(vector v, float w)
{
v.w = w;
return v;
}
static inline void get_trs(vector t, vector r, vector s, struct trs *trs)
{
*trs = (struct trs) {
.t = t,
.r = r,
.s = s,
};
}
static inline void print_matrix(matrix *m)
{
print("[\n");
const u32 cols[] = {0,4,8,12};
u32 i,j;
for(i=0;i<4;++i) {
print(" ");
for(j=0;j<4;++j)
print("%f, ", m->m[cols[j]+i]);
print("\n");
}
print("]\n");
}
static inline void print_vector(vector v)
{
print("[%f, %f, %f, %f]", v.x, v.y, v.z, v.w);
}
static inline void println_vector(vector v)
{
print("[%f, %f, %f, %f]\n", v.x, v.y, v.z, v.w);
}
static inline void print_box(struct box *b)
{
for(u32 i=0; i < cl_array_size(b->p) / 2; ++i) {
if (i == 0) {
print_vector(b->p[i]); print(" | box %uh", b);
} else {
println_vector(b->p[i]);
}
}
}
static inline void array_to_vector(float *arr, vector v)
{
memcpy(&v, arr, sizeof(*arr) * 4);
}
static inline vector scalar_mul_vector(vector v, float s)
{
__m128 a = _mm_load_ps(&v.x);
__m128 b = _mm_set1_ps(s);
a = _mm_mul_ps(a,b);
vector r;
_mm_store_ps(&r.x, a);
return r;
}
#define scale_vector(v, s) scalar_mul_vector(v, s)
static inline vector scalar_div_vector(vector v, float s)
{
__m128 a = _mm_load_ps(&v.x);
__m128 b = _mm_set1_ps(s);
a = _mm_div_ps(a,b);
vector r;
_mm_store_ps(&r.x, a);
return r;
}
static inline vector mul_vector(vector v1, vector v2)
{
__m128 a = _mm_load_ps(&v1.x);
__m128 b = _mm_load_ps(&v2.x);
a = _mm_mul_ps(a,b);
vector r;
_mm_store_ps(&r.x, a);
return r;
}
static inline vector div_vector(vector v1, vector v2)
{
__m128 a = _mm_load_ps(&v1.x);
__m128 b = _mm_load_ps(&v2.x);
a = _mm_div_ps(a,b);
vector r;
_mm_store_ps(&r.x, a);
return r;
}
static inline float sq_vector(vector v)
{
v = mul_vector(v, v);
return v.x + v.y + v.z + v.w;
}
static inline float dot(vector v1, vector v2)
{
vector v3 = mul_vector(v1, v2);
return v3.x + v3.y + v3.z + v3.w;
}
// w component returned as 0
static inline vector cross(vector p, vector q)
{
vector ret;
ret.x = p.y * q.z - p.z * q.y;
ret.y = p.z * q.x - p.x * q.z;
ret.z = p.x * q.y - p.y * q.x;
ret.w = 0;
return ret;
}
static inline vector add_vector(vector v1, vector v2)
{
__m128 a = _mm_load_ps(&v1.x);
__m128 b = _mm_load_ps(&v2.x);
a = _mm_add_ps(a,b);
vector r;
_mm_store_ps(&r.x, a);
return r;
}
static inline vector sub_vector(vector v1, vector v2)
{
__m128 a = _mm_load_ps(&v1.x);
__m128 b = _mm_load_ps(&v2.x);
a = _mm_sub_ps(a,b);
vector r;
_mm_store_ps(&r.x, a);
return r;
}
static inline float vector_len(vector v) {
__m128 a = _mm_load_ps(&v.x);
__m128 b = a;
a = _mm_mul_ps(a,b);
float *f = (float*)&a;
return sqrtf(f[0] + f[1] + f[2]);
}
#define magnitude_vector(v) vector_len(v)
static inline vector normalize(vector v) {
float f = vector_len(v);
return scalar_div_vector(v, f);
}
static inline vector lerp_vector(vector a, vector b, float c) {
vector ret;
ret = sub_vector(b, a);
ret = scalar_mul_vector(ret, c);
return add_vector(a, ret);
}
// angle in radians
static inline vector quaternion(float angle, vector v)
{
v = normalize(v);
float f = angle/2;
float sf = sinf(f);
vector r;
__m128 a;
__m128 b;
a = _mm_load_ps(&v.x);
b = _mm_set1_ps(sf);
a = _mm_mul_ps(a, b);
_mm_store_ps(&r.x, a);
r.w = cosf(f);
return r;
}
static inline vector invert_quaternion(vector q)
{
return vector4(-q.x, -q.y, -q.z, q.w);
}
// equivalent to applying rotation q2, followed by rotation q1
static inline vector hamilton_product(vector q1, vector q2)
{
#if 0
return (vector) {
.x = q1.w * q2.x + q1.x * q2.w + q1.y * q2.z - q1.z * q2.y,
.y = q1.w * q2.y - q1.x * q2.z + q1.y * q2.w + q1.z * q2.x,
.z = q1.w * q2.z + q1.x * q2.y - q1.y * q2.x + q1.z * q2.w,
.w = q1.w * q2.w - q1.x * q2.x - q1.y * q2.y - q1.z * q2.z,
};
#endif
__m128 a,b,c,d,e;
a = _mm_load_ps(&q2.x);
b = _mm_set_ps1(q1.w);
c = _mm_set_ps1(q1.x);
d = _mm_set_ps1(q1.y);
e = _mm_set_ps1(q1.z);
b = _mm_mul_ps(a,b);
c = _mm_mul_ps(a,c);
d = _mm_mul_ps(a,d);
e = _mm_mul_ps(a,e);
vector x,y,z,w;
_mm_store_ps(&w.x, b);
_mm_store_ps(&x.x, c);
_mm_store_ps(&y.x, d);
_mm_store_ps(&z.x, e);
return (vector) { // @Optimise This could likely be done better, idk.
.x = w.x + x.w + y.z - z.y,
.y = w.y - x.z + y.w + z.x,
.z = w.z + x.y - y.x + z.w,
.w = w.w - x.x - y.y - z.z,
};
}
#define mul_quaternion(p, q) hamilton_product(p, q)
// rotate like the inverse of a rotation matrix (like a view matrix)
static inline vector rotate_active(vector p, vector q)
{
vector v = hamilton_product(hamilton_product(invert_quaternion(q), p), q);
return vector3(v.x, v.y, v.z);
}
// rotate like a rotation matrix
static inline vector rotate_passive(vector p, vector q)
{
vector v = hamilton_product(hamilton_product(q, p), invert_quaternion(q));
return vector3(v.x, v.y, v.z);
}
// rotate axis of rotation of p by q
static inline vector rotate_quaternion_axis(vector p, vector q)
{
vector v = vector3(p.x, p.y, p.z);
v = rotate_passive(p, q);
return vector4(v.x, v.y, v.z, p.w);
}
static inline float quaternion_angle(vector q)
{
return acosf(q.w) * 2;
}
static inline vector quaternion_axis(vector q)
{
float a = quaternion_angle(q);
vector r = scalar_div_vector(q, sinf(a/2));
r.w = 0;
return r;
}
static inline void copy_matrix(matrix *to, matrix *from)