1 #ifdef STORAGE_WITH_MATH
7 #include "storage_number.h"
10 #if __x86_64__ || __ppc64__
17 storage_number pack_storage_number(calculated_number value, uint32_t flags)
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
25 storage_number r = get_storage_number_flags(flags);
29 calculated_number n = value;
31 // if the value is negative
32 // add the sign bit and make it positive
34 r += (1 << 31); // the sign bit 32
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) {
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
51 if(n > (calculated_number)0x00ffffff) {
52 error("Number " CALCULATED_NUMBER_FORMAT " is too big.", value);
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
63 while(m < 7 && n < (calculated_number)0x0019999e) {
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
73 #ifdef STORAGE_WITH_MATH
74 // without this there are rounding problems
75 // example: 0.9 becomes 0.89
78 r += (storage_number)n;
84 calculated_number unpack_storage_number(storage_number value)
88 int sign = 0, exp = 0;
90 value ^= get_storage_number_flags(value);
92 if(value & (1 << 31)) {
97 if(value & (1 << 30)) {
102 int mul = value >> 27;
105 calculated_number n = value;
107 // fprintf(stderr, "UNPACK: %08X, sign = %d, exp = %d, mul = %d, n = " CALCULATED_NUMBER_FORMAT "\n", value, sign, exp, mul, n);
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.
125 static char *print_calculated_number_lu_r(char *str, unsigned long uvalue) {
129 do *wstr++ = (char)(48 + (uvalue % 10)); while(uvalue /= 10);
133 static char *print_calculated_number_llu_r(char *str, unsigned long long uvalue) {
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);
143 int print_calculated_number(char *str, calculated_number value)
147 int sign = (value < 0) ? 1 : 0;
148 if(sign) value = -value;
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);
155 unsigned long long uvalue = value * (calculated_number)100000;
159 if(uvalue > (unsigned long long)0xffffffff)
160 wstr = print_calculated_number_llu_r(str, uvalue);
162 wstr = print_calculated_number_lu_r(str, uvalue);
164 do *wstr++ = (char)(48 + (uvalue % 10)); while(uvalue /= 10);
167 // make sure we have 6 bytes at least
168 while((wstr - str) < 6) *wstr++ = '0';
171 if(sign) *wstr++ = '-';
174 char *begin = str, *end = --wstr, aux;
175 while (end > begin) aux = *end, *end-- = *begin, *begin++ = aux;
177 // strreverse(str, wstr);
179 // remove trailing zeros
181 while(decimal > 0 && *wstr == '0') {
186 // terminate it, one position to the right
187 // to let space for a dot
190 // make space for the dot
192 for(i = 0; i < decimal ;i++) {
198 if(wstr[2] == '\0') { wstr[1] = '\0'; decimal--; }
201 // return the buffer length
202 return ( (wstr - str) + 2 + decimal );