All of lore.kernel.org
 help / color / mirror / Atom feed
From: Petr Machata <me@pmachata.org>
To: netdev@vger.kernel.org, dsahern@gmail.com, stephen@networkplumber.org
Cc: Po.Liu@nxp.com, toke@toke.dk, dave.taht@gmail.com,
	edumazet@google.com, tahiliani@nitk.edu.in, leon@kernel.org,
	Petr Machata <me@pmachata.org>
Subject: [PATCH iproute2-next v2 4/7] lib: sprint_size(): Uncrustify the code a bit
Date: Sat,  5 Dec 2020 22:13:32 +0100	[thread overview]
Message-ID: <5de07d7849479adb8b660060d06004ab8582ed48.1607201857.git.me@pmachata.org> (raw)
In-Reply-To: <cover.1607201857.git.me@pmachata.org>

Ideally this and the rate printing would both be converted to a common
helper, but unfortunately the two format differently and this would break
tests and scripts out there. So just make the code look less like a wad of
hay.

Signed-off-by: Petr Machata <me@pmachata.org>
---

Notes:
    v2:
    - This patch is new. It addresses a request from Stephen Hemminger to
      clean up the sprint_size() function.

 lib/json_print.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/lib/json_print.c b/lib/json_print.c
index c1df637642fd..d28e957c9603 100644
--- a/lib/json_print.c
+++ b/lib/json_print.c
@@ -344,13 +344,15 @@ int print_color_rate(bool use_iec, enum output_type type, enum color_attr color,
 
 char *sprint_size(__u32 sz, char *buf)
 {
+	long kilo = 1024;
+	long mega = kilo * kilo;
 	size_t len = SPRINT_BSIZE - 1;
 	double tmp = sz;
 
-	if (sz >= 1024*1024 && fabs(1024*1024*rint(tmp/(1024*1024)) - sz) < 1024)
-		snprintf(buf, len, "%gMb", rint(tmp/(1024*1024)));
-	else if (sz >= 1024 && fabs(1024*rint(tmp/1024) - sz) < 16)
-		snprintf(buf, len, "%gKb", rint(tmp/1024));
+	if (sz >= mega && fabs(mega * rint(tmp / mega) - sz) < 1024)
+		snprintf(buf, len, "%gMb", rint(tmp / mega));
+	else if (sz >= kilo && fabs(kilo * rint(tmp / kilo) - sz) < 16)
+		snprintf(buf, len, "%gKb", rint(tmp / kilo));
 	else
 		snprintf(buf, len, "%ub", sz);
 
-- 
2.25.1


  parent reply	other threads:[~2020-12-05 21:15 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-05 21:13 [PATCH iproute2-next v2 0/7] Move rate and size parsing and output to lib Petr Machata
2020-12-05 21:13 ` [PATCH iproute2-next v2 1/7] Move the use_iec declaration to the tools Petr Machata
2020-12-05 21:13 ` [PATCH iproute2-next v2 2/7] lib: Move print_rate() from tc here; modernize Petr Machata
2020-12-05 21:13 ` [PATCH iproute2-next v2 3/7] lib: Move sprint_size() from tc here, add print_size() Petr Machata
2020-12-05 21:13 ` Petr Machata [this message]
2020-12-05 21:13 ` [PATCH iproute2-next v2 5/7] lib: print_color_rate(): Fix formatting small rates in IEC mode Petr Machata
2020-12-05 21:13 ` [PATCH iproute2-next v2 6/7] lib: Move get_rate(), get_rate64() from tc here Petr Machata
2020-12-05 21:13 ` [PATCH iproute2-next v2 7/7] lib: Move get_size() " Petr Machata
2020-12-09  2:34 ` [PATCH iproute2-next v2 0/7] Move rate and size parsing and output to lib David Ahern

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5de07d7849479adb8b660060d06004ab8582ed48.1607201857.git.me@pmachata.org \
    --to=me@pmachata.org \
    --cc=Po.Liu@nxp.com \
    --cc=dave.taht@gmail.com \
    --cc=dsahern@gmail.com \
    --cc=edumazet@google.com \
    --cc=leon@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=stephen@networkplumber.org \
    --cc=tahiliani@nitk.edu.in \
    --cc=toke@toke.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.