5 #include "../compiler/compiler.h"
8 #define ARRAY_SIZE(x) (sizeof((x)) / (sizeof((x)[0])))
11 * num2str() - Cheesy number->string conversion, complete with carry rounding error.
12 * @num: quantity (e.g., number of blocks, bytes or bits)
13 * @maxlen: max number of digits in the output string (not counting prefix and units, but counting .)
14 * @base: multiplier for num (e.g., if num represents Ki, use 1024)
15 * @pow2: select unit prefix - 0=power-of-10 decimal SI, nonzero=power-of-2 binary IEC
16 * @units: select units - N2S_* macros defined in num2str.h
17 * @returns a malloc'd buffer containing "number[<unit prefix>][<units>]"
19 char *num2str(uint64_t num, int maxlen, int base, int pow2, int units)
21 const char *sistr[] = { "", "k", "M", "G", "T", "P" };
22 const char *iecstr[] = { "", "Ki", "Mi", "Gi", "Ti", "Pi" };
23 const char **unitprefix;
24 const char *unitstr[] = { "", "/s", "B", "bit", "B/s", "bit/s" };
25 const unsigned int thousand[] = { 1000, 1024 };
27 int unit_index = 0, post_index, carry = 0;
28 char tmp[32], fmt[32];
31 compiletime_assert(sizeof(sistr) == sizeof(iecstr), "unit prefix arrays must be identical sizes");
42 for (post_index = 0; base > 1; post_index++)
43 base /= thousand[!!pow2];
66 * Divide by K/Ki until string length of num <= maxlen.
69 while (post_index < sizeof(sistr)) {
70 sprintf(tmp, "%llu", (unsigned long long) num);
71 if (strlen(tmp) <= maxlen)
74 modulo = num % thousand[!!pow2];
75 num /= thousand[!!pow2];
76 carry = modulo >= thousand[!!pow2] / 2;
81 * If no modulo, then we're done.
85 if (post_index >= ARRAY_SIZE(sistr))
88 sprintf(buf, "%llu%s%s", (unsigned long long) num,
89 unitprefix[post_index], unitstr[unit_index]);
94 * If no room for decimals, then we're done.
96 sprintf(tmp, "%llu", (unsigned long long) num);
97 if ((int)(maxlen - strlen(tmp)) <= 1) {
104 * Fill in everything and return the result.
106 assert(maxlen - strlen(tmp) - 1 > 0);
107 assert(modulo < thousand[!!pow2]);
108 sprintf(fmt, "%%.%df", (int)(maxlen - strlen(tmp) - 1));
109 sprintf(tmp, fmt, (double)modulo / (double)thousand[!!pow2]);
111 sprintf(buf, "%llu.%s%s%s", (unsigned long long) num, &tmp[2],
112 unitprefix[post_index], unitstr[unit_index]);