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