GBAlatro
A Demake of Balatro for the GBA
Loading...
Searching...
No Matches
util.h
Go to the documentation of this file.
1
7#ifndef UTIL_H
8#define UTIL_H
9
10#include <stdint.h>
11#ifdef MGBA_LOGGING
12#include "mgba_logger.h"
13#endif
14
19#define GBAL_UNUSED __attribute__((unused))
20
21#define UNDEFINED -1
22
27#define MAX_BASE36 0x81BF0FFF
28
35#define SIGN(x) ((x > 0) - (x < 0))
36
43#define NUM_ELEM_IN_ARR(arr) (sizeof(arr) / sizeof((arr)[0]))
44
45#define INT_MAX_DIGITS 11 // strlen(str(INT_MAX)) = strlen("-2147483647")
46#define UINT_MAX_DIGITS 10 // strlen(str(UINT32_MAX)) = strlen("4294967295")
47#define UINT8_MAX_DIGITS 3 // strlen(str(UINT8_MAX)) = strlen("255")
48#define BASE36_MAX_DIGITS 6 // strlen("ZZZZZZ")
49
50#define ONE_K 1000
51#define ONE_M 1000000
52#define ONE_B 1000000000
53
54#define ONE_K_ZEROS 3
55#define ONE_M_ZEROS 6
56#define ONE_B_ZEROS 9
57
58// The suffix replaces everything past the third digit, e.g. "999K" -> "1M"
59// so it needs at least this number of chars to be able to display any suffixed number
60#define SUFFIXED_NUM_MIN_REQ_CHARS 4
61
62#ifdef MGBA_LOGGING
63#define LOG_ERROR(...) MGBA_FUNC_ERROR(__VA_ARGS__)
64#else
65// TODO: Add a define to conditionally compile print error to console and add it to the tests?
66#define LOG_ERROR(...) ((void)(0))
67#endif
68
76#define GBAL_RETURN_IF_NULL_VOID(param) \
77 do \
78 { \
79 if ((param) == NULL) \
80 { \
81 LOG_ERROR("Unexpected value: %s == NULL", #param); \
82 return; \
83 } \
84 } while (0)
85
95#define GBAL_RETURN_IF_NULL_RET(param, ret_val) \
96 do \
97 { \
98 if ((param) == NULL) \
99 { \
100 LOG_ERROR("Unexpected value: %s == NULL", #param); \
101 return (ret_val); \
102 } \
103 } while (0)
104
113uint32_t u32_protected_add(uint32_t a, uint32_t b);
114
123uint16_t u16_protected_add(uint16_t a, uint16_t b);
124
133uint32_t u32_protected_mult(uint32_t a, uint32_t b);
134
143uint16_t u16_protected_mult(uint16_t a, uint16_t b);
144
170 uint32_t num,
171 int num_req_chars,
172 char out_str_buff[UINT_MAX_DIGITS + 1]
173);
174
183static inline int u32_get_digits(uint32_t n)
184{
185 if (n < 10)
186 return 1;
187 if (n < 100)
188 return 2;
189 if (n < 1000)
190 return 3;
191 if (n < 10000)
192 return 4;
193 if (n < 100000)
194 return 5;
195 if (n < 1000000)
196 return 6;
197 if (n < 10000000)
198 return 7;
199 if (n < 100000000)
200 return 8;
201 if (n < 1000000000)
202 return 9;
203 return 10;
204}
205
217uint32_t base36_to_u32(const char b36_str[]);
218
246void u32_to_base36(uint32_t n, char b36_str[]);
247
248#endif // UTIL_H
Interface to interact with the mgba logger.
uint16_t u16_protected_mult(uint16_t a, uint16_t b)
Avoid overflow when multiplying two u16 integers.
Definition util.c:190
uint32_t u32_protected_add(uint32_t a, uint32_t b)
Avoid overflow when adding two u32 integers.
Definition util.c:175
static int u32_get_digits(uint32_t n)
Get the number of digits in a 32-bit unsigned number https://stackoverflow.com/questions/1068849/how-...
Definition util.h:183
uint32_t base36_to_u32(const char b36_str[])
Convert a base-36 string representation to a 32-bit unsigned integer. Since we are dealing with base-...
Definition util.c:260
uint16_t u16_protected_add(uint16_t a, uint16_t b)
Avoid overflow when adding two u16 integers.
Definition util.c:180
void truncate_uint_to_suffixed_str(uint32_t num, int num_req_chars, char out_str_buff[UINT_MAX_DIGITS+1])
Truncate an unsigned number into a suffixed string representation e.g. 12000 -> "12K" The least signi...
Definition util.c:116
uint32_t u32_protected_mult(uint32_t a, uint32_t b)
Avoid overflow when multiplying two u32 integers.
Definition util.c:185
void u32_to_base36(uint32_t n, char b36_str[])
Convert a 32-bit unsigned integer to its base-36 string representation. This will perform 6 divisions...
Definition util.c:272