Files
balatro-gba-chinese-jocker-…/source/util.c
T
RickeyandMeirGavish 35ecbf33ae Format and document utility files (#246)
* Format and document utility files

* Add Doxyfile

* small updates for cleanup

* Final touches for util docs

* Replace `_` with `s_` for static functions

* update for clang-format changes

* Update include/audio_utils.h

Co-authored-by: MeirGavish <meir.gavish@gmail.com>

* Apply suggestions from code review

Co-authored-by: MeirGavish <meir.gavish@gmail.com>

* Update include/audio_utils.h

Co-authored-by: MeirGavish <meir.gavish@gmail.com>

* Update include/graphic_utils.h

Co-authored-by: MeirGavish <meir.gavish@gmail.com>

* Make clang-format happy :)

* Update gitignore for doxygen

* restor graphic_utils.c to main

* update graphics utils

* tmp

* tmp

* get it to compile

* fix some bad merge mistakes

* Fix clang format

* Apply suggestions from code review

Co-authored-by: MeirGavish <meir.gavish@gmail.com>

* make clang-format happy

---------

Co-authored-by: MeirGavish <meir.gavish@gmail.com>
2025-12-07 12:56:39 -08:00

83 lines
2.1 KiB
C

#include "util.h"
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
int int_arr_max(int int_arr[], int size)
{
int max = INT_MIN;
for (int i = 0; i < size; i++)
{
if (int_arr[i] > max)
{
max = int_arr[i];
}
}
return max;
}
void truncate_uint_to_suffixed_str(
uint32_t num,
int num_req_chars,
char out_str_buff[UINT_MAX_DIGITS + 1]
)
{
bool inevitable_overflow = num_req_chars < SUFFIXED_NUM_MIN_REQ_CHARS;
if (inevitable_overflow)
{
num_req_chars = SUFFIXED_NUM_MIN_REQ_CHARS;
}
int num_digits = u32_get_digits(num);
int overflow_size = num_digits - num_req_chars;
char* suffix = "";
/* If there is overflow, divide by the next suffixed power of 10
* to truncate the number back within num_req_chars.
* UINT32_MAX is in the billions so no need to check larger numbers
* or perform complex mathematical operations.
*/
if (overflow_size >= ONE_M_ZEROS)
{
num /= ONE_B;
suffix = "B";
}
else if (overflow_size >= ONE_K_ZEROS)
{
num /= ONE_M;
suffix = "M";
}
else if (overflow_size > 0 || (inevitable_overflow && num_digits == SUFFIXED_NUM_MIN_REQ_CHARS))
// Special case - alleviate inevitable overflow for 1000s and truncate them to "1K"s
{
num /= ONE_K;
suffix = "K";
}
snprintf(out_str_buff, UINT_MAX_DIGITS + 1, "%lu%s", num, suffix);
}
// Avoid uint overflow when add/multiplying score
uint32_t u32_protected_add(uint32_t a, uint32_t b)
{
return (a > (UINT32_MAX - b)) ? UINT32_MAX : (a + b);
}
uint16_t u16_protected_add(uint16_t a, uint16_t b)
{
return (a > (UINT16_MAX - b)) ? UINT16_MAX : (a + b);
}
uint32_t u32_protected_mult(uint32_t a, uint32_t b)
{
return (a == 0 || b == 0) ? 0 : (a > (UINT32_MAX / b) ? UINT32_MAX : a * b);
}
uint16_t u16_protected_mult(uint16_t a, uint16_t b)
{
return (a == 0 || b == 0) ? 0 : (a > (UINT16_MAX / b) ? UINT16_MAX : a * b);
}