Branch and cache miss speedups
[fio.git] / lib / num2str.c
CommitLineData
1ec3d69b
JA
1#include <stdlib.h>
2#include <stdio.h>
3#include <string.h>
4
5/*
6 * Cheesy number->string conversion, complete with carry rounding error.
7 */
73798eb2 8char *num2str(unsigned long num, int maxlen, int base, int pow2, int unit_base)
1ec3d69b 9{
73798eb2
SN
10 const char *postfix[] = { "", "K", "M", "G", "P", "E" };
11 const char *byte_postfix[] = { "", "B", "bit" };
12 const unsigned int thousand[] = { 1000, 1024 };
1ec3d69b 13 unsigned int modulo, decimals;
73798eb2 14 int byte_post_index = 0, post_index, carry = 0;
05463816 15 char tmp[32];
1ec3d69b
JA
16 char *buf;
17
18 buf = malloc(128);
19
20 for (post_index = 0; base > 1; post_index++)
21 base /= thousand[!!pow2];
22
73798eb2
SN
23 switch (unit_base) {
24 case 1:
25 byte_post_index = 2;
26 num *= 8;
27 break;
28 case 8:
29 byte_post_index = 1;
30 break;
31 }
32
1ec3d69b
JA
33 modulo = -1U;
34 while (post_index < sizeof(postfix)) {
35 sprintf(tmp, "%lu", num);
36 if (strlen(tmp) <= maxlen)
37 break;
38
39 modulo = num % thousand[!!pow2];
40 num /= thousand[!!pow2];
05463816 41 carry = modulo >= thousand[!!pow2] / 2;
1ec3d69b
JA
42 post_index++;
43 }
44
45 if (modulo == -1U) {
46done:
73798eb2
SN
47 sprintf(buf, "%lu%s%s", num, postfix[post_index],
48 byte_postfix[byte_post_index]);
1ec3d69b
JA
49 return buf;
50 }
51
52 sprintf(tmp, "%lu", num);
53 decimals = maxlen - strlen(tmp);
05463816
JA
54 if (decimals <= 1) {
55 if (carry)
56 num++;
1ec3d69b 57 goto done;
05463816
JA
58 }
59
60 do {
61 sprintf(tmp, "%u", modulo);
62 if (strlen(tmp) <= decimals - 1)
63 break;
64
65 modulo = (modulo + 9) / 10;
66 } while (1);
1ec3d69b 67
73798eb2
SN
68 sprintf(buf, "%lu.%u%s%s", num, modulo, postfix[post_index],
69 byte_postfix[byte_post_index]);
1ec3d69b
JA
70 return buf;
71}