On 30/5/22 17:07, Paolo Bonzini wrote:
Extract the knowledge of IEC and SI prefixes out of size_to_str and
freq_to_str, so that it can be reused when printing statistics.

Signed-off-by: Paolo Bonzini <pbonz...@redhat.com>
---
  include/qemu/cutils.h    | 18 ++++++++++++++++++
  tests/unit/test-cutils.c | 32 ++++++++++++++++++++++++++++++++
  util/cutils.c            | 34 +++++++++++++++++++++++++---------
  3 files changed, 75 insertions(+), 9 deletions(-)

diff --git a/util/cutils.c b/util/cutils.c
index 19fb4d04f8..485e9b0cea 100644
--- a/util/cutils.c
+++ b/util/cutils.c
@@ -872,6 +872,25 @@ int parse_debug_env(const char *name, int max, int initial)
      return debug;
  }
+const char *si_prefix(unsigned int exp10)
+{
+    static const char *prefixes[] = {
+        "a", "f", "p", "n", "u", "m", "", "k", "M", "G", "T", "P", "E"
+    };
+
+    exp10 += 18;
+    assert(exp10 % 3 == 0 && exp10 / 3 < ARRAY_SIZE(prefixes));

Can we add parenthesis to ease code review?

+    return prefixes[exp10 / 3];
+}
+
+const char *iec_binary_prefix(unsigned int exp2)
+{
+    static const char *prefixes[] = { "", "ki", "Mi", "Gi", "Ti", "Pi", "Ei" };
+
+    assert(exp2 % 10 == 0 && exp2 / 10 < ARRAY_SIZE(prefixes));

Ditto.

+    return prefixes[exp2 / 10];
+}

Reviewed-by: Philippe Mathieu-Daudé <f4...@amsat.org>

Reply via email to