aboutsummaryrefslogtreecommitdiff
path: root/src/ungrateful.h
blob: ba78eca8deed94ad717e9331b35a6aa844e2c7ac (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
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
#if !defined(UNGRATEFUL_H)
#   define UNGRATEFUL_H

/*
    Ungrateful - standard library for game development.

    LICENSE:

    Copyright (C) 2025 Bogdan Masyutin (bonmas14)

    This software is provided 'as-is', without any express or implied
    warranty.  In no event will the authors be held liable for any damages
    arising from the use of this software.

    Permission is granted to anyone to use this software for any purpose,
    including commercial applications, and to alter it and redistribute it
    freely, subject to the following restrictions:

      1. The origin of this software must not be misrepresented; you must not
         claim that you wrote the original software. If you use this software
         in a product, an acknowledgment in the product documentation would be
         appreciated but is not required.
      2. Altered source versions must be plainly marked as such, and must not be
         misrepresented as being the original software.
      3. This notice may not be removed or altered from any source distribution.

    Bogdan Masyutin - bonmas14@gmail.com
*/

#if defined(__clang__) || defined(__GNUC__)
#   define CLANG_COMPILER
#   define __TRAP() __builtin_trap()
#elif _MSC_VER >= 1939
#   define MSVC_COMPILER
#   include <intrin.h>
#   define __TRAP() __debugbreak()
#else
#   error "Unknown compiler"
#endif

#if defined(__cplusplus)
#   define CLITERAL(type)      type
#else
#   define CLITERAL(type)      (type)
#endif

#if defined(_WIN32)
#   define OS_WINDOWS
#elif defined(__linux__)
#   define OS_LINUX
#else
#   error "unknown platform!"
#endif

#define UN_TEMP_SIZE UN_MB(50)

#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdarg.h>
#include <limits.h>
#include <math.h>
#include <string.h>
#include <wchar.h>

#define UNUSED(x) (void)(x)

#define UN_KB(s) ((u64)(s) * 1024LL)
#define UN_MB(s) (UN_KB(s) * 1024LL)
#define UN_GB(s) (UN_MB(s) * 1024LL)

#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
#define UN_INLINE inline
#elif defined(__GNUC__) || defined(__clang__)
#define UN_INLINE __inline__
#elif defined(_MSC_VER)
#define UN_INLINE __inline
#else
#define UN_INLINE
#endif

#define UN_MAX(a, b) (a) > (b) ? (a) : (b)
#define UN_MIN(a, b) (a) < (b) ? (a) : (b)

#define UN_TODO(msg) assert((msg, false))
#define UN_STR(cstr) un_string_from_cstring(cstr)

#define UN_CLEAR(var) memset((void*)&var, 0, sizeof(var))
#define UN_POW_OF2(x) (((uintptr_t)(x) > 0) && (((uintptr_t)(x) & ((uintptr_t)(x) - 1)) == 0))
#define UN_DEFAULT_ALIGN (2 * sizeof(void *))

#if !defined(assert)
#if !defined(NDEBUG)
#include <stdio.h>

#define assert(expr) if ((int)(expr) == 0) {\
    fprintf(stderr, "Assert at %s:%d failed!\n", __FILE__, __LINE__); \
    *((int *)0) = 0; \
}

#else
#define assert(expr) (void)(expr)
#endif /* NDEBUG */
#endif /* assert */


#if defined(__cplusplus)
extern "C" {
#endif

typedef uint64_t u64;
typedef uint32_t u32;
typedef uint16_t u16;
typedef uint8_t  u8;

typedef int64_t  s64;
typedef int32_t  s32;
typedef int16_t  s16;
typedef int8_t   s8;

typedef int32_t  b32;
typedef int8_t   b8;

typedef wchar_t wchar;

typedef struct float2 { float x, y; } float2;
typedef struct float3 { float x, y, z; } float3;
typedef struct float4 { float x, y, z, w; } float4;

typedef struct double2 { double x, y; } double2;
typedef struct double3 { double x, y, z; } double3;
typedef struct double4 { double x, y, z, w; } double4;

#if defined(UN_DOUBLE_PRECISION)
    typedef float  real;
    typedef float2 real2;
    typedef float3 real3;
    typedef float4 real4;
#else
    typedef double  real;
    typedef double2 real2;
    typedef double3 real3;
    typedef double4 real4;
#endif /* UN_DOUBLE_PRECISION */

#if !defined(PI)
#   define PI 3.14159265358979323846
#endif

#if !defined(TAU)
#   define TAU (PI * 2)
#endif

#if !defined(EPSILON)
#   define EPSILON 0.000001
#endif

#if !defined(DEG2RAD)
#   define DEG2RAD (PI/180.0)
#endif

#if !defined(RAD2DEG)
#   define RAD2DEG (180.0/PI)
#endif

#if !defined(EPSILON_D)
#   define EPSILON_D 0.000000000000001
#endif

#define un_vec_unwrap2(vec) (vec).x, (vec).y
#define un_vec_unwrap3(vec) (vec).x, (vec).y, (vec).z
#define un_vec_unwrap4(vec) (vec).x, (vec).y, (vec).z, (vec).w

/* ---- Memory Allocators API ---- */

typedef enum {
    UN_ALLOC_MSG_ALLOCATE,
    UN_ALLOC_MSG_FREE,
    UN_ALLOC_MSG_SELF_DELETE,
} Allocator_Message;

#define ALLOCATOR_PROC_SIGNATURE(name)\
    void *name(void *p, u64 size, u32 align, Allocator_Message message, void *data)

typedef ALLOCATOR_PROC_SIGNATURE(Allocator_Proc);

typedef struct {
    Allocator_Proc *proc;
    void           *data;
} Allocator;

extern Allocator un_alloc_std_get(void);

extern void      un_alloc_temp_init(u64 byte_size);
extern void      un_alloc_temp_reset(void);
extern Allocator un_alloc_temp_get(void);

extern Allocator un_alloc_arena_create(u64 initial_size);
extern void      un_alloc_arena_destroy(Allocator *alloc); 

extern void*     un_memory_alloc(u64 size, Allocator alloc);
extern void*     un_memory_alloc_align(u64 size, u32 align, Allocator alloc);
extern void      un_memory_free(void *ptr, Allocator alloc);

/* ---- Generic list structure ---- */

#define UN_LIST_STANDARD_CAPACITY UN_KB(1)

typedef struct {
    u64 count;
    u64 capacity;
    u64 element_size;
    Allocator alloc;
    void *data;
} List;

extern List un_list_create(u64 start_capacity, u64 element_size, Allocator alloc);
extern void un_list_destroy(List *list);

extern List  un_list_clone(List *list, Allocator alloc);
extern b32   un_list_append(List *list, void *data); /* Returns true if succeed. */
extern void *un_list_get(List *list, u64 index);
extern void  un_list_remove(List *list, u64 index);

/* ---- no-wide string API ---- */

typedef struct {
    u64 size;
    u8 *data;
} String;

extern String un_string_from_cstring(char *cstring);
extern u8*    un_string_to_cstring(String string, Allocator alloc);

extern String un_string_copy(String source, Allocator alloc);
extern s32    un_string_compare(String left, String right);
extern String un_string_concat(String left, String right, Allocator alloc);

extern String un_string_swap(String input, u8 from, u8 to, Allocator alloc);
extern List   un_string_split(String input, String pattern, Allocator alloc);
extern String un_string_join(List string_list, String separator, Allocator alloc);

extern String un_string_substring(String input, u64 start, u64 max_size);

extern s64    un_string_index_of(String input, u8 value, u64 skip_count);
extern s64    un_string_index_of_last(String input, u8 value);

/* Simplest formatter, supports:
 *
 * %% - %
 * %c - char (internally u32)
 * %u - u64
 * %d - s64
 * %s - String
 * */
extern String un_string_format(Allocator alloc, String buffer, ...);
extern String un_string_vformat(Allocator alloc, String buffer, va_list args);

/* ---- wide strings API ---- */

extern wchar* un_wstring_from_string(String utf8_string, Allocator alloc);
extern wchar* un_wstring_from_cstring(u8 *utf8_string, Allocator alloc);

/* ---- math, and vecmath ---- */

#if defined(UN_DOUBLE_PRECISION)
#define un_m_lerpr un_m_lerpd
#else
#define un_m_lerpr un_m_lerpf
#endif

extern UN_INLINE float  un_m_lerpf(float a, float b, float t);
extern UN_INLINE double un_m_lerpd(double a, double b, double t);

/* 2d float */
extern UN_INLINE float2 un_m_add2f(float2 a, float2 b);
extern UN_INLINE float2 un_m_sub2f(float2 a, float2 b);
extern UN_INLINE float2 un_m_add_scalar2f(float2 a, float scalar);
extern UN_INLINE float2 un_m_sub_scalar2f(float2 a, float scalar);
extern UN_INLINE float2 un_m_mul_scalar2f(float2 a, float scalar);
extern UN_INLINE float2 un_m_div_scalar2f(float2 a, float scalar);

extern UN_INLINE float  un_m_dot2f(float2 a, float2 b);
extern UN_INLINE float2 un_m_hadamard2f(float2 a, float2 b);
extern UN_INLINE float2 un_m_project2f(float2 a, float2 onto);
extern UN_INLINE float2 un_m_reflect2f(float2 a, float2 normal);
extern UN_INLINE float2 un_m_cross2f(float2 a);

extern UN_INLINE float2 un_m_normalize2f(float2 a);
extern UN_INLINE float  un_m_magnitude2f(float2 a);
extern UN_INLINE float  un_m_magnitude_sqr2f(float2 a);
extern UN_INLINE float  un_m_distance2f(float2 a, float2 b);
extern UN_INLINE float  un_m_distance_sqr2f(float2 a, float2 b);

/* 3d float */
extern UN_INLINE float3 un_m_add3f(float3 a, float3 b);
extern UN_INLINE float3 un_m_sub3f(float3 a, float3 b);
extern UN_INLINE float3 un_m_add_scalar3f(float3 a, float scalar);
extern UN_INLINE float3 un_m_sub_scalar3f(float3 a, float scalar);
extern UN_INLINE float3 un_m_mul_scalar3f(float3 a, float scalar);
extern UN_INLINE float3 un_m_div_scalar3f(float3 a, float scalar);

extern UN_INLINE float  un_m_dot3f(float3 a, float3 b);
extern UN_INLINE float3 un_m_hadamard3f(float3 a, float3 b);
extern UN_INLINE float3 un_m_project3f(float3 a, float3 onto);
extern UN_INLINE float3 un_m_reflect3f(float3 a, float3 normal);
extern UN_INLINE float3 un_m_cross3f(float3 a, float3 b);

extern UN_INLINE float3 un_m_normalize3f(float3 a);
extern UN_INLINE float  un_m_magnitude3f(float3 a);
extern UN_INLINE float  un_m_magnitude_sqr3f(float3 a);
extern UN_INLINE float  un_m_distance3f(float3 a, float3 b);
extern UN_INLINE float  un_m_distance_sqr3f(float3 a, float3 b);

/* 4d float */
extern UN_INLINE float4 un_m_add4f(float4 a, float4 b);
extern UN_INLINE float4 un_m_sub4f(float4 a, float4 b);
extern UN_INLINE float4 un_m_add_scalar4f(float4 a, float scalar);
extern UN_INLINE float4 un_m_sub_scalar4f(float4 a, float scalar);
extern UN_INLINE float4 un_m_mul_scalar4f(float4 a, float scalar);
extern UN_INLINE float4 un_m_div_scalar4f(float4 a, float scalar);

extern UN_INLINE float  un_m_dot4f(float4 a, float4 b);
extern UN_INLINE float4 un_m_hadamard4f(float4 a, float4 b);
extern UN_INLINE float4 un_m_project4f(float4 a, float4 onto);
extern UN_INLINE float4 un_m_reflect4f(float4 a, float4 normal);

extern UN_INLINE float4 un_m_normalize4f(float4 a);
extern UN_INLINE float  un_m_magnitude4f(float4 a);
extern UN_INLINE float  un_m_magnitude_sqr4f(float4 a);
extern UN_INLINE float  un_m_distance4f(float4 a, float4 b);
extern UN_INLINE float  un_m_distance_sqr4f(float4 a, float4 b);

/* 2d double */
extern UN_INLINE double2 un_m_add2d(double2 a, double2 b);
extern UN_INLINE double2 un_m_sub2d(double2 a, double2 b);

extern UN_INLINE double2 un_m_add_scalar2d(double2 a, double scalar);
extern UN_INLINE double2 un_m_sub_scalar2d(double2 a, double scalar);
extern UN_INLINE double2 un_m_mul_scalar2d(double2 a, double scalar);
extern UN_INLINE double2 un_m_div_scalar2d(double2 a, double scalar);

extern UN_INLINE double  un_m_dot2d(double2 a, double2 b);
extern UN_INLINE double2 un_m_hadamard2d(double2 a, double2 b);
extern UN_INLINE double2 un_m_project2d(double2 a, double2 onto);
extern UN_INLINE double2 un_m_reflect2d(double2 a, double2 normal);
extern UN_INLINE double2 un_m_cross2d(double2 a);

extern UN_INLINE double2 un_m_normalize2d(double2 a);
extern UN_INLINE double  un_m_magnitude2d(double2 a);
extern UN_INLINE double  un_m_magnitude_sqr2d(double2 a);
extern UN_INLINE double  un_m_distance2d(double2 a, double2 b);
extern UN_INLINE double  un_m_distance_sqr2d(double2 a, double2 b);

/* 3d double */
extern UN_INLINE double3 un_m_add3d(double3 a, double3 b);
extern UN_INLINE double3 un_m_sub3d(double3 a, double3 b);

extern UN_INLINE double3 un_m_add_scalar3d(double3 a, double scalar);
extern UN_INLINE double3 un_m_sub_scalar3d(double3 a, double scalar);
extern UN_INLINE double3 un_m_mul_scalar3d(double3 a, double scalar);
extern UN_INLINE double3 un_m_div_scalar3d(double3 a, double scalar);

extern UN_INLINE double  un_m_dot3d(double3 a, double3 b);
extern UN_INLINE double3 un_m_hadamard3d(double3 a, double3 b);
extern UN_INLINE double3 un_m_project3d(double3 a, double3 onto);
extern UN_INLINE double3 un_m_reflect3d(double3 a, double3 normal);
extern UN_INLINE double3 un_m_cross3d(double3 a, double3 b);

extern UN_INLINE double3 un_m_normalize3d(double3 a);
extern UN_INLINE double  un_m_magnitude3d(double3 a);
extern UN_INLINE double  un_m_magnitude_sqr3d(double3 a);
extern UN_INLINE double  un_m_distance3d(double3 a, double3 b);
extern UN_INLINE double  un_m_distance_sqr3d(double3 a, double3 b);

/* 4d double */
extern UN_INLINE double4 un_m_add4d(double4 a, double4 b);
extern UN_INLINE double4 un_m_sub4d(double4 a, double4 b);

extern UN_INLINE double4 un_m_add_scalar4d(double4 a, double scalar);
extern UN_INLINE double4 un_m_sub_scalar4d(double4 a, double scalar);
extern UN_INLINE double4 un_m_mul_scalar4d(double4 a, double scalar);
extern UN_INLINE double4 un_m_div_scalar4d(double4 a, double scalar);

extern UN_INLINE double  un_m_dot4d(double4 a, double4 b);
extern UN_INLINE double4 un_m_hadamard4d(double4 a, double4 b);
extern UN_INLINE double4 un_m_project4d(double4 a, double4 onto);
extern UN_INLINE double4 un_m_reflect4d(double4 a, double4 normal);

extern UN_INLINE double4 un_m_normalize4d(double4 a);
extern UN_INLINE double  un_m_magnitude4d(double4 a);
extern UN_INLINE double  un_m_magnitude_sqr4d(double4 a);
extern UN_INLINE double  un_m_distance4d(double4 a, double4 b);
extern UN_INLINE double  un_m_distance_sqr4d(double4 a, double4 b);

#if defined(UN_DOUBLE_PRECISION) 

/* 2d real */
#define un_m_add2r un_m_add2d
#define un_m_sub2r un_m_sub2d

#define un_m_add_scalar2r un_m_add_scalar2d
#define un_m_sub_scalar2r un_m_sub_scalar2d
#define un_m_mul_scalar2r un_m_mul_scalar2d
#define un_m_div_scalar2r un_m_div_scalar2d

#define un_m_dot2r un_m_dot2d
#define un_m_hadamard2r un_m_hadamard2d
#define un_m_project2r un_m_project2d
#define un_m_reflect2r un_m_reflect2d
#define un_m_cross2r un_m_cross2d

#define un_m_normalize2r un_m_normalize2d
#define un_m_magnitude2r un_m_magnitude2d
#define un_m_magnitude_sqr2r un_m_magnitude_sqr2d
#define un_m_distance2r un_m_distance2d
#define un_m_distance_sqr2r un_m_distance_sqr2d

/* 3d real */
#define un_m_add3r un_m_add3d
#define un_m_sub3r un_m_sub3d

#define un_m_add_scalar3r un_m_add_scalar3d
#define un_m_sub_scalar3r un_m_sub_scalar3d
#define un_m_mul_scalar3r un_m_mul_scalar3d
#define un_m_div_scalar3r un_m_div_scalar3d

#define un_m_dot3r un_m_dot3d
#define un_m_hadamard3r un_m_hadamard3d
#define un_m_project3r un_m_project3d
#define un_m_reflect3r un_m_reflect3d
#define un_m_cross3r un_m_cross3d

#define un_m_normalize3r un_m_normalize3d
#define un_m_magnitude3r un_m_magnitude3d
#define un_m_magnitude_sqr3r un_m_magnitude_sqr3d
#define un_m_distance3r un_m_distance3d
#define un_m_distance_sqr3r un_m_distance_sqr3d

/* 4d real */
#define un_m_add4r un_m_add4d
#define un_m_sub4r un_m_sub4d

#define un_m_add_scalar4r un_m_add_scalar4d
#define un_m_sub_scalar4r un_m_sub_scalar4d
#define un_m_mul_scalar4r un_m_mul_scalar4d
#define un_m_div_scalar4r un_m_div_scalar4d

#define un_m_dot4r un_m_dot4d
#define un_m_hadamard4r un_m_hadamard4d
#define un_m_project4r un_m_project4d
#define un_m_reflect4r un_m_reflect4d

#define un_m_normalize4r un_m_normalize4d
#define un_m_magnitude4r un_m_magnitude4d
#define un_m_magnitude_sqr4r un_m_magnitude_sqr4d
#define un_m_distance4r un_m_distance4d
#define un_m_distance_sqr4r un_m_distance_sqr4d

#else

/* 2d real */
#define un_m_add2r un_m_add2f
#define un_m_sub2r un_m_sub2f

#define un_m_add_scalar2r un_m_add_scalar2f
#define un_m_sub_scalar2r un_m_sub_scalar2f
#define un_m_mul_scalar2r un_m_mul_scalar2f
#define un_m_div_scalar2r un_m_div_scalar2f

#define un_m_dot2r un_m_dot2f
#define un_m_hadamard2r un_m_hadamard2f
#define un_m_project2r un_m_project2f
#define un_m_reflect2r un_m_reflect2f
#define un_m_cross2r un_m_cross2f

#define un_m_normalize2r un_m_normalize2f
#define un_m_magnitude2r un_m_magnitude2f
#define un_m_magnitude_sqr2r un_m_magnitude_sqr2f
#define un_m_distance2r un_m_distance2f
#define un_m_distance_sqr2r un_m_distance_sqr2f

/* 3d real */
#define un_m_add3r un_m_add3f
#define un_m_sub3r un_m_sub3f

#define un_m_add_scalar3r un_m_add_scalar3f
#define un_m_sub_scalar3r un_m_sub_scalar3f
#define un_m_mul_scalar3r un_m_mul_scalar3f
#define un_m_div_scalar3r un_m_div_scalar3f

#define un_m_dot3r un_m_dot3f
#define un_m_hadamard3r un_m_hadamard3f
#define un_m_project3r un_m_project3f
#define un_m_reflect3r un_m_reflect3f
#define un_m_cross3r un_m_cross3f

#define un_m_normalize3r un_m_normalize3f
#define un_m_magnitude3r un_m_magnitude3f
#define un_m_magnitude_sqr3r un_m_magnitude_sqr3f
#define un_m_distance3r un_m_distance3f
#define un_m_distance_sqr3r un_m_distance_sqr3f

/* 4d real */
#define un_m_add4r un_m_add4f
#define un_m_sub4r un_m_sub4f

#define un_m_add_scalar4r un_m_add_scalar4f
#define un_m_sub_scalar4r un_m_sub_scalar4f
#define un_m_mul_scalar4r un_m_mul_scalar4f
#define un_m_div_scalar4r un_m_div_scalar4f

#define un_m_dot4r un_m_dot4f
#define un_m_hadamard4r un_m_hadamard4f
#define un_m_project4r un_m_project4f
#define un_m_reflect4r un_m_reflect4f

#define un_m_normalize4r un_m_normalize4f
#define un_m_magnitude4r un_m_magnitude4f
#define un_m_magnitude_sqr4r un_m_magnitude_sqr4f
#define un_m_distance4r un_m_distance4f
#define un_m_distance_sqr4r un_m_distance_sqr4f

#endif // UN_DOUBLE_PRECISION

/* ---- complex ---- */

#define un_m_complex_addf un_m_add2f
#define un_m_complex_subf un_m_sub2f
extern float2 un_m_complex_divf(float2 a, float2 b);
extern float2 un_m_complex_mulf(float2 a, float2 b);

#define un_m_complex_addd un_m_add2d
#define un_m_complex_subd un_m_sub2d
extern double2 un_m_complex_divd(double2 a, double2 b);
extern double2 un_m_complex_muld(double2 a, double2 b);

#define un_m_complex_magnitudef un_m_magnitude2f
#define un_m_complex_magnituded un_m_magnitude2d

#if defined(UN_DOUBLE_PRECISION)
#define un_m_complex_addr       un_m_complex_addd
#define un_m_complex_subr       un_m_complex_subd
#define un_m_complex_divr       un_m_complex_divd
#define un_m_complex_mulr       un_m_complex_muld
#define un_m_complex_magnituder un_m_complex_magnituded
#else
#define un_m_complex_addr       un_m_complex_addf
#define un_m_complex_subr       un_m_complex_subf
#define un_m_complex_divr       un_m_complex_divf
#define un_m_complex_mulr       un_m_complex_mulf
#define un_m_complex_magnituder un_m_complex_magnitudef
#endif // UN_DOUBLE_PRECISION

/* ---- splines ---- */

extern UN_INLINE float  un_m_bezierf(float   a, float  q0, float  q1, float  b, float t);
extern UN_INLINE float2 un_m_bezier2f(float2 a, float2 q0, float2 q1, float2 b, float t);
extern UN_INLINE float3 un_m_bezier3f(float3 a, float3 q0, float3 q1, float3 b, float t);
extern UN_INLINE float4 un_m_bezier4f(float4 a, float4 q0, float4 q1, float4 b, float t);
       
extern UN_INLINE double  un_m_bezierd(double   a, double  q0,  double q1,  double b, double t);
extern UN_INLINE double2 un_m_bezier2d(double2 a, double2 q0, double2 q1, double2 b, double t);
extern UN_INLINE double3 un_m_bezier3d(double3 a, double3 q0, double3 q1, double3 b, double t);
extern UN_INLINE double4 un_m_bezier4d(double4 a, double4 q0, double4 q1, double4 b, double t);

#if defined(UN_DOUBLE_PRECISION)
#define un_m_bezierr  un_m_bezierd
#define un_m_bezier2r un_m_bezier2d
#define un_m_bezier3r un_m_bezier3d
#define un_m_bezier4r un_m_bezier4d
#else
#define un_m_bezierr  un_m_bezierf
#define un_m_bezier2r un_m_bezier2f
#define un_m_bezier3r un_m_bezier3f
#define un_m_bezier4r un_m_bezier4f
#endif // UN_DOUBLE_PRECISION

/* ---- easing ---- */

extern UN_INLINE float  un_m_ease_isinef(float t);
extern UN_INLINE float  un_m_ease_iosinef(float t);
extern UN_INLINE float  un_m_ease_osinef(float t);
extern UN_INLINE float  un_m_ease_iquadf(float t);
extern UN_INLINE float  un_m_ease_ioquadf(float t);
extern UN_INLINE float  un_m_ease_oquadf(float t);
       
extern UN_INLINE double un_m_ease_isined(double t);
extern UN_INLINE double un_m_ease_iosined(double t);
extern UN_INLINE double un_m_ease_osined(double t);
extern UN_INLINE double un_m_ease_iquadd(double t);
extern UN_INLINE double un_m_ease_ioquadd(double t);
extern UN_INLINE double un_m_ease_oquadd(double t);

#if defined(UN_DOUBLE_PRECISION)
#define un_m_ease_isiner  un_m_ease_isined
#define un_m_ease_iosiner un_m_ease_iosined
#define un_m_ease_osiner  un_m_ease_osined
#define un_m_ease_iquadr  un_m_ease_iquadd
#define un_m_ease_ioquadr un_m_ease_ioquadd
#define un_m_ease_oquadr  un_m_ease_oquadd
#else
#define un_m_ease_isiner  un_m_ease_isinef
#define un_m_ease_iosiner un_m_ease_iosinef
#define un_m_ease_osiner  un_m_ease_osinef
#define un_m_ease_iquadr  un_m_ease_iquadf
#define un_m_ease_ioquadr un_m_ease_ioquadf
#define un_m_ease_oquadr  un_m_ease_oquadf
#endif // UN_DOUBLE_PRECISION

/* --- matrix ---- 
 * Convention of matrices!
 * column-major matrices and all matrices are 4x4.
 */

#define UN_MATRIX_SIZE (16)
#define UN_MATRIX_STRIPE (4)

extern void un_m_mat_mulf(float *v, float *left, float *right);
extern void un_m_mat_identityf(float *v);

#if defined(__cplusplus)
}
#endif

#endif // UNGRATEFUL_H