From 1afe496fed2e9901c893757e918d28fa372fc688 Mon Sep 17 00:00:00 2001 From: Archez Date: Wed, 13 Nov 2024 14:18:47 -0500 Subject: [PATCH] Fix clock spinning violently from undefined behavior with bad casting (#849) --- mm/src/overlays/actors/ovl_Obj_Tokeidai/z_obj_tokeidai.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/mm/src/overlays/actors/ovl_Obj_Tokeidai/z_obj_tokeidai.c b/mm/src/overlays/actors/ovl_Obj_Tokeidai/z_obj_tokeidai.c index 07a75650b..cd32c7243 100644 --- a/mm/src/overlays/actors/ovl_Obj_Tokeidai/z_obj_tokeidai.c +++ b/mm/src/overlays/actors/ovl_Obj_Tokeidai/z_obj_tokeidai.c @@ -38,9 +38,12 @@ #define GET_CURRENT_CLOCK_HOUR(this) ((s32)TIME_TO_HOURS_F((this)->clockTime)) #define GET_CURRENT_CLOCK_MINUTE(this) ((s32)((this)->clockTime * (360 * 2.0f / 0x10000)) % 30) -#define GET_CLOCK_FACE_ROTATION(currentClockHour) ((s16)(currentClockHour * (0x10000 / 24.0f))) +// #region 2S2H [Port] Additional pre-cast to s32 prevents undefined behavior with casting float values larger than s16 +// This undefined behavior lead to the clock rings and face spinning violently on some compilers +#define GET_CLOCK_FACE_ROTATION(currentClockHour) ((s16)(s32)(currentClockHour * (0x10000 / 24.0f))) #define GET_MINUTE_RING_OR_EXTERIOR_GEAR_ROTATION(currentClockMinute) \ - ((s16)(currentClockMinute * (0x10000 * 12.0f / 360))) + ((s16)(s32)(currentClockMinute * (0x10000 * 12.0f / 360))) +// #endregion void ObjTokeidai_Init(Actor* thisx, PlayState* play); void ObjTokeidai_Destroy(Actor* thisx, PlayState* play);