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!
This commit is contained in:
Jordan Woyak 2013-03-05 03:12:17 -06:00
parent 240238308c
commit 10d57a3402
2 changed files with 33 additions and 18 deletions

View File

@ -152,6 +152,28 @@ float MathFloatVectorSum(const std::vector<float>&);
#define ROUND_UP(x, a) (((x) + (a) - 1) & ~((a) - 1)) #define ROUND_UP(x, a) (((x) + (a) - 1) & ~((a) - 1))
#define ROUND_DOWN(x, a) ((x) & ~((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. // Tiny matrix/vector library.
// Used for things like Free-Look in the gfx backend. // Used for things like Free-Look in the gfx backend.

View File

@ -35,6 +35,7 @@
#include "CDUtils.h" #include "CDUtils.h"
#include "WxUtils.h" #include "WxUtils.h"
#include "Main.h" #include "Main.h"
#include "MathUtil.h"
#include "../resources/Flag_Europe.xpm" #include "../resources/Flag_Europe.xpm"
#include "../resources/Flag_Germany.xpm" #include "../resources/Flag_Germany.xpm"
@ -380,26 +381,18 @@ void CGameListCtrl::Update()
SetFocus(); SetFocus();
} }
wxString NiceSizeFormat(s64 _size) wxString NiceSizeFormat(u64 _size)
{ {
const char* sizes[] = {"b", "KB", "MB", "GB", "TB", "PB", "EB"}; const char* const unit_symbols[] = {"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"};
int s = 0;
int frac = 0; 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;
while (_size > (s64)1024) return StrToWxStr(StringFromFormat("%llu.%llu %s", value, frac, unit_symbols[unit]));
{
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);
} }
void CGameListCtrl::InsertItemInReportView(long _Index) void CGameListCtrl::InsertItemInReportView(long _Index)