Improve IEC binary and SI decimal prefix handling
[fio.git] / lib / num2str.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4
5 #include "../fio.h"
6
7 #define ARRAY_LENGTH(arr)       sizeof(arr) / sizeof((arr)[0])
8
9 /**
10  * num2str() - Cheesy number->string conversion, complete with carry rounding error.
11  * @num: quantity (e.g., number of blocks, bytes or bits)
12  * @maxlen: max number of digits in the output string (not counting prefix and units)
13  * @base: multiplier for num (e.g., if num represents Ki, use 1024)
14  * @pow2: select unit prefix - 0=power-of-10 decimal SI, nonzero=power-of-2 binary IEC
15  * @units: select units - N2S_* macros defined in fio.h
16  * @returns a malloc'd buffer containing "number[<unit prefix>][<units>]"
17  */
18 char *num2str(uint64_t num, int maxlen, int base, int pow2, int units)
19 {
20         const char *sistr[] = { "", "k", "M", "G", "T", "P" };
21         const char *iecstr[] = { "", "Ki", "Mi", "Gi", "Ti", "Pi" };
22         const char **unitprefix;
23         const char *unitstr[] = { "", "/s", "B", "bit", "B/s", "bit/s" };
24         const unsigned int thousand[] = { 1000, 1024 };
25         unsigned int modulo, decimals;
26         int unit_index = 0, post_index, carry = 0;
27         char tmp[32];
28         char *buf;
29
30         compiletime_assert(sizeof(sistr) == sizeof(iecstr), "unit prefix arrays must be identical sizes");
31
32         buf = malloc(128);
33         if (!buf)
34                 return NULL;
35
36         if (pow2)
37                 unitprefix = iecstr;
38         else
39                 unitprefix = sistr;
40
41         for (post_index = 0; base > 1; post_index++)
42                 base /= thousand[!!pow2];
43
44         switch (units) {
45         case N2S_PERSEC:
46                 unit_index = 1;
47                 break;
48         case N2S_BYTE:
49                 unit_index = 2;
50                 break;
51         case N2S_BIT:
52                 unit_index = 3;
53                 num *= 8;
54                 break;
55         case N2S_BYTEPERSEC:
56                 unit_index = 4;
57                 break;
58         case N2S_BITPERSEC:
59                 unit_index = 5;
60                 num *= 8;
61                 break;
62         }
63
64         modulo = -1U;
65         while (post_index < sizeof(sistr)) {
66                 sprintf(tmp, "%llu", (unsigned long long) num);
67                 if (strlen(tmp) <= maxlen)
68                         break;
69
70                 modulo = num % thousand[!!pow2];
71                 num /= thousand[!!pow2];
72                 carry = modulo >= thousand[!!pow2] / 2;
73                 post_index++;
74         }
75
76         if (modulo == -1U) {
77 done:
78                 if (post_index >= ARRAY_LENGTH(sistr))
79                         post_index = 0;
80
81                 sprintf(buf, "%llu%s%s", (unsigned long long) num,
82                         unitprefix[post_index], unitstr[unit_index]);
83                 return buf;
84         }
85
86         sprintf(tmp, "%llu", (unsigned long long) num);
87         decimals = maxlen - strlen(tmp);
88         if (decimals <= 1) {
89                 if (carry)
90                         num++;
91                 goto done;
92         }
93
94         do {
95                 sprintf(tmp, "%u", modulo);
96                 if (strlen(tmp) <= decimals - 1)
97                         break;
98
99                 modulo = (modulo + 9) / 10;
100         } while (1);
101
102         sprintf(buf, "%llu.%u%s%s", (unsigned long long) num, modulo,
103                         unitprefix[post_index], unitstr[unit_index]);
104         return buf;
105 }