Date: Fri, 25 Mar 2011 14:33:06 -0700 From: Xin LI <delphij@gmail.com> To: Alexander Best <arundel@freebsd.org> Cc: freebsd-hackers@freebsd.org Subject: Re: Switching to [KMGTPE]i prefixes? Message-ID: <AANLkTinEcT__Wtc6LkSyqqMnQwuKVUbZC4dPZvZH_dSX@mail.gmail.com> In-Reply-To: <20110325195325.GA69264@freebsd.org> References: <20110325002115.GA323@freebsd.org> <20110325015508.GA14565@freebsd.org> <20110325024658.GA19544@freebsd.org> <336A9ACD-29BF-41C9-BC25-917CC1E4587D@bsdimp.com> <20110325195325.GA69264@freebsd.org>
next in thread | previous in thread | raw e-mail | index | archive | help
[-- Attachment #1 --] FYI I have a patch and I have incorporated some of Alexander's idea. Difference: - Use of both HN_DIVISOR_1000 and HN_IEC_PREFIXES triggers an assertion. I think it doesn't make sense to return since this is an API violation and we should just tell the caller explicitly; - DIVISOR_1000 and !1000 cases use just same prefixes, so merge them while keeping divisor intact; - Make prefixes table consistently long. I have no strong opinion on this one, though, it's just what my original version used and I can change it to the way Alexander did if there is an advantage of doing that way. (Note, it seems that we use HN_ prefix for both 'scale' and 'flags', I have sorted them by value but HN_IEC_PREFIXES should really belong to the flags group). Cheers, -- Xin LI <delphij@delphij.net> http://www.delphij.net [-- Attachment #2 --] Index: humanize_number.c =================================================================== --- humanize_number.c (revision 220009) +++ humanize_number.c (working copy) @@ -54,29 +54,31 @@ assert(buf != NULL); assert(suffix != NULL); assert(scale >= 0); + assert(!((flags & HN_DIVISOR_1000) && (flags & HN_IEC_PREFIXES))); remainder = 0; - if (flags & HN_DIVISOR_1000) { - /* SI for decimal multiplies */ - divisor = 1000; + if (flags & HN_IEC_PREFIXES) { + baselen = 2; + divisor = 1024; if (flags & HN_B) - prefixes = "B\0k\0M\0G\0T\0P\0E"; + prefixes = "B\0\0Ki\0Mi\0Gi\0Ti\0Pi\0Ei"; else - prefixes = "\0\0k\0M\0G\0T\0P\0E"; + prefixes = "\0\0Ki\0Mi\0Gi\0Ti\0Pi\0Ei"; } else { - /* - * binary multiplies - * XXX IEC 60027-2 recommends Ki, Mi, Gi... - */ - divisor = 1024; + baselen = 1; + if (flags & HN_DIVISOR_1000) + divisor = 1000; + else + divisor = 1024; + if (flags & HN_B) - prefixes = "B\0K\0M\0G\0T\0P\0E"; + prefixes = "B\0\0k\0\0M\0\0G\0\0T\0\0P\0\0E"; else - prefixes = "\0\0K\0M\0G\0T\0P\0E"; + prefixes = "\0\0\0k\0\0M\0\0G\0\0T\0\0P\0\0E"; } -#define SCALE2PREFIX(scale) (&prefixes[(scale) << 1]) +#define SCALE2PREFIX(scale) (&prefixes[(scale) * 3]) maxscale = 7; if (scale >= maxscale && @@ -91,10 +93,10 @@ if (quotient < 0) { sign = -1; quotient = -quotient; - baselen = 3; /* sign, digit, prefix */ + baselen += 2; /* sign, digit */ } else { sign = 1; - baselen = 2; /* digit, prefix */ + baselen += 1; /* digit */ } if (flags & HN_NOSPACE) sep = ""; Index: libutil.h =================================================================== --- libutil.h (revision 220009) +++ libutil.h (working copy) @@ -223,6 +223,7 @@ #define HN_GETSCALE 0x10 #define HN_AUTOSCALE 0x20 +#define HN_IEC_PREFIXES 0x40 /* hexdump(3) */ #define HD_COLUMN_MASK 0xff
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?AANLkTinEcT__Wtc6LkSyqqMnQwuKVUbZC4dPZvZH_dSX>
