#pragma once #include "irrt_typedefs.hpp" namespace { template T max(T a, T b) { return a > b ? a : b; } template T min(T a, T b) { return a > b ? b : a; } template bool arrays_match(int len, T *as, T *bs) { for (int i = 0; i < len; i++) { if (as[i] != bs[i]) return false; } return true; } template uint32_t int_log_floor(T value, T base) { uint32_t result = 0; while (value < base) { result++; value /= base; } return result; } bool string_is_empty(const char *str) { return str[0] == '\0'; } // TODO: DOCUMENT ME!!!!! // returns false if `src_str` could not be fully copied over to `dst_str` bool string_copy(uint32_t dst_max_size, char* dst_str, const char* src_str) { // This function guarantess that `dst_str` will be null-terminated, for (uint32_t i = 0; i < dst_max_size; i++) { bool is_last = i + 1 == dst_max_size; if (is_last && src_str[i] != '\0') { dst_str[i] = '\0'; return false; } if (src_str[i] == '\0') { dst_str[i] = '\0'; return true; } dst_str[i] = src_str[i]; } __builtin_unreachable(); } void irrt_panic() { // Crash the program for now. // TODO: Don't crash the program // ... or at least produce a good message when doing testing IRRT uint8_t* death = nullptr; *death = 0; // TODO: address 0 on hardware might be writable? } // TODO: Make this a macro and allow it to be toggled on/off (e.g., debug vs release) void irrt_assert(bool condition) { if (!condition) irrt_panic(); } }