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_ATTRIBUTE(attr) __attribute__((attr))
20
25#define GBAL_UNUSED GBAL_ATTRIBUTE(unused)
26
31#define GBAL_FALLTHROUGH GBAL_ATTRIBUTE(fallthrough)
32
33#define UNDEFINED -1
34
39#define MAX_BASE36 0x81BF0FFF
40
47#define SIGN(x) ((x > 0) - (x < 0))
48
55#define NUM_ELEM_IN_ARR(arr) (sizeof(arr) / sizeof((arr)[0]))
56
57#define INT_MAX_DIGITS 11 // strlen(str(INT_MAX)) = strlen("-2147483647")
58#define UINT_MAX_DIGITS 10 // strlen(str(UINT32_MAX)) = strlen("4294967295")
59#define UINT8_MAX_DIGITS 3 // strlen(str(UINT8_MAX)) = strlen("255")
60#define BASE36_MAX_DIGITS 6 // strlen("ZZZZZZ")
61
62#define ONE_K 1000
63#define ONE_M 1000000
64#define ONE_B 1000000000
65
66#define ONE_K_ZEROS 3
67#define ONE_M_ZEROS 6
68#define ONE_B_ZEROS 9
69
70// The suffix replaces everything past the third digit, e.g. "999K" -> "1M"
71// so it needs at least this number of chars to be able to display any suffixed number
72#define SUFFIXED_NUM_MIN_REQ_CHARS 4
73
74#ifdef MGBA_LOGGING
75#define LOG_ERROR(...) MGBA_FUNC_ERROR(__VA_ARGS__)
76#else
77// TODO: Add a define to conditionally compile print error to console and add it to the tests?
78#define LOG_ERROR(...) ((void)(0))
79#endif
80
90#define GBAL_CUST_MSG_RETURN_IF_ASSERT_FAILS(expression, ret_val, message, ...) \
91 do \
92 { \
93 if (!(expression)) \
94 { \
95 LOG_ERROR(message __VA_OPT__(, ) __VA_ARGS__); \
96 return ret_val; \
97 } \
98 } while (0)
99
109#define GBAL_RETURN_IF_ASSERT_FAILS(expression, ret_val) \
110 GBAL_CUST_MSG_RETURN_IF_ASSERT_FAILS(expression, ret_val, "Assert failed: %s", #expression)
111
121#define GBAL_CUST_MSG_RETURN_IF_NULL(param, ret_val, message, ...) \
122 GBAL_CUST_MSG_RETURN_IF_ASSERT_FAILS( \
123 (param) != NULL, \
124 ret_val, \
125 message __VA_OPT__(, ) __VA_ARGS__ \
126 )
127
138#define GBAL_RETURN_IF_NULL(param, ret_val) \
139 GBAL_CUST_MSG_RETURN_IF_NULL(param, ret_val, "Unexpected %s == NULL", #param)
140
146#define RET_NONE
147
156uint32_t u32_protected_add(uint32_t a, uint32_t b);
157
166uint16_t u16_protected_add(uint16_t a, uint16_t b);
167
176uint32_t u32_protected_mult(uint32_t a, uint32_t b);
177
186uint16_t u16_protected_mult(uint16_t a, uint16_t b);
187
213 uint32_t num,
214 int num_req_chars,
215 char out_str_buff[UINT_MAX_DIGITS + 1]
216);
217
226static inline int u32_get_digits(uint32_t n)
227{
228 if (n < 10)
229 return 1;
230 if (n < 100)
231 return 2;
232 if (n < 1000)
233 return 3;
234 if (n < 10000)
235 return 4;
236 if (n < 100000)
237 return 5;
238 if (n < 1000000)
239 return 6;
240 if (n < 10000000)
241 return 7;
242 if (n < 100000000)
243 return 8;
244 if (n < 1000000000)
245 return 9;
246 return 10;
247}
248
260uint32_t base36_to_u32(const char b36_str[]);
261
289void u32_to_base36(uint32_t n, char b36_str[]);
290
291#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:226
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