Skip to content

Commit

Permalink
Fix truncation problem in values shown for Memory when using occainfo. (
Browse files Browse the repository at this point in the history
#81)

[Sys] Added extra precision to stringifyBytes
  • Loading branch information
pdhahn authored and dmed256 committed Jan 28, 2018
1 parent 62f82cb commit 2eceaa5
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1194,23 +1194,29 @@ namespace occa {

std::string stringifyBytes(uintptr_t bytes) {
if (0 < bytes) {

std::stringstream ss;
uint64_t bigBytes = bytes;
uint64_t big1 = 1;

char bufr[32];
# define __ROUNDIT(b) (sprintf( bufr, "%.3g", bigBytes / (double)((uint64_t)1 << (b)) ), bufr )

if (bigBytes < (big1 << 10))
ss << bigBytes << " bytes";
else if (bigBytes < (big1 << 20))
ss << (bigBytes >> 10) << " KB";
ss << __ROUNDIT(10) << " KB";
else if (bigBytes < (big1 << 30))
ss << (bigBytes >> 20) << " MB";
ss << __ROUNDIT(20) << " MB";
else if (bigBytes < (big1 << 40))
ss << (bigBytes >> 30) << " GB";
ss << __ROUNDIT(30) << " GB";
else if (bigBytes < (big1 << 50))
ss << (bigBytes >> 40) << " TB";
ss << __ROUNDIT(40) << " TB";
else
ss << bigBytes << " bytes";

# undef __ROUNDIT

return ss.str();
}

Expand Down

0 comments on commit 2eceaa5

Please sign in to comment.