From 3f75078174dd7cd7f696b59c3e43bd2842c6359c Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Thu, 23 Jul 2020 21:42:35 -0700 Subject: [PATCH] Util: Factor out gcd code --- include/mgba-util/math.h | 13 +++++++++++++ src/platform/qt/VideoView.cpp | 12 +++--------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/include/mgba-util/math.h b/include/mgba-util/math.h index eaf2087a2..9e239869c 100644 --- a/include/mgba-util/math.h +++ b/include/mgba-util/math.h @@ -58,6 +58,19 @@ static inline uint32_t toPow2(uint32_t bits) { return 1 << (32 - lz); } +static inline int reduceFraction(int* num, int* den) { + int n = *num; + int d = *den; + while (d != 0) { + int temp = n % d; + n = d; + d = temp; + } + *num /= n; + *den /= n; + return n; +} + CXX_GUARD_END #endif diff --git a/src/platform/qt/VideoView.cpp b/src/platform/qt/VideoView.cpp index 307245aa8..da09fc150 100644 --- a/src/platform/qt/VideoView.cpp +++ b/src/platform/qt/VideoView.cpp @@ -10,6 +10,8 @@ #include "GBAApp.h" #include "LogController.h" +#include + #include using namespace QGBA; @@ -396,15 +398,7 @@ void VideoView::updateAspectRatio(int width, int height, bool force) { } else { int w = m_width; int h = m_height; - // Get greatest common divisor - while (w != 0) { - int temp = h % w; - h = w; - w = temp; - } - int gcd = h; - w = m_width / gcd; - h = m_height / gcd; + reduceFraction(&h, &w); safelySet(m_ui.wratio, w); safelySet(m_ui.hratio, h); }