4 #ifdef STORAGE_WITH_MATH
10 #include "storage_number.h"
13 #if __x86_64__ || __ppc64__
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);
23 storage_number pack_storage_number(calculated_number value, uint32_t flags)
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
31 storage_number r = get_storage_number_flags(flags);
35 calculated_number n = value;
37 // if the value is negative
38 // add the sign bit and make it positive
40 r += (1 << 31); // the sign bit 32
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) {
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
57 if(n > (calculated_number)0x00ffffff) {
58 error("Number " CALCULATED_NUMBER_FORMAT " is too big.", value);
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
69 while(m < 7 && n < (calculated_number)0x0019999e) {
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
79 #ifdef STORAGE_WITH_MATH
80 // without this there are rounding problems
81 // example: 0.9 becomes 0.89
82 r += lrint((double) n);
84 r += (storage_number)n;
90 calculated_number unpack_storage_number(storage_number value)
94 int sign = 0, exp = 0;
96 value ^= get_storage_number_flags(value);
98 if(value & (1 << 31)) {
103 if(value & (1 << 30)) {
108 int mul = value >> 27;
111 calculated_number n = value;
113 // fprintf(stderr, "UNPACK: %08X, sign = %d, exp = %d, mul = %d, n = " CALCULATED_NUMBER_FORMAT "\n", value, sign, exp, mul, n);
125 int print_calculated_number(char *str, calculated_number value)
129 int sign = (value < 0) ? 1 : 0;
130 if(sign) value = -value;
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);
137 unsigned long long uvalue = value * (calculated_number)100000;
141 if(uvalue > (unsigned long long)0xffffffff)
142 wstr = print_number_llu_r(str, uvalue);
144 wstr = print_number_lu_r(str, uvalue);
146 do *wstr++ = (char)('0' + (uvalue % 10)); while(uvalue /= 10);
149 // make sure we have 6 bytes at least
150 while((wstr - str) < 6) *wstr++ = '0';
153 if(sign) *wstr++ = '-';
156 char *begin = str, *end = --wstr, aux;
157 while (end > begin) aux = *end, *end-- = *begin, *begin++ = aux;
159 // strreverse(str, wstr);
161 // remove trailing zeros
163 while(decimal > 0 && *wstr == '0') {
168 // terminate it, one position to the right
169 // to let space for a dot
172 // make space for the dot
174 for(i = 0; i < decimal ;i++) {
180 if(wstr[2] == '\0') { wstr[1] = '\0'; decimal--; }
183 // return the buffer length
184 return (int) ((wstr - str) + 2 + decimal );