Merge lp:~gthorslund/drizzle/replace-macro-functions into lp:drizzle/7.0
- replace-macro-functions
- Merge into 7.0
Proposed by
Gustaf Thorslund
Status: | Merged |
---|---|
Approved by: | Brian Aker |
Approved revision: | no longer in the source branch. |
Merged at revision: | 1892 |
Proposed branch: | lp:~gthorslund/drizzle/replace-macro-functions |
Merge into: | lp:drizzle/7.0 |
Diff against target: |
652 lines (+157/-157) 2 files modified
drizzled/decimal.cc (+138/-147) drizzled/decimal.h (+19/-10) |
To merge this branch: | bzr merge lp:~gthorslund/drizzle/replace-macro-functions |
Related bugs: | |
Related blueprints: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Drizzle Developers | Pending | ||
Review via email: mp+39656@code.launchpad.net |
Commit message
Description of the change
Replaced macros with functions/
https:/
Files done:
drizzled/
drizzled/
To post a comment you must log in.
- 1892. By Lee Bieber
-
Merge Gustaf- Replaced macros with functions/templates
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'drizzled/decimal.cc' | |||
2 | --- drizzled/decimal.cc 2010-10-20 20:25:52 +0000 | |||
3 | +++ drizzled/decimal.cc 2010-10-29 19:47:53 +0000 | |||
4 | @@ -363,7 +363,13 @@ | |||
5 | 363 | #define DIG_MASK 100000000 | 363 | #define DIG_MASK 100000000 |
6 | 364 | #define DIG_BASE 1000000000 | 364 | #define DIG_BASE 1000000000 |
7 | 365 | #define DIG_MAX (DIG_BASE-1) | 365 | #define DIG_MAX (DIG_BASE-1) |
9 | 366 | #define ROUND_UP(X) (((X)+DIG_PER_DEC1-1)/DIG_PER_DEC1) | 366 | |
10 | 367 | template<typename T> | ||
11 | 368 | inline static T round_up(const T &x) | ||
12 | 369 | { | ||
13 | 370 | return (x+DIG_PER_DEC1-1)/DIG_PER_DEC1; | ||
14 | 371 | } | ||
15 | 372 | |||
16 | 367 | static const dec1 powers10[DIG_PER_DEC1+1]={ | 373 | static const dec1 powers10[DIG_PER_DEC1+1]={ |
17 | 368 | 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000}; | 374 | 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000}; |
18 | 369 | static const int dig2bytes[DIG_PER_DEC1+1]={0, 1, 1, 2, 2, 3, 3, 4, 4, 4}; | 375 | static const int dig2bytes[DIG_PER_DEC1+1]={0, 1, 1, 2, 2, 3, 3, 4, 4, 4}; |
19 | @@ -379,86 +385,71 @@ | |||
20 | 379 | (d)->buf[(d)->len-1] | 1)) | 385 | (d)->buf[(d)->len-1] | 1)) |
21 | 380 | #endif | 386 | #endif |
22 | 381 | 387 | ||
103 | 382 | #define FIX_INTG_FRAC_ERROR(len, intg1, frac1, error) \ | 388 | inline static void fix_intg_frac_error(const int len, int &intg1, int &frac1, int &error) |
104 | 383 | do \ | 389 | { |
105 | 384 | { \ | 390 | if (unlikely(intg1+frac1 > len)) |
106 | 385 | if (unlikely(intg1+frac1 > (len))) \ | 391 | { |
107 | 386 | { \ | 392 | if (unlikely(intg1 > len)) |
108 | 387 | if (unlikely(intg1 > (len))) \ | 393 | { |
109 | 388 | { \ | 394 | intg1=(len); |
110 | 389 | intg1=(len); \ | 395 | frac1=0; |
111 | 390 | frac1=0; \ | 396 | error=E_DEC_OVERFLOW; |
112 | 391 | error=E_DEC_OVERFLOW; \ | 397 | } |
113 | 392 | } \ | 398 | else |
114 | 393 | else \ | 399 | { |
115 | 394 | { \ | 400 | frac1=(len)-intg1; |
116 | 395 | frac1=(len)-intg1; \ | 401 | error=E_DEC_TRUNCATED; |
117 | 396 | error=E_DEC_TRUNCATED; \ | 402 | } |
118 | 397 | } \ | 403 | } |
119 | 398 | } \ | 404 | else |
120 | 399 | else \ | 405 | error=E_DEC_OK; |
121 | 400 | error=E_DEC_OK; \ | 406 | } |
122 | 401 | } while(0) | 407 | |
123 | 402 | 408 | /* assume carry <= 1 */ | |
124 | 403 | #define ADD(to, from1, from2, carry) /* assume carry <= 1 */ \ | 409 | inline static void add(dec1 &to, const dec1 &from1, const dec1& from2, dec1 &carry) |
125 | 404 | do \ | 410 | { |
126 | 405 | { \ | 411 | dec1 a=from1+from2+carry; |
127 | 406 | dec1 a=(from1)+(from2)+(carry); \ | 412 | assert(carry <= 1); |
128 | 407 | assert((carry) <= 1); \ | 413 | if ((carry= (a >= DIG_BASE))) /* no division here! */ |
129 | 408 | if (((carry)= a >= DIG_BASE)) /* no division here! */ \ | 414 | a-=DIG_BASE; |
130 | 409 | a-=DIG_BASE; \ | 415 | to=a; |
131 | 410 | (to)=a; \ | 416 | } |
132 | 411 | } while(0) | 417 | |
133 | 412 | 418 | inline static void add2(dec1 &to, const dec1 &from1, const dec1 &from2, dec1 &carry) | |
134 | 413 | #define ADD2(to, from1, from2, carry) \ | 419 | { |
135 | 414 | do \ | 420 | dec2 a=dec2(from1)+from2+carry; |
136 | 415 | { \ | 421 | if ((carry= (a >= DIG_BASE))) |
137 | 416 | dec2 a=((dec2)(from1))+(from2)+(carry); \ | 422 | a-=DIG_BASE; |
138 | 417 | if (((carry)= a >= DIG_BASE)) \ | 423 | if (unlikely(a >= DIG_BASE)) |
139 | 418 | a-=DIG_BASE; \ | 424 | { |
140 | 419 | if (unlikely(a >= DIG_BASE)) \ | 425 | a-=DIG_BASE; |
141 | 420 | { \ | 426 | carry++; |
142 | 421 | a-=DIG_BASE; \ | 427 | } |
143 | 422 | carry++; \ | 428 | to=dec1(a); |
144 | 423 | } \ | 429 | } |
145 | 424 | (to)=(dec1) a; \ | 430 | |
146 | 425 | } while(0) | 431 | /* to=from1-from2 */ |
147 | 426 | 432 | inline static void sub(dec1 &to, const dec1 &from1, const dec1 &from2, dec1 &carry) | |
148 | 427 | #define SUB(to, from1, from2, carry) /* to=from1-from2 */ \ | 433 | { |
149 | 428 | do \ | 434 | dec1 a=from1-from2-carry; |
150 | 429 | { \ | 435 | if ((carry= (a < 0))) |
151 | 430 | dec1 a=(from1)-(from2)-(carry); \ | 436 | a+=DIG_BASE; |
152 | 431 | if (((carry)= a < 0)) \ | 437 | to=a; |
153 | 432 | a+=DIG_BASE; \ | 438 | } |
154 | 433 | (to)=a; \ | 439 | |
155 | 434 | } while(0) | 440 | /* to=from1-from2 */ |
156 | 435 | 441 | inline static void sub2(dec1 &to, const dec1 &from1, const dec1 &from2, dec1 &carry) | |
157 | 436 | #define SUB2(to, from1, from2, carry) /* to=from1-from2 */ \ | 442 | { |
158 | 437 | do \ | 443 | dec1 a=from1-from2-carry; |
159 | 438 | { \ | 444 | if ((carry= (a < 0))) |
160 | 439 | dec1 a=(from1)-(from2)-(carry); \ | 445 | a+=DIG_BASE; |
161 | 440 | if (((carry)= a < 0)) \ | 446 | if (unlikely(a < 0)) |
162 | 441 | a+=DIG_BASE; \ | 447 | { |
163 | 442 | if (unlikely(a < 0)) \ | 448 | a+=DIG_BASE; |
164 | 443 | { \ | 449 | carry++; |
165 | 444 | a+=DIG_BASE; \ | 450 | } |
166 | 445 | carry++; \ | 451 | to=a; |
167 | 446 | } \ | 452 | } |
88 | 447 | (to)=a; \ | ||
89 | 448 | } while(0) | ||
90 | 449 | |||
91 | 450 | /** | ||
92 | 451 | Swap the contents of two variables. | ||
93 | 452 | */ | ||
94 | 453 | #define swap_variables(TYPE, a, b) \ | ||
95 | 454 | do { \ | ||
96 | 455 | TYPE dummy; \ | ||
97 | 456 | dummy= a; \ | ||
98 | 457 | a= b; \ | ||
99 | 458 | b= dummy; \ | ||
100 | 459 | } while (0) | ||
101 | 460 | |||
102 | 461 | |||
168 | 462 | 453 | ||
169 | 463 | /** | 454 | /** |
170 | 464 | @brief Get maximum value for given precision and scale | 455 | @brief Get maximum value for given precision and scale |
171 | @@ -527,7 +518,7 @@ | |||
172 | 527 | int decimal_actual_fraction(decimal_t *from) | 518 | int decimal_actual_fraction(decimal_t *from) |
173 | 528 | { | 519 | { |
174 | 529 | int frac= from->frac, i; | 520 | int frac= from->frac, i; |
176 | 530 | dec1 *buf0= from->buf + ROUND_UP(from->intg) + ROUND_UP(frac) - 1; | 521 | dec1 *buf0= from->buf + round_up(from->intg) + round_up(frac) - 1; |
177 | 531 | 522 | ||
178 | 532 | if (frac == 0) | 523 | if (frac == 0) |
179 | 533 | return 0; | 524 | return 0; |
180 | @@ -632,7 +623,7 @@ | |||
181 | 632 | { | 623 | { |
182 | 633 | char *s1= s + intg_len; | 624 | char *s1= s + intg_len; |
183 | 634 | fill= frac_len - frac; | 625 | fill= frac_len - frac; |
185 | 635 | buf=buf0+ROUND_UP(intg); | 626 | buf=buf0+round_up(intg); |
186 | 636 | *s1++='.'; | 627 | *s1++='.'; |
187 | 637 | for (; frac>0; frac-=DIG_PER_DEC1) | 628 | for (; frac>0; frac-=DIG_PER_DEC1) |
188 | 638 | { | 629 | { |
189 | @@ -657,7 +648,7 @@ | |||
190 | 657 | if (intg) | 648 | if (intg) |
191 | 658 | { | 649 | { |
192 | 659 | s+=intg; | 650 | s+=intg; |
194 | 660 | for (buf=buf0+ROUND_UP(intg); intg>0; intg-=DIG_PER_DEC1) | 651 | for (buf=buf0+round_up(intg); intg>0; intg-=DIG_PER_DEC1) |
195 | 661 | { | 652 | { |
196 | 662 | dec1 x=*--buf; | 653 | dec1 x=*--buf; |
197 | 663 | for (i=min(intg, DIG_PER_DEC1); i; i--) | 654 | for (i=min(intg, DIG_PER_DEC1); i; i--) |
198 | @@ -687,7 +678,7 @@ | |||
199 | 687 | { | 678 | { |
200 | 688 | int start, stop, i; | 679 | int start, stop, i; |
201 | 689 | dec1 *buf_beg= from->buf; | 680 | dec1 *buf_beg= from->buf; |
203 | 690 | dec1 *end= from->buf + ROUND_UP(from->intg) + ROUND_UP(from->frac); | 681 | dec1 *end= from->buf + round_up(from->intg) + round_up(from->frac); |
204 | 691 | dec1 *buf_end= end - 1; | 682 | dec1 *buf_end= end - 1; |
205 | 692 | 683 | ||
206 | 693 | /* find non-zero digit from number begining */ | 684 | /* find non-zero digit from number begining */ |
207 | @@ -753,8 +744,8 @@ | |||
208 | 753 | */ | 744 | */ |
209 | 754 | static void do_mini_left_shift(decimal_t *dec, int shift, int beg, int last) | 745 | static void do_mini_left_shift(decimal_t *dec, int shift, int beg, int last) |
210 | 755 | { | 746 | { |
213 | 756 | dec1 *from= dec->buf + ROUND_UP(beg + 1) - 1; | 747 | dec1 *from= dec->buf + round_up(beg + 1) - 1; |
214 | 757 | dec1 *end= dec->buf + ROUND_UP(last) - 1; | 748 | dec1 *end= dec->buf + round_up(last) - 1; |
215 | 758 | int c_shift= DIG_PER_DEC1 - shift; | 749 | int c_shift= DIG_PER_DEC1 - shift; |
216 | 759 | assert(from >= dec->buf); | 750 | assert(from >= dec->buf); |
217 | 760 | assert(end < dec->buf + dec->len); | 751 | assert(end < dec->buf + dec->len); |
218 | @@ -781,8 +772,8 @@ | |||
219 | 781 | */ | 772 | */ |
220 | 782 | static void do_mini_right_shift(decimal_t *dec, int shift, int beg, int last) | 773 | static void do_mini_right_shift(decimal_t *dec, int shift, int beg, int last) |
221 | 783 | { | 774 | { |
224 | 784 | dec1 *from= dec->buf + ROUND_UP(last) - 1; | 775 | dec1 *from= dec->buf + round_up(last) - 1; |
225 | 785 | dec1 *end= dec->buf + ROUND_UP(beg + 1) - 1; | 776 | dec1 *end= dec->buf + round_up(beg + 1) - 1; |
226 | 786 | int c_shift= DIG_PER_DEC1 - shift; | 777 | int c_shift= DIG_PER_DEC1 - shift; |
227 | 787 | assert(from < dec->buf + dec->len); | 778 | assert(from < dec->buf + dec->len); |
228 | 788 | assert(end >= dec->buf); | 779 | assert(end >= dec->buf); |
229 | @@ -818,7 +809,7 @@ | |||
230 | 818 | /* index of position after last decimal digit */ | 809 | /* index of position after last decimal digit */ |
231 | 819 | int end; | 810 | int end; |
232 | 820 | /* index of digit position just after point */ | 811 | /* index of digit position just after point */ |
234 | 821 | int point= ROUND_UP(dec->intg) * DIG_PER_DEC1; | 812 | int point= round_up(dec->intg) * DIG_PER_DEC1; |
235 | 822 | /* new point position */ | 813 | /* new point position */ |
236 | 823 | int new_point= point + shift; | 814 | int new_point= point + shift; |
237 | 824 | /* number of digits in result */ | 815 | /* number of digits in result */ |
238 | @@ -845,7 +836,7 @@ | |||
239 | 845 | digits_frac= end - new_point; | 836 | digits_frac= end - new_point; |
240 | 846 | set_if_bigger(digits_frac, 0); | 837 | set_if_bigger(digits_frac, 0); |
241 | 847 | 838 | ||
243 | 848 | if ((new_len= ROUND_UP(digits_int) + (new_frac_len= ROUND_UP(digits_frac))) > | 839 | if ((new_len= round_up(digits_int) + (new_frac_len= round_up(digits_frac))) > |
244 | 849 | dec->len) | 840 | dec->len) |
245 | 850 | { | 841 | { |
246 | 851 | int lack= new_len - dec->len; | 842 | int lack= new_len - dec->len; |
247 | @@ -938,8 +929,8 @@ | |||
248 | 938 | { | 929 | { |
249 | 939 | /* move left */ | 930 | /* move left */ |
250 | 940 | d_shift= new_front / DIG_PER_DEC1; | 931 | d_shift= new_front / DIG_PER_DEC1; |
253 | 941 | to= dec->buf + (ROUND_UP(beg + 1) - 1 - d_shift); | 932 | to= dec->buf + (round_up(beg + 1) - 1 - d_shift); |
254 | 942 | barier= dec->buf + (ROUND_UP(end) - 1 - d_shift); | 933 | barier= dec->buf + (round_up(end) - 1 - d_shift); |
255 | 943 | assert(to >= dec->buf); | 934 | assert(to >= dec->buf); |
256 | 944 | assert(barier + d_shift < dec->buf + dec->len); | 935 | assert(barier + d_shift < dec->buf + dec->len); |
257 | 945 | for(; to <= barier; to++) | 936 | for(; to <= barier; to++) |
258 | @@ -952,8 +943,8 @@ | |||
259 | 952 | { | 943 | { |
260 | 953 | /* move right */ | 944 | /* move right */ |
261 | 954 | d_shift= (1 - new_front) / DIG_PER_DEC1; | 945 | d_shift= (1 - new_front) / DIG_PER_DEC1; |
264 | 955 | to= dec->buf + ROUND_UP(end) - 1 + d_shift; | 946 | to= dec->buf + round_up(end) - 1 + d_shift; |
265 | 956 | barier= dec->buf + ROUND_UP(beg + 1) - 1 + d_shift; | 947 | barier= dec->buf + round_up(beg + 1) - 1 + d_shift; |
266 | 957 | assert(to < dec->buf + dec->len); | 948 | assert(to < dec->buf + dec->len); |
267 | 958 | assert(barier - d_shift >= dec->buf); | 949 | assert(barier - d_shift >= dec->buf); |
268 | 959 | for(; to >= barier; to--) | 950 | for(; to >= barier; to--) |
269 | @@ -972,13 +963,13 @@ | |||
270 | 972 | 963 | ||
271 | 973 | Only one of following 'for' loops will work becouse beg <= end | 964 | Only one of following 'for' loops will work becouse beg <= end |
272 | 974 | */ | 965 | */ |
275 | 975 | beg= ROUND_UP(beg + 1) - 1; | 966 | beg= round_up(beg + 1) - 1; |
276 | 976 | end= ROUND_UP(end) - 1; | 967 | end= round_up(end) - 1; |
277 | 977 | assert(new_point >= 0); | 968 | assert(new_point >= 0); |
278 | 978 | 969 | ||
279 | 979 | /* We don't want negative new_point below */ | 970 | /* We don't want negative new_point below */ |
280 | 980 | if (new_point != 0) | 971 | if (new_point != 0) |
282 | 981 | new_point= ROUND_UP(new_point) - 1; | 972 | new_point= round_up(new_point) - 1; |
283 | 982 | 973 | ||
284 | 983 | if (new_point > end) | 974 | if (new_point > end) |
285 | 984 | { | 975 | { |
286 | @@ -1073,8 +1064,8 @@ | |||
287 | 1073 | error=E_DEC_OVERFLOW; | 1064 | error=E_DEC_OVERFLOW; |
288 | 1074 | intg=to->intg; | 1065 | intg=to->intg; |
289 | 1075 | } | 1066 | } |
292 | 1076 | intg1=ROUND_UP(intg); | 1067 | intg1=round_up(intg); |
293 | 1077 | frac1=ROUND_UP(frac); | 1068 | frac1=round_up(frac); |
294 | 1078 | if (intg1+frac1 > to->len) | 1069 | if (intg1+frac1 > to->len) |
295 | 1079 | { | 1070 | { |
296 | 1080 | error= E_DEC_OOM; | 1071 | error= E_DEC_OOM; |
297 | @@ -1083,9 +1074,9 @@ | |||
298 | 1083 | } | 1074 | } |
299 | 1084 | else | 1075 | else |
300 | 1085 | { | 1076 | { |
304 | 1086 | intg1=ROUND_UP(intg); | 1077 | intg1=round_up(intg); |
305 | 1087 | frac1=ROUND_UP(frac); | 1078 | frac1=round_up(frac); |
306 | 1088 | FIX_INTG_FRAC_ERROR(to->len, intg1, frac1, error); | 1079 | fix_intg_frac_error(to->len, intg1, frac1, error); |
307 | 1089 | if (unlikely(error)) | 1080 | if (unlikely(error)) |
308 | 1090 | { | 1081 | { |
309 | 1091 | frac=frac1*DIG_PER_DEC1; | 1082 | frac=frac1*DIG_PER_DEC1; |
310 | @@ -1549,7 +1540,7 @@ | |||
311 | 1549 | d_copy[0]^= 0x80; | 1540 | d_copy[0]^= 0x80; |
312 | 1550 | from= d_copy; | 1541 | from= d_copy; |
313 | 1551 | 1542 | ||
315 | 1552 | FIX_INTG_FRAC_ERROR(to->len, intg1, frac1, error); | 1543 | fix_intg_frac_error(to->len, intg1, frac1, error); |
316 | 1553 | if (unlikely(error)) | 1544 | if (unlikely(error)) |
317 | 1554 | { | 1545 | { |
318 | 1555 | if (intg1 < intg0+(intg0x>0)) | 1546 | if (intg1 < intg0+(intg0x>0)) |
319 | @@ -1669,10 +1660,10 @@ | |||
320 | 1669 | decimal_round(const decimal_t *from, decimal_t *to, int scale, | 1660 | decimal_round(const decimal_t *from, decimal_t *to, int scale, |
321 | 1670 | decimal_round_mode mode) | 1661 | decimal_round_mode mode) |
322 | 1671 | { | 1662 | { |
327 | 1672 | int frac0=scale>0 ? ROUND_UP(scale) : scale/DIG_PER_DEC1, | 1663 | int frac0=scale>0 ? round_up(scale) : scale/DIG_PER_DEC1, |
328 | 1673 | frac1=ROUND_UP(from->frac), round_digit= 0, | 1664 | frac1=round_up(from->frac), round_digit= 0, |
329 | 1674 | intg0=ROUND_UP(from->intg), error=E_DEC_OK, len=to->len, | 1665 | intg0=round_up(from->intg), error=E_DEC_OK, len=to->len, |
330 | 1675 | intg1=ROUND_UP(from->intg + | 1666 | intg1=round_up(from->intg + |
331 | 1676 | (((intg0 + frac0)>0) && (from->buf[0] == DIG_MAX))); | 1667 | (((intg0 + frac0)>0) && (from->buf[0] == DIG_MAX))); |
332 | 1677 | dec1 *buf0=from->buf, *buf1=to->buf, x, y, carry=0; | 1668 | dec1 *buf0=from->buf, *buf1=to->buf, x, y, carry=0; |
333 | 1678 | int first_dig; | 1669 | int first_dig; |
334 | @@ -1807,7 +1798,7 @@ | |||
335 | 1807 | carry=1; | 1798 | carry=1; |
336 | 1808 | *buf1-=DIG_BASE; | 1799 | *buf1-=DIG_BASE; |
337 | 1809 | while (carry && --buf1 >= to->buf) | 1800 | while (carry && --buf1 >= to->buf) |
339 | 1810 | ADD(*buf1, *buf1, 0, carry); | 1801 | add(*buf1, *buf1, 0, carry); |
340 | 1811 | if (unlikely(carry)) | 1802 | if (unlikely(carry)) |
341 | 1812 | { | 1803 | { |
342 | 1813 | /* shifting the number to create space for new digit */ | 1804 | /* shifting the number to create space for new digit */ |
343 | @@ -1860,8 +1851,8 @@ | |||
344 | 1860 | 1851 | ||
345 | 1861 | static int do_add(const decimal_t *from1, const decimal_t *from2, decimal_t *to) | 1852 | static int do_add(const decimal_t *from1, const decimal_t *from2, decimal_t *to) |
346 | 1862 | { | 1853 | { |
349 | 1863 | int intg1=ROUND_UP(from1->intg), intg2=ROUND_UP(from2->intg), | 1854 | int intg1=round_up(from1->intg), intg2=round_up(from2->intg), |
350 | 1864 | frac1=ROUND_UP(from1->frac), frac2=ROUND_UP(from2->frac), | 1855 | frac1=round_up(from1->frac), frac2=round_up(from2->frac), |
351 | 1865 | frac0=max(frac1, frac2), intg0=max(intg1, intg2), error; | 1856 | frac0=max(frac1, frac2), intg0=max(intg1, intg2), error; |
352 | 1866 | dec1 *buf1, *buf2, *buf0, *stop, *stop2, x, carry; | 1857 | dec1 *buf1, *buf2, *buf0, *stop, *stop2, x, carry; |
353 | 1867 | 1858 | ||
354 | @@ -1877,7 +1868,7 @@ | |||
355 | 1877 | to->buf[0]=0; /* safety */ | 1868 | to->buf[0]=0; /* safety */ |
356 | 1878 | } | 1869 | } |
357 | 1879 | 1870 | ||
359 | 1880 | FIX_INTG_FRAC_ERROR(to->len, intg0, frac0, error); | 1871 | fix_intg_frac_error(to->len, intg0, frac0, error); |
360 | 1881 | if (unlikely(error == E_DEC_OVERFLOW)) | 1872 | if (unlikely(error == E_DEC_OVERFLOW)) |
361 | 1882 | { | 1873 | { |
362 | 1883 | max_decimal(to->len * DIG_PER_DEC1, 0, to); | 1874 | max_decimal(to->len * DIG_PER_DEC1, 0, to); |
363 | @@ -1920,7 +1911,7 @@ | |||
364 | 1920 | carry=0; | 1911 | carry=0; |
365 | 1921 | while (buf1 > stop2) | 1912 | while (buf1 > stop2) |
366 | 1922 | { | 1913 | { |
368 | 1923 | ADD(*--buf0, *--buf1, *--buf2, carry); | 1914 | add(*--buf0, *--buf1, *--buf2, carry); |
369 | 1924 | } | 1915 | } |
370 | 1925 | 1916 | ||
371 | 1926 | /* part 3 - cmin(intg) ... cmax(intg) */ | 1917 | /* part 3 - cmin(intg) ... cmax(intg) */ |
372 | @@ -1928,7 +1919,7 @@ | |||
373 | 1928 | ((stop=from2->buf)+intg2-intg1) ; | 1919 | ((stop=from2->buf)+intg2-intg1) ; |
374 | 1929 | while (buf1 > stop) | 1920 | while (buf1 > stop) |
375 | 1930 | { | 1921 | { |
377 | 1931 | ADD(*--buf0, *--buf1, 0, carry); | 1922 | add(*--buf0, *--buf1, 0, carry); |
378 | 1932 | } | 1923 | } |
379 | 1933 | 1924 | ||
380 | 1934 | if (unlikely(carry)) | 1925 | if (unlikely(carry)) |
381 | @@ -1942,8 +1933,8 @@ | |||
382 | 1942 | if to==0, return -1/0/+1 - the result of the comparison */ | 1933 | if to==0, return -1/0/+1 - the result of the comparison */ |
383 | 1943 | static int do_sub(const decimal_t *from1, const decimal_t *from2, decimal_t *to) | 1934 | static int do_sub(const decimal_t *from1, const decimal_t *from2, decimal_t *to) |
384 | 1944 | { | 1935 | { |
387 | 1945 | int intg1=ROUND_UP(from1->intg), intg2=ROUND_UP(from2->intg), | 1936 | int intg1=round_up(from1->intg), intg2=round_up(from2->intg), |
388 | 1946 | frac1=ROUND_UP(from1->frac), frac2=ROUND_UP(from2->frac); | 1937 | frac1=round_up(from1->frac), frac2=round_up(from2->frac); |
389 | 1947 | int frac0=max(frac1, frac2), error; | 1938 | int frac0=max(frac1, frac2), error; |
390 | 1948 | dec1 *buf1, *buf2, *buf0, *stop1, *stop2, *start1, *start2, carry=0; | 1939 | dec1 *buf1, *buf2, *buf0, *stop1, *stop2, *start1, *start2, carry=0; |
391 | 1949 | 1940 | ||
392 | @@ -2009,14 +2000,14 @@ | |||
393 | 2009 | /* ensure that always from1 > from2 (and intg1 >= intg2) */ | 2000 | /* ensure that always from1 > from2 (and intg1 >= intg2) */ |
394 | 2010 | if (carry) | 2001 | if (carry) |
395 | 2011 | { | 2002 | { |
400 | 2012 | swap_variables(const decimal_t *,from1, from2); | 2003 | swap(from1, from2); |
401 | 2013 | swap_variables(dec1 *,start1, start2); | 2004 | swap(start1, start2); |
402 | 2014 | swap_variables(int,intg1,intg2); | 2005 | swap(intg1, intg2); |
403 | 2015 | swap_variables(int,frac1,frac2); | 2006 | swap(frac1, frac2); |
404 | 2016 | to->sign= 1 - to->sign; | 2007 | to->sign= 1 - to->sign; |
405 | 2017 | } | 2008 | } |
406 | 2018 | 2009 | ||
408 | 2019 | FIX_INTG_FRAC_ERROR(to->len, intg1, frac0, error); | 2010 | fix_intg_frac_error(to->len, intg1, frac0, error); |
409 | 2020 | buf0=to->buf+intg1+frac0; | 2011 | buf0=to->buf+intg1+frac0; |
410 | 2021 | 2012 | ||
411 | 2022 | to->frac=max(from1->frac, from2->frac); | 2013 | to->frac=max(from1->frac, from2->frac); |
412 | @@ -2050,20 +2041,20 @@ | |||
413 | 2050 | *--buf0=0; | 2041 | *--buf0=0; |
414 | 2051 | while (buf2 > stop2) | 2042 | while (buf2 > stop2) |
415 | 2052 | { | 2043 | { |
417 | 2053 | SUB(*--buf0, 0, *--buf2, carry); | 2044 | sub(*--buf0, 0, *--buf2, carry); |
418 | 2054 | } | 2045 | } |
419 | 2055 | } | 2046 | } |
420 | 2056 | 2047 | ||
421 | 2057 | /* part 2 - cmin(frac) ... intg2 */ | 2048 | /* part 2 - cmin(frac) ... intg2 */ |
422 | 2058 | while (buf2 > start2) | 2049 | while (buf2 > start2) |
423 | 2059 | { | 2050 | { |
425 | 2060 | SUB(*--buf0, *--buf1, *--buf2, carry); | 2051 | sub(*--buf0, *--buf1, *--buf2, carry); |
426 | 2061 | } | 2052 | } |
427 | 2062 | 2053 | ||
428 | 2063 | /* part 3 - intg2 ... intg1 */ | 2054 | /* part 3 - intg2 ... intg1 */ |
429 | 2064 | while (carry && buf1 > start1) | 2055 | while (carry && buf1 > start1) |
430 | 2065 | { | 2056 | { |
432 | 2066 | SUB(*--buf0, *--buf1, 0, carry); | 2057 | sub(*--buf0, *--buf1, 0, carry); |
433 | 2067 | } | 2058 | } |
434 | 2068 | 2059 | ||
435 | 2069 | while (buf1 > start1) | 2060 | while (buf1 > start1) |
436 | @@ -2107,7 +2098,7 @@ | |||
437 | 2107 | int decimal_is_zero(const decimal_t *from) | 2098 | int decimal_is_zero(const decimal_t *from) |
438 | 2108 | { | 2099 | { |
439 | 2109 | dec1 *buf1=from->buf, | 2100 | dec1 *buf1=from->buf, |
441 | 2110 | *end=buf1+ROUND_UP(from->intg)+ROUND_UP(from->frac); | 2101 | *end=buf1+round_up(from->intg)+round_up(from->frac); |
442 | 2111 | while (buf1 < end) | 2102 | while (buf1 < end) |
443 | 2112 | if (*buf1++) | 2103 | if (*buf1++) |
444 | 2113 | return 0; | 2104 | return 0; |
445 | @@ -2136,9 +2127,9 @@ | |||
446 | 2136 | */ | 2127 | */ |
447 | 2137 | int decimal_mul(const decimal_t *from1, const decimal_t *from2, decimal_t *to) | 2128 | int decimal_mul(const decimal_t *from1, const decimal_t *from2, decimal_t *to) |
448 | 2138 | { | 2129 | { |
452 | 2139 | int intg1=ROUND_UP(from1->intg), intg2=ROUND_UP(from2->intg), | 2130 | int intg1=round_up(from1->intg), intg2=round_up(from2->intg), |
453 | 2140 | frac1=ROUND_UP(from1->frac), frac2=ROUND_UP(from2->frac), | 2131 | frac1=round_up(from1->frac), frac2=round_up(from2->frac), |
454 | 2141 | intg0=ROUND_UP(from1->intg+from2->intg), | 2132 | intg0=round_up(from1->intg+from2->intg), |
455 | 2142 | frac0=frac1+frac2, error, i, j, d_to_move; | 2133 | frac0=frac1+frac2, error, i, j, d_to_move; |
456 | 2143 | dec1 *buf1=from1->buf+intg1, *buf2=from2->buf+intg2, *buf0, | 2134 | dec1 *buf1=from1->buf+intg1, *buf2=from2->buf+intg2, *buf0, |
457 | 2144 | *start2, *stop2, *stop1, *start0, carry; | 2135 | *start2, *stop2, *stop1, *start0, carry; |
458 | @@ -2147,7 +2138,7 @@ | |||
459 | 2147 | 2138 | ||
460 | 2148 | i=intg0; | 2139 | i=intg0; |
461 | 2149 | j=frac0; | 2140 | j=frac0; |
463 | 2150 | FIX_INTG_FRAC_ERROR(to->len, intg0, frac0, error); | 2141 | fix_intg_frac_error(to->len, intg0, frac0, error); |
464 | 2151 | to->sign=from1->sign != from2->sign; | 2142 | to->sign=from1->sign != from2->sign; |
465 | 2152 | to->frac=from1->frac+from2->frac; | 2143 | to->frac=from1->frac+from2->frac; |
466 | 2153 | to->intg=intg0*DIG_PER_DEC1; | 2144 | to->intg=intg0*DIG_PER_DEC1; |
467 | @@ -2188,20 +2179,20 @@ | |||
468 | 2188 | dec2 p= ((dec2)*buf1) * ((dec2)*buf2); | 2179 | dec2 p= ((dec2)*buf1) * ((dec2)*buf2); |
469 | 2189 | hi=(dec1)(p/DIG_BASE); | 2180 | hi=(dec1)(p/DIG_BASE); |
470 | 2190 | lo=(dec1)(p-((dec2)hi)*DIG_BASE); | 2181 | lo=(dec1)(p-((dec2)hi)*DIG_BASE); |
472 | 2191 | ADD2(*buf0, *buf0, lo, carry); | 2182 | add2(*buf0, *buf0, lo, carry); |
473 | 2192 | carry+=hi; | 2183 | carry+=hi; |
474 | 2193 | } | 2184 | } |
475 | 2194 | if (carry) | 2185 | if (carry) |
476 | 2195 | { | 2186 | { |
477 | 2196 | if (buf0 < to->buf) | 2187 | if (buf0 < to->buf) |
478 | 2197 | return E_DEC_OVERFLOW; | 2188 | return E_DEC_OVERFLOW; |
480 | 2198 | ADD2(*buf0, *buf0, 0, carry); | 2189 | add2(*buf0, *buf0, 0, carry); |
481 | 2199 | } | 2190 | } |
482 | 2200 | for (buf0--; carry; buf0--) | 2191 | for (buf0--; carry; buf0--) |
483 | 2201 | { | 2192 | { |
484 | 2202 | if (buf0 < to->buf) | 2193 | if (buf0 < to->buf) |
485 | 2203 | return E_DEC_OVERFLOW; | 2194 | return E_DEC_OVERFLOW; |
487 | 2204 | ADD(*buf0, *buf0, 0, carry); | 2195 | add(*buf0, *buf0, 0, carry); |
488 | 2205 | } | 2196 | } |
489 | 2206 | } | 2197 | } |
490 | 2207 | 2198 | ||
491 | @@ -2224,7 +2215,7 @@ | |||
492 | 2224 | } | 2215 | } |
493 | 2225 | } | 2216 | } |
494 | 2226 | buf1= to->buf; | 2217 | buf1= to->buf; |
496 | 2227 | d_to_move= intg0 + ROUND_UP(to->frac); | 2218 | d_to_move= intg0 + round_up(to->frac); |
497 | 2228 | while (!*buf1 && (to->intg > DIG_PER_DEC1)) | 2219 | while (!*buf1 && (to->intg > DIG_PER_DEC1)) |
498 | 2229 | { | 2220 | { |
499 | 2230 | buf1++; | 2221 | buf1++; |
500 | @@ -2254,8 +2245,8 @@ | |||
501 | 2254 | static int do_div_mod(const decimal_t *from1, const decimal_t *from2, | 2245 | static int do_div_mod(const decimal_t *from1, const decimal_t *from2, |
502 | 2255 | decimal_t *to, decimal_t *mod, int scale_incr) | 2246 | decimal_t *to, decimal_t *mod, int scale_incr) |
503 | 2256 | { | 2247 | { |
506 | 2257 | int frac1=ROUND_UP(from1->frac)*DIG_PER_DEC1, prec1=from1->intg+frac1, | 2248 | int frac1=round_up(from1->frac)*DIG_PER_DEC1, prec1=from1->intg+frac1, |
507 | 2258 | frac2=ROUND_UP(from2->frac)*DIG_PER_DEC1, prec2=from2->intg+frac2, | 2249 | frac2=round_up(from2->frac)*DIG_PER_DEC1, prec2=from2->intg+frac2, |
508 | 2259 | error= 0, i, intg0, frac0, len1, len2, dintg, div_mod=(!mod); | 2250 | error= 0, i, intg0, frac0, len1, len2, dintg, div_mod=(!mod); |
509 | 2260 | dec1 *buf0, *buf1=from1->buf, *buf2=from2->buf, *tmp1, | 2251 | dec1 *buf0, *buf1=from1->buf, *buf2=from2->buf, *tmp1, |
510 | 2261 | *start2, *stop2, *stop1, *stop0, norm2, carry, *start1, dcarry; | 2252 | *start2, *stop2, *stop1, *stop0, norm2, carry, *start1, dcarry; |
511 | @@ -2305,7 +2296,7 @@ | |||
512 | 2305 | intg0=0; | 2296 | intg0=0; |
513 | 2306 | } | 2297 | } |
514 | 2307 | else | 2298 | else |
516 | 2308 | intg0=ROUND_UP(dintg); | 2299 | intg0=round_up(dintg); |
517 | 2309 | if (mod) | 2300 | if (mod) |
518 | 2310 | { | 2301 | { |
519 | 2311 | /* we're calculating N1 % N2. | 2302 | /* we're calculating N1 % N2. |
520 | @@ -2324,13 +2315,13 @@ | |||
521 | 2324 | N2 is in the buf2, has prec2 digits. Scales are frac1 and | 2315 | N2 is in the buf2, has prec2 digits. Scales are frac1 and |
522 | 2325 | frac2 accordingly. | 2316 | frac2 accordingly. |
523 | 2326 | Thus, the result will have | 2317 | Thus, the result will have |
525 | 2327 | frac = ROUND_UP(frac1+frac2+scale_incr) | 2318 | frac = round_up(frac1+frac2+scale_incr) |
526 | 2328 | and | 2319 | and |
527 | 2329 | intg = (prec1-frac1) - (prec2-frac2) + 1 | 2320 | intg = (prec1-frac1) - (prec2-frac2) + 1 |
528 | 2330 | prec = intg+frac | 2321 | prec = intg+frac |
529 | 2331 | */ | 2322 | */ |
532 | 2332 | frac0=ROUND_UP(frac1+frac2+scale_incr); | 2323 | frac0=round_up(frac1+frac2+scale_incr); |
533 | 2333 | FIX_INTG_FRAC_ERROR(to->len, intg0, frac0, error); | 2324 | fix_intg_frac_error(to->len, intg0, frac0, error); |
534 | 2334 | to->sign=from1->sign != from2->sign; | 2325 | to->sign=from1->sign != from2->sign; |
535 | 2335 | to->intg=intg0*DIG_PER_DEC1; | 2326 | to->intg=intg0*DIG_PER_DEC1; |
536 | 2336 | to->frac=frac0*DIG_PER_DEC1; | 2327 | to->frac=frac0*DIG_PER_DEC1; |
537 | @@ -2341,7 +2332,7 @@ | |||
538 | 2341 | while (dintg++ < 0) | 2332 | while (dintg++ < 0) |
539 | 2342 | *buf0++=0; | 2333 | *buf0++=0; |
540 | 2343 | 2334 | ||
542 | 2344 | len1=(i=ROUND_UP(prec1))+ROUND_UP(2*frac2+scale_incr+1) + 1; | 2335 | len1=(i=round_up(prec1))+round_up(2*frac2+scale_incr+1) + 1; |
543 | 2345 | set_if_bigger(len1, 3); | 2336 | set_if_bigger(len1, 3); |
544 | 2346 | if (!(tmp1=(dec1 *)alloca(len1*sizeof(dec1)))) | 2337 | if (!(tmp1=(dec1 *)alloca(len1*sizeof(dec1)))) |
545 | 2347 | return E_DEC_OOM; | 2338 | return E_DEC_OOM; |
546 | @@ -2351,7 +2342,7 @@ | |||
547 | 2351 | start1=tmp1; | 2342 | start1=tmp1; |
548 | 2352 | stop1=start1+len1; | 2343 | stop1=start1+len1; |
549 | 2353 | start2=buf2; | 2344 | start2=buf2; |
551 | 2354 | stop2=buf2+ROUND_UP(prec2)-1; | 2345 | stop2=buf2+round_up(prec2)-1; |
552 | 2355 | 2346 | ||
553 | 2356 | /* removing end zeroes */ | 2347 | /* removing end zeroes */ |
554 | 2357 | while (*stop2 == 0 && stop2 >= start2) | 2348 | while (*stop2 == 0 && stop2 >= start2) |
555 | @@ -2410,7 +2401,7 @@ | |||
556 | 2410 | x=guess * (*--buf2); | 2401 | x=guess * (*--buf2); |
557 | 2411 | hi=(dec1)(x/DIG_BASE); | 2402 | hi=(dec1)(x/DIG_BASE); |
558 | 2412 | lo=(dec1)(x-((dec2)hi)*DIG_BASE); | 2403 | lo=(dec1)(x-((dec2)hi)*DIG_BASE); |
560 | 2413 | SUB2(*buf1, *buf1, lo, carry); | 2404 | sub2(*buf1, *buf1, lo, carry); |
561 | 2414 | carry+=hi; | 2405 | carry+=hi; |
562 | 2415 | } | 2406 | } |
563 | 2416 | carry= dcarry < carry; | 2407 | carry= dcarry < carry; |
564 | @@ -2424,7 +2415,7 @@ | |||
565 | 2424 | buf1=start1+len2; | 2415 | buf1=start1+len2; |
566 | 2425 | for (carry=0; buf2 > start2; buf1--) | 2416 | for (carry=0; buf2 > start2; buf1--) |
567 | 2426 | { | 2417 | { |
569 | 2427 | ADD(*buf1, *buf1, *--buf2, carry); | 2418 | add(*buf1, *buf1, *--buf2, carry); |
570 | 2428 | } | 2419 | } |
571 | 2429 | } | 2420 | } |
572 | 2430 | } | 2421 | } |
573 | @@ -2443,8 +2434,8 @@ | |||
574 | 2443 | if (dcarry) | 2434 | if (dcarry) |
575 | 2444 | *--start1=dcarry; | 2435 | *--start1=dcarry; |
576 | 2445 | buf0=to->buf; | 2436 | buf0=to->buf; |
579 | 2446 | intg0=(int) (ROUND_UP(prec1-frac1)-(start1-tmp1)); | 2437 | intg0=(int) (round_up(prec1-frac1)-(start1-tmp1)); |
580 | 2447 | frac0=ROUND_UP(to->frac); | 2438 | frac0=round_up(to->frac); |
581 | 2448 | error=E_DEC_OK; | 2439 | error=E_DEC_OK; |
582 | 2449 | if (unlikely(frac0==0 && intg0==0)) | 2440 | if (unlikely(frac0==0 && intg0==0)) |
583 | 2450 | { | 2441 | { |
584 | @@ -2474,7 +2465,7 @@ | |||
585 | 2474 | error=E_DEC_OVERFLOW; | 2465 | error=E_DEC_OVERFLOW; |
586 | 2475 | goto done; | 2466 | goto done; |
587 | 2476 | } | 2467 | } |
589 | 2477 | assert(intg0 <= ROUND_UP(from2->intg)); | 2468 | assert(intg0 <= round_up(from2->intg)); |
590 | 2478 | stop1=start1+frac0+intg0; | 2469 | stop1=start1+frac0+intg0; |
591 | 2479 | to->intg=min(intg0*DIG_PER_DEC1, from2->intg); | 2470 | to->intg=min(intg0*DIG_PER_DEC1, from2->intg); |
592 | 2480 | } | 2471 | } |
593 | @@ -2554,7 +2545,7 @@ | |||
594 | 2554 | { | 2545 | { |
595 | 2555 | int i; | 2546 | int i; |
596 | 2556 | printf("/* intg=%d, frac=%d, sign=%d, buf[]={", d->intg, d->frac, d->sign); | 2547 | printf("/* intg=%d, frac=%d, sign=%d, buf[]={", d->intg, d->frac, d->sign); |
598 | 2557 | for (i=0; i < ROUND_UP(d->frac)+ROUND_UP(d->intg)-1; i++) | 2548 | for (i=0; i < round_up(d->frac)+round_up(d->intg)-1; i++) |
599 | 2558 | printf("%09d, ", d->buf[i]); | 2549 | printf("%09d, ", d->buf[i]); |
600 | 2559 | printf("%09d} */ ", d->buf[i]); | 2550 | printf("%09d} */ ", d->buf[i]); |
601 | 2560 | } | 2551 | } |
602 | 2561 | 2552 | ||
603 | === modified file 'drizzled/decimal.h' | |||
604 | --- drizzled/decimal.h 2010-10-20 20:25:52 +0000 | |||
605 | +++ drizzled/decimal.h 2010-10-29 19:47:53 +0000 | |||
606 | @@ -63,27 +63,36 @@ | |||
607 | 63 | int decimal_is_zero(const decimal_t *from); | 63 | int decimal_is_zero(const decimal_t *from); |
608 | 64 | void max_decimal(int precision, int frac, decimal_t *to); | 64 | void max_decimal(int precision, int frac, decimal_t *to); |
609 | 65 | 65 | ||
611 | 66 | #define string2decimal(A,B,C) internal_str2dec((A), (B), (C), 0) | 66 | inline int string2decimal(char *from, decimal_t *to, char **end) |
612 | 67 | { | ||
613 | 68 | return internal_str2dec(from, to, end, false); | ||
614 | 69 | } | ||
615 | 67 | 70 | ||
616 | 68 | /* set a decimal_t to zero */ | 71 | /* set a decimal_t to zero */ |
617 | 69 | 72 | ||
624 | 70 | #define decimal_make_zero(dec) do { \ | 73 | inline void decimal_make_zero(decimal_t *dec) |
625 | 71 | (dec)->buf[0]=0; \ | 74 | { |
626 | 72 | (dec)->intg=1; \ | 75 | dec->buf[0]=0; |
627 | 73 | (dec)->frac=0; \ | 76 | dec->intg=1; |
628 | 74 | (dec)->sign=0; \ | 77 | dec->frac=0; |
629 | 75 | } while(0) | 78 | dec->sign=0; |
630 | 79 | } | ||
631 | 76 | 80 | ||
632 | 77 | /* | 81 | /* |
633 | 78 | returns the length of the buffer to hold string representation | 82 | returns the length of the buffer to hold string representation |
634 | 79 | of the decimal (including decimal dot, possible sign and \0) | 83 | of the decimal (including decimal dot, possible sign and \0) |
635 | 80 | */ | 84 | */ |
636 | 81 | 85 | ||
639 | 82 | #define decimal_string_size(dec) (((dec)->intg ? (dec)->intg : 1) + \ | 86 | inline int decimal_string_size(const decimal_t *dec) |
640 | 83 | (dec)->frac + ((dec)->frac > 0) + 2) | 87 | { |
641 | 88 | return (dec->intg ? dec->intg : 1) + dec->frac + (dec->frac > 0) + 2; | ||
642 | 89 | } | ||
643 | 84 | 90 | ||
644 | 85 | /* negate a decimal */ | 91 | /* negate a decimal */ |
646 | 86 | #define decimal_neg(dec) do { (dec)->sign^=1; } while(0) | 92 | inline void decimal_neg(decimal_t *dec) |
647 | 93 | { | ||
648 | 94 | dec->sign^=1; | ||
649 | 95 | } | ||
650 | 87 | 96 | ||
651 | 88 | /* | 97 | /* |
652 | 89 | conventions: | 98 | conventions: |