Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 25 Mar 2011 01:55:08 +0000
From:      Alexander Best <arundel@freebsd.org>
To:        freebsd-hackers@freebsd.org
Subject:   Re: Switching to [KMGTPE]i prefixes?
Message-ID:  <20110325015508.GA14565@freebsd.org>
In-Reply-To: <20110325002115.GA323@freebsd.org>
References:  <20110325002115.GA323@freebsd.org>

next in thread | previous in thread | raw e-mail | index | archive | help

--YiEDa0DAkWCtVeE4
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

here's a rough draft. it introduces a new flag called HN_IEC_PREFIXES so
nothing should get broken and each utility can decide for itself whether to use
the SI binary, SI decimal or the IEC certified prefixes.

another possibility would be to #ifdef the extra code. this would offer the
possibility to turn the new code on or off without having to adjust any
utilities. of course this behaviour should be disabled by default in order to
maintain compatibility.

cheers.
alex

-- 
a13x

--YiEDa0DAkWCtVeE4
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="IEC_80000_13.diff"

diff --git a/lib/libutil/humanize_number.c b/lib/libutil/humanize_number.c
index 75bcb46..b6677c3 100644
--- a/lib/libutil/humanize_number.c
+++ b/lib/libutil/humanize_number.c
@@ -47,7 +47,7 @@ humanize_number(char *buf, size_t len, int64_t quotient,
     const char *suffix, int scale, int flags)
 {
 	const char *prefixes, *sep;
-	int	i, r, remainder, maxscale, s1, s2, sign;
+	int	i, r, remainder, maxscale, s1, s2, shift, sign;
 	int64_t	divisor, max;
 	size_t	baselen;
 
@@ -57,26 +57,45 @@ humanize_number(char *buf, size_t len, int64_t quotient,
 
 	remainder = 0;
 
+	/*
+	 * With HN_DIVISOR_1000 defined in flags, SI prefixes for decimal
+	 * multiplies are being used. Without this flag, SI prefixes for
+	 * binary multiplies are being used. If however HN_IEC_PREFIXES is
+	 * defined in flags, the prefixes recommended by the International
+	 * Electrotechnical Commission (IEC) for binary multiplies in
+	 * IEC 80000-3 (superseeding IEC 60027-2) (i.e. Ki, Mi, Gi...) are
+	 * being used.
+	 *
+	 * Please note that HN_DIVISOR_1000 and HN_IEC_PREFIXES are mutually
+	 * exclusive. The default behavior is to use SI prefixes for binary
+	 * multiplies.
+	 */
+	if ((flags & HN_DIVISOR_1000) && (flags & HN_IEC_PREFIXES))
+		return (-1);
 	if (flags & HN_DIVISOR_1000) {
-		/* SI for decimal multiplies */
 		divisor = 1000;
+		shift = 1;
 		if (flags & HN_B)
 			prefixes = "B\0k\0M\0G\0T\0P\0E";
 		else
 			prefixes = "\0\0k\0M\0G\0T\0P\0E";
+	} else if (flags & HN_IEC_PREFIXES) {
+		divisor = 1024;
+		shift = 2;
+		if (flags & HN_B)
+			prefixes = "B\0\0\0Ki\0\0Mi\0\0Gi\0\0Ti\0\0Pi\0\0Ei";
+		else
+			prefixes = "\0\0\0\0Ki\0\0Mi\0\0Gi\0\0Ti\0\0Pi\0\0Ei";
 	} else {
-		/*
-		 * binary multiplies
-		 * XXX IEC 60027-2 recommends Ki, Mi, Gi...
-		 */
 		divisor = 1024;
+		shift = 1;
 		if (flags & HN_B)
 			prefixes = "B\0K\0M\0G\0T\0P\0E";
 		else
 			prefixes = "\0\0K\0M\0G\0T\0P\0E";
 	}
 
-#define	SCALE2PREFIX(scale)	(&prefixes[(scale) << 1])
+#define	SCALE2PREFIX(scale)	(&prefixes[(scale) << shift])
 	maxscale = 7;
 
 	if (scale >= maxscale &&
@@ -102,6 +121,10 @@ humanize_number(char *buf, size_t len, int64_t quotient,
 		sep = " ";
 		baselen++;
 	}
+	if (flags & HN_IEC_PREFIXES) {
+		baselen ++;
+		len ++;
+	}
 	baselen += strlen(suffix);
 
 	/* Check if enough room for `x y' + suffix + `\0' */
diff --git a/lib/libutil/libutil.h b/lib/libutil/libutil.h
index 66104e9..295a8d3 100644
--- a/lib/libutil/libutil.h
+++ b/lib/libutil/libutil.h
@@ -220,6 +220,7 @@ __END_DECLS
 #define HN_NOSPACE		0x02
 #define HN_B			0x04
 #define HN_DIVISOR_1000		0x08
+#define HN_IEC_PREFIXES		0x0f
 
 #define HN_GETSCALE		0x10
 #define HN_AUTOSCALE		0x20

--YiEDa0DAkWCtVeE4--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20110325015508.GA14565>