On 01/30/2014 09:42 AM, Paolo Bonzini wrote: > This fixes several bugs or shortcomings of the previous pretty-printer. > In particular: > > * use PRIu64 instead of casting to long long > > * the exact value is included too > > * the correct unit of measure (MiB, GiB, etc.) is used. PiB and EiB > are added too. > > * due to an off-by-one error, 512*2^30 was printed as 0.500MiB rather than > 512MiB. floor(log2(val)) is equal to 63 - clz(val), while the code used 64. > > * The desired specification is %g rather than %f, which always uses three > decimals in the current code. However %g would switch to scientific > notation when the integer part is >= 1000 (e.g. 1000*2^30). To keep the > code simple, switch to the higher power when the integer part is >= 1000; > overflow is avoided by using frexp instead of clz. > > Suggested-by: Eric Blake <ebl...@redhat.com> > Signed-off-by: Paolo Bonzini <pbonz...@redhat.com> > --- > qapi/string-output-visitor.c | 23 ++++++++++++----------- > 1 file changed, 12 insertions(+), 11 deletions(-) >
> > - out = g_strdup_printf("%0.03f%c", (double)val/div, suffixes[i]); > + out = g_strdup_printf("%"PRIu64" (%0.3g %c%s)", val, For 1152, the old code would print 1.125k, while the new code prints only 1.12k. But that's acceptable in my eyes (Remember, %0.3g is how many significant digits, where both 0.999 and 1.01 have 3 digits). I also checked that overflow is not possible, and checked several border cases. Much nicer! Reviewed-by: Eric Blake <ebl...@redhat.com> -- Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
signature.asc
Description: OpenPGP digital signature