From 2eceaa5706ad6cf3a1b153c1f2a8a2fffa2d5945 Mon Sep 17 00:00:00 2001 From: "Paul D. Hahn" Date: Sun, 28 Jan 2018 11:43:57 -0600 Subject: [PATCH] Fix truncation problem in values shown for Memory when using occainfo. (#81) [Sys] Added extra precision to stringifyBytes --- src/tools.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/tools.cpp b/src/tools.cpp index aa2088722..25e7b3f15 100644 --- a/src/tools.cpp +++ b/src/tools.cpp @@ -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(); }