Improve IEC binary and SI decimal prefix handling
[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
b7e147d1
JA
7#define ARRAY_LENGTH(arr) sizeof(arr) / sizeof((arr)[0])
8
d694a6a7
RE
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>]"
1ec3d69b 17 */
d694a6a7 18char *num2str(uint64_t num, int maxlen, int base, int pow2, int units)
1ec3d69b 19{
d694a6a7
RE
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" };
73798eb2 24 const unsigned int thousand[] = { 1000, 1024 };
1ec3d69b 25 unsigned int modulo, decimals;
d694a6a7 26 int unit_index = 0, post_index, carry = 0;
05463816 27 char tmp[32];
1ec3d69b
JA
28 char *buf;
29
d694a6a7
RE
30 compiletime_assert(sizeof(sistr) == sizeof(iecstr), "unit prefix arrays must be identical sizes");
31
1ec3d69b 32 buf = malloc(128);
d694a6a7
RE
33 if (!buf)
34 return NULL;
35
36 if (pow2)
37 unitprefix = iecstr;
38 else
39 unitprefix = sistr;
1ec3d69b
JA
40
41 for (post_index = 0; base > 1; post_index++)
42 base /= thousand[!!pow2];
43
d694a6a7
RE
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;
73798eb2
SN
53 num *= 8;
54 break;
d694a6a7
RE
55 case N2S_BYTEPERSEC:
56 unit_index = 4;
57 break;
58 case N2S_BITPERSEC:
59 unit_index = 5;
60 num *= 8;
73798eb2
SN
61 break;
62 }
63
1ec3d69b 64 modulo = -1U;
d694a6a7 65 while (post_index < sizeof(sistr)) {
ca09be4b 66 sprintf(tmp, "%llu", (unsigned long long) num);
1ec3d69b
JA
67 if (strlen(tmp) <= maxlen)
68 break;
69
70 modulo = num % thousand[!!pow2];
71 num /= thousand[!!pow2];
05463816 72 carry = modulo >= thousand[!!pow2] / 2;
1ec3d69b
JA
73 post_index++;
74 }
75
76 if (modulo == -1U) {
77done:
d694a6a7 78 if (post_index >= ARRAY_LENGTH(sistr))
b7e147d1
JA
79 post_index = 0;
80
ca09be4b 81 sprintf(buf, "%llu%s%s", (unsigned long long) num,
d694a6a7 82 unitprefix[post_index], unitstr[unit_index]);
1ec3d69b
JA
83 return buf;
84 }
85
ca09be4b 86 sprintf(tmp, "%llu", (unsigned long long) num);
1ec3d69b 87 decimals = maxlen - strlen(tmp);
05463816
JA
88 if (decimals <= 1) {
89 if (carry)
90 num++;
1ec3d69b 91 goto done;
05463816
JA
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);
1ec3d69b 101
ca09be4b 102 sprintf(buf, "%llu.%u%s%s", (unsigned long long) num, modulo,
d694a6a7 103 unitprefix[post_index], unitstr[unit_index]);
1ec3d69b
JA
104 return buf;
105}