docs: update for new data placement options
[fio.git] / lib / num2str.c
CommitLineData
3d2d14bc 1#include <assert.h>
1ec3d69b
JA
2#include <stdlib.h>
3#include <stdio.h>
4#include <string.h>
5
5e746820 6#include "../compiler/compiler.h"
38b00241 7#include "../oslib/asprintf.h"
5e746820
TK
8#include "num2str.h"
9
d694a6a7
RE
10/**
11 * num2str() - Cheesy number->string conversion, complete with carry rounding error.
12 * @num: quantity (e.g., number of blocks, bytes or bits)
9e55f2c7 13 * @maxlen: max number of digits in the output string (not counting prefix and units, but counting .)
d694a6a7
RE
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
41a87019 16 * @units: select units - N2S_* constants defined in num2str.h
d694a6a7 17 * @returns a malloc'd buffer containing "number[<unit prefix>][<units>]"
1ec3d69b 18 */
41a87019 19char *num2str(uint64_t num, int maxlen, int base, int pow2, enum n2s_unit units)
1ec3d69b 20{
78c14fab
BVA
21 const char *sistr[] = { "", "k", "M", "G", "T", "P", "E" };
22 const char *iecstr[] = { "", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei" };
d694a6a7 23 const char **unitprefix;
cbb8289d
BVA
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 };
3b6879b2 32 const unsigned int thousand = pow2 ? 1024 : 1000;
9e55f2c7 33 unsigned int modulo;
cbb8289d 34 int post_index, carry = 0;
832a3186 35 char tmp[32];
1ec3d69b
JA
36 char *buf;
37
d694a6a7 38 compiletime_assert(sizeof(sistr) == sizeof(iecstr), "unit prefix arrays must be identical sizes");
59f94d26 39 assert(units < FIO_ARRAY_SIZE(unitstr));
d694a6a7 40
d694a6a7
RE
41 if (pow2)
42 unitprefix = iecstr;
43 else
44 unitprefix = sistr;
1ec3d69b
JA
45
46 for (post_index = 0; base > 1; post_index++)
3b6879b2 47 base /= thousand;
1ec3d69b 48
d694a6a7 49 switch (units) {
41a87019
BVA
50 case N2S_NONE:
51 break;
d694a6a7 52 case N2S_PERSEC:
d694a6a7
RE
53 break;
54 case N2S_BYTE:
d694a6a7
RE
55 break;
56 case N2S_BIT:
73798eb2
SN
57 num *= 8;
58 break;
d694a6a7 59 case N2S_BYTEPERSEC:
d694a6a7
RE
60 break;
61 case N2S_BITPERSEC:
d694a6a7 62 num *= 8;
73798eb2
SN
63 break;
64 }
65
9e55f2c7
TK
66 /*
67 * Divide by K/Ki until string length of num <= maxlen.
68 */
1ec3d69b 69 modulo = -1U;
59f94d26 70 while (post_index < FIO_ARRAY_SIZE(sistr)) {
ca09be4b 71 sprintf(tmp, "%llu", (unsigned long long) num);
1ec3d69b
JA
72 if (strlen(tmp) <= maxlen)
73 break;
74
3b6879b2
BVA
75 modulo = num % thousand;
76 num /= thousand;
77 carry = modulo >= thousand / 2;
1ec3d69b
JA
78 post_index++;
79 }
80
59f94d26 81 if (post_index >= FIO_ARRAY_SIZE(sistr))
fd99605c
BVA
82 post_index = 0;
83
9e55f2c7
TK
84 /*
85 * If no modulo, then we're done.
86 */
1ec3d69b
JA
87 if (modulo == -1U) {
88done:
38b00241
BVA
89 if (asprintf(&buf, "%llu%s%s", (unsigned long long) num,
90 unitprefix[post_index], unitstr[units]) < 0)
91 buf = NULL;
1ec3d69b
JA
92 return buf;
93 }
94
9e55f2c7
TK
95 /*
96 * If no room for decimals, then we're done.
97 */
ca09be4b 98 sprintf(tmp, "%llu", (unsigned long long) num);
9e55f2c7 99 if ((int)(maxlen - strlen(tmp)) <= 1) {
05463816
JA
100 if (carry)
101 num++;
1ec3d69b 102 goto done;
05463816
JA
103 }
104
9e55f2c7
TK
105 /*
106 * Fill in everything and return the result.
107 */
108 assert(maxlen - strlen(tmp) - 1 > 0);
3b6879b2 109 assert(modulo < thousand);
832a3186
BVA
110 sprintf(tmp, "%.*f", (int)(maxlen - strlen(tmp) - 1),
111 (double)modulo / (double)thousand);
1ec3d69b 112
c736b361 113 if (tmp[0] == '1')
114 num++;
115
38b00241
BVA
116 if (asprintf(&buf, "%llu.%s%s%s", (unsigned long long) num, &tmp[2],
117 unitprefix[post_index], unitstr[units]) < 0)
118 buf = NULL;
1ec3d69b
JA
119 return buf;
120}