11#include " string_util.h"
22
33#include < array>
4+ #include < cinttypes>
45#ifdef BENCHMARK_STL_ANDROID_GNUSTL
56#include < cerrno>
67#endif
1516namespace benchmark {
1617namespace {
1718
19+ // Thousands, Millions, Billions, Trillions, Quadrillions, Quintillions,
20+ // Sextillions, Septillions.
21+ const std::array<std::string, 8 > base10Units = {" k" , " M" , " B" , " T" ,
22+ " Q" , " Qi" , " Sx" , " Sp" };
23+
1824// kilo, Mega, Giga, Tera, Peta, Exa, Zetta, Yotta.
1925const char kBigSIUnits [] = " kMGTPEZY" ;
2026// Kibi, Mebi, Gibi, Tebi, Pebi, Exbi, Zebi, Yobi.
@@ -32,7 +38,8 @@ static const int64_t kUnitsSize = arraysize(kBigSIUnits);
3238
3339void ToExponentAndMantissa (double val, double thresh, int precision,
3440 double one_k, std::string* mantissa,
35- int64_t * exponent) {
41+ int64_t * exponent,
42+ bool inclusiveBigThreshhold = false ) {
3643 std::stringstream mantissa_stream;
3744
3845 if (val < 0 ) {
@@ -44,7 +51,8 @@ void ToExponentAndMantissa(double val, double thresh, int precision,
4451 // in 'precision' digits.
4552 const double adjusted_threshold =
4653 std::max (thresh, 1.0 / std::pow (10.0 , precision));
47- const double big_threshold = adjusted_threshold * one_k;
54+ const double big_threshold =
55+ (adjusted_threshold * one_k) - inclusiveBigThreshhold;
4856 const double small_threshold = adjusted_threshold;
4957 // Values in ]simple_threshold,small_threshold[ will be printed as-is
5058 const double simple_threshold = 0.01 ;
@@ -262,4 +270,35 @@ double stod(const std::string& str, size_t* pos) {
262270}
263271#endif
264272
273+ std::string ExponentToBase10Prefix (int64_t exponent) {
274+ if (exponent == 0 ) return " " ;
275+
276+ const int64_t index = (exponent > 0 ? exponent - 1 : -exponent - 1 );
277+ if (index >= kUnitsSize ) return " " ;
278+
279+ return base10Units[index];
280+ }
281+
282+ std::string Base10HumanReadableFormat (const int64_t & arg) {
283+ std::string mantissa;
284+ int64_t exponent;
285+ ToExponentAndMantissa (arg, 1 , 1 , 1000 , &mantissa, &exponent, true );
286+ return mantissa + ExponentToBase10Prefix (exponent);
287+ }
288+
289+ bool IsPowerOfTwo (const int64_t & val) {
290+ return (val & (val - 1 )) == 0 && (val > 1 );
291+ }
292+
293+ std::string Base2HumanReadableFormat (const int64_t & arg) {
294+ return StrFormat (" 2^%.0f" , std::log2 (arg));
295+ }
296+
297+ std::string FormatHumanReadable (const int64_t & arg) {
298+ if (IsPowerOfTwo (arg)) {
299+ return Base2HumanReadableFormat (arg);
300+ }
301+ return Base10HumanReadableFormat (arg);
302+ }
303+
265304} // end namespace benchmark
0 commit comments