qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH v5 05/10] cutils: add functions for IEC and SI prefixes


From: Philippe Mathieu-Daudé
Subject: Re: [PATCH v5 05/10] cutils: add functions for IEC and SI prefixes
Date: Mon, 30 May 2022 23:59:53 +0200
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Thunderbird/91.9.1

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 <pbonzini@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é <f4bug@amsat.org>



reply via email to

[Prev in Thread] Current Thread [Next in Thread]