diff options
author | bonmas14 <bonmas14@gmail.com> | 2025-09-25 14:17:07 +0300 |
---|---|---|
committer | bonmas14 <bonmas14@gmail.com> | 2025-09-25 14:17:07 +0300 |
commit | 166877cf15f6afa89c9f8a61e267d485868d0ee1 (patch) | |
tree | 9f080314edc4fb293f91227cdc1c01cfec8d7bae /src | |
parent | 8ebdc95621bc61fdf3c98cd7ae4ddca67398df23 (diff) | |
download | ungrateful-166877cf15f6afa89c9f8a61e267d485868d0ee1.tar.gz ungrateful-166877cf15f6afa89c9f8a61e267d485868d0ee1.zip |
+disgrace and rework of ungrateful.h
Diffstat (limited to 'src')
-rw-r--r-- | src/cyn_file.c | 11 | ||||
-rw-r--r-- | src/cyn_file_linux_x64.c | 9 | ||||
-rw-r--r-- | src/cyn_file_std.c | 10 | ||||
-rw-r--r-- | src/cyn_file_win_x64.c | 37 | ||||
-rw-r--r-- | src/cyn_log.c | 24 | ||||
-rw-r--r-- | src/cynic.c | 1 | ||||
-rw-r--r-- | src/cynic.h | 83 | ||||
-rw-r--r-- | src/cynic_internal.h | 11 | ||||
-rw-r--r-- | src/d_tcp_win.c | 78 | ||||
-rw-r--r-- | src/d_udp_win.c | 9 | ||||
-rw-r--r-- | src/disgrace.c | 52 | ||||
-rw-r--r-- | src/disgrace.h | 58 | ||||
-rw-r--r-- | src/un_list.c | 23 | ||||
-rw-r--r-- | src/un_math.c | 182 | ||||
-rw-r--r-- | src/un_mem_linux_x64.c | 1 | ||||
-rw-r--r-- | src/un_mem_std.c | 1 | ||||
-rw-r--r-- | src/un_mem_win_x64.c | 1 | ||||
-rw-r--r-- | src/un_memory.c | 282 | ||||
-rw-r--r-- | src/un_strings.c | 53 | ||||
-rw-r--r-- | src/un_vec.c | 506 | ||||
-rw-r--r-- | src/un_vecd.c | 506 | ||||
-rw-r--r-- | src/un_wstrings.c | 2 | ||||
-rw-r--r-- | src/ungrateful.h | 396 |
23 files changed, 1413 insertions, 923 deletions
diff --git a/src/cyn_file.c b/src/cyn_file.c new file mode 100644 index 0000000..4537eb7 --- /dev/null +++ b/src/cyn_file.c @@ -0,0 +1,11 @@ +#if defined(OS_WINDOWS) +#include "cyn_file_win_x64.c" + +#elif defined(OS_LINUX) +#include "cyn_file_linux_x64.c" + +#else +#include "cyn_file_std.c" + +#endif + diff --git a/src/cyn_file_linux_x64.c b/src/cyn_file_linux_x64.c new file mode 100644 index 0000000..ff0c1c2 --- /dev/null +++ b/src/cyn_file_linux_x64.c @@ -0,0 +1,9 @@ +File_Handle *cyn_file_open(String path, File_Status *status) { + UNUSED(path); + + if (status != NULL) { + *status = CYN_FILE_ERROR; + } + + return NULL; +} diff --git a/src/cyn_file_std.c b/src/cyn_file_std.c new file mode 100644 index 0000000..3106818 --- /dev/null +++ b/src/cyn_file_std.c @@ -0,0 +1,10 @@ + +File_Handle *cyn_file_open(String path, File_Status *status) { + UNUSED(path); + + if (status != NULL) { + *status = CYN_FILE_ERROR; + } + + return NULL; +} diff --git a/src/cyn_file_win_x64.c b/src/cyn_file_win_x64.c new file mode 100644 index 0000000..1ded1de --- /dev/null +++ b/src/cyn_file_win_x64.c @@ -0,0 +1,37 @@ +File_Handle *cyn_file_open(String path, File_Status *status) { + wchar *final_path; + Allocator talloc; + + talloc = un_alloc_temp_get(); + + final_path = un_wstring_from_string(path, talloc); + + if (path.size > MAX_PATH) { + if (status != NULL) *status = CYN_FILE_ERROR_MAX_PATH; + return NULL; + } + + if (final_path == NULL) { + if (status != NULL) *status = CYN_FILE_ERROR_NULL_FILENAME; + return NULL; // @todo status + } + + //CreateFileW(); + + if (status != NULL) *status = CYN_FILE_ERROR; + + return NULL; +} + +/* + +HANDLE CreateFileW( + LPCWSTR lpFileName, + DWORD dwDesiredAccess, + DWORD dwShareMode, + NULL, + DWORD dwCreationDisposition, + DWORD dwFlagsAndAttributes, + NULL +); +*/ diff --git a/src/cyn_log.c b/src/cyn_log.c index 7b50914..6e6cf0c 100644 --- a/src/cyn_log.c +++ b/src/cyn_log.c @@ -1,6 +1,6 @@ #include <stdio.h> -Log_Level cyn_current_log_level = UN_LOG_INFO; +Log_Level cyn_current_log_level = CYN_LOG_INFO; void cyn_log_write_internal(Log_Level level, String format, va_list vaptr) { Allocator talloc; @@ -8,25 +8,25 @@ void cyn_log_write_internal(Log_Level level, String format, va_list vaptr) { if (level < cyn_current_log_level) return; - talloc = un_allocator_get_temporary(); + talloc = un_alloc_temp_get(); switch (level) { - case UN_LOG_TRACE: + case CYN_LOG_TRACE: output = UN_STR("[TRACE] "); break; - case UN_LOG_DEBUG: + case CYN_LOG_DEBUG: output = UN_STR("[DEBUG] "); break; - case UN_LOG_INFO: + case CYN_LOG_INFO: output = UN_STR("[INFO] "); break; - case UN_LOG_WARNING: + case CYN_LOG_WARNING: output = UN_STR("[WARNING] "); break; - case UN_LOG_ERROR: + case CYN_LOG_ERROR: output = UN_STR("[ERROR] "); break; - case UN_LOG_FATAL: + case CYN_LOG_FATAL: output = UN_STR("[FATAL] "); break; default: @@ -36,15 +36,15 @@ void cyn_log_write_internal(Log_Level level, String format, va_list vaptr) { output = un_string_concat(output, un_string_vformat(talloc, format, vaptr), talloc); switch (level) { - case UN_LOG_RAW: + case CYN_LOG_RAW: break; - case UN_LOG_FATAL: // @todo, logging should be part of Cynic. + case CYN_LOG_FATAL: // @todo, logging should be part of Cynic. default: output = un_string_concat(output, UN_STR("\n"), talloc); break; } - fprintf(stderr, CSTR un_string_to_cstring(output, talloc)); + fprintf(stderr, (const char *)un_string_to_cstring(output, talloc)); } extern void cyn_log_write_cstring(Log_Level level, u8 *format, ...) { @@ -53,7 +53,7 @@ extern void cyn_log_write_cstring(Log_Level level, u8 *format, ...) { if (level < cyn_current_log_level) return; - temp.size = un_string_get_length(format); + temp.size = strlen((const char *)format); temp.data = format; va_start(vaptr, format); diff --git a/src/cynic.c b/src/cynic.c index e0831e7..a8f9174 100644 --- a/src/cynic.c +++ b/src/cynic.c @@ -2,3 +2,4 @@ #include "cynic_internal.h" #include "cyn_log.c" +#include "cyn_file.c" diff --git a/src/cynic.h b/src/cynic.h index 2aeb6f9..5380b84 100644 --- a/src/cynic.h +++ b/src/cynic.h @@ -28,6 +28,8 @@ #include <ungrateful.h> +#define CYN_FS_MAX_FILE_SIZE (256) + #if defined(__cplusplus) extern "C" { #endif @@ -37,13 +39,13 @@ extern "C" { /* -------- Logging API -------- */ typedef enum { - UN_LOG_TRACE, - UN_LOG_DEBUG, - UN_LOG_INFO, - UN_LOG_WARNING, - UN_LOG_ERROR, - UN_LOG_FATAL, - UN_LOG_RAW + CYN_LOG_TRACE, + CYN_LOG_DEBUG, + CYN_LOG_INFO, + CYN_LOG_WARNING, + CYN_LOG_ERROR, + CYN_LOG_FATAL, + CYN_LOG_RAW } Log_Level; extern Log_Level cyn_current_log_level; @@ -52,8 +54,73 @@ extern void cyn_log_write(Log_Level level, String format, ...); extern void cyn_log_write_cstring(Log_Level level, u8 *format, ...); /* --------- Timer API --------- */ -/* --------- File API ---------- */ /* ------ Filesystem API ------- */ + +typedef enum { + CYN_FS_ATRIB_NONE = 0, +} FS_Attributes; + +typedef struct { + u64 size; + u8 data[1]; +} FS_File; + +typedef struct { + u64 attributes; + u64 file_size; + + struct { + u32 size; + u8 data[CYN_FS_MAX_FILE_SIZE]; + } name; + + u8 data[1]; +} FS_File_Header; + +typedef struct { + u8 signature[4]; + u16 reserved_a; + u16 reserved_b; + + u64 attributes; + u64 file_count; + + u8 content[1]; +} FS_Header; + +/* --------- File API ---------- */ + +typedef enum { + CYN_FILE_OK, + CYN_FILE_ERROR_MAX_PATH, + CYN_FILE_ERROR_NULL_FILENAME, + CYN_FILE_ERROR, +} File_Status; + +typedef enum { + CYN_FILE_CREATE = 1 << 2, + + CYN_FILE_ACCESS_READ = 1 << 3, + CYN_FILE_ACCESS_WRITE = 1 << 4, + + CYN_FILE_SHARE_DELETE = 1 << 5, + CYN_FILE_SHARE_READ = 1 << 6, + CYN_FILE_SHARE_WRITE = 1 << 7, + + CYN_FILE_OPEN_NEW = 1 << 8, + CYN_FILE_OPEN_EXISTING = 1 << 9, +} File_Open_Flags; + +typedef struct File_Handle File_Handle; + +// extern File_Handle *cyn_file_open(String path, File_Open_Flags flags, File_Status *status); +extern File_Handle *cyn_file_open(String path, File_Status *status); +/* path - could be partial or full, should be always with '/' + * status - is optional, + * returns NULL on error and writes error into a `status`. + * */ +extern void cyn_file_close(File_Handle *handle); + /* --------- Config API -------- */ #if defined(__cplusplus) diff --git a/src/cynic_internal.h b/src/cynic_internal.h index 1c1e8bb..2de1e8c 100644 --- a/src/cynic_internal.h +++ b/src/cynic_internal.h @@ -26,6 +26,17 @@ Bogdan Masyutin - bonmas14@gmail.com */ +#if defined(OS_WINDOWS) +# include <windows.h> +#elif defined(OS_LINUX) +#endif + /* Windows threading, loading libraries and so on. */ +typedef struct File_Handle { + String filename; + +} File_Handle; + + #endif // CYNIC_INTERNAL_H diff --git a/src/d_tcp_win.c b/src/d_tcp_win.c new file mode 100644 index 0000000..e033153 --- /dev/null +++ b/src/d_tcp_win.c @@ -0,0 +1,78 @@ + +extern String d_get_hostname_ip(String host, Allocator alloc) { + char *host_cstr; + Allocator talloc; + int err; + String ip; + + talloc = un_alloc_temp_get(); + host_cstr = (char *) un_string_to_cstring(host, talloc); + + // @note, host_info can return multiple ip addresses? + HOSTENT *host_info = gethostbyname(host_cstr); + + if (!host_info) { + + + err = WSAGetLastError(); + + switch (err) { + case WSANOTINITIALISED: + cyn_log_write_cstring(CYN_LOG_ERROR, (u8*) "d_get_hostname_ip: disgrace wasn't initialized, run d_init() before this call."); + break; + case WSAENETDOWN: + cyn_log_write_cstring(CYN_LOG_ERROR, (u8*) "d_get_hostname_ip: network error."); + break; + case WSATRY_AGAIN: + cyn_log_write_cstring(CYN_LOG_ERROR, (u8*) "d_get_hostname_ip: host not found (WSATRY_AGAIN)."); + break; + case WSAHOST_NOT_FOUND: + cyn_log_write_cstring(CYN_LOG_ERROR, (u8*) "d_get_hostname_ip: host not found (WSAHOST_NOT_FOUND)."); + break; + case WSANO_DATA: + cyn_log_write_cstring(CYN_LOG_ERROR, (u8*) "d_get_hostname_ip: no host data (WSA_NO_DATA)."); + break; + case WSAEFAULT: + cyn_log_write_cstring(CYN_LOG_ERROR, (u8*) "d_get_hostname_ip: host name is not valid."); + break; + default: + cyn_log_write_cstring(CYN_LOG_ERROR, (u8*) "d_get_hostname_ip: unspecified error."); + break; + } + + return CLITERAL(String) {}; + } + + if (host_info) { + assert(host_info->h_addrtype == AF_INET); + + assert(host_info->h_length >= sizeof(struct in_addr)); + ip = un_string_from_cstring(inet_ntoa(*(struct in_addr *)*host_info->h_addr_list)); + + return un_string_copy(ip, alloc); + } + + return CLITERAL(String) {}; +} + + +DHandle* d_tcp_connect(String ip, u16 port) { + SOCKET sock; + struct sockaddr_in addr_info; + char *ip_addr_cstr; + + ip_addr_cstr = (char *) un_string_to_cstring(ip, un_alloc_temp_get()); + + sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + + if (sock == INVALID_SOCKET) { + return NULL; + } + + addr_info.sin_family = AF_INET; + addr_info.sin_addr.s_addr = inet_addr(ip_addr_cstr); + addr_info.sin_port = htons(port); + + //connect() + +} diff --git a/src/d_udp_win.c b/src/d_udp_win.c new file mode 100644 index 0000000..e3c0074 --- /dev/null +++ b/src/d_udp_win.c @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/src/disgrace.c b/src/disgrace.c new file mode 100644 index 0000000..cf81b75 --- /dev/null +++ b/src/disgrace.c @@ -0,0 +1,52 @@ +#include "disgrace.h" + +#if defined(OS_WINDOWS) + +#include <winsock2.h> +#include <windows.h> + +#if defined(MSVC_COMPILER) +#pragma comment(lib, "Ws2_32.lib") +#endif + +enum { + D_TYPE_TCP, + D_TYPE_UDP +}; + +typedef struct DHandle { + u64 type; +} DHandle; + +b32 d_init(void) { + WSADATA data; + int error; + + if (WSAStartup(MAKEWORD(2, 2), &data)) { + error = WSAGetLastError(); + cyn_log_write_cstring(CYN_LOG_ERROR, (u8*) "WsaStartup failed, code: %d.", (s64)error); + return false; + } + + return true; +} + + +#include "d_tcp_win.c" +#include "d_udp_win.c" + +#elif defined(OS_LINUX) + +b32 d_init(void) { + return true; +} + +#error "Linux todo" +#include "d_udp_linux.c" +#else + +#error "unknown platform" + +#endif + + diff --git a/src/disgrace.h b/src/disgrace.h new file mode 100644 index 0000000..43f78fb --- /dev/null +++ b/src/disgrace.h @@ -0,0 +1,58 @@ + +#if !defined(DISGRACE_H) +# define DISGRACE_H +/* + Disgrace - platform layer for UDP and TCP. + + 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 +*/ + +#include <ungrateful.h> +#include <cynic.h> + +#if defined(__cplusplus) +extern "C" { +#endif + +struct DHandle; + +b32 d_init(void); + +extern String d_get_hostname_ip(String host, Allocator alloc); + +extern struct DHandle* d_tcp_connect(String ip, u16 port); +extern struct DHandle* d_tcp_listen(); + +extern struct DHandle* d_udp_bind(); +extern u64 d_udp_recive(); +extern u64 d_udp_send(); + + +#if defined(__cplusplus) +} +#endif +#endif /* DISGRACE_H */ + + + + diff --git a/src/un_list.c b/src/un_list.c index 9d6a5ce..c90ae25 100644 --- a/src/un_list.c +++ b/src/un_list.c @@ -9,15 +9,14 @@ List un_list_create(u64 start_capacity, u64 element_size, Allocator alloc) { list.data = un_memory_alloc(list.capacity * list.element_size, alloc); if (list.data == NULL) { - list.alloc = un_allocator_get_temporary(); - list.data = un_memory_alloc(list.capacity * list.element_size, list.alloc); + UN_CLEAR(list); } return list; } -void un_list_destroy(List *list, b32 delete_allocator) { - if (delete_allocator) { un_memory_destroy(&list->alloc); } +void un_list_destroy(List *list) { + un_memory_free(list->data, list->alloc); UN_CLEAR(*list); } @@ -27,15 +26,13 @@ List un_list_clone(List *list, Allocator alloc) { result = *list; result.alloc = alloc; result.data = un_memory_alloc(result.capacity * result.element_size, alloc); - assert(result.data != NULL); if (result.data == NULL) { - result.alloc = un_allocator_get_temporary(); - result.data = un_memory_alloc(result.capacity * result.element_size, result.alloc); + UN_CLEAR(list); + } else { + memcpy(result.data, list->data, result.count * result.element_size); } - un_memory_copy(result.data, list->data, result.count * result.element_size); - return result; } @@ -49,7 +46,7 @@ static b32 list_grow_if_needed(List *list) { return false; } - un_memory_copy(mem, list->data, list->element_size * list->capacity); + memcpy(mem, list->data, list->element_size * list->capacity); un_memory_free(list->data, list->alloc); list->data = mem; @@ -68,7 +65,7 @@ static void un_list_create_if_needed(List *list) { assert(list->element_size != 0); if (!list->data) { - Allocator alloc = list->alloc.proc == NULL ? un_allocator_get_standard() : list->alloc; + Allocator alloc = list->alloc.proc == NULL ? un_alloc_std_get() : list->alloc; *list = un_list_create(UN_LIST_STANDARD_CAPACITY, list->element_size, alloc); } } @@ -79,7 +76,7 @@ b32 un_list_append(List *list, void *data) { if (list_grow_if_needed(list)) { addr = (u8*)list->data + list->count * list->element_size; - un_memory_copy(addr, data, list->element_size); + memcpy(addr, data, list->element_size); list->count++; return true; } else { @@ -106,7 +103,7 @@ void un_list_remove(List *list, u64 index) { move_elements = list->count - (index + 1); if (move_elements) { - un_memory_move(addr, (u8*)addr + list->element_size, move_elements * list->element_size); + memmove(addr, (u8*)addr + list->element_size, move_elements * list->element_size); } list->count--; diff --git a/src/un_math.c b/src/un_math.c index a2ff65e..f6b7cab 100644 --- a/src/un_math.c +++ b/src/un_math.c @@ -1,104 +1,113 @@ /* lerps */ -f32 un_m_lerpf(f32 a, f32 b, f32 t) { +UN_INLINE float un_m_lerpf(float a, float b, float t) { return (1.0 - t) * a + b * t; } -f64 un_m_lerpd(f64 a, f64 b, f64 t) { +UN_INLINE double un_m_lerpd(double a, double b, double t) { return (1.0 - t) * a + b * t; } /* easing */ -f32 un_m_ease_isinef(f32 t) { +UN_INLINE float un_m_ease_isinef(float t) { return 1.0 - cosf((t * PI) / 2.0); } -f32 un_m_ease_iosinef(f32 t) { +UN_INLINE float un_m_ease_iosinef(float t) { return sinf((t * PI) / 2.0f); } -f32 un_m_ease_osinef(f32 t) { +UN_INLINE float un_m_ease_osinef(float t) { return -(cosf(t * PI) - 1.0) / 2.0; } -f32 un_m_ease_iquadf(f32 t) { +UN_INLINE float un_m_ease_iquadf(float t) { return t * t; } -f32 un_m_ease_ioquadf(f32 t) { +UN_INLINE float un_m_ease_ioquadf(float t) { return 1.0 - (1.0 - t) * (1.0 - t); } -f32 un_m_ease_oquadf(f32 t) { +UN_INLINE float un_m_ease_oquadf(float t) { return t < 0.5 ? 2.0 * t * t : 1.0 - ((-2.0 * t + 2.0) * (-2.0 * t + 2.0)) / 2.0; } -f64 un_m_ease_isined(f64 t) { +UN_INLINE double un_m_ease_isined(double t) { return 1.0 - cos((t * PI) / 2.0); } -f64 un_m_ease_iosined(f64 t) { +UN_INLINE double un_m_ease_iosined(double t) { return sin((t * PI) / 2.0f); } -f64 un_m_ease_osined(f64 t) { +UN_INLINE double un_m_ease_osined(double t) { return -(cos(t * PI) - 1.0) / 2.0; } -f64 un_m_ease_iquadd(f64 t) { +UN_INLINE double un_m_ease_iquadd(double t) { return t * t; } -f64 un_m_ease_ioquadd(f64 t) { +UN_INLINE double un_m_ease_ioquadd(double t) { return 1.0 - (1.0 - t) * (1.0 - t); } -f64 un_m_ease_oquadd(f64 t) { +UN_INLINE double un_m_ease_oquadd(double t) { return t < 0.5 ? 2.0 * t * t : 1.0 - ((-2.0 * t + 2.0) * (-2.0 * t + 2.0)) / 2.0; } /* complex */ -void un_m_complex_divf(f32 *v, f32 *a, f32 *b) { - f32 divisor; +UN_INLINE float2 un_m_complex_divf(float2 a, float2 b) { + float f; + float2 result; // un_m_complex_mulf(v, a, conjugated_b) - v[0] = a[0] * b[0] - a[1] * -b[1]; - v[1] = a[0] * -b[1] + a[1] * b[0]; + result.x = a.x * b.x - a.y * -b.y; + result.y = a.x * -b.y + a.y * b.x; - divisor = b[0] * b[0] + b[1] * b[1]; + f = b.x * b.x + b.y * b.y; - v[0] /= divisor; - v[1] /= divisor; + result.x /= f; + result.y /= f; + + return result; } -void un_m_complex_mulf(f32 *v, f32 *a, f32 *b) { - v[0] = a[0] * b[0] - a[1] * b[1]; - v[1] = a[0] * b[1] + a[1] * b[0]; +UN_INLINE float2 un_m_complex_mulf(float2 a, float2 b) { + float2 result; + result.x = a.x * b.x - a.y * b.y; + result.y = a.x * b.y + a.y * b.x; + return result; } +UN_INLINE double2 un_m_complex_divd(double2 a, double2 b) { + double f; + double2 result; -void un_m_complex_divd(f64 *v, f64 *a, f64 *b) { - f64 divisor; + // un_m_complex_mulf(v, a, conjugated_b) + result.x = a.x * b.x - a.y * -b.y; + result.y = a.x * -b.y + a.y * b.x; - // un_m_complex_muld(v, a, conjugated_b) - v[0] = a[0] * b[0] - a[1] * -b[1]; - v[1] = a[0] * -b[1] + a[1] * b[0]; + f = b.x * b.x + b.y * b.y; - divisor = b[0] * b[0] + b[1] * b[1]; + result.x /= f; + result.y /= f; - v[0] /= divisor; - v[1] /= divisor; + return result; } -void un_m_complex_muld(f64 *v, f64 *a, f64 *b) { - v[0] = a[0] * b[0] - a[1] * b[1]; - v[1] = a[0] * b[1] + a[1] * b[0]; +UN_INLINE double2 un_m_complex_muld(double2 a, double2 b) { + double2 result; + result.x = a.x * b.x - a.y * b.y; + result.y = a.x * b.y + a.y * b.x; + return result; } /* splines */ -void un_m_bezierf(f32 *v, f32 a, f32 q0, f32 q1, f32 b, f32 t) { - f32 i0, i1, i2, p0, p1, ti; +UN_INLINE float un_m_bezierf(float a, float q0, float q1, float b, float t) { + float i0, i1, i2, p0, p1, ti; ti = 1.0 - t; @@ -109,11 +118,35 @@ void un_m_bezierf(f32 *v, f32 a, f32 q0, f32 q1, f32 b, f32 t) { p0 = ti * i0 + i1 * t; p1 = ti * i1 + i2 * t; - *v = ti * p0 + p1 * t; + return ti * p0 + p1 * t; } -void un_m_bezierd(f64 *v, f64 a, f64 q0, f64 q1, f64 b, f64 t) { - f64 i0, i1, i2, p0, p1, ti; +UN_INLINE float2 un_m_bezier2f(float2 a, float2 q0, float2 q1, float2 b, float t) { + float2 result; + result.x = un_m_bezierf(a.x, q0.x, q1.x, b.x, t); + result.y = un_m_bezierf(a.y, q0.y, q1.y, b.y, t); + return result; +} + +float3 un_m_bezier3f(float3 a, float3 q0, float3 q1, float3 b, float t) { + float3 result; + result.x = un_m_bezierf(a.x, q0.x, q1.x, b.x, t); + result.y = un_m_bezierf(a.y, q0.y, q1.y, b.y, t); + result.z = un_m_bezierf(a.z, q0.z, q1.z, b.z, t); + return result; +} + +UN_INLINE float4 un_m_bezier4f(float4 a, float4 q0, float4 q1, float4 b, float t) { + float4 result; + result.x = un_m_bezierf(a.x, q0.x, q1.x, b.x, t); + result.y = un_m_bezierf(a.y, q0.y, q1.y, b.y, t); + result.z = un_m_bezierf(a.z, q0.z, q1.z, b.z, t); + result.w = un_m_bezierf(a.w, q0.w, q1.w, b.w, t); + return result; +} + +UN_INLINE double un_m_bezierd(double a, double q0, double q1, double b, double t) { + double i0, i1, i2, p0, p1, ti; ti = 1.0 - t; @@ -124,48 +157,33 @@ void un_m_bezierd(f64 *v, f64 a, f64 q0, f64 q1, f64 b, f64 t) { p0 = ti * i0 + i1 * t; p1 = ti * i1 + i2 * t; - *v = ti * p0 + p1 * t; + return ti * p0 + p1 * t; } -void un_m_bezier2f(f32 *v, f32 *a, f32 *q0, f32 *q1, f32 *b, f32 t) { - un_m_bezierf(v + 0, a[0], q0[0], q1[0], b[0], t); - un_m_bezierf(v + 1, a[1], q0[1], q1[1], b[1], t); +UN_INLINE double2 un_m_bezier2d(double2 a, double2 q0, double2 q1, double2 b, double t) { + double2 result; + result.x = un_m_bezierd(a.x, q0.x, q1.x, b.x, t); + result.y = un_m_bezierd(a.y, q0.y, q1.y, b.y, t); + return result; } -void un_m_bezier3f(f32 *v, f32 *a, f32 *q0, f32 *q1, f32 *b, f32 t) { - un_m_bezierf(v + 0, a[0], q0[0], q1[0], b[0], t); - un_m_bezierf(v + 1, a[1], q0[1], q1[1], b[1], t); - un_m_bezierf(v + 2, a[2], q0[2], q1[2], b[2], t); +UN_INLINE double3 un_m_bezier3d(double3 a, double3 q0, double3 q1, double3 b, double t) { + double3 result; + result.x = un_m_bezierd(a.x, q0.x, q1.x, b.x, t); + result.y = un_m_bezierd(a.y, q0.y, q1.y, b.y, t); + result.z = un_m_bezierd(a.z, q0.z, q1.z, b.z, t); + return result; } -void un_m_bezier4f(f32 *v, f32 *a, f32 *q0, f32 *q1, f32 *b, f32 t) { - un_m_bezierf(v + 0, a[0], q0[0], q1[0], b[0], t); - un_m_bezierf(v + 1, a[1], q0[1], q1[1], b[1], t); - un_m_bezierf(v + 2, a[2], q0[2], q1[2], b[2], t); - un_m_bezierf(v + 3, a[3], q0[3], q1[3], b[3], t); +UN_INLINE double4 un_m_bezier4d(double4 a, double4 q0, double4 q1, double4 b, double t) { + double4 result; + result.x = un_m_bezierd(a.x, q0.x, q1.x, b.x, t); + result.y = un_m_bezierd(a.y, q0.y, q1.y, b.y, t); + result.z = un_m_bezierd(a.z, q0.z, q1.z, b.z, t); + result.w = un_m_bezierd(a.w, q0.w, q1.w, b.w, t); + return result; } - -void un_m_bezier2d(f64 *v, f64 *a, f64 *q0, f64 *q1, f64 *b, f64 t) { - un_m_bezierd(v + 0, a[0], q0[0], q1[0], b[0], t); - un_m_bezierd(v + 1, a[1], q0[1], q1[1], b[1], t); -} - -void un_m_bezier3d(f64 *v, f64 *a, f64 *q0, f64 *q1, f64 *b, f64 t) { - un_m_bezierd(v + 0, a[0], q0[0], q1[0], b[0], t); - un_m_bezierd(v + 1, a[1], q0[1], q1[1], b[1], t); - un_m_bezierd(v + 2, a[2], q0[2], q1[2], b[2], t); -} - -void un_m_bezier4d(f64 *v, f64 *a, f64 *q0, f64 *q1, f64 *b, f64 t) { - un_m_bezierd(v + 0, a[0], q0[0], q1[0], b[0], t); - un_m_bezierd(v + 1, a[1], q0[1], q1[1], b[1], t); - un_m_bezierd(v + 2, a[2], q0[2], q1[2], b[2], t); - un_m_bezierd(v + 3, a[3], q0[3], q1[3], b[3], t); -} - - - /* matrix and quaternions */ /* convention of matrix is: Column-major @@ -179,16 +197,16 @@ void un_m_bezier4d(f64 *v, f64 *a, f64 *q0, f64 *q1, f64 *b, f64 t) { * rotate * quaternion -> matrix * matrix -> quaternion - * matrix to components: un_mat_disassemble(f32 *pos3, f32 *scale3, f32 *quat4) - * un_mat_assemble(f32 *v, f32 *pos3, f32 *scale3, f32 *quat4) + * matrix to components: un_mat_disassemble(float *pos3, float *scale3, float *quat4) + * un_mat_assemble(float *v, float *pos3, float *scale3, float *quat4) * assembe_inverse * * */ -void un_m_mat_identityf(f32 *v) { - un_memory_set((void*)v, 0, sizeof(*v) * UN_MATRIX_SIZE); +UN_INLINE void un_m_mat_identityf(float *v) { + memset((void*)v, 0, sizeof(*v) * UN_MATRIX_SIZE); v[0 + 0 * UN_MATRIX_STRIPE] = 1.0f; v[1 + 1 * UN_MATRIX_STRIPE] = 1.0f; @@ -196,9 +214,9 @@ void un_m_mat_identityf(f32 *v) { v[3 + 3 * UN_MATRIX_STRIPE] = 1.0f; } -void un_m_mat_mulf(f32 *v, f32 *left, f32 *right) { +UN_INLINE void un_m_mat_mulf(float *v, float *left, float *right) { u64 row, col, i; - un_memory_set((void*)v, 0, sizeof(*v) * UN_MATRIX_SIZE); + memset((void*)v, 0, sizeof(*v) * UN_MATRIX_SIZE); for (col = 0; col < 4; col++) { for (row = 0; row < 4; row++) { @@ -209,7 +227,7 @@ void un_m_mat_mulf(f32 *v, f32 *left, f32 *right) { } } -void un_mat_xformf(f32 *v, f32 *xform) { +UN_INLINE void un_mat_xformf(float *v, float *xform) { un_m_mat_identityf(v); v[3 + 0 * UN_MATRIX_STRIPE] += xform[0]; @@ -217,7 +235,7 @@ void un_mat_xformf(f32 *v, f32 *xform) { v[3 + 2 * UN_MATRIX_STRIPE] += xform[2]; } -void un_mat_scalef(f32 *v, f32 *scale) { +UN_INLINE void un_mat_scalef(float *v, float *scale) { un_m_mat_identityf(v); v[0 + 0 * UN_MATRIX_STRIPE] = scale[0]; diff --git a/src/un_mem_linux_x64.c b/src/un_mem_linux_x64.c index 3d498bc..c9d5e36 100644 --- a/src/un_mem_linux_x64.c +++ b/src/un_mem_linux_x64.c @@ -2,6 +2,7 @@ static ALLOCATOR_PROC_SIGNATURE(un_std_alloc_proc) { UNUSED(data); + UNUSED(align); switch (message) { case UN_ALLOC_MSG_ALLOCATE: diff --git a/src/un_mem_std.c b/src/un_mem_std.c index 0d5d288..63c416a 100644 --- a/src/un_mem_std.c +++ b/src/un_mem_std.c @@ -1,5 +1,6 @@ static ALLOCATOR_PROC_SIGNATURE(un_std_alloc_proc) { UNUSED(data); + UNUSED(align); switch (message) { case UN_ALLOC_MSG_ALLOCATE: diff --git a/src/un_mem_win_x64.c b/src/un_mem_win_x64.c index 91f838f..cc2e182 100644 --- a/src/un_mem_win_x64.c +++ b/src/un_mem_win_x64.c @@ -2,6 +2,7 @@ static ALLOCATOR_PROC_SIGNATURE(un_std_alloc_proc) { UNUSED(data); + UNUSED(align); switch (message) { case UN_ALLOC_MSG_ALLOCATE: diff --git a/src/un_memory.c b/src/un_memory.c index 178fcbc..dd1bc87 100644 --- a/src/un_memory.c +++ b/src/un_memory.c @@ -2,267 +2,173 @@ /// -------- Basic functions -void un_memory_set(u8 *buffer, u8 value, u64 size) { - if (size == 0) return; - assert(buffer != NULL); - - while (size-- > 0) { - *buffer++ = value; - } -} - -void un_memory_copy(u8 *dest, u8 *source, u64 size) { - if (size == 0) return; - - assert(dest != NULL); - assert(source != NULL); - -#ifdef DEBUG - - if (dest < source) { - assert(((ptrdiff_t)source - (ptrdiff_t)dest) >= (ptrdiff_t)size); - } else { - assert(((ptrdiff_t)dest - (ptrdiff_t)source) >= (ptrdiff_t)size); - } -#endif // DEBUG - - while (size-- > 0) { - *dest++ = *source++; - } -} - -s32 un_memory_compare(u8 *left, u8 *right, u64 size) { - while (size-- > 0) { - if (*left++ == *right++) - continue; - - return left[-1] > right[-1] ? 1 : -1; - } - - return 0; -} - -void un_memory_move(u8 *dest, u8 *src, u64 size) { - Allocator talloc = un_allocator_get_temporary(); - - void *temp = un_memory_alloc(size, talloc); - - un_memory_copy(temp, src, size); - un_memory_copy(dest, temp, size); -} - void *un_memory_alloc(u64 size, Allocator alloc) { assert(alloc.proc != NULL); - return alloc.proc(NULL, size, UN_ALLOC_MSG_ALLOCATE, alloc.data); + return alloc.proc(NULL, size, UN_DEFAULT_ALIGN, UN_ALLOC_MSG_ALLOCATE, alloc.data); } -void un_memory_free(void *ptr, Allocator alloc) { +void *un_memory_alloc_align(u64 size, u32 align, Allocator alloc) { assert(alloc.proc != NULL); - alloc.proc(ptr, 0, UN_ALLOC_MSG_FREE, alloc.data); + return alloc.proc(NULL, size, align, UN_ALLOC_MSG_ALLOCATE, alloc.data); } -void un_memory_destroy(Allocator *alloc) { - assert(alloc != NULL); - assert(alloc->proc != NULL); - - alloc->proc(NULL, 0, UN_ALLOC_MSG_SELF_DELETE, alloc->data); - - UN_CLEAR(*alloc); +void un_memory_free(void *ptr, Allocator alloc) { + assert(alloc.proc != NULL); + alloc.proc(ptr, 0, 0, UN_ALLOC_MSG_FREE, alloc.data); } -/// -------- Stdlib allocator +/// -------- Std allocator #if defined(OS_WINDOWS) -#include "un_mem_win_x64.c" - +# include "un_mem_win_x64.c" #elif defined(OS_LINUX) -#include "un_mem_linux_x64.c" - +# include "un_mem_linux_x64.c" #else -#include "un_mem_std.c" - +# include "un_mem_std.c" #endif -Allocator un_allocator_get_standard(void) { +Allocator un_alloc_std_get(void) { return CLITERAL(Allocator) { .proc = un_std_alloc_proc }; } -/// -------- Temp allocator - -static struct { - u64 index; - u8 data[UN_TEMP_SIZE]; -} _temp; - -static void temp_reset(void) { - _temp.index = 0; -} +/// Arena allocator -static void *temp_alloc(u64 size) { - if ((_temp.index + size) > UN_TEMP_SIZE) { - // @todo: Decice what is the best behaviour for wrapping - assert(false); - temp_reset(); - } +typedef struct Arena { + u8 *buffer; + u64 size, curr_offset, prev_offset; +} Arena; - if ((_temp.index + size) > UN_TEMP_SIZE) { - return NULL; - } +static Arena __temp_arena; - void *ptr = (u8*)_temp.data + _temp.index; - _temp.index += size; - un_memory_set(ptr, 0x00, size); +static Arena arena_create(u64 size) { + Allocator alloc; + Arena arena; - return ptr; -} + UN_CLEAR(arena); -static ALLOCATOR_PROC_SIGNATURE(un_temp_alloc_proc) { - UNUSED(data); - UNUSED(p); + alloc = un_alloc_std_get(); + arena.buffer = un_memory_alloc(size, alloc); - switch (message) { - case UN_ALLOC_MSG_ALLOCATE: - return temp_alloc(size); - case UN_ALLOC_MSG_FREE: - break; - case UN_ALLOC_MSG_SELF_DELETE: break; + if (arena.buffer == NULL) { + return arena; } - return NULL; + arena.size = size; + return arena; } -Allocator un_allocator_get_temporary(void) { - return CLITERAL(Allocator) { .proc = un_temp_alloc_proc }; -} +// special thanks to ginger bill: https://www.gingerbill.org/article/2019/02/08/memory-allocation-strategies-002/ +static uintptr_t align_forward(uintptr_t ptr, u32 align) { + uintptr_t p, a, modulo; -/// Arena allocator + assert(UN_POW_OF2(align)); -typedef struct Arena { - u64 size; - u64 occupied; - struct Arena *next; - u8 data[1]; -} Arena; + p = ptr; + a = (uintptr_t)align; + modulo = p & (a-1); -static Arena *arena_create(u64 size) { - Allocator alloc; - Arena *arena; + if (modulo != 0) { + p += a - modulo; + } - alloc = un_allocator_get_standard(); - arena = CLITERAL(Arena*)un_memory_alloc(size, alloc); + return p; +} - if (!arena) { - return NULL; - } +static void* arena_allocate_align(Arena *arena, u32 align, u64 size) { + uintptr_t curr_ptr, offset; + u8 *ptr; - UN_CLEAR(*arena); - arena->size = size - sizeof(Arena); - return arena; -} + assert(arena != NULL); -static void arena_delete(Arena *arena) { - if (arena->next != NULL) { - arena_delete(arena->next); + curr_ptr = (uintptr_t)arena->buffer + arena->curr_offset; + offset = align_forward(curr_ptr, align); + offset -= (uintptr_t)arena->buffer; + + if ((offset + size) <= arena->size) { + ptr = (u8*)(arena->buffer + offset); + arena->prev_offset = offset; + arena->curr_offset = offset + size; + memset(ptr, 0, size); + } else { + ptr = NULL; } - Allocator alloc = un_allocator_get_standard(); - un_memory_free(arena, alloc); + return ptr; } -static void *arena_allocate(u64 size, Arena *arena) { - u8* pos; - - if (size <= (arena->size - arena->occupied)) { - pos = arena->data + arena->occupied; - arena->occupied += size; - return (void*)pos; - } +static void arena_free_memory(Arena *arena) { + if (arena == &__temp_arena) return; - if (!arena->next) { - arena->next = arena_create((arena->size + sizeof(Arena)) * 2LL); - } + assert(arena != NULL); - return arena_allocate(size, arena->next); + if (arena->buffer != NULL) { + un_memory_free(arena->buffer, un_alloc_std_get()); + } } static ALLOCATOR_PROC_SIGNATURE(un_arena_alloc_proc) { - assert(data != NULL); UNUSED(p); switch (message) { case UN_ALLOC_MSG_ALLOCATE: - return arena_allocate(size, (Arena*)data); + return arena_allocate_align((Arena*)data, align, size); case UN_ALLOC_MSG_FREE: break; case UN_ALLOC_MSG_SELF_DELETE: - arena_delete((Arena*)data); + arena_free_memory((Arena*)data); break; } return NULL; } -Allocator un_allocator_create_arena(u64 size) { - Allocator alloc = { 0 }; +Allocator un_alloc_arena_create(u64 byte_size) { + Allocator alloc, std; + Arena arena; + UN_CLEAR(alloc); + std = un_alloc_std_get(); alloc.proc = un_arena_alloc_proc; - alloc.data = arena_create(size); - - return alloc; -} + arena = arena_create(byte_size); -/// Wrapping allocator + if(arena.buffer == NULL) { + UN_CLEAR(alloc); + } + alloc.data = un_memory_alloc(sizeof(arena), std); // yeah, it allocs 4kb for 32 bytes... -typedef struct Allocation_Info { - void *p; - u64 size; -} Allocation_Info; + if(alloc.data == NULL) { + arena_free_memory(&arena); + UN_CLEAR(alloc); + return alloc; + } -typedef struct Wrapper { - // we need to create hashmap and add all allocations in here - // List allocations; - Allocator target; -} Wrapper; + *((Arena *)alloc.data) = arena; + return alloc; +} -static Wrapper *wrapper_create(Allocator target) { - Wrapper *wrapper = (Wrapper *)un_memory_alloc(sizeof(Wrapper), un_allocator_get_standard()); +void un_alloc_arena_destroy(Allocator *alloc) { + arena_free_memory((Arena*)alloc->data); - un_memory_set((void*)wrapper, 0, sizeof(Wrapper)); - wrapper->target = target; + un_memory_free(alloc->data, un_alloc_std_get()); - return wrapper; + UN_CLEAR(*alloc); } -static void wrapper_destroy(Wrapper *wrapper) { - assert(wrapper != NULL); - un_memory_free(wrapper, un_allocator_get_standard()); +void un_alloc_temp_init(u64 byte_size) { + __temp_arena = arena_create(byte_size); } -static ALLOCATOR_PROC_SIGNATURE(un_wrapper_alloc_proc) { - assert(data != NULL); +void un_alloc_temp_reset(void) { + __temp_arena.curr_offset = 0; + __temp_arena.prev_offset = 0; +} - switch (message) { - case UN_ALLOC_MSG_ALLOCATE: - return un_memory_alloc(size, ((Wrapper*)data)->target); - case UN_ALLOC_MSG_FREE: - un_memory_free(p, ((Wrapper*)data)->target); - break; - case UN_ALLOC_MSG_SELF_DELETE: - un_memory_destroy(&((Wrapper*)data)->target); - wrapper_destroy((Wrapper*)data); - break; - } +Allocator un_alloc_temp_get(void) { + assert(__temp_arena.buffer != NULL); - return NULL; + return CLITERAL(Allocator) { un_arena_alloc_proc, &__temp_arena }; } -Allocator un_allocator_create_wrapper(Allocator target) { - Allocator alloc = { 0 }; - - alloc.proc = un_wrapper_alloc_proc; - alloc.data = wrapper_create(target); - return alloc; -} diff --git a/src/un_strings.c b/src/un_strings.c index ff74c39..cf4fee8 100644 --- a/src/un_strings.c +++ b/src/un_strings.c @@ -1,19 +1,11 @@ -u64 un_string_get_length(u8 *cstring) { - u8* start = cstring; - - while (*start != '\0') { - start++; - } - - return start - cstring; -} - -String un_string_from_cstring(u8* cstring) { +String un_string_from_cstring(char *cstring) { String result; - u64 length = un_string_get_length(cstring); + u64 length; + + length = strlen(cstring); assert(length < LLONG_MAX); result.size = length; - result.data = cstring; + result.data = (u8*)cstring; return result; } @@ -23,7 +15,8 @@ u8* un_string_to_cstring(String string, Allocator alloc) { assert(string.size > 0); mem = (u8*)un_memory_alloc(string.size + 1, alloc); - un_memory_copy(mem, string.data, (u64)string.size); + assert(mem != NULL); + memcpy(mem, string.data, (u64)string.size); return mem; } @@ -33,7 +26,8 @@ String un_string_copy(String source, Allocator alloc) { String result; mem = (u8*)un_memory_alloc(source.size, alloc); - un_memory_copy(mem, source.data, (u64)source.size); + assert(mem != NULL); + memcpy(mem, source.data, (u64)source.size); result.size = source.size; result.data = mem; @@ -48,7 +42,7 @@ s32 un_string_compare(String left, String right) { if (left.size == 0) return -1; if (right.size == 0) return 1; - result = un_memory_compare(left.data, right.data, left.size); + result = memcmp(left.data, right.data, left.size); if (result == 0) { if (left.size > right.size) return 1; @@ -67,15 +61,16 @@ String un_string_concat(String left, String right, Allocator alloc) { } u8* data = (u8*)un_memory_alloc(left.size + right.size, alloc); + assert(data != NULL); if (left.size > 0) { assert(left.data != NULL); - un_memory_copy(data, left.data, left.size); + memcpy(data, left.data, left.size); } if (right.size > 0) { assert(right.data != NULL); - un_memory_copy((data + left.size), right.data, right.size); + memcpy((data + left.size), right.data, right.size); } result.size = left.size + right.size; @@ -118,7 +113,7 @@ List un_string_split(String input, String pattern, Allocator alloc) { } for (i = 0, matches = 1; i < (input.size - (pattern.size - 1)); i++) { - if (un_memory_compare(input.data + i, pattern.data, pattern.size) != 0) { + if (memcmp(input.data + i, pattern.data, pattern.size) != 0) { continue; } @@ -130,7 +125,7 @@ List un_string_split(String input, String pattern, Allocator alloc) { start = 0; for (i = 0; i < (input.size - (pattern.size - 1)); i++) { - if (un_memory_compare(input.data + i, pattern.data, pattern.size) != 0) { + if (memcmp(input.data + i, pattern.data, pattern.size) != 0) { continue; } @@ -142,14 +137,16 @@ List un_string_split(String input, String pattern, Allocator alloc) { } buffer = (u8*)un_memory_alloc(size, alloc); - un_memory_copy(buffer, input.data + start, size); + assert(buffer != NULL); + + memcpy(buffer, input.data + start, size); string = CLITERAL(String) { .size = size, .data = buffer }; un_list_append(&splits, &string); start = i + pattern.size; } if (start != input.size - pattern.size) { - if (un_memory_compare(input.data + start, pattern.data, pattern.size) == 0) { + if (memcmp(input.data + start, pattern.data, pattern.size) == 0) { return splits; } @@ -159,7 +156,8 @@ List un_string_split(String input, String pattern, Allocator alloc) { } buffer = (u8*)un_memory_alloc(size, alloc); - un_memory_copy(buffer, input.data + start, size); + assert(buffer != NULL); + memcpy(buffer, input.data + start, size); string = CLITERAL(String) { .size = size, .data = buffer }; un_list_append(&splits, &string); } @@ -174,11 +172,10 @@ String un_string_join(List string_list, String separator, Allocator alloc) { UN_CLEAR(cont); - talloc = un_allocator_get_temporary(); + talloc = un_alloc_temp_get(); assert(string_list.element_size == sizeof(String)); - for (i = 0; i < string_list.count; i++) { temp = *(String *)un_list_get(&string_list, i); @@ -267,7 +264,7 @@ static String format_u64(u64 value) { output.size = size; output.data = buffer; - return un_string_copy(output, un_allocator_get_temporary()); + return un_string_copy(output, un_alloc_temp_get()); } static String format_s64(s64 value) { @@ -309,7 +306,7 @@ static String format_s64(s64 value) { output.size = size; output.data = buffer; - return un_string_copy(output, un_allocator_get_temporary()); + return un_string_copy(output, un_alloc_temp_get()); } String un_string_format(Allocator alloc, String buffer, ...) { @@ -327,7 +324,7 @@ String un_string_vformat(Allocator alloc, String buffer, va_list args) { u64 i, j; u32 b; - Allocator talloc = un_allocator_get_temporary(); + Allocator talloc = un_alloc_temp_get(); // @todo @bug: this would fail on realloc, because we need to realloc output = un_list_create(UN_KB(1), sizeof(u8), talloc); diff --git a/src/un_vec.c b/src/un_vec.c index d6c984e..daf08a5 100644 --- a/src/un_vec.c +++ b/src/un_vec.c @@ -1,307 +1,409 @@ -void un_m_add2f(f32 *v, f32 *a, f32 *b) { - v[0] = a[0] + b[0]; - v[1] = a[1] + b[1]; +UN_INLINE float2 un_m_add2f(float2 a, float2 b) { + float2 result; + result.x = a.x + b.x; + result.y = a.y + b.y; + return result; } -void un_m_add3f(f32 *v, f32 *a, f32 *b) { - v[0] = a[0] + b[0]; - v[1] = a[1] + b[1]; - v[2] = a[2] + b[2]; +UN_INLINE float3 un_m_add3f(float3 a, float3 b) { + float3 result; + result.x = a.x + b.x; + result.y = a.y + b.y; + result.z = a.z + b.z; + return result; } -void un_m_add4f(f32 *v, f32 *a, f32 *b) { - v[0] = a[0] + b[0]; - v[1] = a[1] + b[1]; - v[2] = a[2] + b[2]; - v[3] = a[3] + b[3]; +UN_INLINE float4 un_m_add4f(float4 a, float4 b) { + float4 result; + result.x = a.x + b.x; + result.y = a.y + b.y; + result.z = a.z + b.z; + result.w = a.w + b.w; + return result; } -void un_m_sub2f(f32 *v, f32 *a, f32 *b) { - v[0] = a[0] - b[0]; - v[1] = a[1] - b[1]; +UN_INLINE float2 un_m_sub2f(float2 a, float2 b) { + float2 result; + result.x = a.x - b.x; + result.y = a.y - b.y; + return result; } -void un_m_sub3f(f32 *v, f32 *a, f32 *b) { - v[0] = a[0] - b[0]; - v[1] = a[1] - b[1]; - v[2] = a[2] - b[2]; +UN_INLINE float3 un_m_sub3f(float3 a, float3 b) { + float3 result; + result.x = a.x - b.x; + result.y = a.y - b.y; + result.z = a.z - b.z; + return result; } -void un_m_sub4f(f32 *v, f32 *a, f32 *b) { - v[0] = a[0] - b[0]; - v[1] = a[1] - b[1]; - v[2] = a[2] - b[2]; - v[3] = a[3] - b[3]; +UN_INLINE float4 un_m_sub4f(float4 a, float4 b) { + float4 result; + result.x = a.x - b.x; + result.y = a.y - b.y; + result.z = a.z - b.z; + result.w = a.w - b.w; + return result; } -void un_m_add_scalar2f(f32 *v, f32 *a, f32 scalar) { - v[0] = a[0] + scalar; - v[1] = a[1] + scalar; +UN_INLINE float2 un_m_add_scalar2f(float2 a, float scalar) { + float2 result; + result.x = a.x + scalar; + result.y = a.y + scalar; + return result; } -void un_m_add_scalar3f(f32 *v, f32 *a, f32 scalar) { - v[0] = a[0] + scalar; - v[1] = a[1] + scalar; - v[2] = a[2] + scalar; +UN_INLINE float3 un_m_add_scalar3f(float3 a, float scalar) { + float3 result; + result.x = a.x + scalar; + result.y = a.y + scalar; + result.z = a.z + scalar; + return result; } -void un_m_add_scalar4f(f32 *v, f32 *a, f32 scalar) { - v[0] = a[0] + scalar; - v[1] = a[1] + scalar; - v[2] = a[2] + scalar; - v[3] = a[3] + scalar; +UN_INLINE float4 un_m_add_scalar4f(float4 a, float scalar) { + float4 result; + result.x = a.x + scalar; + result.y = a.y + scalar; + result.z = a.z + scalar; + result.w = a.w + scalar; + return result; } -void un_m_sub_scalar2f(f32 *v, f32 *a, f32 scalar) { - v[0] = a[0] - scalar; - v[1] = a[1] - scalar; +UN_INLINE float2 un_m_sub_scalar2f(float2 a, float scalar) { + float2 result; + result.x = a.x - scalar; + result.y = a.y - scalar; + return result; } -void un_m_sub_scalar3f(f32 *v, f32 *a, f32 scalar) { - v[0] = a[0] - scalar; - v[1] = a[1] - scalar; - v[2] = a[2] - scalar; +UN_INLINE float3 un_m_sub_scalar3f(float3 a, float scalar) { + float3 result; + result.x = a.x - scalar; + result.y = a.y - scalar; + result.z = a.z - scalar; + return result; } -void un_m_sub_scalar4f(f32 *v, f32 *a, f32 scalar) { - v[0] = a[0] - scalar; - v[1] = a[1] - scalar; - v[2] = a[2] - scalar; - v[3] = a[3] - scalar; +UN_INLINE float4 un_m_sub_scalar4f(float4 a, float scalar) { + float4 result; + result.x = a.x - scalar; + result.y = a.y - scalar; + result.z = a.z - scalar; + result.w = a.w - scalar; + return result; } -void un_m_mul_scalar2f(f32 *v, f32 *a, f32 scalar) { - v[0] = a[0] * scalar; - v[1] = a[1] * scalar; +UN_INLINE float2 un_m_mul_scalar2f(float2 a, float scalar) { + float2 result; + result.x = a.x * scalar; + result.y = a.y * scalar; + return result; } -void un_m_mul_scalar3f(f32 *v, f32 *a, f32 scalar) { - v[0] = a[0] * scalar; - v[1] = a[1] * scalar; - v[2] = a[2] * scalar; +UN_INLINE float3 un_m_mul_scalar3f(float3 a, float scalar) { + float3 result; + result.x = a.x * scalar; + result.y = a.y * scalar; + result.z = a.z * scalar; + return result; } -void un_m_mul_scalar4f(f32 *v, f32 *a, f32 scalar) { - v[0] = a[0] * scalar; - v[1] = a[1] * scalar; - v[2] = a[2] * scalar; - v[3] = a[3] * scalar; +UN_INLINE float4 un_m_mul_scalar4f(float4 a, float scalar) { + float4 result; + result.x = a.x * scalar; + result.y = a.y * scalar; + result.z = a.z * scalar; + result.w = a.w * scalar; + return result; } -void un_m_div_scalar2f(f32 *v, f32 *a, f32 scalar) { - v[0] = a[0] / scalar; - v[1] = a[1] / scalar; +UN_INLINE float2 un_m_div_scalar2f(float2 a, float scalar) { + float2 result; + result.x = a.x / scalar; + result.y = a.y / scalar; + return result; } -void un_m_div_scalar3f(f32 *v, f32 *a, f32 scalar) { - v[0] = a[0] / scalar; - v[1] = a[1] / scalar; - v[2] = a[2] / scalar; +UN_INLINE float3 un_m_div_scalar3f(float3 a, float scalar) { + float3 result; + result.x = a.x / scalar; + result.y = a.y / scalar; + result.z = a.z / scalar; + return result; } -void un_m_div_scalar4f(f32 *v, f32 *a, f32 scalar) { - v[0] = a[0] / scalar; - v[1] = a[1] / scalar; - v[2] = a[2] / scalar; - v[3] = a[3] / scalar; +UN_INLINE float4 un_m_div_scalar4f(float4 a, float scalar) { + float4 result; + result.x = a.x / scalar; + result.y = a.y / scalar; + result.z = a.z / scalar; + result.w = a.w / scalar; + return result; } -void un_m_dot2f(f32 *v, f32 *a, f32 *b) { - *v = a[0] * b[0] + a[1] * b[1]; +UN_INLINE float un_m_dot2f(float2 a, float2 b) { + return a.x * b.x + a.y * b.y; } -void un_m_dot3f(f32 *v, f32 *a, f32 *b) { - *v = a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; +UN_INLINE float un_m_dot3f(float3 a, float3 b) { + return a.x * b.x + a.y * b.y + a.z * b.z; } -void un_m_dot4f(f32 *v, f32 *a, f32 *b) { - *v = a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3]; +UN_INLINE float un_m_dot4f(float4 a, float4 b) { + return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w; } -void un_m_hadamard2f(f32 *v, f32 *a, f32 *b) { - v[0] = a[0] * b[0]; - v[1] = a[1] * b[1]; +UN_INLINE float2 un_m_hadamard2f(float2 a, float2 b) { + float2 result; + result.x = a.x * b.x; + result.y = a.y * b.y; + return result; } -void un_m_hadamard3f(f32 *v, f32 *a, f32 *b) { - v[0] = a[0] * b[0]; - v[1] = a[1] * b[1]; - v[2] = a[2] * b[2]; +UN_INLINE float3 un_m_hadamard3f(float3 a, float3 b) { + float3 result; + result.x = a.x * b.x; + result.y = a.y * b.y; + result.z = a.z * b.z; + return result; } -void un_m_hadamard4f(f32 *v, f32 *a, f32 *b) { - v[0] = a[0] * b[0]; - v[1] = a[1] * b[1]; - v[2] = a[2] * b[2]; - v[3] = a[3] * b[3]; +UN_INLINE float4 un_m_hadamard4f(float4 a, float4 b) { + float4 result; + result.x = a.x * b.x; + result.y = a.y * b.y; + result.z = a.z * b.z; + result.w = a.w * b.w; + return result; } -void un_m_cross2f(f32 *v, f32 *a) { - v[0] = a[1]; - v[1] =-a[0]; +UN_INLINE float2 un_m_cross2f(float2 a) { + float2 result; + result.x = a.y; + result.y =-a.x; + return result; } -void un_m_cross3f(f32 *v, f32 *a, f32 *b) { - v[0] = a[1] * b[2] - a[2] * b[1]; - v[1] = a[2] * b[0] - a[0] * b[2]; - v[2] = a[0] * b[1] - a[1] * b[0]; +UN_INLINE float3 un_m_cross3f(float3 a, float3 b) { + float3 result; + result.x = a.y * b.z - a.z * b.y; + result.y = a.z * b.x - a.x * b.z; + result.z = a.x * b.y - a.y * b.x; + return result; } -void un_m_magnitude2f(f32 *v, f32 *a) { - *v = sqrtf(a[0] * a[0] + a[1] * a[1]); +UN_INLINE float un_m_magnitude2f(float2 a) { + return sqrtf(a.x * a.x + a.y * a.y); } -void un_m_magnitude3f(f32 *v, f32 *a) { - *v = sqrtf(a[0] * a[0] + a[1] * a[1] + a[2] * a[2]); +UN_INLINE float un_m_magnitude3f(float3 a) { + return sqrtf(a.x * a.x + a.y * a.y + a.z * a.z); } -void un_m_magnitude4f(f32 *v, f32 *a) { - *v = sqrtf(a[0] * a[0] + a[1] * a[1] + a[2] * a[2] + a[3] * a[3]); +UN_INLINE float un_m_magnitude4f(float4 a) { + return sqrtf(a.x * a.x + a.y * a.y + a.z * a.z + a.w * a.w); } -void un_m_magnitude_sqr2f(f32 *v, f32 *a) { - *v = a[0] * a[0] + a[1] * a[1]; +UN_INLINE float un_m_magnitude_sqr2f(float2 a) { + return a.x * a.x + a.y * a.y; } -void un_m_magnitude_sqr3f(f32 *v, f32 *a) { - *v = a[0] * a[0] + a[1] * a[1] + a[2] * a[2]; +UN_INLINE float un_m_magnitude_sqr3f(float3 a) { + return a.x * a.x + a.y * a.y + a.z * a.z; } -void un_m_magnitude_sqr4f(f32 *v, f32 *a) { - *v = a[0] * a[0] + a[1] * a[1] + a[2] * a[2] + a[3] * a[3]; +UN_INLINE float un_m_magnitude_sqr4f(float4 a) { + return a.x * a.x + a.y * a.y + a.z * a.z + a.w * a.w; } -void un_m_distance2f(f32 *v, f32 *a, f32 *b) { - f32 f[2]; - un_m_sub2f(f, b, a); - *v = sqrtf(f[0] * f[0] + f[1] * f[1]); +UN_INLINE float un_m_distance2f(float2 a, float2 b) { + float2 result; + + result.x = b.x - a.x; + result.y = b.y - a.y; + + return sqrtf(result.x * result.x + result.y * result.y); } -void un_m_distance3f(f32 *v, f32 *a, f32 *b) { - f32 f[3]; - un_m_sub3f(f, b, a); - *v = sqrtf(f[0] * f[0] + f[1] * f[1] + f[2] * f[2]); +UN_INLINE float un_m_distance3f(float3 a, float3 b) { + float3 result; + + result.x = b.x - a.x; + result.y = b.y - a.y; + result.z = b.z - a.z; + + return sqrtf(result.x * result.x + result.y * result.y + result.z * result.z); } -void un_m_distance4f(f32 *v, f32 *a, f32 *b) { - f32 f[4]; - un_m_sub4f(f, b, a); - *v = sqrtf(f[0] * f[0] + f[1] * f[1] + f[2] * f[2] + f[3] * f[3]); +UN_INLINE float un_m_distance4f(float4 a, float4 b) { + float4 result; + + result.x = b.x - a.x; + result.y = b.y - a.y; + result.z = b.z - a.z; + result.w = b.w - a.w; + + return sqrtf(result.x * result.x + result.y * result.y + result.z * result.z + result.w * result.w); } -void un_m_distance_sqr2f(f32 *v, f32 *a, f32 *b) { - f32 f[2]; - un_m_sub2f(f, b, a); - *v = f[0] * f[0] + f[1] * f[1]; +UN_INLINE float un_m_distance_sqr2f(float2 a, float2 b) { + float2 result; + + result.x = b.x - a.x; + result.y = b.y - a.y; + + return result.x * result.x + result.y * result.y; } -void un_m_distance_sqr3f(f32 *v, f32 *a, f32 *b) { - f32 f[3]; - un_m_sub3f(f, b, a); - *v = f[0] * f[0] + f[1] * f[1] + f[2] * f[2]; +UN_INLINE float un_m_distance_sqr3f(float3 a, float3 b) { + float3 result; + + result.x = b.x - a.x; + result.y = b.y - a.y; + result.z = b.z - a.z; + + return result.x * result.x + result.y * result.y + result.z * result.z; } -void un_m_distance_sqr4f(f32 *v, f32 *a, f32 *b) { - f32 f[4]; - un_m_sub4f(f, b, a); - *v = f[0] * f[0] + f[1] * f[1] + f[2] * f[2] + f[3] * f[3]; +UN_INLINE float un_m_distance_sqr4f(float4 a, float4 b) { + float4 result; + + result.x = b.x - a.x; + result.y = b.y - a.y; + result.z = b.z - a.z; + result.w = b.w - a.w; + + return result.x * result.x + result.y * result.y + result.z * result.z + result.w * result.w; } -void un_m_normalize2f(f32 *v, f32 *a) { - f32 f; - un_m_magnitude2f(&f, a); - v[0] = a[0] / f; - v[1] = a[1] / f; +UN_INLINE float2 un_m_normalize2f(float2 a) { + float2 result; + float mag; + + mag = sqrtf(a.x * a.x + a.y * a.y); + + result.x = a.x / mag; + result.y = a.y / mag; + + return result; } -void un_m_normalize3f(f32 *v, f32 *a) { - f32 f; - un_m_magnitude3f(&f, a); - v[0] = a[0] / f; - v[1] = a[1] / f; - v[2] = a[2] / f; +UN_INLINE float3 un_m_normalize3f(float3 a) { + float3 result; + float mag; + + mag = sqrtf(a.x * a.x + a.y * a.y + a.z * a.z); + + result.x = a.x / mag; + result.y = a.y / mag; + result.z = a.z / mag; + + return result; } -void un_m_normalize4f(f32 *v, f32 *a) { - f32 f; - un_m_magnitude4f(&f, a); - v[0] = a[0] / f; - v[1] = a[1] / f; - v[2] = a[2] / f; - v[3] = a[3] / f; +UN_INLINE float4 un_m_normalize4f(float4 a) { + float4 result; + float mag; + + mag = sqrtf(a.x * a.x + a.y * a.y + a.z * a.z + a.w * a.w); + + result.x = a.x / mag; + result.y = a.y / mag; + result.z = a.z / mag; + result.w = a.w / mag; + + return result; } -void un_m_project2f(f32 *v, f32 *a, f32 *onto) { - f32 dot, magn, scale; +UN_INLINE float2 un_m_project2f(float2 a, float2 onto) { + float f; + float2 result; - un_m_dot2f(&dot, a, onto); - un_m_magnitude_sqr2f(&magn, onto); + f = a.x * onto.x + a.y * onto.y; + f /= onto.x * onto.x + onto.y * onto.y; - if (magn < EPSILON) { - un_memory_set((void*)v, 0, sizeof(*v) * 2); - return; - } + result.x = onto.x * f; + result.y = onto.y * f; - scale = dot / magn; - v[0] = onto[0] * scale; - v[1] = onto[1] * scale; + return result; } -void un_m_project3f(f32 *v, f32 *a, f32 *onto) { - f32 dot, magn, scale; +UN_INLINE float3 un_m_project3f(float3 a, float3 onto) { + float f; + float3 result; - un_m_dot3f(&dot, a, onto); - un_m_magnitude_sqr3f(&magn, onto); + f = a.x * onto.x + a.y * onto.y + a.z * onto.z; + f /= onto.x * onto.x + onto.y * onto.y + onto.z * onto.z; - if (magn < EPSILON) { - un_memory_set((void*)v, 0, sizeof(*v) * 3); - return; - } + result.x = onto.x * f; + result.y = onto.y * f; + result.z = onto.z * f; - scale = dot / magn; - v[0] = onto[0] * scale; - v[1] = onto[1] * scale; - v[2] = onto[2] * scale; + return result; } -void un_m_project4f(f32 *v, f32 *a, f32 *onto) { - f32 dot, magn, scale; +UN_INLINE float4 un_m_project4f(float4 a, float4 onto) { + float f; + float4 result; - un_m_dot4f(&dot, a, onto); - un_m_magnitude_sqr4f(&magn, onto); + f = a.x * onto.x + a.y * onto.y + a.z * onto.z + a.w * onto.w; + f /= onto.x * onto.x + onto.y * onto.y + onto.z * onto.z + onto.w * onto.w; - if (magn < EPSILON) { - un_memory_set((void*)v, 0, sizeof(*v) * 4); - return; - } + result.x = onto.x * f; + result.y = onto.y * f; + result.z = onto.z * f; + result.w = onto.w * f; - scale = dot / magn; - v[0] = onto[0] * scale; - v[1] = onto[1] * scale; - v[2] = onto[2] * scale; - v[3] = onto[3] * scale; + return result; } -void un_m_reflect2f(f32 *v, f32 *a, f32 *normal) { - f32 dot; +UN_INLINE float2 un_m_reflect2f(float2 a, float2 normal) { + float f; + float2 result; + + f = a.x * normal.x + a.y * normal.y; + result.x = normal.x * f * 2.0f; + result.y = normal.y * f * 2.0f; - un_m_dot2f(&dot, a, normal); - un_m_mul_scalar2f(v, normal, 2.0f * dot); - un_m_sub2f(v, v, a); + result.x -= a.x; + result.y -= a.y; + + return result; } -void un_m_reflect3f(f32 *v, f32 *a, f32 *normal) { - f32 dot; +UN_INLINE float3 un_m_reflect3f(float3 a, float3 normal) { + float f; + float3 result; + + f = a.x * normal.x + a.y * normal.y + a.z * normal.z; + + result.x = normal.x * f * 2.0f; + result.y = normal.y * f * 2.0f; + result.z = normal.z * f * 2.0f; + + result.x -= a.x; + result.y -= a.y; + result.z -= a.z; - un_m_dot3f(&dot, a, normal); - un_m_mul_scalar3f(v, normal, 2.0f * dot); - un_m_sub3f(v, v, a); + return result; } -void un_m_reflect4f(f32 *v, f32 *a, f32 *normal) { - f32 dot; +UN_INLINE float4 un_m_reflect4f(float4 a, float4 normal) { + float f; + float4 result; + + f = a.x * normal.x + a.y * normal.y + a.z * normal.z + a.w * normal.w; + + result.x = normal.x * f * 2.0f; + result.y = normal.y * f * 2.0f; + result.z = normal.z * f * 2.0f; + result.w = normal.w * f * 2.0f; + + result.x -= a.x; + result.y -= a.y; + result.z -= a.z; + result.w -= a.w; - un_m_dot4f(&dot, a, normal); - un_m_mul_scalar4f(v, normal, 2.0f * dot); - un_m_sub4f(v, v, a); + return result; } diff --git a/src/un_vecd.c b/src/un_vecd.c index 0d138a4..6e738fc 100644 --- a/src/un_vecd.c +++ b/src/un_vecd.c @@ -1,307 +1,409 @@ -void un_m_add2d(f64 *v, f64 *a, f64 *b) { - v[0] = a[0] + b[0]; - v[1] = a[1] + b[1]; +inline double2 un_m_add2d(double2 a, double2 b) { + double2 result; + result.x = a.x + b.x; + result.y = a.y + b.y; + return result; } -void un_m_add3d(f64 *v, f64 *a, f64 *b) { - v[0] = a[0] + b[0]; - v[1] = a[1] + b[1]; - v[2] = a[2] + b[2]; +inline double3 un_m_add3d(double3 a, double3 b) { + double3 result; + result.x = a.x + b.x; + result.y = a.y + b.y; + result.z = a.z + b.z; + return result; } -void un_m_add4d(f64 *v, f64 *a, f64 *b) { - v[0] = a[0] + b[0]; - v[1] = a[1] + b[1]; - v[2] = a[2] + b[2]; - v[3] = a[3] + b[3]; +inline double4 un_m_add4d(double4 a, double4 b) { + double4 result; + result.x = a.x + b.x; + result.y = a.y + b.y; + result.z = a.z + b.z; + result.w = a.w + b.w; + return result; } -void un_m_sub2d(f64 *v, f64 *a, f64 *b) { - v[0] = a[0] - b[0]; - v[1] = a[1] - b[1]; +inline double2 un_m_sub2d(double2 a, double2 b) { + double2 result; + result.x = a.x - b.x; + result.y = a.y - b.y; + return result; } -void un_m_sub3d(f64 *v, f64 *a, f64 *b) { - v[0] = a[0] - b[0]; - v[1] = a[1] - b[1]; - v[2] = a[2] - b[2]; +inline double3 un_m_sub3d(double3 a, double3 b) { + double3 result; + result.x = a.x - b.x; + result.y = a.y - b.y; + result.z = a.z - b.z; + return result; } -void un_m_sub4d(f64 *v, f64 *a, f64 *b) { - v[0] = a[0] - b[0]; - v[1] = a[1] - b[1]; - v[2] = a[2] - b[2]; - v[3] = a[3] - b[3]; +inline double4 un_m_sub4d(double4 a, double4 b) { + double4 result; + result.x = a.x - b.x; + result.y = a.y - b.y; + result.z = a.z - b.z; + result.w = a.w - b.w; + return result; } -void un_m_add_scalar2d(f64 *v, f64 *a, f64 scalar) { - v[0] = a[0] + scalar; - v[1] = a[1] + scalar; +inline double2 un_m_add_scalar2d(double2 a, double scalar) { + double2 result; + result.x = a.x + scalar; + result.y = a.y + scalar; + return result; } -void un_m_add_scalar3d(f64 *v, f64 *a, f64 scalar) { - v[0] = a[0] + scalar; - v[1] = a[1] + scalar; - v[2] = a[2] + scalar; +inline double3 un_m_add_scalar3d(double3 a, double scalar) { + double3 result; + result.x = a.x + scalar; + result.y = a.y + scalar; + result.z = a.z + scalar; + return result; } -void un_m_add_scalar4d(f64 *v, f64 *a, f64 scalar) { - v[0] = a[0] + scalar; - v[1] = a[1] + scalar; - v[2] = a[2] + scalar; - v[3] = a[3] + scalar; +inline double4 un_m_add_scalar4d(double4 a, double scalar) { + double4 result; + result.x = a.x + scalar; + result.y = a.y + scalar; + result.z = a.z + scalar; + result.w = a.w + scalar; + return result; } -void un_m_sub_scalar2d(f64 *v, f64 *a, f64 scalar) { - v[0] = a[0] - scalar; - v[1] = a[1] - scalar; +inline double2 un_m_sub_scalar2d(double2 a, double scalar) { + double2 result; + result.x = a.x - scalar; + result.y = a.y - scalar; + return result; } -void un_m_sub_scalar3d(f64 *v, f64 *a, f64 scalar) { - v[0] = a[0] - scalar; - v[1] = a[1] - scalar; - v[2] = a[2] - scalar; +inline double3 un_m_sub_scalar3d(double3 a, double scalar) { + double3 result; + result.x = a.x - scalar; + result.y = a.y - scalar; + result.z = a.z - scalar; + return result; } -void un_m_sub_scalar4d(f64 *v, f64 *a, f64 scalar) { - v[0] = a[0] - scalar; - v[1] = a[1] - scalar; - v[2] = a[2] - scalar; - v[3] = a[3] - scalar; +inline double4 un_m_sub_scalar4d(double4 a, double scalar) { + double4 result; + result.x = a.x - scalar; + result.y = a.y - scalar; + result.z = a.z - scalar; + result.w = a.w - scalar; + return result; } -void un_m_mul_scalar2d(f64 *v, f64 *a, f64 scalar) { - v[0] = a[0] * scalar; - v[1] = a[1] * scalar; +inline double2 un_m_mul_scalar2d(double2 a, double scalar) { + double2 result; + result.x = a.x * scalar; + result.y = a.y * scalar; + return result; } -void un_m_mul_scalar3d(f64 *v, f64 *a, f64 scalar) { - v[0] = a[0] * scalar; - v[1] = a[1] * scalar; - v[2] = a[2] * scalar; +inline double3 un_m_mul_scalar3d(double3 a, double scalar) { + double3 result; + result.x = a.x * scalar; + result.y = a.y * scalar; + result.z = a.z * scalar; + return result; } -void un_m_mul_scalar4d(f64 *v, f64 *a, f64 scalar) { - v[0] = a[0] * scalar; - v[1] = a[1] * scalar; - v[2] = a[2] * scalar; - v[3] = a[3] * scalar; +inline double4 un_m_mul_scalar4d(double4 a, double scalar) { + double4 result; + result.x = a.x * scalar; + result.y = a.y * scalar; + result.z = a.z * scalar; + result.w = a.w * scalar; + return result; } -void un_m_div_scalar2d(f64 *v, f64 *a, f64 scalar) { - v[0] = a[0] / scalar; - v[1] = a[1] / scalar; +inline double2 un_m_div_scalar2d(double2 a, double scalar) { + double2 result; + result.x = a.x / scalar; + result.y = a.y / scalar; + return result; } -void un_m_div_scalar3d(f64 *v, f64 *a, f64 scalar) { - v[0] = a[0] / scalar; - v[1] = a[1] / scalar; - v[2] = a[2] / scalar; +inline double3 un_m_div_scalar3d(double3 a, double scalar) { + double3 result; + result.x = a.x / scalar; + result.y = a.y / scalar; + result.z = a.z / scalar; + return result; } -void un_m_div_scalar4d(f64 *v, f64 *a, f64 scalar) { - v[0] = a[0] / scalar; - v[1] = a[1] / scalar; - v[2] = a[2] / scalar; - v[3] = a[3] / scalar; +inline double4 un_m_div_scalar4d(double4 a, double scalar) { + double4 result; + result.x = a.x / scalar; + result.y = a.y / scalar; + result.z = a.z / scalar; + result.w = a.w / scalar; + return result; } -void un_m_dot2d(f64 *v, f64 *a, f64 *b) { - *v = a[0] * b[0] + a[1] * b[1]; +inline double un_m_dot2d(double2 a, double2 b) { + return a.x * b.x + a.y * b.y; } -void un_m_dot3d(f64 *v, f64 *a, f64 *b) { - *v = a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; +inline double un_m_dot3d(double3 a, double3 b) { + return a.x * b.x + a.y * b.y + a.z * b.z; } -void un_m_dot4d(f64 *v, f64 *a, f64 *b) { - *v = a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3]; +inline double un_m_dot4d(double4 a, double4 b) { + return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w; } -void un_m_hadamard2d(f64 *v, f64 *a, f64 *b) { - v[0] = a[0] * b[0]; - v[1] = a[1] * b[1]; +inline double2 un_m_hadamard2d(double2 a, double2 b) { + double2 result; + result.x = a.x * b.x; + result.y = a.y * b.y; + return result; } -void un_m_hadamard3d(f64 *v, f64 *a, f64 *b) { - v[0] = a[0] * b[0]; - v[1] = a[1] * b[1]; - v[2] = a[2] * b[2]; +inline double3 un_m_hadamard3d(double3 a, double3 b) { + double3 result; + result.x = a.x * b.x; + result.y = a.y * b.y; + result.z = a.z * b.z; + return result; } -void un_m_hadamard4d(f64 *v, f64 *a, f64 *b) { - v[0] = a[0] * b[0]; - v[1] = a[1] * b[1]; - v[2] = a[2] * b[2]; - v[3] = a[3] * b[3]; +inline double4 un_m_hadamard4d(double4 a, double4 b) { + double4 result; + result.x = a.x * b.x; + result.y = a.y * b.y; + result.z = a.z * b.z; + result.w = a.w * b.w; + return result; } -void un_m_cross2d(f64 *v, f64 *a) { - v[0] = a[1]; - v[1] =-a[0]; +inline double2 un_m_cross2d(double2 a) { + double2 result; + result.x = a.y; + result.y =-a.x; + return result; } -void un_m_cross3d(f64 *v, f64 *a, f64 *b) { - v[0] = a[1] * b[2] - a[2] * b[1]; - v[1] = a[2] * b[0] - a[0] * b[2]; - v[2] = a[0] * b[1] - a[1] * b[0]; +inline double3 un_m_cross3d(double3 a, double3 b) { + double3 result; + result.x = a.y * b.z - a.z * b.y; + result.y = a.z * b.x - a.x * b.z; + result.z = a.x * b.y - a.y * b.x; + return result; } -void un_m_magnitude2d(f64 *v, f64 *a) { - *v = sqrt(a[0] * a[0] + a[1] * a[1]); +inline double un_m_magnitude2d(double2 a) { + return sqrt(a.x * a.x + a.y * a.y); } -void un_m_magnitude3d(f64 *v, f64 *a) { - *v = sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2]); +inline double un_m_magnitude3d(double3 a) { + return sqrt(a.x * a.x + a.y * a.y + a.z * a.z); } -void un_m_magnitude4d(f64 *v, f64 *a) { - *v = sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2] + a[3] * a[3]); +inline double un_m_magnitude4d(double4 a) { + return sqrt(a.x * a.x + a.y * a.y + a.z * a.z + a.w * a.w); } -void un_m_magnitude_sqr2d(f64 *v, f64 *a) { - *v = a[0] * a[0] + a[1] * a[1]; +inline double un_m_magnitude_sqr2d(double2 a) { + return a.x * a.x + a.y * a.y; } -void un_m_magnitude_sqr3d(f64 *v, f64 *a) { - *v = a[0] * a[0] + a[1] * a[1] + a[2] * a[2]; +inline double un_m_magnitude_sqr3d(double3 a) { + return a.x * a.x + a.y * a.y + a.z * a.z; } -void un_m_magnitude_sqr4d(f64 *v, f64 *a) { - *v = a[0] * a[0] + a[1] * a[1] + a[2] * a[2] + a[3] * a[3]; +inline double un_m_magnitude_sqr4d(double4 a) { + return a.x * a.x + a.y * a.y + a.z * a.z + a.w * a.w; } -void un_m_distance2d(f64 *v, f64 *a, f64 *b) { - f64 f[2]; - un_m_sub2d(f, b, a); - *v = sqrt(f[0] * f[0] + f[1] * f[1]); +inline double un_m_distance2d(double2 a, double2 b) { + double2 result; + + result.x = b.x - a.x; + result.y = b.y - a.y; + + return sqrt(result.x * result.x + result.y * result.y); } -void un_m_distance3d(f64 *v, f64 *a, f64 *b) { - f64 f[3]; - un_m_sub3d(f, b, a); - *v = sqrt(f[0] * f[0] + f[1] * f[1] + f[2] * f[2]); +inline double un_m_distance3d(double3 a, double3 b) { + double3 result; + + result.x = b.x - a.x; + result.y = b.y - a.y; + result.z = b.z - a.z; + + return sqrt(result.x * result.x + result.y * result.y + result.z * result.z); } -void un_m_distance4d(f64 *v, f64 *a, f64 *b) { - f64 f[4]; - un_m_sub4d(f, b, a); - *v = sqrt(f[0] * f[0] + f[1] * f[1] + f[2] * f[2] + f[3] * f[3]); +inline double un_m_distance4d(double4 a, double4 b) { + double4 result; + + result.x = b.x - a.x; + result.y = b.y - a.y; + result.z = b.z - a.z; + result.w = b.w - a.w; + + return sqrt(result.x * result.x + result.y * result.y + result.z * result.z + result.w * result.w); } -void un_m_distance_sqr2d(f64 *v, f64 *a, f64 *b) { - f64 f[2]; - un_m_sub2d(f, b, a); - *v = f[0] * f[0] + f[1] * f[1]; +inline double un_m_distance_sqr2d(double2 a, double2 b) { + double2 result; + + result.x = b.x - a.x; + result.y = b.y - a.y; + + return result.x * result.x + result.y * result.y; } -void un_m_distance_sqr3d(f64 *v, f64 *a, f64 *b) { - f64 f[3]; - un_m_sub3d(f, b, a); - *v = f[0] * f[0] + f[1] * f[1] + f[2] * f[2]; +inline double un_m_distance_sqr3d(double3 a, double3 b) { + double3 result; + + result.x = b.x - a.x; + result.y = b.y - a.y; + result.z = b.z - a.z; + + return result.x * result.x + result.y * result.y + result.z * result.z; } -void un_m_distance_sqr4d(f64 *v, f64 *a, f64 *b) { - f64 f[4]; - un_m_sub4d(f, b, a); - *v = f[0] * f[0] + f[1] * f[1] + f[2] * f[2] + f[3] * f[3]; +inline double un_m_distance_sqr4d(double4 a, double4 b) { + double4 result; + + result.x = b.x - a.x; + result.y = b.y - a.y; + result.z = b.z - a.z; + result.w = b.w - a.w; + + return result.x * result.x + result.y * result.y + result.z * result.z + result.w * result.w; } -void un_m_normalize2d(f64 *v, f64 *a) { - f64 f; - un_m_magnitude2d(&f, a); - v[0] = a[0] / f; - v[1] = a[1] / f; +inline double2 un_m_normalize2d(double2 a) { + double2 result; + double mag; + + mag = sqrt(a.x * a.x + a.y * a.y); + + result.x = a.x / mag; + result.y = a.y / mag; + + return result; } -void un_m_normalize3d(f64 *v, f64 *a) { - f64 f; - un_m_magnitude3d(&f, a); - v[0] = a[0] / f; - v[1] = a[1] / f; - v[2] = a[2] / f; +inline double3 un_m_normalize3d(double3 a) { + double3 result; + double mag; + + mag = sqrt(a.x * a.x + a.y * a.y + a.z * a.z); + + result.x = a.x / mag; + result.y = a.y / mag; + result.z = a.z / mag; + + return result; } -void un_m_normalize4d(f64 *v, f64 *a) { - f64 f; - un_m_magnitude4d(&f, a); - v[0] = a[0] / f; - v[1] = a[1] / f; - v[2] = a[2] / f; - v[3] = a[3] / f; +inline double4 un_m_normalize4d(double4 a) { + double4 result; + double mag; + + mag = sqrt(a.x * a.x + a.y * a.y + a.z * a.z + a.w * a.w); + + result.x = a.x / mag; + result.y = a.y / mag; + result.z = a.z / mag; + result.w = a.w / mag; + + return result; } -void un_m_project2d(f64 *v, f64 *a, f64 *onto) { - f64 dot, magn, scale; +inline double2 un_m_project2d(double2 a, double2 onto) { + double d; + double2 result; - un_m_dot2d(&dot, a, onto); - un_m_magnitude_sqr2d(&magn, onto); + d = a.x * onto.x + a.y * onto.y; + d /= onto.x * onto.x + onto.y * onto.y; - if (magn < EPSILON) { - un_memory_set((void*)v, 0, sizeof(*v) * 2); - return; - } + result.x = onto.x * d; + result.y = onto.y * d; - scale = dot / magn; - v[0] = onto[0] * scale; - v[1] = onto[1] * scale; + return result; } -void un_m_project3d(f64 *v, f64 *a, f64 *onto) { - f64 dot, magn, scale; +inline double3 un_m_project3d(double3 a, double3 onto) { + double d; + double3 result; - un_m_dot3d(&dot, a, onto); - un_m_magnitude_sqr3d(&magn, onto); + d = a.x * onto.x + a.y * onto.y + a.z * onto.z; + d /= onto.x * onto.x + onto.y * onto.y + onto.z * onto.z; - if (magn < EPSILON_D) { - un_memory_set((void*)v, 0, sizeof(*v) * 3); - return; - } + result.x = onto.x * d; + result.y = onto.y * d; + result.z = onto.z * d; - scale = dot / magn; - v[0] = onto[0] * scale; - v[1] = onto[1] * scale; - v[2] = onto[2] * scale; + return result; } -void un_m_project4d(f64 *v, f64 *a, f64 *onto) { - f64 dot, magn, scale; +inline double4 un_m_project4d(double4 a, double4 onto) { + double d; + double4 result; - un_m_dot4d(&dot, a, onto); - un_m_magnitude_sqr4d(&magn, onto); + d = a.x * onto.x + a.y * onto.y + a.z * onto.z + a.w * onto.w; + d /= onto.x * onto.x + onto.y * onto.y + onto.z * onto.z + onto.w * onto.w; - if (magn < EPSILON_D) { - un_memory_set((void*)v, 0, sizeof(*v) * 4.0); - return; - } + result.x = onto.x * d; + result.y = onto.y * d; + result.z = onto.z * d; + result.w = onto.w * d; - scale = dot / magn; - v[0] = onto[0] * scale; - v[1] = onto[1] * scale; - v[2] = onto[2] * scale; - v[3] = onto[3] * scale; + return result; } -void un_m_reflect2d(f64 *v, f64 *a, f64 *normal) { - f64 dot; +inline double2 un_m_reflect2d(double2 a, double2 normal) { + double d; + double2 result; + + d = a.x * normal.x + a.y * normal.y; + result.x = normal.x * d * 2.0; + result.y = normal.y * d * 2.0; - un_m_dot2d(&dot, a, normal); - un_m_mul_scalar2d(v, normal, 2.0 * dot); - un_m_sub2d(v, v, a); + result.x -= a.x; + result.y -= a.y; + + return result; } -void un_m_reflect3d(f64 *v, f64 *a, f64 *normal) { - f64 dot; +inline double3 un_m_reflect3d(double3 a, double3 normal) { + double d; + double3 result; + + d = a.x * normal.x + a.y * normal.y + a.z * normal.z; + + result.x = normal.x * d * 2.0; + result.y = normal.y * d * 2.0; + result.z = normal.z * d * 2.0; + + result.x -= a.x; + result.y -= a.y; + result.z -= a.z; - un_m_dot3d(&dot, a, normal); - un_m_mul_scalar3d(v, normal, 2.0 * dot); - un_m_sub3d(v, v, a); + return result; } -void un_m_reflect4d(f64 *v, f64 *a, f64 *normal) { - f64 dot; +inline double4 un_m_reflect4d(double4 a, double4 normal) { + double d; + double4 result; + + d = a.x * normal.x + a.y * normal.y + a.z * normal.z + a.w * normal.w; + + result.x = normal.x * d * 2.0; + result.y = normal.y * d * 2.0; + result.z = normal.z * d * 2.0; + result.w = normal.w * d * 2.0; + + result.x -= a.x; + result.y -= a.y; + result.z -= a.z; + result.w -= a.w; - un_m_dot4d(&dot, a, normal); - un_m_mul_scalar4d(v, normal, 2.0 * dot); - un_m_sub4d(v, v, a); + return result; } diff --git a/src/un_wstrings.c b/src/un_wstrings.c index 601c809..9e5115d 100644 --- a/src/un_wstrings.c +++ b/src/un_wstrings.c @@ -1,6 +1,6 @@ #if defined(OS_WINDOWS) wchar* un_wstring_from_string(String utf8_string, Allocator alloc) { - return un_wstring_from_cstring(un_string_to_cstring(utf8_string, un_allocator_get_temporary()), alloc); + return un_wstring_from_cstring(un_string_to_cstring(utf8_string, un_alloc_temp_get()), alloc); } wchar* un_wstring_from_cstring(u8 *utf8_string, Allocator alloc) { diff --git a/src/ungrateful.h b/src/ungrateful.h index 82152fa..ba78eca 100644 --- a/src/ungrateful.h +++ b/src/ungrateful.h @@ -59,8 +59,8 @@ #include <stdbool.h> #include <stdarg.h> #include <limits.h> -#include <assert.h> #include <math.h> +#include <string.h> #include <wchar.h> #define UNUSED(x) (void)(x) @@ -69,15 +69,40 @@ #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_CSTR (u8*) -#define UN_STR(cstr) un_string_from_cstring(UN_CSTR cstr) -#define CSTR (char*) +#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 */ -#define UN_CLEAR(var) un_memory_set((void*)&var, 0, sizeof(var)) #if defined(__cplusplus) extern "C" { @@ -88,60 +113,63 @@ 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 int8_t b32; -typedef int8_t b8; +typedef int64_t s64; +typedef int32_t s32; +typedef int16_t s16; +typedef int8_t s8; -typedef float f32; -typedef double f64; +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.14159265358979323846f +# define PI 3.14159265358979323846 +#endif + +#if !defined(TAU) +# define TAU (PI * 2) #endif #if !defined(EPSILON) -# define EPSILON 0.000001f +# define EPSILON 0.000001 #endif #if !defined(DEG2RAD) -# define DEG2RAD (PI/180.0f) +# define DEG2RAD (PI/180.0) #endif #if !defined(RAD2DEG) -# define RAD2DEG (180.0f/PI) -#endif - -#if defined(UN_DOUBLE_PRECISION) - typedef double real; -#else - typedef float real; -#endif // UN_DOUBLE_PRECISION - -#if !defined(PI_D) -# define PI_D 3.14159265358979323846 +# define RAD2DEG (180.0/PI) #endif #if !defined(EPSILON_D) # define EPSILON_D 0.000000000000001 #endif -#if !defined(DEG2RAD_D) -# define DEG2RAD_D (PI/180.0) -#endif -#if !defined(RAD2DEG_D) -# define RAD2DEG_D (180.0/PI) -#endif - - -#define un_vec_unwrap2(vec) vec[0], vec[1] -#define un_vec_unwrap3(vec) vec[0], vec[1], vec[2] -#define un_vec_unwrap4(vec) vec[0], vec[1], vec[2], vec[3] +#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 ---- */ @@ -152,7 +180,7 @@ typedef enum { } Allocator_Message; #define ALLOCATOR_PROC_SIGNATURE(name)\ - void *name(void *p, u64 size, Allocator_Message message, void *data) + void *name(void *p, u64 size, u32 align, Allocator_Message message, void *data) typedef ALLOCATOR_PROC_SIGNATURE(Allocator_Proc); @@ -161,21 +189,18 @@ typedef struct { void *data; } Allocator; -// extern Allocator un_allocator_create_heap(s64 chunk_size); /* ... for large things. */ -extern Allocator un_allocator_create_arena(u64 initial_size); /* Grouping allocator, that will recursively grow. */ -extern Allocator un_allocator_create_wrapper(Allocator target); /* Allocator for debug purposes. */ +extern Allocator un_alloc_std_get(void); -extern Allocator un_allocator_get_standard(void); -extern Allocator un_allocator_get_temporary(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 void *un_memory_alloc(u64 size, Allocator alloc); -extern void un_memory_free(void *ptr, Allocator alloc); -extern void un_memory_destroy(Allocator *alloc); +extern Allocator un_alloc_arena_create(u64 initial_size); +extern void un_alloc_arena_destroy(Allocator *alloc); -extern void un_memory_set(u8 *dest, u8 value, u64 size); -extern void un_memory_copy(u8 *dest, u8 *src, u64 size); -extern void un_memory_move(u8 *dest, u8 *src, u64 size); -extern s32 un_memory_compare(u8 *left, u8 *right, u64 size); /* checks for every byte in arrays for condition: if left is bigger that right. */ +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 ---- */ @@ -190,7 +215,7 @@ typedef struct { } List; extern List un_list_create(u64 start_capacity, u64 element_size, Allocator alloc); -extern void un_list_destroy(List *list, b32 delete_allocator); +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. */ @@ -204,9 +229,7 @@ typedef struct { u8 *data; } String; -extern u64 un_string_get_length(u8 *cstring); - -extern String un_string_from_cstring(u8* cstring); +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); @@ -246,130 +269,129 @@ extern wchar* un_wstring_from_cstring(u8 *utf8_string, Allocator alloc); #define un_m_lerpr un_m_lerpf #endif -f32 un_m_lerpf(f32 a, f32 b, f32 t); -f64 un_m_lerpd(f64 a, f64 b, f64 t); +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 f32 */ -extern void un_m_add2f(f32 *v, f32 *a, f32 *b); -extern void un_m_sub2f(f32 *v, f32 *a, f32 *b); -extern void un_m_add_scalar2f(f32 *v, f32 *a, f32 scalar); -extern void un_m_sub_scalar2f(f32 *v, f32 *a, f32 scalar); -extern void un_m_mul_scalar2f(f32 *v, f32 *a, f32 scalar); -extern void un_m_div_scalar2f(f32 *v, f32 *a, f32 scalar); +/* 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 void un_m_dot2f(f32 *v, f32 *a, f32 *b); -extern void un_m_hadamard2f(f32 *v, f32 *a, f32 *b); -extern void un_m_project2f(f32 *v, f32 *a, f32 *onto); -extern void un_m_reflect2f(f32 *v, f32 *a, f32 *normal); -extern void un_m_cross2f(f32 *v, f32 *a); +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 void un_m_normalize2f(f32 *v, f32 *a); -extern void un_m_magnitude2f(f32 *v, f32 *a); -extern void un_m_magnitude_sqr2f(f32 *v, f32 *a); -extern void un_m_distance2f(f32 *v, f32 *a, f32 *b); -extern void un_m_distance_sqr2f(f32 *v, f32 *a, f32 *b); +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 f32 */ -extern void un_m_add3f(f32 *v, f32 *a, f32 *b); -extern void un_m_sub3f(f32 *v, f32 *a, f32 *b); -extern void un_m_add_scalar3f(f32 *v, f32 *a, f32 scalar); -extern void un_m_sub_scalar3f(f32 *v, f32 *a, f32 scalar); -extern void un_m_mul_scalar3f(f32 *v, f32 *a, f32 scalar); -extern void un_m_div_scalar3f(f32 *v, f32 *a, f32 scalar); +/* 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 void un_m_dot3f(f32 *v, f32 *a, f32 *b); -extern void un_m_hadamard3f(f32 *v, f32 *a, f32 *b); -extern void un_m_project3f(f32 *v, f32 *a, f32 *onto); -extern void un_m_reflect3f(f32 *v, f32 *a, f32 *normal); -extern void un_m_cross3f(f32 *v, f32 *a, f32 *b); +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 void un_m_normalize3f(f32 *v, f32 *a); -extern void un_m_magnitude3f(f32 *v, f32 *a); -extern void un_m_magnitude_sqr3f(f32 *v, f32 *a); -extern void un_m_distance3f(f32 *v, f32 *a, f32 *b); -extern void un_m_distance_sqr3f(f32 *v, f32 *a, f32 *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 f32 */ -extern void un_m_add4f(f32 *v, f32 *a, f32 *b); -extern void un_m_sub4f(f32 *v, f32 *a, f32 *b); -extern void un_m_add_scalar4f(f32 *v, f32 *a, f32 scalar); -extern void un_m_sub_scalar4f(f32 *v, f32 *a, f32 scalar); -extern void un_m_mul_scalar4f(f32 *v, f32 *a, f32 scalar); -extern void un_m_div_scalar4f(f32 *v, f32 *a, f32 scalar); +/* 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 void un_m_dot4f(f32 *v, f32 *a, f32 *b); -extern void un_m_hadamard4f(f32 *v, f32 *a, f32 *b); -extern void un_m_project4f(f32 *v, f32 *a, f32 *onto); -extern void un_m_reflect4f(f32 *v, f32 *a, f32 *normal); +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 void un_m_normalize4f(f32 *v, f32 *a); -extern void un_m_magnitude4f(f32 *v, f32 *a); -extern void un_m_magnitude_sqr4f(f32 *v, f32 *a); -extern void un_m_distance4f(f32 *v, f32 *a, f32 *b); -extern void un_m_distance_sqr4f(f32 *v, f32 *a, f32 *b); +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); -/* 2d f64 */ -extern void un_m_add2d(f64 *v, f64 *a, f64 *b); -extern void un_m_sub2d(f64 *v, f64 *a, f64 *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 void un_m_add_scalar2d(f64 *v, f64 *a, f64 scalar); -extern void un_m_sub_scalar2d(f64 *v, f64 *a, f64 scalar); -extern void un_m_mul_scalar2d(f64 *v, f64 *a, f64 scalar); -extern void un_m_div_scalar2d(f64 *v, f64 *a, f64 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 void un_m_dot2d(f64 *v, f64 *a, f64 *b); -extern void un_m_hadamard2d(f64 *v, f64 *a, f64 *b); -extern void un_m_project2d(f64 *v, f64 *a, f64 *onto); -extern void un_m_reflect2d(f64 *v, f64 *a, f64 *normal); -extern void un_m_cross2d(f64 *v, f64 *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); -extern void un_m_normalize2d(f64 *v, f64 *a); -extern void un_m_magnitude2d(f64 *v, f64 *a); -extern void un_m_magnitude_sqr2d(f64 *v, f64 *a); -extern void un_m_distance2d(f64 *v, f64 *a, f64 *b); -extern void un_m_distance_sqr2d(f64 *v, f64 *a, f64 *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); -/* 3d f64 */ -extern void un_m_add3d(f64 *v, f64 *a, f64 *b); -extern void un_m_sub3d(f64 *v, f64 *a, f64 *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 void un_m_add_scalar3d(f64 *v, f64 *a, f64 scalar); -extern void un_m_sub_scalar3d(f64 *v, f64 *a, f64 scalar); -extern void un_m_mul_scalar3d(f64 *v, f64 *a, f64 scalar); -extern void un_m_div_scalar3d(f64 *v, f64 *a, f64 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 void un_m_dot3d(f64 *v, f64 *a, f64 *b); -extern void un_m_hadamard3d(f64 *v, f64 *a, f64 *b); -extern void un_m_project3d(f64 *v, f64 *a, f64 *onto); -extern void un_m_reflect3d(f64 *v, f64 *a, f64 *normal); -extern void un_m_cross3d(f64 *v, f64 *a, f64 *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); -extern void un_m_normalize3d(f64 *v, f64 *a); -extern void un_m_magnitude3d(f64 *v, f64 *a); -extern void un_m_magnitude_sqr3d(f64 *v, f64 *a); -extern void un_m_distance3d(f64 *v, f64 *a, f64 *b); -extern void un_m_distance_sqr3d(f64 *v, f64 *a, f64 *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); -/* 4d f64 */ -extern void un_m_add4d(f64 *v, f64 *a, f64 *b); -extern void un_m_sub4d(f64 *v, f64 *a, f64 *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 void un_m_add_scalar4d(f64 *v, f64 *a, f64 scalar); -extern void un_m_sub_scalar4d(f64 *v, f64 *a, f64 scalar); -extern void un_m_mul_scalar4d(f64 *v, f64 *a, f64 scalar); -extern void un_m_div_scalar4d(f64 *v, f64 *a, f64 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 void un_m_dot4d(f64 *v, f64 *a, f64 *b); -extern void un_m_hadamard4d(f64 *v, f64 *a, f64 *b); -extern void un_m_project4d(f64 *v, f64 *a, f64 *onto); -extern void un_m_reflect4d(f64 *v, f64 *a, f64 *normal); - -extern void un_m_normalize4d(f64 *v, f64 *a); -extern void un_m_magnitude4d(f64 *v, f64 *a); -extern void un_m_magnitude_sqr4d(f64 *v, f64 *a); -extern void un_m_distance4d(f64 *v, f64 *a, f64 *b); -extern void un_m_distance_sqr4d(f64 *v, f64 *a, f64 *b); +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) @@ -505,13 +527,13 @@ extern void un_m_distance_sqr4d(f64 *v, f64 *a, f64 *b); #define un_m_complex_addf un_m_add2f #define un_m_complex_subf un_m_sub2f -extern void un_m_complex_divf(f32 *v, f32 *a, f32 *b); -extern void un_m_complex_mulf(f32 *v, f32 *a, f32 *b); +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 void un_m_complex_divd(f64 *v, f64 *a, f64 *b); -extern void un_m_complex_muld(f64 *v, f64 *a, f64 *b); +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 @@ -532,15 +554,15 @@ extern void un_m_complex_muld(f64 *v, f64 *a, f64 *b); /* ---- splines ---- */ -void un_m_bezierf(f32 *v, f32 a, f32 q0, f32 q1, f32 b, f32 t); -void un_m_bezier2f(f32 *v, f32 *a, f32 *q0, f32 *q1, f32 *b, f32 t); -void un_m_bezier3f(f32 *v, f32 *a, f32 *q0, f32 *q1, f32 *b, f32 t); -void un_m_bezier4f(f32 *v, f32 *a, f32 *q0, f32 *q1, f32 *b, f32 t); - -void un_m_bezierd(f64 *v, f64 a, f64 q0, f64 q1, f64 b, f64 t); -void un_m_bezier2d(f64 *v, f64 *a, f64 *q0, f64 *q1, f64 *b, f64 t); -void un_m_bezier3d(f64 *v, f64 *a, f64 *q0, f64 *q1, f64 *b, f64 t); -void un_m_bezier4d(f64 *v, f64 *a, f64 *q0, f64 *q1, f64 *b, f64 t); +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 @@ -556,19 +578,19 @@ void un_m_bezier4d(f64 *v, f64 *a, f64 *q0, f64 *q1, f64 *b, f64 t); /* ---- easing ---- */ -extern f32 un_m_ease_isinef(f32 t); -extern f32 un_m_ease_iosinef(f32 t); -extern f32 un_m_ease_osinef(f32 t); -extern f32 un_m_ease_iquadf(f32 t); -extern f32 un_m_ease_ioquadf(f32 t); -extern f32 un_m_ease_oquadf(f32 t); - -extern f64 un_m_ease_isined(f64 t); -extern f64 un_m_ease_iosined(f64 t); -extern f64 un_m_ease_osined(f64 t); -extern f64 un_m_ease_iquadd(f64 t); -extern f64 un_m_ease_ioquadd(f64 t); -extern f64 un_m_ease_oquadd(f64 t); +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 @@ -594,8 +616,8 @@ extern f64 un_m_ease_oquadd(f64 t); #define UN_MATRIX_SIZE (16) #define UN_MATRIX_STRIPE (4) -extern void un_m_mat_mulf(f32 *v, f32 *left, f32 *right); -extern void un_m_mat_identityf(f32 *v); +extern void un_m_mat_mulf(float *v, float *left, float *right); +extern void un_m_mat_identityf(float *v); #if defined(__cplusplus) } |