From: Leonid Bloch <lbloch@janustech.com>
To: "qemu-devel@nongnu.org" <qemu-devel@nongnu.org>,
"Philippe Mathieu-Daudé" <philmd@redhat.com>
Cc: "qemu-block@nongnu.org" <qemu-block@nongnu.org>,
Kevin Wolf <kwolf@redhat.com>, Max Reitz <mreitz@redhat.com>,
Alberto Garcia <berto@igalia.com>,
Markus Armbruster <armbru@redhat.com>,
Leonid Bloch <lbloch@janustech.com>
Subject: [Qemu-devel] [PATCH v2 1/1] include: Add a comment to explain the origin of sizes' lookup table
Date: Sun, 4 Nov 2018 18:07:28 +0000 [thread overview]
Message-ID: <20181104180657.7471-2-lbloch@janustech.com> (raw)
In-Reply-To: <20181104180657.7471-1-lbloch@janustech.com>
The lookup table for power-of-two sizes was added in commit 540b8492618eb
for the purpose of having convenient shortcuts for these sizes in cases
when the literal number has to be present at compile time, and
expressions as '(1 * KiB)' can not be used. One such case is the
stringification of sizes. Beyond that, it is convenient to use these
shortcuts for all power-of-two sizes, even if they don't have to be
literal numbers.
Despite its convenience, this table introduced 55 lines of "dumb" code,
the purpose and origin of which are obscure without reading the message
of the commit which introduced it. This patch fixes that by adding a
comment to the code itself with a brief explanation for the reasoning
behind this table. This comment includes the short AWK script that
generated the table, so that anyone who's interested could make sure
that the values in it are correct (otherwise these values look as if
they were typed manually).
Signed-off-by: Leonid Bloch <lbloch@janustech.com>
---
include/qemu/units.h | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git a/include/qemu/units.h b/include/qemu/units.h
index 68a7758650..1c959d182e 100644
--- a/include/qemu/units.h
+++ b/include/qemu/units.h
@@ -17,6 +17,24 @@
#define PiB (INT64_C(1) << 50)
#define EiB (INT64_C(1) << 60)
+/*
+ * The following lookup table is intended to be used when a literal string of
+ * the number of bytes is required (for example if it needs to be stringified).
+ * It can also be used for generic shortcuts of power-of-two sizes.
+ * This table is generated using the AWK script below:
+ *
+ * BEGIN {
+ * suffix="KMGTPE";
+ * for(i=10; i<64; i++) {
+ * val=2**i;
+ * s=substr(suffix, int(i/10), 1);
+ * n=2**(i%10);
+ * pad=21-int(log(n)/log(10));
+ * printf("#define S_%d%siB %*d\n", n, s, pad, val);
+ * }
+ * }
+ */
+
#define S_1KiB 1024
#define S_2KiB 2048
#define S_4KiB 4096
--
2.17.1
next prev parent reply other threads:[~2018-11-04 18:07 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-11-04 18:07 [Qemu-devel] [PATCH v2 0/1] include: Add a comment to explain the origin of Leonid Bloch
2018-11-04 18:07 ` Leonid Bloch [this message]
2018-11-05 15:58 ` [Qemu-devel] [PATCH v2 1/1] include: Add a comment to explain the origin of sizes' lookup table Philippe Mathieu-Daudé
2018-11-06 8:56 ` Leonid Bloch
2018-11-06 11:19 ` Kevin Wolf
2018-11-05 14:30 ` [Qemu-devel] [PATCH v2 0/1] include: Add a comment to explain the origin of Kevin Wolf
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=20181104180657.7471-2-lbloch@janustech.com \
--to=lbloch@janustech.com \
--cc=armbru@redhat.com \
--cc=berto@igalia.com \
--cc=kwolf@redhat.com \
--cc=mreitz@redhat.com \
--cc=philmd@redhat.com \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).