Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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!
  • Loading branch information
jordan-woyak committed Mar 5, 2013
1 parent 2402383 commit 10d57a3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
22 changes: 22 additions & 0 deletions Source/Core/Common/Src/MathUtil.h
Expand Up @@ -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.
Expand Down
29 changes: 11 additions & 18 deletions Source/Core/DolphinWX/Src/GameListCtrl.cpp
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 10d57a3

Please sign in to comment.