summaryrefslogtreecommitdiff
path: root/Source/Core
diff options
context:
space:
mode:
authorJordan Woyak <jordan.woyak@gmail.com>2013-03-05 03:12:17 -0600
committerJordan Woyak <jordan.woyak@gmail.com>2013-03-05 14:58:30 -0600
commit10d57a3402cc698cffc4bf3299113073264eb314 (patch)
tree4e8948448cdeb2848aa87c000eae5159510ae42d /Source/Core
parent240238308cd0501e83ea5077548ca02bbb0bc578 (diff)
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!
Diffstat (limited to 'Source/Core')
-rw-r--r--Source/Core/Common/Src/MathUtil.h22
-rw-r--r--Source/Core/DolphinWX/Src/GameListCtrl.cpp29
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<float>&);
#define ROUND_UP(x, a) (((x) + (a) - 1) & ~((a) - 1))
#define ROUND_DOWN(x, a) ((x) & ~((a) - 1))
+template <typename T>
+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;
-
- while (_size > (s64)1024)
- {
- s++;
- frac = (int)_size & 1023;
- _size /= (s64)1024;
- }
-
- float f = (float)_size + ((float)frac / 1024.0f);
+ const char* const unit_symbols[] = {"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"};
+
+ auto const unit = Log2(std::max<u64>(_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;
- 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)