docs: update for new data placement options
[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 /**
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_* constants defined in num2str.h
17  * @returns a malloc'd buffer containing "number[<unit prefix>][<units>]"
18  */
19 char *num2str(uint64_t num, int maxlen, int base, int pow2, enum n2s_unit units)
20 {
21         const char *sistr[] = { "", "k", "M", "G", "T", "P", "E" };
22         const char *iecstr[] = { "", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei" };
23         const char **unitprefix;
24         static const char *const unitstr[] = {
25                 [N2S_NONE]      = "",
26                 [N2S_PERSEC]    = "/s",
27                 [N2S_BYTE]      = "B",
28                 [N2S_BIT]       = "bit",
29                 [N2S_BYTEPERSEC]= "B/s",
30                 [N2S_BITPERSEC] = "bit/s"
31         };
32         const unsigned int thousand = pow2 ? 1024 : 1000;
33         unsigned int modulo;
34         int post_index, carry = 0;
35         char tmp[32];
36         char *buf;
37
38         compiletime_assert(sizeof(sistr) == sizeof(iecstr), "unit prefix arrays must be identical sizes");
39         assert(units < FIO_ARRAY_SIZE(unitstr));
40
41         if (pow2)
42                 unitprefix = iecstr;
43         else
44                 unitprefix = sistr;
45
46         for (post_index = 0; base > 1; post_index++)
47                 base /= thousand;
48
49         switch (units) {
50         case N2S_NONE:
51                 break;
52         case N2S_PERSEC:
53                 break;
54         case N2S_BYTE:
55                 break;
56         case N2S_BIT:
57                 num *= 8;
58                 break;
59         case N2S_BYTEPERSEC:
60                 break;
61         case N2S_BITPERSEC:
62                 num *= 8;
63                 break;
64         }
65
66         /*
67          * Divide by K/Ki until string length of num <= maxlen.
68          */
69         modulo = -1U;
70         while (post_index < FIO_ARRAY_SIZE(sistr)) {
71                 sprintf(tmp, "%llu", (unsigned long long) num);
72                 if (strlen(tmp) <= maxlen)
73                         break;
74
75                 modulo = num % thousand;
76                 num /= thousand;
77                 carry = modulo >= thousand / 2;
78                 post_index++;
79         }
80
81         if (post_index >= FIO_ARRAY_SIZE(sistr))
82                 post_index = 0;
83
84         /*
85          * If no modulo, then we're done.
86          */
87         if (modulo == -1U) {
88 done:
89                 if (asprintf(&buf, "%llu%s%s", (unsigned long long) num,
90                              unitprefix[post_index], unitstr[units]) < 0)
91                         buf = NULL;
92                 return buf;
93         }
94
95         /*
96          * If no room for decimals, then we're done.
97          */
98         sprintf(tmp, "%llu", (unsigned long long) num);
99         if ((int)(maxlen - strlen(tmp)) <= 1) {
100                 if (carry)
101                         num++;
102                 goto done;
103         }
104
105         /*
106          * Fill in everything and return the result.
107          */
108         assert(maxlen - strlen(tmp) - 1 > 0);
109         assert(modulo < thousand);
110         sprintf(tmp, "%.*f", (int)(maxlen - strlen(tmp) - 1),
111                 (double)modulo / (double)thousand);
112
113         if (tmp[0] == '1')
114                 num++;
115
116         if (asprintf(&buf, "%llu.%s%s%s", (unsigned long long) num, &tmp[2],
117                      unitprefix[post_index], unitstr[units]) < 0)
118                 buf = NULL;
119         return buf;
120 }