forked from M-Labs/nac3
1
0
Fork 0
nac3/nac3core/irrt/irrt_utils.hpp

74 lines
1.9 KiB
C++

#pragma once
#include "irrt_typedefs.hpp"
namespace {
template <typename T>
T max(T a, T b) {
return a > b ? a : b;
}
template <typename T>
T min(T a, T b) {
return a > b ? b : a;
}
template <typename T>
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<typename T>
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();
}
}