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