1 #ifdef STORAGE_WITH_MATH
6 #include "storage_number.h"
9 #if __x86_64__ || __ppc64__
16 storage_number pack_storage_number(calculated_number value)
22 // bit 31 = 0:divide, 1:multiply
23 // bit 30, 29, 28 = (multiplier or divider) 0-7
24 // bit 27 to 25 = reserved for flags
25 // bit 24 to bit 1 = the value
27 storage_number sign = 0, exp = 0, mul;
29 calculated_number n = value;
36 while(m < 7 && n > (calculated_number)0x00ffffff) {
40 while(m > -7 && n < (calculated_number)0x00199999) {
51 if(n > (calculated_number)0x00ffffff) {
52 error("Number " CALCULATED_NUMBER_FORMAT " is too big.", value);
53 n = (calculated_number)0x00ffffff;
58 #ifdef STORAGE_WITH_MATH
59 // without this there are rounding problems
60 // example: 0.9 becomes 0.89
64 r = (sign << 31) + (exp << 30) + (mul << 27) + n;
65 // fprintf(stderr, "PACK: %08X, sign = %d, exp = %d, mul = %d, n = " CALCULATED_NUMBER_FORMAT "\n", r, sign, exp, mul, n);
70 calculated_number unpack_storage_number(storage_number value)
77 if(value & (1 << 31)) {
82 if(value & (1 << 30)) {
87 int mul = value >> 27;
90 calculated_number n = value;
92 // fprintf(stderr, "UNPACK: %08X, sign = %d, exp = %d, mul = %d, n = " CALCULATED_NUMBER_FORMAT "\n", value, sign, exp, mul, n);
105 // This trick seems to give an 80% speed increase in 32bit systems
106 // print_calculated_number_llu_r() will just print the digits up to the
107 // point the remaining value fits in 32 bits, and then calls
108 // print_calculated_number_lu_r() to print the rest with 32 bit arithmetic.
110 static char *print_calculated_number_lu_r(char *str, unsigned long uvalue) {
114 do *wstr++ = (char)(48 + (uvalue % 10)); while(uvalue /= 10);
118 static char *print_calculated_number_llu_r(char *str, unsigned long long uvalue) {
122 do *wstr++ = (char)(48 + (uvalue % 10)); while((uvalue /= 10) && uvalue > (unsigned long long)0xffffffff);
123 if(uvalue) return print_calculated_number_lu_r(wstr, uvalue);
128 int print_calculated_number(char *str, calculated_number value)
132 int sign = (value < 0) ? 1 : 0;
133 if(sign) value = -value;
135 #ifdef STORAGE_WITH_MATH
136 // without llrint() there are rounding problems
137 // for example 0.9 becomes 0.89
138 unsigned long long uvalue = llrint(value * (calculated_number)100000);
140 unsigned long long uvalue = value * (calculated_number)100000;
144 if(uvalue > (unsigned long long)0xffffffff)
145 wstr = print_calculated_number_llu_r(str, uvalue);
147 wstr = print_calculated_number_lu_r(str, uvalue);
149 do *wstr++ = (char)(48 + (uvalue % 10)); while(uvalue /= 10);
152 // make sure we have 6 bytes at least
153 while((wstr - str) < 6) *wstr++ = '0';
156 if(sign) *wstr++ = '-';
159 char *begin = str, *end = --wstr, aux;
160 while (end > begin) aux = *end, *end-- = *begin, *begin++ = aux;
162 // strreverse(str, wstr);
164 // remove trailing zeros
166 while(decimal > 0 && *wstr == '0') {
171 // terminate it, one position to the right
172 // to let space for a dot
175 // make space for the dot
177 for(i = 0; i < decimal ;i++) {
183 if(wstr[2] == '\0') { wstr[1] = '\0'; decimal--; }
186 // return the buffer length
187 return ( (wstr - str) + 2 + decimal );