From 88bd618496b955da4505a8c106dd135235d02542 Mon Sep 17 00:00:00 2001 From: Kurausukun Date: Sat, 26 Jun 2021 04:04:22 -0400 Subject: port UBFIXes from emerald --- include/config.h | 3 +++ 1 file changed, 3 insertions(+) (limited to 'include') diff --git a/include/config.h b/include/config.h index 8c7229504..78651f023 100644 --- a/include/config.h +++ b/include/config.h @@ -41,6 +41,9 @@ #ifndef BUGFIX #define BUGFIX #endif // BUGFIX +#ifndef UBFIX +#define UBFIX +#endif // UBFIX #endif // MODERN #endif // GUARD_CONFIG_H -- cgit v1.2.3 From ff94d49a45ef605cfaed1268aa5a230958e25cd9 Mon Sep 17 00:00:00 2001 From: Kurausukun Date: Sat, 3 Jul 2021 17:18:41 -0400 Subject: add SAFE_DIV macro and usages --- include/global.h | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'include') diff --git a/include/global.h b/include/global.h index 1d12fdae9..f7abcc23d 100644 --- a/include/global.h +++ b/include/global.h @@ -73,6 +73,14 @@ #define abs(x) (((x) < 0) ? -(x) : (x)) #endif +// Used in cases where division by 0 can occur in the retail version. +// Avoids invalid opcodes on some emulators, and the otherwise UB. +#ifdef UBFIX +#define SAFE_DIV(a, b) ((b) ? (a) / (b) : 0) +#else +#define SAFE_DIV(a, b) ((a) / (b)) +#endif + // There are many quirks in the source code which have overarching behavioral differences from // a number of other files. For example, diploma.c seems to declare rodata before each use while // other files declare out of order and must be at the beginning. There are also a number of -- cgit v1.2.3