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