From 10d57a3402cc698cffc4bf3299113073264eb314 Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Tue, 5 Mar 2013 03:12:17 -0600 Subject: [PATCH] Use standard binary multiple unit symbols for game size display. Use integer math for the calculation as we cannot rely on floats for something as important as game size display! --- Source/Core/Common/Src/MathUtil.h | 22 ++++++++++++++++ Source/Core/DolphinWX/Src/GameListCtrl.cpp | 29 ++++++++-------------- 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/Source/Core/Common/Src/MathUtil.h b/Source/Core/Common/Src/MathUtil.h index 114a91bf3c..c6c29ff368 100644 --- a/Source/Core/Common/Src/MathUtil.h +++ b/Source/Core/Common/Src/MathUtil.h @@ -152,6 +152,28 @@ float MathFloatVectorSum(const std::vector&); #define ROUND_UP(x, a) (((x) + (a) - 1) & ~((a) - 1)) #define ROUND_DOWN(x, a) ((x) & ~((a) - 1)) +template +T Log2(T val) +{ +#if defined(_M_X64) + T result; + asm + ( + "bsr %1, %0" + : "=r"(result) + : "r"(val) + ); + return result; +#else + T result = -1; + while (val != 0) + { + val >>= 1; + ++result; + } + return result; +#endif +} // Tiny matrix/vector library. // Used for things like Free-Look in the gfx backend. diff --git a/Source/Core/DolphinWX/Src/GameListCtrl.cpp b/Source/Core/DolphinWX/Src/GameListCtrl.cpp index 1a828bbf14..bf09161555 100644 --- a/Source/Core/DolphinWX/Src/GameListCtrl.cpp +++ b/Source/Core/DolphinWX/Src/GameListCtrl.cpp @@ -35,6 +35,7 @@ #include "CDUtils.h" #include "WxUtils.h" #include "Main.h" +#include "MathUtil.h" #include "../resources/Flag_Europe.xpm" #include "../resources/Flag_Germany.xpm" @@ -380,26 +381,18 @@ void CGameListCtrl::Update() SetFocus(); } -wxString NiceSizeFormat(s64 _size) +wxString NiceSizeFormat(u64 _size) { - const char* sizes[] = {"b", "KB", "MB", "GB", "TB", "PB", "EB"}; - int s = 0; - int frac = 0; + const char* const unit_symbols[] = {"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"}; + + auto const unit = Log2(std::max(_size, 1)) / 10; + auto const unit_size = (1 << (unit * 10)); + + // ugly rounding integer math + auto const value = (_size + unit_size / 2) / unit_size; + auto const frac = (_size % unit_size * 10 + unit_size / 2) / unit_size % 10; - while (_size > (s64)1024) - { - s++; - frac = (int)_size & 1023; - _size /= (s64)1024; - } - - float f = (float)_size + ((float)frac / 1024.0f); - - wxString NiceString; - char tempstr[32]; - sprintf(tempstr,"%3.1f %s", f, sizes[s]); - NiceString = StrToWxStr(tempstr); - return(NiceString); + return StrToWxStr(StringFromFormat("%llu.%llu %s", value, frac, unit_symbols[unit])); } void CGameListCtrl::InsertItemInReportView(long _Index)