Revision e41b509d qapi/string-output-visitor.c
b/qapi/string-output-visitor.c | ||
---|---|---|
15 | 15 |
#include "qapi/visitor-impl.h" |
16 | 16 |
#include "qapi/qmp/qerror.h" |
17 | 17 |
#include "qemu/host-utils.h" |
18 |
#include <math.h> |
|
18 | 19 |
|
19 | 20 |
struct StringOutputVisitor |
20 | 21 |
{ |
... | ... | |
47 | 48 |
Error **errp) |
48 | 49 |
{ |
49 | 50 |
StringOutputVisitor *sov = DO_UPCAST(StringOutputVisitor, visitor, v); |
50 |
static const char suffixes[] = { 'B', 'K', 'M', 'G', 'T' }; |
|
51 |
static const char suffixes[] = { 'B', 'K', 'M', 'G', 'T', 'P', 'E' };
|
|
51 | 52 |
uint64_t div, val; |
52 | 53 |
char *out; |
53 | 54 |
int i; |
54 | 55 |
|
55 | 56 |
if (!sov->human) { |
56 |
out = g_strdup_printf("%llu", (long long) *obj);
|
|
57 |
out = g_strdup_printf("%"PRIu64, *obj);
|
|
57 | 58 |
string_output_set(sov, out); |
58 | 59 |
return; |
59 | 60 |
} |
60 | 61 |
|
61 | 62 |
val = *obj; |
62 | 63 |
|
63 |
/* Compute floor(log2(val)). */ |
|
64 |
i = 64 - clz64(val); |
|
65 |
|
|
66 |
/* Find the power of 1024 that we'll display as the units. */ |
|
67 |
i /= 10; |
|
68 |
if (i >= ARRAY_SIZE(suffixes)) { |
|
69 |
i = ARRAY_SIZE(suffixes) - 1; |
|
70 |
} |
|
64 |
/* The exponent (returned in i) minus one gives us |
|
65 |
* floor(log2(val * 1024 / 1000). The correction makes us |
|
66 |
* switch to the higher power when the integer part is >= 1000. |
|
67 |
*/ |
|
68 |
frexp(val / (1000.0 / 1024.0), &i); |
|
69 |
i = (i - 1) / 10; |
|
70 |
assert(i < ARRAY_SIZE(suffixes)); |
|
71 | 71 |
div = 1ULL << (i * 10); |
72 | 72 |
|
73 |
out = g_strdup_printf("%0.03f%c", (double)val/div, suffixes[i]); |
|
73 |
out = g_strdup_printf("%"PRIu64" (%0.3g %c%s)", val, |
|
74 |
(double)val/div, suffixes[i], i ? "iB" : ""); |
|
74 | 75 |
string_output_set(sov, out); |
75 | 76 |
} |
76 | 77 |
|
Also available in: Unified diff