diff --git a/ccminer.cpp b/ccminer.cpp index 196e958..313779c 100644 --- a/ccminer.cpp +++ b/ccminer.cpp @@ -528,7 +528,7 @@ static void calc_diff(struct work *work, int known) static int share_result(int result, const char *reason) { - char s[345]; + char s[32] = { 0 }; double hashrate = 0.; pthread_mutex_lock(&stats_lock); @@ -542,8 +542,8 @@ static int share_result(int result, const char *reason) global_hashrate = llround(hashrate); - sprintf(s, hashrate >= 1e6 ? "%.0f" : "%.2f", 1e-3 * hashrate); - applog(LOG_NOTICE, "accepted: %lu/%lu (%.2f%%), %s khash/s %s", + format_hashrate(hashrate, s); + applog(LOG_NOTICE, "accepted: %lu/%lu (%.2f%%), %s %s", accepted_count, accepted_count + rejected_count, 100. * accepted_count / (accepted_count + rejected_count), diff --git a/util.cpp b/util.cpp index 0ae77ef..9056d16 100644 --- a/util.cpp +++ b/util.cpp @@ -175,54 +175,31 @@ void get_defconfig_path(char *out, size_t bufsize, char *argv0) void format_hashrate(double hashrate, char *output) { - char prefix; + char prefix = '\0'; - if (hashrate < 1e3) - prefix = ' '; - else if (hashrate < 1e6) - { + if (hashrate < 10000) { + // nop + } + else if (hashrate < 1e7) { prefix = 'k'; hashrate *= 1e-3; } - else if (hashrate < 1e9) - { + else if (hashrate < 1e10) { prefix = 'M'; hashrate *= 1e-6; } - else if (hashrate < 1e12) - { + else if (hashrate < 1e13) { prefix = 'G'; hashrate *= 1e-9; } - else if (hashrate < 1e15) - { + else { prefix = 'T'; hashrate *= 1e-12; } - else if (hashrate < 1e18) - { - prefix = 'P'; - hashrate *= 1e-15; - } - else if (hashrate < 1e21) - { - prefix = 'E'; - hashrate *= 1e-18; - } - else if (hashrate < 1e24) - { - prefix = 'Z'; - hashrate *= 1e-21; - } - else - { - prefix = 'Y'; - hashrate *= 1e-24; - } sprintf( output, - prefix == ' ' ? "%.0f%cH/s" : "%.2f %cH/s", + prefix ? "%.2f %cH/s" : "%.2f H/s%c", hashrate, prefix ); }