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