]> arthur.barton.de Git - netdata.git/blob - src/storage_number.c
e37512991b41a0dbb63465edc1908ca4b04bec6a
[netdata.git] / src / storage_number.c
1 #ifdef STORAGE_WITH_MATH
2 #include <math.h>
3 #endif
4
5 #include "common.h"
6 #include "log.h"
7 #include "storage_number.h"
8
9 #if __GNUC__
10 #if __x86_64__ || __ppc64__
11 #define ENVIRONMENT64
12 #else
13 #define ENVIRONMENT32
14 #endif
15 #endif
16
17 storage_number pack_storage_number(calculated_number value, uint32_t flags)
18 {
19         // bit 32 = sign 0:positive, 1:negative
20         // bit 31 = 0:divide, 1:multiply
21         // bit 30, 29, 28 = (multiplier or divider) 0-6 (7 total)
22         // bit 27, 26, 25 flags
23         // bit 24 to bit 1 = the value
24
25         storage_number r = get_storage_number_flags(flags);
26         if(!value) return r;
27
28         int m = 0;
29         calculated_number n = value;
30
31         // if the value is negative
32         // add the sign bit and make it positive
33         if(n < 0) {
34                 r += (1 << 31); // the sign bit 32
35                 n = -n;
36         }
37
38         // make its integer part fit in 0x00ffffff
39         // by dividing it by 10 up to 7 times
40         // and increasing the multiplier
41         while(m < 7 && n > (calculated_number)0x00ffffff) {
42                 n /= 10;
43                 m++;
44         }
45
46         if(m) {
47                 // the value was too big and we divided it
48                 // so we add a multiplier to unpack it
49                 r += (1 << 30) + (m << 27); // the multiplier m
50
51                 if(n > (calculated_number)0x00ffffff) {
52                         error("Number " CALCULATED_NUMBER_FORMAT " is too big.", value);
53                         r += 0x00ffffff;
54                         return r;
55                 }
56         }
57         else {
58                 // 0x0019999e is the number that can be multiplied
59                 // by 10 to give 0x00ffffff
60                 // while the value is below 0x0019999e we can
61                 // multiply it by 10, up to 7 times, increasing
62                 // the multiplier
63                 while(m < 7 && n < (calculated_number)0x0019999e) {
64                         n *= 10;
65                         m++;
66                 }
67
68                 // the value was small enough and we multiplied it
69                 // so we add a divider to unpack it
70                 r += (0 << 30) + (m << 27); // the divider m
71         }
72
73 #ifdef STORAGE_WITH_MATH
74         // without this there are rounding problems
75         // example: 0.9 becomes 0.89
76         r += lrint(n);
77 #else
78         r += (storage_number)n;
79 #endif
80
81         return r;
82 }
83
84 calculated_number unpack_storage_number(storage_number value)
85 {
86         if(!value) return 0;
87
88         int sign = 0, exp = 0;
89
90         value ^= get_storage_number_flags(value);
91
92         if(value & (1 << 31)) {
93                 sign = 1;
94                 value ^= 1 << 31;
95         }
96
97         if(value & (1 << 30)) {
98                 exp = 1;
99                 value ^= 1 << 30;
100         }
101
102         int mul = value >> 27;
103         value ^= mul << 27;
104
105         calculated_number n = value;
106
107         // fprintf(stderr, "UNPACK: %08X, sign = %d, exp = %d, mul = %d, n = " CALCULATED_NUMBER_FORMAT "\n", value, sign, exp, mul, n);
108
109         while(mul > 0) {
110                 if(exp) n *= 10;
111                 else n /= 10;
112                 mul--;
113         }
114
115         if(sign) n = -n;
116         return n;
117 }
118
119 #ifdef ENVIRONMENT32
120 // This trick seems to give an 80% speed increase in 32bit systems
121 // print_calculated_number_llu_r() will just print the digits up to the
122 // point the remaining value fits in 32 bits, and then calls
123 // print_calculated_number_lu_r() to print the rest with 32 bit arithmetic.
124
125 static char *print_calculated_number_lu_r(char *str, unsigned long uvalue) {
126         char *wstr = str;
127         
128         // print each digit
129         do *wstr++ = (char)(48 + (uvalue % 10)); while(uvalue /= 10);
130         return wstr;
131 }
132
133 static char *print_calculated_number_llu_r(char *str, unsigned long long uvalue) {
134         char *wstr = str;
135
136         // print each digit
137         do *wstr++ = (char)(48 + (uvalue % 10)); while((uvalue /= 10) && uvalue > (unsigned long long)0xffffffff);
138         if(uvalue) return print_calculated_number_lu_r(wstr, uvalue);
139         return wstr;
140 }
141 #endif
142
143 int print_calculated_number(char *str, calculated_number value)
144 {
145         char *wstr = str;
146
147         int sign = (value < 0) ? 1 : 0;
148         if(sign) value = -value;
149
150 #ifdef STORAGE_WITH_MATH
151         // without llrint() there are rounding problems
152         // for example 0.9 becomes 0.89
153         unsigned long long uvalue = llrint(value * (calculated_number)100000);
154 #else
155         unsigned long long uvalue = value * (calculated_number)100000;
156 #endif
157
158 #ifdef ENVIRONMENT32
159         if(uvalue > (unsigned long long)0xffffffff)
160                 wstr = print_calculated_number_llu_r(str, uvalue);
161         else
162                 wstr = print_calculated_number_lu_r(str, uvalue);
163 #else
164         do *wstr++ = (char)(48 + (uvalue % 10)); while(uvalue /= 10);
165 #endif
166
167         // make sure we have 6 bytes at least
168         while((wstr - str) < 6) *wstr++ = '0';
169
170         // put the sign back
171         if(sign) *wstr++ = '-';
172
173         // reverse it
174     char *begin = str, *end = --wstr, aux;
175     while (end > begin) aux = *end, *end-- = *begin, *begin++ = aux;
176         // wstr--;
177         // strreverse(str, wstr);
178
179         // remove trailing zeros
180         int decimal = 5;
181         while(decimal > 0 && *wstr == '0') {
182                 *wstr-- = '\0';
183                 decimal--;
184         }
185
186         // terminate it, one position to the right
187         // to let space for a dot
188         wstr[2] = '\0';
189
190         // make space for the dot
191         int i;
192         for(i = 0; i < decimal ;i++) {
193                 wstr[1] = wstr[0];
194                 wstr--;
195         }
196
197         // put the dot
198         if(wstr[2] == '\0') { wstr[1] = '\0'; decimal--; }
199         else wstr[1] = '.';
200
201         // return the buffer length
202         return ( (wstr - str) + 2 + decimal );
203 }