drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
type/decimal.cc
Go to the documentation of this file.
1/* Copyright (C) 2000 MySQL AB
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 of the License.
6
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
11
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15
25
26/*
27=======================================================================
28 Quoting the standard
29 (SQL:2003, Part 2 Foundations, aka ISO/IEC 9075-2:2003)
30
314.4.2 Characteristics of numbers, page 27:
32
33 An exact numeric type has a precision P and a scale S. P is a positive
34 integer that determines the number of significant digits in a
35 particular radix R, where R is either 2 or 10. S is a non-negative
36 integer. Every value of an exact numeric type of scale S is of the
37 form n*10^{-S}, where n is an integer such that �-R^P <= n <= R^P.
38
39 [...]
40
41 If an assignment of some number would result in a loss of its most
42 significant digit, an exception condition is raised. If least
43 significant digits are lost, implementation-defined rounding or
44 truncating occurs, with no exception condition being raised.
45
46 [...]
47
48 Whenever an exact or approximate numeric value is assigned to an exact
49 numeric value site, an approximation of its value that preserves
50 leading significant digits after rounding or truncating is represented
51 in the declared type of the target. The value is converted to have the
52 precision and scale of the target. The choice of whether to truncate
53 or round is implementation-defined.
54
55 [...]
56
57 All numeric values between the smallest and the largest value,
58 inclusive, in a given exact numeric type have an approximation
59 obtained by rounding or truncation for that type; it is
60 implementation-defined which other numeric values have such
61 approximations.
62
635.3 <literal>, page 143
64
65 <exact numeric literal> ::=
66 <unsigned integer> [ <period> [ <unsigned integer> ] ]
67 | <period> <unsigned integer>
68
696.1 <data type>, page 165:
70
71 19) The <scale> of an <exact numeric type> shall not be greater than
72 the <precision> of the <exact numeric type>.
73
74 20) For the <exact numeric type>s DECIMAL and NUMERIC:
75
76 a) The maximum value of <precision> is implementation-defined.
77 <precision> shall not be greater than this value.
78 b) The maximum value of <scale> is implementation-defined. <scale>
79 shall not be greater than this maximum value.
80
81 21) NUMERIC specifies the data type exact numeric, with the decimal
82 precision and scale specified by the <precision> and <scale>.
83
84 22) DECIMAL specifies the data type exact numeric, with the decimal
85 scale specified by the <scale> and the implementation-defined
86 decimal precision equal to or greater than the value of the
87 specified <precision>.
88
896.26 <numeric value expression>, page 241:
90
91 1) If the declared type of both operands of a dyadic arithmetic
92 operator is exact numeric, then the declared type of the result is
93 an implementation-defined exact numeric type, with precision and
94 scale determined as follows:
95
96 a) Let S1 and S2 be the scale of the first and second operands
97 respectively.
98 b) The precision of the result of addition and subtraction is
99 implementation-defined, and the scale is the maximum of S1 and S2.
100 c) The precision of the result of multiplication is
101 implementation-defined, and the scale is S1 + S2.
102 d) The precision and scale of the result of division are
103 implementation-defined.
104*/
105
106#include <config.h>
107
108#include <drizzled/definitions.h>
109#include <drizzled/internal/m_string.h>
110#include <drizzled/charset.h>
111#include <drizzled/type/decimal.h>
112
113#include <plugin/myisam/myisampack.h>
114#include <drizzled/util/test.h>
115
116#include <alloca.h>
117
118#include <algorithm>
119#include <time.h>
120#include <drizzled/current_session.h>
121#include <drizzled/error.h>
122#include <drizzled/field.h>
123#include <drizzled/internal/my_sys.h>
124
125using namespace std;
126
127namespace drizzled
128{
140
142{
143 switch (result) {
144 case E_DEC_OK:
145 break;
146 case E_DEC_TRUNCATED:
147 push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
148 ER_WARN_DATA_TRUNCATED, ER(ER_WARN_DATA_TRUNCATED),
149 "", (long)-1);
150 break;
151 case E_DEC_OVERFLOW:
152 push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
153 ER_TRUNCATED_WRONG_VALUE,
154 ER(ER_TRUNCATED_WRONG_VALUE),
155 "DECIMAL", "");
156 break;
157 case E_DEC_DIV_ZERO:
158 my_error(ER_DIVISION_BY_ZERO, MYF(0));
159 break;
160 case E_DEC_BAD_NUM:
161 push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
162 ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
163 ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
164 "decimal", "", "", (long)-1);
165 break;
166 case E_DEC_OOM:
167 my_error(ER_OUT_OF_RESOURCES, MYF(0));
168 break;
169 default:
170 assert(0);
171 }
172 return result;
173}
174
175
194
196 uint32_t fixed_dec, String *str)
197{
198 uint32_t mask= E_DEC_FATAL_ERROR;
199
200 /*
201 Calculate the size of the string: For DECIMAL(a,b), fixed_prec==a
202 holds true iff the type is also ZEROFILL, which in turn implies
203 UNSIGNED. Hence the buffer for a ZEROFILLed value is the length
204 the user requested, plus one for a possible decimal point, plus
205 one if the user only wanted decimal places, but we force a leading
206 zero on them. Because the type is implicitly UNSIGNED, we do not
207 need to reserve a character for the sign. For all other cases,
208 fixed_prec will be 0, and class_decimal_string_length() will be called
209 instead to calculate the required size of the buffer.
210 */
211 int length= (int)(0
212 ? (uint32_t)(((0 == fixed_dec) ? 1 : 0) + 1)
213 : (uint32_t)d->string_length());
214 int result;
215 str->alloc(length);
216
217 result= decimal2string((decimal_t*) d, (char*) str->ptr(),
218 &length, (int)0, fixed_dec,
219 '0');
220 str->length(length);
221 return check_result(mask, result);
222}
223
224
243
244namespace type {
245
246int Decimal::val_binary(uint32_t mask, unsigned char *bin, int prec, int scale) const
247{
248 int err1= E_DEC_OK, err2;
249 type::Decimal rounded;
250 class_decimal2decimal(this, &rounded);
251 rounded.frac= decimal_actual_fraction(&rounded);
252 if (scale < rounded.frac)
253 {
254 err1= E_DEC_TRUNCATED;
255 /* decimal_round can return only E_DEC_TRUNCATED */
256 decimal_round(&rounded, &rounded, scale, HALF_UP);
257 }
258 err2= decimal2bin(&rounded, bin, prec, scale);
259 if (!err2)
260 err2= err1;
261 return check_result(mask, err2);
262}
263
264} // namespace type
265
266
282
283int type::Decimal::store(uint32_t mask, const char *from, uint32_t length, const charset_info_st * charset)
284{
285 char *end, *from_end;
286 int err;
287 char buff[STRING_BUFFER_USUAL_SIZE];
288 String tmp(buff, sizeof(buff), &my_charset_bin);
289 if (charset->mbminlen > 1)
290 {
291 tmp.copy(from, length, &my_charset_utf8_general_ci);
292 from= tmp.ptr();
293 length= tmp.length();
294 charset= &my_charset_bin;
295 }
296 from_end= end= (char*) from+length;
297 err= string2decimal((char *)from, (decimal_t*) this, &end);
298 if (end != from_end && !err)
299 {
300 /* Give warning if there is something other than end space */
301 for ( ; end < from_end; end++)
302 {
303 if (not my_charset_utf8_general_ci.isspace(*end))
304 {
305 err= E_DEC_TRUNCATED;
306 break;
307 }
308 }
309 }
310 check_result_and_overflow(mask, err);
311 return err;
312}
313
314void type::Decimal::convert(double &result) const
315{
316 decimal2double(static_cast<const decimal_t*>(this), &result);
317}
318
319type::Decimal *date2_class_decimal(type::Time *ltime, type::Decimal *dec)
320{
321 int64_t date;
322 date = (ltime->year*100L + ltime->month)*100L + ltime->day;
323 if (ltime->time_type > type::DRIZZLE_TIMESTAMP_DATE)
324 date= ((date*100L + ltime->hour)*100L+ ltime->minute)*100L + ltime->second;
325
326 if (int2_class_decimal(E_DEC_FATAL_ERROR, date, false, dec))
327 return dec;
328
329 if (ltime->second_part)
330 {
331 dec->buf[(dec->intg-1) / 9 + 1]= ltime->second_part * 1000;
332 dec->frac= 6;
333 }
334
335 return dec;
336}
337
338
339void class_decimal_trim(uint32_t *precision, uint32_t *scale)
340{
341 if (!(*precision) && !(*scale))
342 {
343 *precision= 10;
344 *scale= 0;
345 return;
346 }
347}
348
349
350/*
351 Internally decimal numbers are stored base 10^9 (see DIG_BASE below)
352 So one variable of type decimal_digit_t is limited:
353
354 0 < decimal_digit <= DIG_MAX < DIG_BASE
355
356 in the struct st_decimal_t:
357
358 intg is the number of *decimal* digits (NOT number of decimal_digit_t's !)
359 before the point
360 frac - number of decimal digits after the point
361 buf is an array of decimal_digit_t's
362 len is the length of buf (length of allocated space) in decimal_digit_t's,
363 not in bytes
364*/
365typedef decimal_digit_t dec1;
366typedef int64_t dec2;
367
368#define DIG_PER_DEC1 9
369#define DIG_MASK 100000000
370#define DIG_BASE 1000000000
371#define DIG_MAX (DIG_BASE-1)
372
373template<typename T>
374inline static T round_up(const T &x)
375{
376 return (x+DIG_PER_DEC1-1)/DIG_PER_DEC1;
377}
378
379static const dec1 powers10[DIG_PER_DEC1+1]={
380 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
381static const int dig2bytes[DIG_PER_DEC1+1]={0, 1, 1, 2, 2, 3, 3, 4, 4, 4};
382static const dec1 frac_max[DIG_PER_DEC1-1]={
383 900000000, 990000000, 999000000,
384 999900000, 999990000, 999999000,
385 999999900, 999999990 };
386
387#ifdef HAVE_VALGRIND
388#define sanity(d) assert((d)->len > 0)
389#else
390#define sanity(d) assert((d)->len >0 && ((d)->buf[0] | \
391 (d)->buf[(d)->len-1] | 1))
392#endif
393
394inline static void fix_intg_frac_error(const int len, int &intg1, int &frac1, int &error)
395{
396 if (DRIZZLE_UNLIKELY(intg1+frac1 > len))
397 {
398 if (DRIZZLE_UNLIKELY(intg1 > len))
399 {
400 intg1=(len);
401 frac1=0;
402 error=E_DEC_OVERFLOW;
403 }
404 else
405 {
406 frac1=(len)-intg1;
407 error=E_DEC_TRUNCATED;
408 }
409 }
410 else
411 error=E_DEC_OK;
412}
413
414/* assume carry <= 1 */
415inline static void add(dec1 &to, const dec1 &from1, const dec1& from2, dec1 &carry)
416{
417 dec1 a=from1+from2+carry;
418 assert(carry <= 1);
419 if ((carry= (a >= DIG_BASE))) /* no division here! */
420 a-=DIG_BASE;
421 to=a;
422}
423
424inline static void add2(dec1 &to, const dec1 &from1, const dec1 &from2, dec1 &carry)
425{
426 dec2 a=dec2(from1)+from2+carry;
427 if ((carry= (a >= DIG_BASE)))
428 a-=DIG_BASE;
429 if (DRIZZLE_UNLIKELY(a >= DIG_BASE))
430 {
431 a-=DIG_BASE;
432 carry++;
433 }
434 to=dec1(a);
435}
436
437/* to=from1-from2 */
438inline static void sub(dec1 &to, const dec1 &from1, const dec1 &from2, dec1 &carry)
439{
440 dec1 a=from1-from2-carry;
441 if ((carry= (a < 0)))
442 a+=DIG_BASE;
443 to=a;
444}
445
446/* to=from1-from2 */
447inline static void sub2(dec1 &to, const dec1 &from1, const dec1 &from2, dec1 &carry)
448{
449 dec1 a=from1-from2-carry;
450 if ((carry= (a < 0)))
451 a+=DIG_BASE;
452 if (DRIZZLE_UNLIKELY(a < 0))
453 {
454 a+=DIG_BASE;
455 carry++;
456 }
457 to=a;
458}
459
467
468void max_decimal(int precision, int frac, decimal_t *to)
469{
470 int intpart;
471 dec1 *buf= to->buf;
472 assert(precision && precision >= frac);
473
474 to->sign= 0;
475 if ((intpart= to->intg= (precision - frac)))
476 {
477 const int firstdigits= intpart % DIG_PER_DEC1;
478 if (firstdigits)
479 *buf++= powers10[firstdigits] - 1; /* get 9 99 999 ... */
480 for(intpart/= DIG_PER_DEC1; intpart; intpart--)
481 *buf++= DIG_MAX;
482 }
483
484 if ((to->frac= frac))
485 {
486 const int lastdigits= frac % DIG_PER_DEC1;
487 for(frac/= DIG_PER_DEC1; frac; frac--)
488 *buf++= DIG_MAX;
489 if (lastdigits)
490 *buf= frac_max[lastdigits - 1];
491 }
492}
493
494
495static dec1 *remove_leading_zeroes(const decimal_t *from, int *intg_result)
496{
497 int intg= from->intg, i;
498 dec1 *buf0= from->buf;
499 i= ((intg - 1) % DIG_PER_DEC1) + 1;
500 while (intg > 0 && *buf0 == 0)
501 {
502 intg-= i;
503 i= DIG_PER_DEC1;
504 buf0++;
505 }
506 if (intg > 0)
507 {
508 for (i= (intg - 1) % DIG_PER_DEC1; *buf0 < powers10[i--]; intg--) ;
509 assert(intg > 0);
510 }
511 else
512 intg=0;
513 *intg_result= intg;
514 return buf0;
515}
516
517
523
525{
526 int frac= from->frac, i;
527 dec1 *buf0= from->buf + round_up(from->intg) + round_up(frac) - 1;
528
529 if (frac == 0)
530 return 0;
531
532 i= ((frac - 1) % DIG_PER_DEC1 + 1);
533 while (frac > 0 && *buf0 == 0)
534 {
535 frac-= i;
536 i= DIG_PER_DEC1;
537 buf0--;
538 }
539 if (frac > 0)
540 {
541 for (i= DIG_PER_DEC1 - ((frac - 1) % DIG_PER_DEC1); *buf0 % powers10[i++] == 0; frac--) {};
542 }
543 return frac;
544}
545
546
568int decimal2string(const decimal_t *from, char *to, int *to_len,
569 int fixed_precision, int fixed_decimals,
570 char filler)
571{
572 int len, intg, frac= from->frac, i, intg_len, frac_len, fill;
573 /* number digits before decimal point */
574 int fixed_intg= (fixed_precision ?
575 (fixed_precision - fixed_decimals) : 0);
576 int error=E_DEC_OK;
577 char *s=to;
578 dec1 *buf, *buf0=from->buf, tmp;
579
580 assert(*to_len >= 2+from->sign);
581
582 /* removing leading zeroes */
583 buf0= remove_leading_zeroes(from, &intg);
584 if (DRIZZLE_UNLIKELY(intg+frac==0))
585 {
586 intg=1;
587 tmp=0;
588 buf0=&tmp;
589 }
590
591 if (!(intg_len= fixed_precision ? fixed_intg : intg))
592 intg_len= 1;
593 frac_len= fixed_precision ? fixed_decimals : frac;
594 len= from->sign + intg_len + test(frac) + frac_len;
595 if (fixed_precision)
596 {
597 if (frac > fixed_decimals)
598 {
599 error= E_DEC_TRUNCATED;
600 frac= fixed_decimals;
601 }
602 if (intg > fixed_intg)
603 {
604 error= E_DEC_OVERFLOW;
605 intg= fixed_intg;
606 }
607 }
608 else if (DRIZZLE_UNLIKELY(len > --*to_len)) /* reserve one byte for \0 */
609 {
610 int j= len-*to_len;
611 error= (frac && j <= frac + 1) ? E_DEC_TRUNCATED : E_DEC_OVERFLOW;
612 if (frac && j >= frac + 1) j--;
613 if (j > frac)
614 {
615 intg-= j-frac;
616 frac= 0;
617 }
618 else
619 frac-=j;
620 len= from->sign + intg_len + test(frac) + frac_len;
621 }
622 *to_len=len;
623 s[len]=0;
624
625 if (from->sign)
626 *s++='-';
627
628 if (frac)
629 {
630 char *s1= s + intg_len;
631 fill= frac_len - frac;
632 buf=buf0+round_up(intg);
633 *s1++='.';
634 for (; frac>0; frac-=DIG_PER_DEC1)
635 {
636 dec1 x=*buf++;
637 for (i=min(frac, DIG_PER_DEC1); i; i--)
638 {
639 dec1 y=x/DIG_MASK;
640 *s1++='0'+(unsigned char)y;
641 x-=y*DIG_MASK;
642 x*=10;
643 }
644 }
645 for(; fill; fill--)
646 *s1++=filler;
647 }
648
649 fill= intg_len - intg;
650 if (intg == 0)
651 fill--; /* symbol 0 before digital point */
652 for(; fill; fill--)
653 *s++=filler;
654 if (intg)
655 {
656 s+=intg;
657 for (buf=buf0+round_up(intg); intg>0; intg-=DIG_PER_DEC1)
658 {
659 dec1 x=*--buf;
660 for (i=min(intg, DIG_PER_DEC1); i; i--)
661 {
662 dec1 y=x/10;
663 *--s='0'+(unsigned char)(x-y*10);
664 x=y;
665 }
666 }
667 }
668 else
669 *s= '0';
670 return error;
671}
672
673
683static void digits_bounds(decimal_t *from, int *start_result, int *end_result)
684{
685 int start, stop, i;
686 dec1 *buf_beg= from->buf;
687 dec1 *end= from->buf + round_up(from->intg) + round_up(from->frac);
688 dec1 *buf_end= end - 1;
689
690 /* find non-zero digit from number begining */
691 while (buf_beg < end && *buf_beg == 0)
692 buf_beg++;
693
694 if (buf_beg >= end)
695 {
696 /* it is zero */
697 *start_result= *end_result= 0;
698 return;
699 }
700
701 /* find non-zero decimal digit from number begining */
702 if (buf_beg == from->buf && from->intg)
703 {
704 start= DIG_PER_DEC1 - (i= ((from->intg-1) % DIG_PER_DEC1 + 1));
705 i--;
706 }
707 else
708 {
709 i= DIG_PER_DEC1 - 1;
710 start= (int) ((buf_beg - from->buf) * DIG_PER_DEC1);
711 }
712 if (buf_beg < end)
713 for (; *buf_beg < powers10[i--]; start++) ;
714 *start_result= start; /* index of first decimal digit (from 0) */
715
716 /* find non-zero digit at the end */
717 while (buf_end > buf_beg && *buf_end == 0)
718 buf_end--;
719 /* find non-zero decimal digit from the end */
720 if (buf_end == end - 1 && from->frac)
721 {
722 stop= (int) (((buf_end - from->buf) * DIG_PER_DEC1 +
723 (i= ((from->frac - 1) % DIG_PER_DEC1 + 1))));
724 i= DIG_PER_DEC1 - i + 1;
725 }
726 else
727 {
728 stop= (int) ((buf_end - from->buf + 1) * DIG_PER_DEC1);
729 i= 1;
730 }
731 for (; *buf_end % powers10[i++] == 0; stop--) {};
732 *end_result= stop; /* index of position after last decimal digit (from 0) */
733}
734
735
751static void do_mini_left_shift(decimal_t *dec, int shift, int beg, int last)
752{
753 dec1 *from= dec->buf + round_up(beg + 1) - 1;
754 dec1 *end= dec->buf + round_up(last) - 1;
755 int c_shift= DIG_PER_DEC1 - shift;
756 assert(from >= dec->buf);
757 assert(end < dec->buf + dec->len);
758 if (beg % DIG_PER_DEC1 < shift)
759 *(from - 1)= (*from) / powers10[c_shift];
760 for(; from < end; from++)
761 *from= ((*from % powers10[c_shift]) * powers10[shift] +
762 (*(from + 1)) / powers10[c_shift]);
763 *from= (*from % powers10[c_shift]) * powers10[shift];
764}
765
766
779static void do_mini_right_shift(decimal_t *dec, int shift, int beg, int last)
780{
781 dec1 *from= dec->buf + round_up(last) - 1;
782 dec1 *end= dec->buf + round_up(beg + 1) - 1;
783 int c_shift= DIG_PER_DEC1 - shift;
784 assert(from < dec->buf + dec->len);
785 assert(end >= dec->buf);
786 if (DIG_PER_DEC1 - ((last - 1) % DIG_PER_DEC1 + 1) < shift)
787 *(from + 1)= (*from % powers10[shift]) * powers10[c_shift];
788 for(; from > end; from--)
789 *from= (*from / powers10[shift] +
790 (*(from - 1) % powers10[shift]) * powers10[c_shift]);
791 *from= *from / powers10[shift];
792}
793
794
811static int decimal_shift(decimal_t *dec, int shift)
812{
813 /* index of first non zero digit (all indexes from 0) */
814 int beg;
815 /* index of position after last decimal digit */
816 int end;
817 /* index of digit position just after point */
818 int point= round_up(dec->intg) * DIG_PER_DEC1;
819 /* new point position */
820 int new_point= point + shift;
821 /* number of digits in result */
822 int digits_int, digits_frac;
823 /* length of result and new fraction in big digits*/
824 int new_len, new_frac_len;
825 /* return code */
826 int err= E_DEC_OK;
827 int new_front;
828
829 if (shift == 0)
830 return E_DEC_OK;
831
832 digits_bounds(dec, &beg, &end);
833
834 if (beg == end)
835 {
836 dec->set_zero();
837 return E_DEC_OK;
838 }
839
840 digits_int= new_point - beg;
841 set_if_bigger(digits_int, 0);
842 digits_frac= end - new_point;
843 set_if_bigger(digits_frac, 0);
844
845 if ((new_len= round_up(digits_int) + (new_frac_len= round_up(digits_frac))) >
846 dec->len)
847 {
848 int lack= new_len - dec->len;
849 int diff;
850
851 if (new_frac_len < lack)
852 return E_DEC_OVERFLOW; /* lack more then we have in fraction */
853
854 /* cat off fraction part to allow new number to fit in our buffer */
855 err= E_DEC_TRUNCATED;
856 new_frac_len-= lack;
857 diff= digits_frac - (new_frac_len * DIG_PER_DEC1);
858 /* Make rounding method as parameter? */
859 decimal_round(dec, dec, end - point - diff, HALF_UP);
860 end-= diff;
861 digits_frac= new_frac_len * DIG_PER_DEC1;
862
863 if (end <= beg)
864 {
865 /*
866 we lost all digits (they will be shifted out of buffer), so we can
867 just return 0
868 */
869 dec->set_zero();
870
871 return E_DEC_TRUNCATED;
872 }
873 }
874
875 if (shift % DIG_PER_DEC1)
876 {
877 int l_mini_shift, r_mini_shift, mini_shift;
878 int do_left;
879 /*
880 Calculate left/right shift to align decimal digits inside our bug
881 digits correctly
882 */
883 if (shift > 0)
884 {
885 l_mini_shift= shift % DIG_PER_DEC1;
886 r_mini_shift= DIG_PER_DEC1 - l_mini_shift;
887 /*
888 It is left shift so prefer left shift, but if we have not place from
889 left, we have to have it from right, because we checked length of
890 result
891 */
892 do_left= l_mini_shift <= beg;
893 assert(do_left || (dec->len * DIG_PER_DEC1 - end) >= r_mini_shift);
894 }
895 else
896 {
897 r_mini_shift= (-shift) % DIG_PER_DEC1;
898 l_mini_shift= DIG_PER_DEC1 - r_mini_shift;
899 /* see comment above */
900 do_left= !((dec->len * DIG_PER_DEC1 - end) >= r_mini_shift);
901 assert(!do_left || l_mini_shift <= beg);
902 }
903 if (do_left)
904 {
905 do_mini_left_shift(dec, l_mini_shift, beg, end);
906 mini_shift= (-l_mini_shift);
907 }
908 else
909 {
910 do_mini_right_shift(dec, r_mini_shift, beg, end);
911 mini_shift= r_mini_shift;
912 }
913 new_point+= mini_shift;
914 /*
915 If number is shifted and correctly aligned in buffer we can
916 finish
917 */
918 if (!(shift+= mini_shift) && (new_point - digits_int) < DIG_PER_DEC1)
919 {
920 dec->intg= digits_int;
921 dec->frac= digits_frac;
922 return err; /* already shifted as it should be */
923 }
924 beg+= mini_shift;
925 end+= mini_shift;
926 }
927
928 /* if new 'decimal front' is in first digit, we do not need move digits */
929 if ((new_front= (new_point - digits_int)) >= DIG_PER_DEC1 ||
930 new_front < 0)
931 {
932 /* need to move digits */
933 int d_shift;
934 dec1 *to, *barier;
935 if (new_front > 0)
936 {
937 /* move left */
938 d_shift= new_front / DIG_PER_DEC1;
939 to= dec->buf + (round_up(beg + 1) - 1 - d_shift);
940 barier= dec->buf + (round_up(end) - 1 - d_shift);
941 assert(to >= dec->buf);
942 assert(barier + d_shift < dec->buf + dec->len);
943 for(; to <= barier; to++)
944 *to= *(to + d_shift);
945 for(barier+= d_shift; to <= barier; to++)
946 *to= 0;
947 d_shift= -d_shift;
948 }
949 else
950 {
951 /* move right */
952 d_shift= (1 - new_front) / DIG_PER_DEC1;
953 to= dec->buf + round_up(end) - 1 + d_shift;
954 barier= dec->buf + round_up(beg + 1) - 1 + d_shift;
955 assert(to < dec->buf + dec->len);
956 assert(barier - d_shift >= dec->buf);
957 for(; to >= barier; to--)
958 *to= *(to - d_shift);
959 for(barier-= d_shift; to >= barier; to--)
960 *to= 0;
961 }
962 d_shift*= DIG_PER_DEC1;
963 beg+= d_shift;
964 end+= d_shift;
965 new_point+= d_shift;
966 }
967
968 /*
969 If there are gaps then fill ren with 0.
970
971 Only one of following 'for' loops will work becouse beg <= end
972 */
973 beg= round_up(beg + 1) - 1;
974 end= round_up(end) - 1;
975 assert(new_point >= 0);
976
977 /* We don't want negative new_point below */
978 if (new_point != 0)
979 new_point= round_up(new_point) - 1;
980
981 if (new_point > end)
982 {
983 do
984 {
985 dec->buf[new_point]=0;
986 } while (--new_point > end);
987 }
988 else
989 {
990 for (; new_point < beg; new_point++)
991 dec->buf[new_point]= 0;
992 }
993 dec->intg= digits_int;
994 dec->frac= digits_frac;
995 return err;
996}
997
998
1018int
1019internal_str2dec(char *from, decimal_t *to, char **end, bool fixed)
1020{
1021 char *s= from, *s1;
1022 char *end_of_string = *end;
1023 char *endp;
1024 int i, intg, frac, error, intg1, frac1;
1025 dec1 x,*buf;
1026 sanity(to);
1027
1028 error= E_DEC_BAD_NUM; /* In case of bad number */
1029 while (s < end_of_string && my_charset_utf8_general_ci.isspace(*s))
1030 s++;
1031 if (s == end_of_string)
1032 goto fatal_error;
1033
1034 if ((to->sign= (*s == '-')))
1035 s++;
1036 else if (*s == '+')
1037 s++;
1038
1039 s1=s;
1040 while (s < end_of_string && my_charset_utf8_general_ci.isdigit(*s))
1041 s++;
1042 intg= (int) (s-s1);
1043 if (s < end_of_string && *s=='.')
1044 {
1045 endp= s+1;
1046 while (endp < end_of_string && my_charset_utf8_general_ci.isdigit(*endp))
1047 endp++;
1048 frac= (int) (endp - s - 1);
1049 }
1050 else
1051 {
1052 frac= 0;
1053 endp= s;
1054 }
1055
1056 *end= endp;
1057
1058 if (frac+intg == 0)
1059 goto fatal_error;
1060
1061 error= 0;
1062 if (fixed)
1063 {
1064 if (frac > to->frac)
1065 {
1066 error=E_DEC_TRUNCATED;
1067 frac=to->frac;
1068 }
1069 if (intg > to->intg)
1070 {
1071 error=E_DEC_OVERFLOW;
1072 intg=to->intg;
1073 }
1074 intg1=round_up(intg);
1075 frac1=round_up(frac);
1076 if (intg1+frac1 > to->len)
1077 {
1078 error= E_DEC_OOM;
1079 goto fatal_error;
1080 }
1081 }
1082 else
1083 {
1084 intg1=round_up(intg);
1085 frac1=round_up(frac);
1086 fix_intg_frac_error(to->len, intg1, frac1, error);
1087 if (DRIZZLE_UNLIKELY(error))
1088 {
1089 frac=frac1*DIG_PER_DEC1;
1090 if (error == E_DEC_OVERFLOW)
1091 intg=intg1*DIG_PER_DEC1;
1092 }
1093 }
1094 /* Error is guranteed to be set here */
1095 to->intg=intg;
1096 to->frac=frac;
1097
1098 buf=to->buf+intg1;
1099 s1=s;
1100
1101 for (x=0, i=0; intg; intg--)
1102 {
1103 x+= (*--s - '0')*powers10[i];
1104
1105 if (DRIZZLE_UNLIKELY(++i == DIG_PER_DEC1))
1106 {
1107 *--buf=x;
1108 x=0;
1109 i=0;
1110 }
1111 }
1112 if (i)
1113 *--buf=x;
1114
1115 buf=to->buf+intg1;
1116 for (x=0, i=0; frac; frac--)
1117 {
1118 x= (*++s1 - '0') + x*10;
1119
1120 if (DRIZZLE_UNLIKELY(++i == DIG_PER_DEC1))
1121 {
1122 *buf++=x;
1123 x=0;
1124 i=0;
1125 }
1126 }
1127 if (i)
1128 *buf=x*powers10[DIG_PER_DEC1-i];
1129
1130 /* Handle exponent */
1131 if (endp+1 < end_of_string && (*endp == 'e' || *endp == 'E'))
1132 {
1133 int str_error;
1134 const int64_t exponent= internal::my_strtoll10(endp+1, (char**) &end_of_string,
1135 &str_error);
1136
1137 if (end_of_string != endp +1) /* If at least one digit */
1138 {
1139 *end= (char*) end_of_string;
1140 if (str_error > 0)
1141 {
1142 error= E_DEC_BAD_NUM;
1143 goto fatal_error;
1144 }
1145 if (exponent > INT_MAX/2 || (str_error == 0 && exponent < 0))
1146 {
1147 error= E_DEC_OVERFLOW;
1148 goto fatal_error;
1149 }
1150 if (exponent < INT_MIN/2 && error != E_DEC_OVERFLOW)
1151 {
1152 error= E_DEC_TRUNCATED;
1153 goto fatal_error;
1154 }
1155 if (error != E_DEC_OVERFLOW)
1156 error= decimal_shift(to, (int) exponent);
1157 }
1158 }
1159 return error;
1160
1161fatal_error:
1162 to->set_zero();
1163 return error;
1164}
1165
1166
1176
1177int decimal2double(const decimal_t *from, double *to)
1178{
1179 char strbuf[FLOATING_POINT_BUFFER];
1180 int len= sizeof(strbuf);
1181 int rc = decimal2string(from, strbuf, &len, 0, 0, 0);
1182 char* end= strbuf + len;
1183 int error;
1184 *to= internal::my_strtod(strbuf, &end, &error);
1185 return rc != E_DEC_OK ? rc : (error ? E_DEC_OVERFLOW : E_DEC_OK);
1186}
1187
1197
1198int double2decimal(const double from, decimal_t *to)
1199{
1200 char buff[FLOATING_POINT_BUFFER], *end;
1201 int res;
1202 end= buff + internal::my_gcvt(from,
1203 internal::MY_GCVT_ARG_DOUBLE,
1204 sizeof(buff) - 1, buff, NULL);
1205 res= string2decimal(buff, to, &end);
1206 return res;
1207}
1208
1209
1210static int ull2dec(uint64_t from, decimal_t *to)
1211{
1212 int intg1, error=E_DEC_OK;
1213 uint64_t x=from;
1214 dec1 *buf;
1215
1216 sanity(to);
1217
1218 for (intg1=1; from >= DIG_BASE; intg1++, from/=DIG_BASE) {};
1219 if (DRIZZLE_UNLIKELY(intg1 > to->len))
1220 {
1221 intg1=to->len;
1222 error=E_DEC_OVERFLOW;
1223 }
1224 to->frac=0;
1225 to->intg=intg1*DIG_PER_DEC1;
1226
1227 for (buf=to->buf+intg1; intg1; intg1--)
1228 {
1229 uint64_t y=x/DIG_BASE;
1230 *--buf=(dec1)(x-y*DIG_BASE);
1231 x=y;
1232 }
1233 return error;
1234}
1235
1236int uint64_t2decimal(const uint64_t from, decimal_t *to)
1237{
1238 to->sign=0;
1239 return ull2dec(from, to);
1240}
1241
1242int int64_t2decimal(const int64_t from, decimal_t *to)
1243{
1244 if ((to->sign= from < 0))
1245 return ull2dec(-from, to);
1246 return ull2dec(from, to);
1247}
1248
1249int decimal2uint64_t(const decimal_t *from, uint64_t *to)
1250{
1251 dec1 *buf=from->buf;
1252 uint64_t x=0;
1253 int intg, frac;
1254
1255 if (from->sign)
1256 {
1257 *to= 0ULL;
1258 return E_DEC_OVERFLOW;
1259 }
1260
1261 for (intg=from->intg; intg > 0; intg-=DIG_PER_DEC1)
1262 {
1263 uint64_t y=x;
1264 x=x*DIG_BASE + *buf++;
1265 if (DRIZZLE_UNLIKELY(y > ((uint64_t) UINT64_MAX/DIG_BASE) || x < y))
1266 {
1267 *to=UINT64_MAX;
1268 return E_DEC_OVERFLOW;
1269 }
1270 }
1271 *to=x;
1272 for (frac=from->frac; DRIZZLE_UNLIKELY(frac > 0); frac-=DIG_PER_DEC1)
1273 if (*buf++)
1274 return E_DEC_TRUNCATED;
1275 return E_DEC_OK;
1276}
1277
1278int decimal2int64_t(const decimal_t *from, int64_t *to)
1279{
1280 dec1 *buf=from->buf;
1281 int64_t x=0;
1282 int intg, frac;
1283
1284 for (intg=from->intg; intg > 0; intg-=DIG_PER_DEC1)
1285 {
1286 int64_t y=x;
1287 /*
1288 Attention: trick!
1289 we're calculating -|from| instead of |from| here
1290 because |INT64_MIN| > INT64_MAX
1291 so we can convert -9223372036854775808 correctly
1292 */
1293 x=x*DIG_BASE - *buf++;
1294 if (DRIZZLE_UNLIKELY(y < (INT64_MIN/DIG_BASE) || x > y))
1295 {
1296 /*
1297 the decimal is bigger than any possible integer
1298 return border integer depending on the sign
1299 */
1300 *to= from->sign ? INT64_MIN : INT64_MAX;
1301 return E_DEC_OVERFLOW;
1302 }
1303 }
1304 /* boundary case: 9223372036854775808 */
1305 if (DRIZZLE_UNLIKELY(from->sign==0 && x == INT64_MIN))
1306 {
1307 *to= INT64_MAX;
1308 return E_DEC_OVERFLOW;
1309 }
1310
1311 *to=from->sign ? x : -x;
1312 for (frac=from->frac; DRIZZLE_UNLIKELY(frac > 0); frac-=DIG_PER_DEC1)
1313 if (*buf++)
1314 return E_DEC_TRUNCATED;
1315 return E_DEC_OK;
1316}
1317
1397int decimal2bin(const decimal_t *from, unsigned char *to, int precision, int frac)
1398{
1399 dec1 mask=from->sign ? -1 : 0, *buf1=from->buf, *stop1;
1400 int error=E_DEC_OK, intg=precision-frac,
1401 isize1, intg1, intg1x, from_intg,
1402 intg0=intg/DIG_PER_DEC1,
1403 frac0=frac/DIG_PER_DEC1,
1404 intg0x=intg-intg0*DIG_PER_DEC1,
1405 frac0x=frac-frac0*DIG_PER_DEC1,
1406 frac1=from->frac/DIG_PER_DEC1,
1407 frac1x=from->frac-frac1*DIG_PER_DEC1,
1408 isize0=intg0*sizeof(dec1)+dig2bytes[intg0x],
1409 fsize0=frac0*sizeof(dec1)+dig2bytes[frac0x],
1410 fsize1=frac1*sizeof(dec1)+dig2bytes[frac1x];
1411 const int orig_isize0= isize0;
1412 const int orig_fsize0= fsize0;
1413 unsigned char *orig_to= to;
1414
1415 buf1= remove_leading_zeroes(from, &from_intg);
1416
1417 if (DRIZZLE_UNLIKELY(from_intg+fsize1==0))
1418 {
1419 mask=0; /* just in case */
1420 intg=1;
1421 buf1=&mask;
1422 }
1423
1424 intg1=from_intg/DIG_PER_DEC1;
1425 intg1x=from_intg-intg1*DIG_PER_DEC1;
1426 isize1=intg1*sizeof(dec1)+dig2bytes[intg1x];
1427
1428 if (intg < from_intg)
1429 {
1430 buf1+=intg1-intg0+(intg1x>0)-(intg0x>0);
1431 intg1=intg0; intg1x=intg0x;
1432 error=E_DEC_OVERFLOW;
1433 }
1434 else if (isize0 > isize1)
1435 {
1436 while (isize0-- > isize1)
1437 *to++= (char)mask;
1438 }
1439 if (fsize0 < fsize1)
1440 {
1441 frac1=frac0; frac1x=frac0x;
1442 error=E_DEC_TRUNCATED;
1443 }
1444 else if (fsize0 > fsize1 && frac1x)
1445 {
1446 if (frac0 == frac1)
1447 {
1448 frac1x=frac0x;
1449 fsize0= fsize1;
1450 }
1451 else
1452 {
1453 frac1++;
1454 frac1x=0;
1455 }
1456 }
1457
1458 /* intg1x part */
1459 if (intg1x)
1460 {
1461 int i=dig2bytes[intg1x];
1462 dec1 x=(*buf1++ % powers10[intg1x]) ^ mask;
1463 switch (i)
1464 {
1465 case 1: mi_int1store(to, x); break;
1466 case 2: mi_int2store(to, x); break;
1467 case 3: mi_int3store(to, x); break;
1468 case 4: mi_int4store(to, x); break;
1469 default: assert(0);
1470 }
1471 to+=i;
1472 }
1473
1474 /* intg1+frac1 part */
1475 for (stop1=buf1+intg1+frac1; buf1 < stop1; to+=sizeof(dec1))
1476 {
1477 dec1 x=*buf1++ ^ mask;
1478 assert(sizeof(dec1) == 4);
1479 mi_int4store(to, x);
1480 }
1481
1482 /* frac1x part */
1483 if (frac1x)
1484 {
1485 dec1 x;
1486 int i=dig2bytes[frac1x],
1487 lim=(frac1 < frac0 ? DIG_PER_DEC1 : frac0x);
1488 while (frac1x < lim && dig2bytes[frac1x] == i)
1489 frac1x++;
1490 x=(*buf1 / powers10[DIG_PER_DEC1 - frac1x]) ^ mask;
1491 switch (i)
1492 {
1493 case 1: mi_int1store(to, x); break;
1494 case 2: mi_int2store(to, x); break;
1495 case 3: mi_int3store(to, x); break;
1496 case 4: mi_int4store(to, x); break;
1497 default: assert(0);
1498 }
1499 to+=i;
1500 }
1501 if (fsize0 > fsize1)
1502 {
1503 unsigned char *to_end= orig_to + orig_fsize0 + orig_isize0;
1504
1505 while (fsize0-- > fsize1 && to < to_end)
1506 *to++= (unsigned char)mask;
1507 }
1508 orig_to[0]^= 0x80;
1509
1510 /* Check that we have written the whole decimal and nothing more */
1511 assert(to == orig_to + orig_fsize0 + orig_isize0);
1512 return error;
1513}
1514
1530int bin2decimal(const unsigned char *from, decimal_t *to, int precision, int scale)
1531{
1532 int error=E_DEC_OK, intg=precision-scale,
1533 intg0=intg/DIG_PER_DEC1, frac0=scale/DIG_PER_DEC1,
1534 intg0x=intg-intg0*DIG_PER_DEC1, frac0x=scale-frac0*DIG_PER_DEC1,
1535 intg1=intg0+(intg0x>0), frac1=frac0+(frac0x>0);
1536 dec1 *buf=to->buf, mask=(*from & 0x80) ? 0 : -1;
1537 const unsigned char *stop;
1538 unsigned char *d_copy;
1539 int bin_size= decimal_bin_size(precision, scale);
1540
1541 sanity(to);
1542 d_copy= (unsigned char*) alloca(bin_size);
1543 memcpy(d_copy, from, bin_size);
1544 d_copy[0]^= 0x80;
1545 from= d_copy;
1546
1547 fix_intg_frac_error(to->len, intg1, frac1, error);
1548 if (DRIZZLE_UNLIKELY(error))
1549 {
1550 if (intg1 < intg0+(intg0x>0))
1551 {
1552 from+=dig2bytes[intg0x]+sizeof(dec1)*(intg0-intg1);
1553 frac0=frac0x=intg0x=0;
1554 intg0=intg1;
1555 }
1556 else
1557 {
1558 frac0x=0;
1559 frac0=frac1;
1560 }
1561 }
1562
1563 to->sign=(mask != 0);
1564 to->intg=intg0*DIG_PER_DEC1+intg0x;
1565 to->frac=frac0*DIG_PER_DEC1+frac0x;
1566
1567 if (intg0x)
1568 {
1569 int i=dig2bytes[intg0x];
1570 dec1 x= 0;
1571 switch (i)
1572 {
1573 case 1: x=mi_sint1korr(from); break;
1574 case 2: x=mi_sint2korr(from); break;
1575 case 3: x=mi_sint3korr(from); break;
1576 case 4: x=mi_sint4korr(from); break;
1577 default: assert(0);
1578 }
1579 from+=i;
1580 *buf=x ^ mask;
1581 if (((uint64_t)*buf) >= (uint64_t) powers10[intg0x+1])
1582 goto err;
1583 if (buf > to->buf || *buf != 0)
1584 buf++;
1585 else
1586 to->intg-=intg0x;
1587 }
1588 for (stop=from+intg0*sizeof(dec1); from < stop; from+=sizeof(dec1))
1589 {
1590 assert(sizeof(dec1) == 4);
1591 *buf=mi_sint4korr(from) ^ mask;
1592 if (((uint32_t)*buf) > DIG_MAX)
1593 goto err;
1594 if (buf > to->buf || *buf != 0)
1595 buf++;
1596 else
1597 to->intg-=DIG_PER_DEC1;
1598 }
1599 assert(to->intg >=0);
1600 for (stop=from+frac0*sizeof(dec1); from < stop; from+=sizeof(dec1))
1601 {
1602 assert(sizeof(dec1) == 4);
1603 *buf=mi_sint4korr(from) ^ mask;
1604 if (((uint32_t)*buf) > DIG_MAX)
1605 goto err;
1606 buf++;
1607 }
1608 if (frac0x)
1609 {
1610 int i=dig2bytes[frac0x];
1611 dec1 x= 0;
1612 switch (i)
1613 {
1614 case 1: x=mi_sint1korr(from); break;
1615 case 2: x=mi_sint2korr(from); break;
1616 case 3: x=mi_sint3korr(from); break;
1617 case 4: x=mi_sint4korr(from); break;
1618 default: assert(0);
1619 }
1620 *buf=(x ^ mask) * powers10[DIG_PER_DEC1 - frac0x];
1621 if (((uint32_t)*buf) > DIG_MAX)
1622 goto err;
1623 buf++;
1624 }
1625 return error;
1626
1627err:
1628 to->set_zero();
1629 return(E_DEC_BAD_NUM);
1630}
1631
1637int decimal_bin_size(int precision, int scale)
1638{
1639 int intg=precision-scale,
1640 intg0=intg/DIG_PER_DEC1, frac0=scale/DIG_PER_DEC1,
1641 intg0x=intg-intg0*DIG_PER_DEC1, frac0x=scale-frac0*DIG_PER_DEC1;
1642
1643 assert(scale >= 0 && precision > 0 && scale <= precision);
1644 return intg0*sizeof(dec1)+dig2bytes[intg0x]+
1645 frac0*sizeof(dec1)+dig2bytes[frac0x];
1646}
1647
1663int
1664decimal_round(const decimal_t *from, decimal_t *to, int scale,
1665 decimal_round_mode mode)
1666{
1667 int frac0=scale>0 ? round_up(scale) : scale/DIG_PER_DEC1,
1668 frac1=round_up(from->frac), round_digit= 0,
1669 intg0=round_up(from->intg), error=E_DEC_OK, len=to->len,
1670 intg1=round_up(from->intg +
1671 (((intg0 + frac0)>0) && (from->buf[0] == DIG_MAX)));
1672 dec1 *buf0=from->buf, *buf1=to->buf, x, y, carry=0;
1673 int first_dig;
1674
1675 sanity(to);
1676
1677 switch (mode) {
1678 case HALF_UP:
1679 case HALF_EVEN: round_digit=5; break;
1680 case CEILING: round_digit= from->sign ? 10 : 0; break;
1681 case FLOOR: round_digit= from->sign ? 0 : 10; break;
1682 case TRUNCATE: round_digit=10; break;
1683 default: assert(0);
1684 }
1685
1686 if (DRIZZLE_UNLIKELY(frac0+intg0 > len))
1687 {
1688 frac0=len-intg0;
1689 scale=frac0*DIG_PER_DEC1;
1690 error=E_DEC_TRUNCATED;
1691 }
1692
1693 if (scale+from->intg < 0)
1694 {
1695 to->set_zero();
1696 return E_DEC_OK;
1697 }
1698
1699 if (to != from || intg1>intg0)
1700 {
1701 dec1 *p0= buf0+intg0+max(frac1, frac0);
1702 dec1 *p1= buf1+intg1+max(frac1, frac0);
1703
1704 while (buf0 < p0)
1705 *(--p1) = *(--p0);
1706 if (DRIZZLE_UNLIKELY(intg1 > intg0))
1707 to->buf[0]= 0;
1708
1709 intg0= intg1;
1710 buf0=to->buf;
1711 buf1=to->buf;
1712 to->sign=from->sign;
1713 to->intg=min(intg0, len)*DIG_PER_DEC1;
1714 }
1715
1716 if (frac0 > frac1)
1717 {
1718 buf1+=intg0+frac1;
1719 while (frac0-- > frac1)
1720 *buf1++=0;
1721 goto done;
1722 }
1723
1724 if (scale >= from->frac)
1725 goto done; /* nothing to do */
1726
1727 buf0+=intg0+frac0-1;
1728 buf1+=intg0+frac0-1;
1729 if (scale == frac0*DIG_PER_DEC1)
1730 {
1731 int do_inc= false;
1732 assert(frac0+intg0 >= 0);
1733 switch (round_digit) {
1734 case 0:
1735 {
1736 dec1 *p0= buf0 + (frac1-frac0);
1737 for (; p0 > buf0; p0--)
1738 {
1739 if (*p0)
1740 {
1741 do_inc= true;
1742 break;
1743 }
1744 }
1745 break;
1746 }
1747 case 5:
1748 {
1749 x= buf0[1]/DIG_MASK;
1750 do_inc= (x>5) || ((x == 5) &&
1751 (mode == HALF_UP || (frac0+intg0 > 0 && *buf0 & 1)));
1752 break;
1753 }
1754 default:
1755 break;
1756 }
1757 if (do_inc)
1758 {
1759 if (frac0+intg0>0)
1760 (*buf1)++;
1761 else
1762 *(++buf1)=DIG_BASE;
1763 }
1764 else if (frac0+intg0==0)
1765 {
1766 to->set_zero();
1767 return E_DEC_OK;
1768 }
1769 }
1770 else
1771 {
1773 int pos=frac0*DIG_PER_DEC1-scale-1;
1774 assert(frac0+intg0 > 0);
1775 x=*buf1 / powers10[pos];
1776 y=x % 10;
1777 if (y > round_digit ||
1778 (round_digit == 5 && y == 5 && (mode == HALF_UP || (x/10) & 1)))
1779 x+=10;
1780 *buf1=powers10[pos]*(x-y);
1781 }
1782 /*
1783 In case we're rounding e.g. 1.5e9 to 2.0e9, the decimal_digit_t's inside
1784 the buffer are as follows.
1785
1786 Before <1, 5e8>
1787 After <2, 5e8>
1788
1789 Hence we need to set the 2nd field to 0.
1790 The same holds if we round 1.5e-9 to 2e-9.
1791 */
1792 if (frac0 < frac1)
1793 {
1794 dec1 *buf= to->buf + ((scale == 0 && intg0 == 0) ? 1 : intg0 + frac0);
1795 dec1 *end= to->buf + len;
1796
1797 while (buf < end)
1798 *buf++=0;
1799 }
1800 if (*buf1 >= DIG_BASE)
1801 {
1802 carry=1;
1803 *buf1-=DIG_BASE;
1804 while (carry && --buf1 >= to->buf)
1805 add(*buf1, *buf1, 0, carry);
1806 if (DRIZZLE_UNLIKELY(carry))
1807 {
1808 /* shifting the number to create space for new digit */
1809 if (frac0+intg0 >= len)
1810 {
1811 frac0--;
1812 scale=frac0*DIG_PER_DEC1;
1813 error=E_DEC_TRUNCATED; /* XXX */
1814 }
1815 for (buf1=to->buf+intg0+max(frac0,0); buf1 > to->buf; buf1--)
1816 {
1817 buf1[0]=buf1[-1];
1818 }
1819 *buf1=1;
1820 to->intg++;
1821 }
1822 }
1823 else
1824 {
1825 for (;;)
1826 {
1827 if (DRIZZLE_LIKELY(*buf1))
1828 break;
1829 if (buf1-- == to->buf)
1830 {
1831 /* making 'zero' with the proper scale */
1832 dec1 *p0= to->buf + frac0 + 1;
1833 to->intg=1;
1834 to->frac= max(scale, 0);
1835 to->sign= 0;
1836 for (buf1= to->buf; buf1<p0; buf1++)
1837 *buf1= 0;
1838 return E_DEC_OK;
1839 }
1840 }
1841 }
1842
1843 /* Here we check 999.9 -> 1000 case when we need to increase intg */
1844 first_dig= to->intg % DIG_PER_DEC1;
1845 if (first_dig && (*buf1 >= powers10[first_dig]))
1846 to->intg++;
1847
1848 if (scale<0)
1849 scale=0;
1850
1851done:
1852 to->frac=scale;
1853 return error;
1854}
1855
1856static int do_add(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
1857{
1858 int intg1=round_up(from1->intg), intg2=round_up(from2->intg),
1859 frac1=round_up(from1->frac), frac2=round_up(from2->frac),
1860 frac0=max(frac1, frac2), intg0=max(intg1, intg2), error;
1861 dec1 *buf1, *buf2, *buf0, *stop, *stop2, x, carry;
1862
1863 sanity(to);
1864
1865 /* is there a need for extra word because of carry ? */
1866 x=intg1 > intg2 ? from1->buf[0] :
1867 intg2 > intg1 ? from2->buf[0] :
1868 from1->buf[0] + from2->buf[0] ;
1869 if (DRIZZLE_UNLIKELY(x > DIG_MAX-1)) /* yes, there is */
1870 {
1871 intg0++;
1872 to->buf[0]=0; /* safety */
1873 }
1874
1875 fix_intg_frac_error(to->len, intg0, frac0, error);
1876 if (DRIZZLE_UNLIKELY(error == E_DEC_OVERFLOW))
1877 {
1878 max_decimal(to->len * DIG_PER_DEC1, 0, to);
1879 return error;
1880 }
1881
1882 buf0=to->buf+intg0+frac0;
1883
1884 to->sign=from1->sign;
1885 to->frac=max(from1->frac, from2->frac);
1886 to->intg=intg0*DIG_PER_DEC1;
1887 if (DRIZZLE_UNLIKELY(error))
1888 {
1889 set_if_smaller(to->frac, frac0*DIG_PER_DEC1);
1890 set_if_smaller(frac1, frac0);
1891 set_if_smaller(frac2, frac0);
1892 set_if_smaller(intg1, intg0);
1893 set_if_smaller(intg2, intg0);
1894 }
1895
1896 /* part 1 - cmax(frac) ... cmin(frac) */
1897 if (frac1 > frac2)
1898 {
1899 buf1=from1->buf+intg1+frac1;
1900 stop=from1->buf+intg1+frac2;
1901 buf2=from2->buf+intg2+frac2;
1902 stop2=from1->buf+(intg1 > intg2 ? intg1-intg2 : 0);
1903 }
1904 else
1905 {
1906 buf1=from2->buf+intg2+frac2;
1907 stop=from2->buf+intg2+frac1;
1908 buf2=from1->buf+intg1+frac1;
1909 stop2=from2->buf+(intg2 > intg1 ? intg2-intg1 : 0);
1910 }
1911 while (buf1 > stop)
1912 *--buf0=*--buf1;
1913
1914 /* part 2 - cmin(frac) ... cmin(intg) */
1915 carry=0;
1916 while (buf1 > stop2)
1917 {
1918 add(*--buf0, *--buf1, *--buf2, carry);
1919 }
1920
1921 /* part 3 - cmin(intg) ... cmax(intg) */
1922 buf1= intg1 > intg2 ? ((stop=from1->buf)+intg1-intg2) :
1923 ((stop=from2->buf)+intg2-intg1) ;
1924 while (buf1 > stop)
1925 {
1926 add(*--buf0, *--buf1, 0, carry);
1927 }
1928
1929 if (DRIZZLE_UNLIKELY(carry))
1930 *--buf0=1;
1931 assert(buf0 == to->buf || buf0 == to->buf+1);
1932
1933 return error;
1934}
1935
1936/* to=from1-from2.
1937 if to==0, return -1/0/+1 - the result of the comparison */
1938static int do_sub(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
1939{
1940 int intg1=round_up(from1->intg), intg2=round_up(from2->intg),
1941 frac1=round_up(from1->frac), frac2=round_up(from2->frac);
1942 int frac0=max(frac1, frac2), error;
1943 dec1 *buf1, *buf2, *buf0, *stop1, *stop2, *start1, *start2, carry=0;
1944
1945 /* let carry:=1 if from2 > from1 */
1946 start1=buf1=from1->buf; stop1=buf1+intg1;
1947 start2=buf2=from2->buf; stop2=buf2+intg2;
1948 if (DRIZZLE_UNLIKELY(*buf1 == 0))
1949 {
1950 while (buf1 < stop1 && *buf1 == 0)
1951 buf1++;
1952 start1=buf1;
1953 intg1= (int) (stop1-buf1);
1954 }
1955 if (DRIZZLE_UNLIKELY(*buf2 == 0))
1956 {
1957 while (buf2 < stop2 && *buf2 == 0)
1958 buf2++;
1959 start2=buf2;
1960 intg2= (int) (stop2-buf2);
1961 }
1962 if (intg2 > intg1)
1963 carry=1;
1964 else if (intg2 == intg1)
1965 {
1966 dec1 *end1= stop1 + (frac1 - 1);
1967 dec1 *end2= stop2 + (frac2 - 1);
1968 while (DRIZZLE_UNLIKELY((buf1 <= end1) && (*end1 == 0)))
1969 end1--;
1970 while (DRIZZLE_UNLIKELY((buf2 <= end2) && (*end2 == 0)))
1971 end2--;
1972 frac1= (int) (end1 - stop1) + 1;
1973 frac2= (int) (end2 - stop2) + 1;
1974 while (buf1 <=end1 && buf2 <= end2 && *buf1 == *buf2)
1975 buf1++, buf2++;
1976 if (buf1 <= end1)
1977 {
1978 if (buf2 <= end2)
1979 carry= *buf2 > *buf1;
1980 else
1981 carry= 0;
1982 }
1983 else
1984 {
1985 if (buf2 <= end2)
1986 carry=1;
1987 else /* short-circuit everything: from1 == from2 */
1988 {
1989 if (to == 0) /* decimal_cmp() */
1990 return 0;
1991
1992 to->set_zero();
1993
1994 return E_DEC_OK;
1995 }
1996 }
1997 }
1998
1999 if (to == 0) /* decimal_cmp() */
2000 return carry == from1->sign ? 1 : -1;
2001
2002 sanity(to);
2003
2004 to->sign=from1->sign;
2005
2006 /* ensure that always from1 > from2 (and intg1 >= intg2) */
2007 if (carry)
2008 {
2009 swap(from1, from2);
2010 swap(start1, start2);
2011 swap(intg1, intg2);
2012 swap(frac1, frac2);
2013 to->sign= 1 - to->sign;
2014 }
2015
2016 fix_intg_frac_error(to->len, intg1, frac0, error);
2017 buf0=to->buf+intg1+frac0;
2018
2019 to->frac=max(from1->frac, from2->frac);
2020 to->intg=intg1*DIG_PER_DEC1;
2021 if (DRIZZLE_UNLIKELY(error))
2022 {
2023 set_if_smaller(to->frac, frac0*DIG_PER_DEC1);
2024 set_if_smaller(frac1, frac0);
2025 set_if_smaller(frac2, frac0);
2026 set_if_smaller(intg2, intg1);
2027 }
2028 carry=0;
2029
2030 /* part 1 - cmax(frac) ... cmin(frac) */
2031 if (frac1 > frac2)
2032 {
2033 buf1=start1+intg1+frac1;
2034 stop1=start1+intg1+frac2;
2035 buf2=start2+intg2+frac2;
2036 while (frac0-- > frac1)
2037 *--buf0=0;
2038 while (buf1 > stop1)
2039 *--buf0=*--buf1;
2040 }
2041 else
2042 {
2043 buf1=start1+intg1+frac1;
2044 buf2=start2+intg2+frac2;
2045 stop2=start2+intg2+frac1;
2046 while (frac0-- > frac2)
2047 *--buf0=0;
2048 while (buf2 > stop2)
2049 {
2050 sub(*--buf0, 0, *--buf2, carry);
2051 }
2052 }
2053
2054 /* part 2 - cmin(frac) ... intg2 */
2055 while (buf2 > start2)
2056 {
2057 sub(*--buf0, *--buf1, *--buf2, carry);
2058 }
2059
2060 /* part 3 - intg2 ... intg1 */
2061 while (carry && buf1 > start1)
2062 {
2063 sub(*--buf0, *--buf1, 0, carry);
2064 }
2065
2066 while (buf1 > start1)
2067 *--buf0=*--buf1;
2068
2069 while (buf0 > to->buf)
2070 *--buf0=0;
2071
2072 return error;
2073}
2074
2075int decimal_intg(const decimal_t *from)
2076{
2077 int res;
2078 remove_leading_zeroes(from, &res);
2079 return res;
2080}
2081
2082int decimal_add(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
2083{
2084 if (DRIZZLE_LIKELY(from1->sign == from2->sign))
2085 return do_add(from1, from2, to);
2086 return do_sub(from1, from2, to);
2087}
2088
2089int decimal_sub(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
2090{
2091 if (DRIZZLE_LIKELY(from1->sign == from2->sign))
2092 return do_sub(from1, from2, to);
2093 return do_add(from1, from2, to);
2094}
2095
2096int decimal_cmp(const decimal_t *from1, const decimal_t *from2)
2097{
2098 if (DRIZZLE_LIKELY(from1->sign == from2->sign))
2099 return do_sub(from1, from2, 0);
2100 return from1->sign > from2->sign ? -1 : 1;
2101}
2102
2103int decimal_t::isZero() const
2104{
2105 dec1 *buf1= buf,
2106 *end= buf1 +round_up(intg) +round_up(frac);
2107
2108 while (buf1 < end)
2109 {
2110 if (*buf1++)
2111 {
2112 return 0;
2113 }
2114 }
2115
2116 return 1;
2117}
2118
2139int decimal_mul(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
2140{
2141 int intg1=round_up(from1->intg), intg2=round_up(from2->intg),
2142 frac1=round_up(from1->frac), frac2=round_up(from2->frac),
2143 intg0=round_up(from1->intg+from2->intg),
2144 frac0=frac1+frac2, error, i, j, d_to_move;
2145 dec1 *buf1=from1->buf+intg1, *buf2=from2->buf+intg2, *buf0,
2146 *start2, *stop2, *stop1, *start0, carry;
2147
2148 sanity(to);
2149
2150 i=intg0;
2151 j=frac0;
2152 fix_intg_frac_error(to->len, intg0, frac0, error);
2153 to->sign=from1->sign != from2->sign;
2154 to->frac=from1->frac+from2->frac;
2155 to->intg=intg0*DIG_PER_DEC1;
2156
2157 if (DRIZZLE_UNLIKELY(error))
2158 {
2159 set_if_smaller(to->frac, frac0*DIG_PER_DEC1);
2160 set_if_smaller(to->intg, intg0*DIG_PER_DEC1);
2161 if (DRIZZLE_UNLIKELY(i > intg0))
2162 {
2163 i-=intg0;
2164 j=i >> 1;
2165 intg1-= j;
2166 intg2-=i-j;
2167 frac1=frac2=0; /* frac0 is already 0 here */
2168 }
2169 else
2170 {
2171 j-=frac0;
2172 i=j >> 1;
2173 frac1-= i;
2174 frac2-=j-i;
2175 }
2176 }
2177 start0=to->buf+intg0+frac0-1;
2178 start2=buf2+frac2-1;
2179 stop1=buf1-intg1;
2180 stop2=buf2-intg2;
2181
2182 memset(to->buf, 0, (intg0+frac0)*sizeof(dec1));
2183
2184 for (buf1+=frac1-1; buf1 >= stop1; buf1--, start0--)
2185 {
2186 carry=0;
2187 for (buf0=start0, buf2=start2; buf2 >= stop2; buf2--, buf0--)
2188 {
2189 dec1 hi, lo;
2190 dec2 p= ((dec2)*buf1) * ((dec2)*buf2);
2191 hi=(dec1)(p/DIG_BASE);
2192 lo=(dec1)(p-((dec2)hi)*DIG_BASE);
2193 add2(*buf0, *buf0, lo, carry);
2194 carry+=hi;
2195 }
2196 if (carry)
2197 {
2198 if (buf0 < to->buf)
2199 return E_DEC_OVERFLOW;
2200 add2(*buf0, *buf0, 0, carry);
2201 }
2202 for (buf0--; carry; buf0--)
2203 {
2204 if (buf0 < to->buf)
2205 return E_DEC_OVERFLOW;
2206 add(*buf0, *buf0, 0, carry);
2207 }
2208 }
2209
2210 /* Now we have to check for -0.000 case */
2211 if (to->sign)
2212 {
2213 dec1 *buf= to->buf;
2214 dec1 *end= to->buf + intg0 + frac0;
2215 assert(buf != end);
2216 for (;;)
2217 {
2218 if (*buf)
2219 break;
2220 if (++buf == end)
2221 {
2222 /* We got decimal zero */
2223 to->set_zero();
2224 break;
2225 }
2226 }
2227 }
2228 buf1= to->buf;
2229 d_to_move= intg0 + round_up(to->frac);
2230 while (!*buf1 && (to->intg > DIG_PER_DEC1))
2231 {
2232 buf1++;
2233 to->intg-= DIG_PER_DEC1;
2234 d_to_move--;
2235 }
2236 if (to->buf < buf1)
2237 {
2238 dec1 *cur_d= to->buf;
2239 for (; d_to_move--; cur_d++, buf1++)
2240 *cur_d= *buf1;
2241 }
2242 return error;
2243}
2244
2256static int do_div_mod(const decimal_t *from1, const decimal_t *from2,
2257 decimal_t *to, decimal_t *mod, int scale_incr)
2258{
2259 int frac1=round_up(from1->frac)*DIG_PER_DEC1, prec1=from1->intg+frac1,
2260 frac2=round_up(from2->frac)*DIG_PER_DEC1, prec2=from2->intg+frac2,
2261 error= 0, i, intg0, frac0, len1, len2, dintg, div_mod=(!mod);
2262 dec1 *buf0, *buf1=from1->buf, *buf2=from2->buf, *tmp1,
2263 *start2, *stop2, *stop1, *stop0, norm2, carry, *start1, dcarry;
2264 dec2 norm_factor, x, guess, y;
2265
2266 if (mod)
2267 to=mod;
2268
2269 sanity(to);
2270
2271 /* removing all the leading zeroes */
2272 i= ((prec2 - 1) % DIG_PER_DEC1) + 1;
2273 while (prec2 > 0 && *buf2 == 0)
2274 {
2275 prec2-= i;
2276 i= DIG_PER_DEC1;
2277 buf2++;
2278 }
2279 if (prec2 <= 0) /* short-circuit everything: from2 == 0 */
2280 return E_DEC_DIV_ZERO;
2281 for (i= (prec2 - 1) % DIG_PER_DEC1; *buf2 < powers10[i--]; prec2--) ;
2282 assert(prec2 > 0);
2283
2284 i=((prec1-1) % DIG_PER_DEC1)+1;
2285 while (prec1 > 0 && *buf1 == 0)
2286 {
2287 prec1-=i;
2288 i=DIG_PER_DEC1;
2289 buf1++;
2290 }
2291 if (prec1 <= 0)
2292 { /* short-circuit everything: from1 == 0 */
2293 to->set_zero();
2294 return E_DEC_OK;
2295 }
2296 for (i=(prec1-1) % DIG_PER_DEC1; *buf1 < powers10[i--]; prec1--) ;
2297 assert(prec1 > 0);
2298
2299 /* let's fix scale_incr, taking into account frac1,frac2 increase */
2300 if ((scale_incr-= frac1 - from1->frac + frac2 - from2->frac) < 0)
2301 scale_incr=0;
2302
2303 dintg=(prec1-frac1)-(prec2-frac2)+(*buf1 >= *buf2);
2304 if (dintg < 0)
2305 {
2306 dintg/=DIG_PER_DEC1;
2307 intg0=0;
2308 }
2309 else
2310 intg0=round_up(dintg);
2311 if (mod)
2312 {
2313 /* we're calculating N1 % N2.
2314 The result will have
2315 frac=cmax(frac1, frac2), as for subtraction
2316 intg=intg2
2317 */
2318 to->sign=from1->sign;
2319 to->frac=max(from1->frac, from2->frac);
2320 frac0=0;
2321 }
2322 else
2323 {
2324 /*
2325 we're calculating N1/N2. N1 is in the buf1, has prec1 digits
2326 N2 is in the buf2, has prec2 digits. Scales are frac1 and
2327 frac2 accordingly.
2328 Thus, the result will have
2329 frac = round_up(frac1+frac2+scale_incr)
2330 and
2331 intg = (prec1-frac1) - (prec2-frac2) + 1
2332 prec = intg+frac
2333 */
2334 frac0=round_up(frac1+frac2+scale_incr);
2335 fix_intg_frac_error(to->len, intg0, frac0, error);
2336 to->sign=from1->sign != from2->sign;
2337 to->intg=intg0*DIG_PER_DEC1;
2338 to->frac=frac0*DIG_PER_DEC1;
2339 }
2340 buf0=to->buf;
2341 stop0=buf0+intg0+frac0;
2342 if (DRIZZLE_LIKELY(div_mod))
2343 while (dintg++ < 0)
2344 *buf0++=0;
2345
2346 len1=(i=round_up(prec1))+round_up(2*frac2+scale_incr+1) + 1;
2347 set_if_bigger(len1, 3);
2348 if (!(tmp1=(dec1 *)alloca(len1*sizeof(dec1))))
2349 return E_DEC_OOM;
2350 memcpy(tmp1, buf1, i*sizeof(dec1));
2351 memset(tmp1+i, 0, (len1-i)*sizeof(dec1));
2352
2353 start1=tmp1;
2354 stop1=start1+len1;
2355 start2=buf2;
2356 stop2=buf2+round_up(prec2)-1;
2357
2358 /* removing end zeroes */
2359 while (*stop2 == 0 && stop2 >= start2)
2360 stop2--;
2361 len2= (int) (stop2++ - start2);
2362
2363 /*
2364 calculating norm2 (normalized *start2) - we need *start2 to be large
2365 (at least > DIG_BASE/2), but unlike Knuth's Alg. D we don't want to
2366 normalize input numbers (as we don't make a copy of the divisor).
2367 Thus we normalize first dec1 of buf2 only, and we'll normalize *start1
2368 on the fly for the purpose of guesstimation only.
2369 It's also faster, as we're saving on normalization of buf2
2370 */
2371 norm_factor=DIG_BASE/(*start2+1);
2372 norm2=(dec1)(norm_factor*start2[0]);
2373 if (DRIZZLE_LIKELY(len2>0))
2374 norm2+=(dec1)(norm_factor*start2[1]/DIG_BASE);
2375
2376 if (*start1 < *start2)
2377 dcarry=*start1++;
2378 else
2379 dcarry=0;
2380
2381 /* main loop */
2382 for (; buf0 < stop0; buf0++)
2383 {
2384 /* short-circuit, if possible */
2385 if (DRIZZLE_UNLIKELY(dcarry == 0 && *start1 < *start2))
2386 guess=0;
2387 else
2388 {
2389 /* D3: make a guess */
2390 x=start1[0]+((dec2)dcarry)*DIG_BASE;
2391 y=start1[1];
2392 guess=(norm_factor*x+norm_factor*y/DIG_BASE)/norm2;
2393 if (DRIZZLE_UNLIKELY(guess >= DIG_BASE))
2394 guess=DIG_BASE-1;
2395 if (DRIZZLE_LIKELY(len2>0))
2396 {
2397 /* hmm, this is a suspicious trick - I removed normalization here */
2398 if (start2[1]*guess > (x-guess*start2[0])*DIG_BASE+y)
2399 guess--;
2400 if (DRIZZLE_UNLIKELY(start2[1]*guess > (x-guess*start2[0])*DIG_BASE+y))
2401 guess--;
2402 assert(start2[1]*guess <= (x-guess*start2[0])*DIG_BASE+y);
2403 }
2404
2405 /* D4: multiply and subtract */
2406 buf2=stop2;
2407 buf1=start1+len2;
2408 assert(buf1 < stop1);
2409 for (carry=0; buf2 > start2; buf1--)
2410 {
2411 dec1 hi, lo;
2412 x=guess * (*--buf2);
2413 hi=(dec1)(x/DIG_BASE);
2414 lo=(dec1)(x-((dec2)hi)*DIG_BASE);
2415 sub2(*buf1, *buf1, lo, carry);
2416 carry+=hi;
2417 }
2418 carry= dcarry < carry;
2419
2420 /* D5: check the remainder */
2421 if (DRIZZLE_UNLIKELY(carry))
2422 {
2423 /* D6: correct the guess */
2424 guess--;
2425 buf2=stop2;
2426 buf1=start1+len2;
2427 for (carry=0; buf2 > start2; buf1--)
2428 {
2429 add(*buf1, *buf1, *--buf2, carry);
2430 }
2431 }
2432 }
2433 if (DRIZZLE_LIKELY(div_mod))
2434 *buf0=(dec1)guess;
2435 dcarry= *start1;
2436 start1++;
2437 }
2438 if (mod)
2439 {
2440 /*
2441 now the result is in tmp1, it has
2442 intg=prec1-frac1
2443 frac=cmax(frac1, frac2)=to->frac
2444 */
2445 if (dcarry)
2446 *--start1=dcarry;
2447 buf0=to->buf;
2448 intg0=(int) (round_up(prec1-frac1)-(start1-tmp1));
2449 frac0=round_up(to->frac);
2450 error=E_DEC_OK;
2451 if (DRIZZLE_UNLIKELY(frac0==0 && intg0==0))
2452 {
2453 to->set_zero();
2454 goto done;
2455 }
2456 if (intg0<=0)
2457 {
2458 if (DRIZZLE_UNLIKELY(-intg0 >= to->len))
2459 {
2460 to->set_zero();
2461 error=E_DEC_TRUNCATED;
2462 goto done;
2463 }
2464 stop1=start1+frac0;
2465 frac0+=intg0;
2466 to->intg=0;
2467 while (intg0++ < 0)
2468 *buf0++=0;
2469 }
2470 else
2471 {
2472 if (DRIZZLE_UNLIKELY(intg0 > to->len))
2473 {
2474 frac0=0;
2475 intg0=to->len;
2476 error=E_DEC_OVERFLOW;
2477 goto done;
2478 }
2479 assert(intg0 <= round_up(from2->intg));
2480 stop1=start1+frac0+intg0;
2481 to->intg=min(intg0*DIG_PER_DEC1, from2->intg);
2482 }
2483 if (DRIZZLE_UNLIKELY(intg0+frac0 > to->len))
2484 {
2485 stop1-=frac0+intg0-to->len;
2486 frac0=to->len-intg0;
2487 to->frac=frac0*DIG_PER_DEC1;
2488 error=E_DEC_TRUNCATED;
2489 }
2490 assert(buf0 + (stop1 - start1) <= to->buf + to->len);
2491 while (start1 < stop1)
2492 *buf0++=*start1++;
2493 }
2494done:
2495 return error;
2496}
2497
2511int
2512decimal_div(const decimal_t *from1, const decimal_t *from2, decimal_t *to, int scale_incr)
2513{
2514 return do_div_mod(from1, from2, to, 0, scale_incr);
2515}
2516
2542int decimal_mod(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
2543{
2544 return do_div_mod(from1, from2, 0, to, 0);
2545}
2546
2547std::ostream& operator<<(std::ostream& output, const type::Decimal &dec)
2548{
2549 drizzled::String str;
2550
2551 class_decimal2string(&dec, 0, &str);
2552
2553 output << "type::Decimal:(";
2554 output << str.c_ptr();
2555 output << ")";
2556
2557 return output; // for multiple << operators.
2558}
2559
2560} /* namespace drizzled */
2561
2562#ifdef MAIN
2563
2564int full= 0;
2565decimal_t a, b, c;
2566char buf1[100], buf2[100], buf3[100];
2567
2568void dump_decimal(decimal_t *d)
2569{
2570 int i;
2571 printf("/* intg=%d, frac=%d, sign=%d, buf[]={", d->intg, d->frac, d->sign);
2572 for (i=0; i < round_up(d->frac)+round_up(d->intg)-1; i++)
2573 printf("%09d, ", d->buf[i]);
2574 printf("%09d} */ ", d->buf[i]);
2575}
2576
2577
2578void check_result_code(int actual, int want)
2579{
2580 if (actual != want)
2581 {
2582 printf("\n^^^^^^^^^^^^^ must return %d\n", want);
2583 exit(1);
2584 }
2585}
2586
2587
2588void print_decimal(decimal_t *d, const char *orig, int actual, int want)
2589{
2590 char s[100];
2591 int slen=sizeof(s);
2592
2593 if (full) dump_decimal(d);
2594 decimal2string(d, s, &slen, 0, 0, 0);
2595 printf("'%s'", s);
2596 check_result_code(actual, want);
2597 if (orig && strcmp(orig, s))
2598 {
2599 printf("\n^^^^^^^^^^^^^ must've been '%s'\n", orig);
2600 exit(1);
2601 }
2602}
2603
2604void test_d2s()
2605{
2606 char s[100];
2607 int slen, res;
2608
2609 /***********************************/
2610 printf("==== decimal2string ====\n");
2611 a.buf[0]=12345; a.intg=5; a.frac=0; a.sign=0;
2612 slen=sizeof(s);
2613 res=decimal2string(&a, s, &slen, 0, 0, 0);
2614 dump_decimal(&a); printf(" --> res=%d str='%s' len=%d\n", res, s, slen);
2615
2616 a.buf[1]=987000000; a.frac=3;
2617 slen=sizeof(s);
2618 res=decimal2string(&a, s, &slen, 0, 0, 0);
2619 dump_decimal(&a); printf(" --> res=%d str='%s' len=%d\n", res, s, slen);
2620
2621 a.sign=1;
2622 slen=sizeof(s);
2623 res=decimal2string(&a, s, &slen, 0, 0, 0);
2624 dump_decimal(&a); printf(" --> res=%d str='%s' len=%d\n", res, s, slen);
2625
2626 slen=8;
2627 res=decimal2string(&a, s, &slen, 0, 0, 0);
2628 dump_decimal(&a); printf(" --> res=%d str='%s' len=%d\n", res, s, slen);
2629
2630 slen=5;
2631 res=decimal2string(&a, s, &slen, 0, 0, 0);
2632 dump_decimal(&a); printf(" --> res=%d str='%s' len=%d\n", res, s, slen);
2633
2634 a.buf[0]=987000000; a.frac=3; a.intg=0;
2635 slen=sizeof(s);
2636 res=decimal2string(&a, s, &slen, 0, 0, 0);
2637 dump_decimal(&a); printf(" --> res=%d str='%s' len=%d\n", res, s, slen);
2638}
2639
2640void test_s2d(const char *s, const char *orig, int ex)
2641{
2642 char s1[100], *end;
2643 int res;
2644 snprintf(s1, sizeof(s1), "'%s'", s);
2645 end= strend(s);
2646 printf("len=%2d %-30s => res=%d ", a.len, s1,
2647 (res= string2decimal(s, &a, &end)));
2648 print_decimal(&a, orig, res, ex);
2649 printf("\n");
2650}
2651
2652void test_d2f(const char *s, int ex)
2653{
2654 char s1[100], *end;
2655 double x;
2656 int res;
2657
2658 snprintf(s1, sizeof(s1), "'%s'", s);
2659 end= strend(s);
2660 string2decimal(s, &a, &end);
2661 res=decimal2double(&a, &x);
2662 if (full) dump_decimal(&a);
2663 printf("%-40s => res=%d %.*g\n", s1, res, a.intg+a.frac, x);
2664 check_result_code(res, ex);
2665}
2666
2667void test_d2b2d(const char *str, int p, int s, const char *orig, int ex)
2668{
2669 char s1[100], buf[100], *end;
2670 int size=decimal_bin_size(p, s);
2671
2672 snprintf(s1, sizeof(s1), "'%s'", str);
2673 end= strend(str);
2674 string2decimal(str, &a, &end);
2675 int res=decimal2bin(&a, buf, p, s);
2676 printf("%-31s {%2d, %2d} => res=%d size=%-2d ", s1, p, s, res, size);
2677 if (full)
2678 {
2679 printf("0x");
2680 for (int i= 0; i < size; i++)
2681 printf("%02x", ((unsigned char *)buf)[i]);
2682 }
2683 res=bin2decimal(buf, &a, p, s);
2684 printf(" => res=%d ", res);
2685 print_decimal(&a, orig, res, ex);
2686 printf("\n");
2687}
2688
2689void test_f2d(double from, int ex)
2690{
2691 int res;
2692
2693 res=double2decimal(from, &a);
2694 printf("%-40.*f => res=%d ", DBL_DIG-2, from, res);
2695 print_decimal(&a, 0, res, ex);
2696 printf("\n");
2697}
2698
2699void test_ull2d(uint64_t from, const char *orig, int ex)
2700{
2701 char s[100];
2702 int res;
2703
2704 res=uint64_t2decimal(from, &a);
2705 internal::int64_t10_to_str(from,s,10);
2706 printf("%-40s => res=%d ", s, res);
2707 print_decimal(&a, orig, res, ex);
2708 printf("\n");
2709}
2710
2711void test_ll2d(int64_t from, const char *orig, int ex)
2712{
2713 char s[100];
2714 int res;
2715
2716 res=int64_t2decimal(from, &a);
2717 internal::int64_t10_to_str(from,s,-10);
2718 printf("%-40s => res=%d ", s, res);
2719 print_decimal(&a, orig, res, ex);
2720 printf("\n");
2721}
2722
2723void test_d2ull(const char *s, const char *orig, int ex)
2724{
2725 char s1[100], *end;
2726 uint64_t x;
2727 int res;
2728
2729 end= strend(s);
2730 string2decimal(s, &a, &end);
2731 res=decimal2uint64_t(&a, &x);
2732 if (full) dump_decimal(&a);
2733 internal::int64_t10_to_str(x,s1,10);
2734 printf("%-40s => res=%d %s\n", s, res, s1);
2735 check_result_code(res, ex);
2736 if (orig && strcmp(orig, s1))
2737 {
2738 printf("\n^^^^^^^^^^^^^ must've been '%s'\n", orig);
2739 exit(1);
2740 }
2741}
2742
2743void test_d2ll(const char *s, const char *orig, int ex)
2744{
2745 char s1[100], *end;
2746 int64_t x;
2747 int res;
2748
2749 end= strend(s);
2750 string2decimal(s, &a, &end);
2751 res=decimal2int64_t(&a, &x);
2752 if (full) dump_decimal(&a);
2753 internal::int64_t10_to_str(x,s1,-10);
2754 printf("%-40s => res=%d %s\n", s, res, s1);
2755 check_result_code(res, ex);
2756 if (orig && strcmp(orig, s1))
2757 {
2758 printf("\n^^^^^^^^^^^^^ must've been '%s'\n", orig);
2759 exit(1);
2760 }
2761}
2762
2763void test_da(const char *s1, const char *s2, const char *orig, int ex)
2764{
2765 char s[100], *end;
2766 int res;
2767 snprintf(s, sizeof(s), "'%s' + '%s'", s1, s2);
2768 end= strend(s1);
2769 string2decimal(s1, &a, &end);
2770 end= strend(s2);
2771 string2decimal(s2, &b, &end);
2772 res=decimal_add(&a, &b, &c);
2773 printf("%-40s => res=%d ", s, res);
2774 print_decimal(&c, orig, res, ex);
2775 printf("\n");
2776}
2777
2778void test_ds(const char *s1, const char *s2, const char *orig, int ex)
2779{
2780 char s[100], *end;
2781 int res;
2782 snprintf(s, sizeof(s), "'%s' - '%s'", s1, s2);
2783 end= strend(s1);
2784 string2decimal(s1, &a, &end);
2785 end= strend(s2);
2786 string2decimal(s2, &b, &end);
2787 res=decimal_sub(&a, &b, &c);
2788 printf("%-40s => res=%d ", s, res);
2789 print_decimal(&c, orig, res, ex);
2790 printf("\n");
2791}
2792
2793void test_dc(const char *s1, const char *s2, int orig)
2794{
2795 char s[100], *end;
2796 int res;
2797 snprintf(s, sizeof(s), "'%s' <=> '%s'", s1, s2);
2798 end= strend(s1);
2799 string2decimal(s1, &a, &end);
2800 end= strend(s2);
2801 string2decimal(s2, &b, &end);
2802 res=decimal_cmp(&a, &b);
2803 printf("%-40s => res=%d\n", s, res);
2804 if (orig != res)
2805 {
2806 printf("\n^^^^^^^^^^^^^ must've been %d\n", orig);
2807 exit(1);
2808 }
2809}
2810
2811void test_dm(const char *s1, const char *s2, const char *orig, int ex)
2812{
2813 char s[100], *end;
2814 int res;
2815 snprintf(s, sizeof(s), "'%s' * '%s'", s1, s2);
2816 end= strend(s1);
2817 string2decimal(s1, &a, &end);
2818 end= strend(s2);
2819 string2decimal(s2, &b, &end);
2820 res=decimal_mul(&a, &b, &c);
2821 printf("%-40s => res=%d ", s, res);
2822 print_decimal(&c, orig, res, ex);
2823 printf("\n");
2824}
2825
2826void test_dv(const char *s1, const char *s2, const char *orig, int ex)
2827{
2828 char s[100], *end;
2829 int res;
2830 snprintf(s, sizeof(s), "'%s' / '%s'", s1, s2);
2831 end= strend(s1);
2832 string2decimal(s1, &a, &end);
2833 end= strend(s2);
2834 string2decimal(s2, &b, &end);
2835 res=decimal_div(&a, &b, &c, 5);
2836 printf("%-40s => res=%d ", s, res);
2837 check_result_code(res, ex);
2838 if (res == E_DEC_DIV_ZERO)
2839 printf("E_DEC_DIV_ZERO");
2840 else
2841 print_decimal(&c, orig, res, ex);
2842 printf("\n");
2843}
2844
2845void test_md(const char *s1, const char *s2, const char *orig, int ex)
2846{
2847 char s[100], *end;
2848 int res;
2849 snprintf(s, sizeof(s), "'%s' %% '%s'", s1, s2);
2850 end= strend(s1);
2851 string2decimal(s1, &a, &end);
2852 end= strend(s2);
2853 string2decimal(s2, &b, &end);
2854 res=decimal_mod(&a, &b, &c);
2855 printf("%-40s => res=%d ", s, res);
2856 check_result_code(res, ex);
2857 if (res == E_DEC_DIV_ZERO)
2858 printf("E_DEC_DIV_ZERO");
2859 else
2860 print_decimal(&c, orig, res, ex);
2861 printf("\n");
2862}
2863
2864const char *round_mode[]=
2865{"TRUNCATE", "HALF_EVEN", "HALF_UP", "CEILING", "FLOOR"};
2866
2867void test_ro(const char *s1, int n, decimal_round_mode mode, const char *orig,
2868 int ex)
2869{
2870 char s[100], *end;
2871 int res;
2872 snprintf(s, sizeof(s), "'%s', %d, %s", s1, n, round_mode[mode]);
2873 end= strend(s1);
2874 string2decimal(s1, &a, &end);
2875 res=decimal_round(&a, &b, n, mode);
2876 printf("%-40s => res=%d ", s, res);
2877 print_decimal(&b, orig, res, ex);
2878 printf("\n");
2879}
2880
2881
2882void test_mx(int precision, int frac, const char *orig)
2883{
2884 char s[100];
2885 snprintf(s, sizeof(s), "%d, %d", precision, frac);
2886 max_decimal(precision, frac, &a);
2887 printf("%-40s => ", s);
2888 print_decimal(&a, orig, 0, 0);
2889 printf("\n");
2890}
2891
2892
2893void test_pr(const char *s1, int prec, int dec, char filler, const char *orig,
2894 int ex)
2895{
2896 char s[100], *end;
2897 char s2[100];
2898 int slen= sizeof(s2);
2899 int res;
2900
2901 snprintf(s, sizeof(s), filler ? "'%s', %d, %d, '%c'" : "'%s', %d, %d, '\\0'",
2902 s1, prec, dec, filler);
2903 end= strend(s1);
2904 string2decimal(s1, &a, &end);
2905 res= decimal2string(&a, s2, &slen, prec, dec, filler);
2906 printf("%-40s => res=%d '%s'", s, res, s2);
2907 check_result_code(res, ex);
2908 if (orig && strcmp(orig, s2))
2909 {
2910 printf("\n^^^^^^^^^^^^^ must've been '%s'\n", orig);
2911 exit(1);
2912 }
2913 printf("\n");
2914}
2915
2916
2917void test_sh(const char *s1, int shift, const char *orig, int ex)
2918{
2919 char s[100], *end;
2920 int res;
2921 snprintf(s, sizeof(s), "'%s' %s %d", s1, ((shift < 0) ? ">>" : "<<"), abs(shift));
2922 end= strend(s1);
2923 string2decimal(s1, &a, &end);
2924 res= decimal_shift(&a, shift);
2925 printf("%-40s => res=%d ", s, res);
2926 print_decimal(&a, orig, res, ex);
2927 printf("\n");
2928}
2929
2930
2931void test_fr(const char *s1, const char *orig)
2932{
2933 char s[100], *end;
2934 snprintf(s, sizeof(s), "'%s'", s1);
2935 printf("%-40s => ", s);
2936 end= strend(s1);
2937 string2decimal(s1, &a, &end);
2938 a.frac= decimal_actual_fraction(&a);
2939 print_decimal(&a, orig, 0, 0);
2940 printf("\n");
2941}
2942
2943
2944int main()
2945{
2946 a.buf=(void*)buf1;
2947 a.len=sizeof(buf1)/sizeof(dec1);
2948 b.buf=(void*)buf2;
2949 b.len=sizeof(buf2)/sizeof(dec1);
2950 c.buf=(void*)buf3;
2951 c.len=sizeof(buf3)/sizeof(dec1);
2952
2953 if (full)
2954 test_d2s();
2955
2956 printf("==== string2decimal ====\n");
2957 test_s2d("12345", "12345", 0);
2958 test_s2d("12345.", "12345", 0);
2959 test_s2d("123.45", "123.45", 0);
2960 test_s2d("-123.45", "-123.45", 0);
2961 test_s2d(".00012345000098765", "0.00012345000098765", 0);
2962 test_s2d(".12345000098765", "0.12345000098765", 0);
2963 test_s2d("-.000000012345000098765", "-0.000000012345000098765", 0);
2964 test_s2d("1234500009876.5", "1234500009876.5", 0);
2965 a.len=1;
2966 test_s2d("123450000098765", "98765", 2);
2967 test_s2d("123450.000098765", "123450", 1);
2968 a.len=sizeof(buf1)/sizeof(dec1);
2969 test_s2d("123E5", "12300000", 0);
2970 test_s2d("123E-2", "1.23", 0);
2971
2972 printf("==== decimal2double ====\n");
2973 test_d2f("12345", 0);
2974 test_d2f("123.45", 0);
2975 test_d2f("-123.45", 0);
2976 test_d2f("0.00012345000098765", 0);
2977 test_d2f("1234500009876.5", 0);
2978
2979 printf("==== double2decimal ====\n");
2980 test_f2d(12345, 0);
2981 test_f2d(1.0/3, 0);
2982 test_f2d(-123.45, 0);
2983 test_f2d(0.00012345000098765, 0);
2984 test_f2d(1234500009876.5, 0);
2985
2986 printf("==== uint64_t2decimal ====\n");
2987 test_ull2d(12345ULL, "12345", 0);
2988 test_ull2d(0ULL, "0", 0);
2989 test_ull2d(18446744073709551615ULL, "18446744073709551615", 0);
2990
2991 printf("==== decimal2uint64_t ====\n");
2992 test_d2ull("12345", "12345", 0);
2993 test_d2ull("0", "0", 0);
2994 test_d2ull("18446744073709551615", "18446744073709551615", 0);
2995 test_d2ull("18446744073709551616", "18446744073", 2);
2996 test_d2ull("-1", "0", 2);
2997 test_d2ull("1.23", "1", 1);
2998 test_d2ull("9999999999999999999999999.000", "9999999999999999", 2);
2999
3000 printf("==== int64_t2decimal ====\n");
3001 test_ll2d(12345LL, "-12345", 0);
3002 test_ll2d(1LL, "-1", 0);
3003 test_ll2d(9223372036854775807LL, "-9223372036854775807", 0);
3004 test_ll2d(9223372036854775808ULL, "-9223372036854775808", 0);
3005
3006 printf("==== decimal2int64_t ====\n");
3007 test_d2ll("18446744073709551615", "18446744073", 2);
3008 test_d2ll("-1", "-1", 0);
3009 test_d2ll("-1.23", "-1", 1);
3010 test_d2ll("-9223372036854775807", "-9223372036854775807", 0);
3011 test_d2ll("-9223372036854775808", "-9223372036854775808", 0);
3012 test_d2ll("9223372036854775808", "9223372036854775807", 2);
3013
3014 printf("==== do_add ====\n");
3015 test_da(".00012345000098765" ,"123.45", "123.45012345000098765", 0);
3016 test_da(".1" ,".45", "0.55", 0);
3017 test_da("1234500009876.5" ,".00012345000098765", "1234500009876.50012345000098765", 0);
3018 test_da("9999909999999.5" ,".555", "9999910000000.055", 0);
3019 test_da("99999999" ,"1", "100000000", 0);
3020 test_da("989999999" ,"1", "990000000", 0);
3021 test_da("999999999" ,"1", "1000000000", 0);
3022 test_da("12345" ,"123.45", "12468.45", 0);
3023 test_da("-12345" ,"-123.45", "-12468.45", 0);
3024 test_ds("-12345" ,"123.45", "-12468.45", 0);
3025 test_ds("12345" ,"-123.45", "12468.45", 0);
3026
3027 printf("==== do_sub ====\n");
3028 test_ds(".00012345000098765", "123.45","-123.44987654999901235", 0);
3029 test_ds("1234500009876.5", ".00012345000098765","1234500009876.49987654999901235", 0);
3030 test_ds("9999900000000.5", ".555","9999899999999.945", 0);
3031 test_ds("1111.5551", "1111.555","0.0001", 0);
3032 test_ds(".555", ".555","0", 0);
3033 test_ds("10000000", "1","9999999", 0);
3034 test_ds("1000001000", ".1","1000000999.9", 0);
3035 test_ds("1000000000", ".1","999999999.9", 0);
3036 test_ds("12345", "123.45","12221.55", 0);
3037 test_ds("-12345", "-123.45","-12221.55", 0);
3038 test_da("-12345", "123.45","-12221.55", 0);
3039 test_da("12345", "-123.45","12221.55", 0);
3040 test_ds("123.45", "12345","-12221.55", 0);
3041 test_ds("-123.45", "-12345","12221.55", 0);
3042 test_da("123.45", "-12345","-12221.55", 0);
3043 test_da("-123.45", "12345","12221.55", 0);
3044 test_da("5", "-6.0","-1.0", 0);
3045
3046 printf("==== decimal_mul ====\n");
3047 test_dm("12", "10","120", 0);
3048 test_dm("-123.456", "98765.4321","-12193185.1853376", 0);
3049 test_dm("-123456000000", "98765432100000","-12193185185337600000000000", 0);
3050 test_dm("123456", "987654321","121931851853376", 0);
3051 test_dm("123456", "9876543210","1219318518533760", 0);
3052 test_dm("123", "0.01","1.23", 0);
3053 test_dm("123", "0","0", 0);
3054
3055 printf("==== decimal_div ====\n");
3056 test_dv("120", "10","12.000000000", 0);
3057 test_dv("123", "0.01","12300.000000000", 0);
3058 test_dv("120", "100000000000.00000","0.000000001200000000", 0);
3059 test_dv("123", "0","", 4);
3060 test_dv("0", "0", "", 4);
3061 test_dv("-12193185.1853376", "98765.4321","-123.456000000000000000", 0);
3062 test_dv("121931851853376", "987654321","123456.000000000", 0);
3063 test_dv("0", "987","0", 0);
3064 test_dv("1", "3","0.333333333", 0);
3065 test_dv("1.000000000000", "3","0.333333333333333333", 0);
3066 test_dv("1", "1","1.000000000", 0);
3067 test_dv("0.0123456789012345678912345", "9999999999","0.000000000001234567890246913578148141", 0);
3068 test_dv("10.333000000", "12.34500","0.837019036046982584042122316", 0);
3069 test_dv("10.000000000060", "2","5.000000000030000000", 0);
3070
3071 printf("==== decimal_mod ====\n");
3072 test_md("234","10","4", 0);
3073 test_md("234.567","10.555","2.357", 0);
3074 test_md("-234.567","10.555","-2.357", 0);
3075 test_md("234.567","-10.555","2.357", 0);
3076 c.buf[1]=0x3ABECA;
3077 test_md("99999999999999999999999999999999999999","3","0", 0);
3078 if (c.buf[1] != 0x3ABECA)
3079 {
3080 printf("%X - overflow\n", c.buf[1]);
3081 exit(1);
3082 }
3083
3084 printf("==== decimal2bin/bin2decimal ====\n");
3085 test_d2b2d("-10.55", 4, 2,"-10.55", 0);
3086 test_d2b2d("0.0123456789012345678912345", 30, 25,"0.0123456789012345678912345", 0);
3087 test_d2b2d("12345", 5, 0,"12345", 0);
3088 test_d2b2d("12345", 10, 3,"12345.000", 0);
3089 test_d2b2d("123.45", 10, 3,"123.450", 0);
3090 test_d2b2d("-123.45", 20, 10,"-123.4500000000", 0);
3091 test_d2b2d(".00012345000098765", 15, 14,"0.00012345000098", 0);
3092 test_d2b2d(".00012345000098765", 22, 20,"0.00012345000098765000", 0);
3093 test_d2b2d(".12345000098765", 30, 20,"0.12345000098765000000", 0);
3094 test_d2b2d("-.000000012345000098765", 30, 20,"-0.00000001234500009876", 0);
3095 test_d2b2d("1234500009876.5", 30, 5,"1234500009876.50000", 0);
3096 test_d2b2d("111111111.11", 10, 2,"11111111.11", 0);
3097 test_d2b2d("000000000.01", 7, 3,"0.010", 0);
3098 test_d2b2d("123.4", 10, 2, "123.40", 0);
3099
3100
3101 printf("==== decimal_cmp ====\n");
3102 test_dc("12","13",-1);
3103 test_dc("13","12",1);
3104 test_dc("-10","10",-1);
3105 test_dc("10","-10",1);
3106 test_dc("-12","-13",1);
3107 test_dc("0","12",-1);
3108 test_dc("-10","0",-1);
3109 test_dc("4","4",0);
3110
3111 printf("==== decimal_round ====\n");
3112 test_ro("5678.123451",-4,TRUNCATE,"0", 0);
3113 test_ro("5678.123451",-3,TRUNCATE,"5000", 0);
3114 test_ro("5678.123451",-2,TRUNCATE,"5600", 0);
3115 test_ro("5678.123451",-1,TRUNCATE,"5670", 0);
3116 test_ro("5678.123451",0,TRUNCATE,"5678", 0);
3117 test_ro("5678.123451",1,TRUNCATE,"5678.1", 0);
3118 test_ro("5678.123451",2,TRUNCATE,"5678.12", 0);
3119 test_ro("5678.123451",3,TRUNCATE,"5678.123", 0);
3120 test_ro("5678.123451",4,TRUNCATE,"5678.1234", 0);
3121 test_ro("5678.123451",5,TRUNCATE,"5678.12345", 0);
3122 test_ro("5678.123451",6,TRUNCATE,"5678.123451", 0);
3123 test_ro("-5678.123451",-4,TRUNCATE,"0", 0);
3124 memset(buf2, 33, sizeof(buf2));
3125 test_ro("99999999999999999999999999999999999999",-31,TRUNCATE,"99999990000000000000000000000000000000", 0);
3126 test_ro("15.1",0,HALF_UP,"15", 0);
3127 test_ro("15.5",0,HALF_UP,"16", 0);
3128 test_ro("15.9",0,HALF_UP,"16", 0);
3129 test_ro("-15.1",0,HALF_UP,"-15", 0);
3130 test_ro("-15.5",0,HALF_UP,"-16", 0);
3131 test_ro("-15.9",0,HALF_UP,"-16", 0);
3132 test_ro("15.1",1,HALF_UP,"15.1", 0);
3133 test_ro("-15.1",1,HALF_UP,"-15.1", 0);
3134 test_ro("15.17",1,HALF_UP,"15.2", 0);
3135 test_ro("15.4",-1,HALF_UP,"20", 0);
3136 test_ro("-15.4",-1,HALF_UP,"-20", 0);
3137 test_ro("5.4",-1,HALF_UP,"10", 0);
3138 test_ro(".999", 0, HALF_UP, "1", 0);
3139 memset(buf2, 33, sizeof(buf2));
3140 test_ro("999999999", -9, HALF_UP, "1000000000", 0);
3141 test_ro("15.1",0,HALF_EVEN,"15", 0);
3142 test_ro("15.5",0,HALF_EVEN,"16", 0);
3143 test_ro("14.5",0,HALF_EVEN,"14", 0);
3144 test_ro("15.9",0,HALF_EVEN,"16", 0);
3145 test_ro("15.1",0,CEILING,"16", 0);
3146 test_ro("-15.1",0,CEILING,"-15", 0);
3147 test_ro("15.1",0,FLOOR,"15", 0);
3148 test_ro("-15.1",0,FLOOR,"-16", 0);
3149 test_ro("999999999999999999999.999", 0, CEILING,"1000000000000000000000", 0);
3150 test_ro("-999999999999999999999.999", 0, FLOOR,"-1000000000000000000000", 0);
3151
3152 b.buf[0]=DIG_BASE+1;
3153 b.buf++;
3154 test_ro(".3", 0, HALF_UP, "0", 0);
3155 b.buf--;
3156 if (b.buf[0] != DIG_BASE+1)
3157 {
3158 printf("%d - underflow\n", b.buf[0]);
3159 exit(1);
3160 }
3161
3162 printf("==== max_decimal ====\n");
3163 test_mx(1,1,"0.9");
3164 test_mx(1,0,"9");
3165 test_mx(2,1,"9.9");
3166 test_mx(4,2,"99.99");
3167 test_mx(6,3,"999.999");
3168 test_mx(8,4,"9999.9999");
3169 test_mx(10,5,"99999.99999");
3170 test_mx(12,6,"999999.999999");
3171 test_mx(14,7,"9999999.9999999");
3172 test_mx(16,8,"99999999.99999999");
3173 test_mx(18,9,"999999999.999999999");
3174 test_mx(20,10,"9999999999.9999999999");
3175 test_mx(20,20,"0.99999999999999999999");
3176 test_mx(20,0,"99999999999999999999");
3177 test_mx(40,20,"99999999999999999999.99999999999999999999");
3178
3179 printf("==== decimal2string ====\n");
3180 test_pr("123.123", 0, 0, 0, "123.123", 0);
3181 test_pr("123.123", 7, 3, '0', "123.123", 0);
3182 test_pr("123.123", 9, 3, '0', "00123.123", 0);
3183 test_pr("123.123", 9, 4, '0', "0123.1230", 0);
3184 test_pr("123.123", 9, 5, '0', "123.12300", 0);
3185 test_pr("123.123", 9, 2, '0', "000123.12", 1);
3186 test_pr("123.123", 9, 6, '0', "23.123000", 2);
3187
3188 printf("==== decimal_shift ====\n");
3189 test_sh("123.123", 1, "1231.23", 0);
3190 test_sh("123457189.123123456789000", 1, "1234571891.23123456789", 0);
3191 test_sh("123457189.123123456789000", 4, "1234571891231.23456789", 0);
3192 test_sh("123457189.123123456789000", 8, "12345718912312345.6789", 0);
3193 test_sh("123457189.123123456789000", 9, "123457189123123456.789", 0);
3194 test_sh("123457189.123123456789000", 10, "1234571891231234567.89", 0);
3195 test_sh("123457189.123123456789000", 17, "12345718912312345678900000", 0);
3196 test_sh("123457189.123123456789000", 18, "123457189123123456789000000", 0);
3197 test_sh("123457189.123123456789000", 19, "1234571891231234567890000000", 0);
3198 test_sh("123457189.123123456789000", 26, "12345718912312345678900000000000000", 0);
3199 test_sh("123457189.123123456789000", 27, "123457189123123456789000000000000000", 0);
3200 test_sh("123457189.123123456789000", 28, "1234571891231234567890000000000000000", 0);
3201 test_sh("000000000000000000000000123457189.123123456789000", 26, "12345718912312345678900000000000000", 0);
3202 test_sh("00000000123457189.123123456789000", 27, "123457189123123456789000000000000000", 0);
3203 test_sh("00000000000000000123457189.123123456789000", 28, "1234571891231234567890000000000000000", 0);
3204 test_sh("123", 1, "1230", 0);
3205 test_sh("123", 10, "1230000000000", 0);
3206 test_sh(".123", 1, "1.23", 0);
3207 test_sh(".123", 10, "1230000000", 0);
3208 test_sh(".123", 14, "12300000000000", 0);
3209 test_sh("000.000", 1000, "0", 0);
3210 test_sh("000.", 1000, "0", 0);
3211 test_sh(".000", 1000, "0", 0);
3212 test_sh("1", 1000, "1", 2);
3213 test_sh("123.123", -1, "12.3123", 0);
3214 test_sh("123987654321.123456789000", -1, "12398765432.1123456789", 0);
3215 test_sh("123987654321.123456789000", -2, "1239876543.21123456789", 0);
3216 test_sh("123987654321.123456789000", -3, "123987654.321123456789", 0);
3217 test_sh("123987654321.123456789000", -8, "1239.87654321123456789", 0);
3218 test_sh("123987654321.123456789000", -9, "123.987654321123456789", 0);
3219 test_sh("123987654321.123456789000", -10, "12.3987654321123456789", 0);
3220 test_sh("123987654321.123456789000", -11, "1.23987654321123456789", 0);
3221 test_sh("123987654321.123456789000", -12, "0.123987654321123456789", 0);
3222 test_sh("123987654321.123456789000", -13, "0.0123987654321123456789", 0);
3223 test_sh("123987654321.123456789000", -14, "0.00123987654321123456789", 0);
3224 test_sh("00000087654321.123456789000", -14, "0.00000087654321123456789", 0);
3225 a.len= 2;
3226 test_sh("123.123", -2, "1.23123", 0);
3227 test_sh("123.123", -3, "0.123123", 0);
3228 test_sh("123.123", -6, "0.000123123", 0);
3229 test_sh("123.123", -7, "0.0000123123", 0);
3230 test_sh("123.123", -15, "0.000000000000123123", 0);
3231 test_sh("123.123", -16, "0.000000000000012312", 1);
3232 test_sh("123.123", -17, "0.000000000000001231", 1);
3233 test_sh("123.123", -18, "0.000000000000000123", 1);
3234 test_sh("123.123", -19, "0.000000000000000012", 1);
3235 test_sh("123.123", -20, "0.000000000000000001", 1);
3236 test_sh("123.123", -21, "0", 1);
3237 test_sh(".000000000123", -1, "0.0000000000123", 0);
3238 test_sh(".000000000123", -6, "0.000000000000000123", 0);
3239 test_sh(".000000000123", -7, "0.000000000000000012", 1);
3240 test_sh(".000000000123", -8, "0.000000000000000001", 1);
3241 test_sh(".000000000123", -9, "0", 1);
3242 test_sh(".000000000123", 1, "0.00000000123", 0);
3243 test_sh(".000000000123", 8, "0.0123", 0);
3244 test_sh(".000000000123", 9, "0.123", 0);
3245 test_sh(".000000000123", 10, "1.23", 0);
3246 test_sh(".000000000123", 17, "12300000", 0);
3247 test_sh(".000000000123", 18, "123000000", 0);
3248 test_sh(".000000000123", 19, "1230000000", 0);
3249 test_sh(".000000000123", 20, "12300000000", 0);
3250 test_sh(".000000000123", 21, "123000000000", 0);
3251 test_sh(".000000000123", 22, "1230000000000", 0);
3252 test_sh(".000000000123", 23, "12300000000000", 0);
3253 test_sh(".000000000123", 24, "123000000000000", 0);
3254 test_sh(".000000000123", 25, "1230000000000000", 0);
3255 test_sh(".000000000123", 26, "12300000000000000", 0);
3256 test_sh(".000000000123", 27, "123000000000000000", 0);
3257 test_sh(".000000000123", 28, "0.000000000123", 2);
3258 test_sh("123456789.987654321", -1, "12345678.998765432", 1);
3259 test_sh("123456789.987654321", -2, "1234567.899876543", 1);
3260 test_sh("123456789.987654321", -8, "1.234567900", 1);
3261 test_sh("123456789.987654321", -9, "0.123456789987654321", 0);
3262 test_sh("123456789.987654321", -10, "0.012345678998765432", 1);
3263 test_sh("123456789.987654321", -17, "0.000000001234567900", 1);
3264 test_sh("123456789.987654321", -18, "0.000000000123456790", 1);
3265 test_sh("123456789.987654321", -19, "0.000000000012345679", 1);
3266 test_sh("123456789.987654321", -26, "0.000000000000000001", 1);
3267 test_sh("123456789.987654321", -27, "0", 1);
3268 test_sh("123456789.987654321", 1, "1234567900", 1);
3269 test_sh("123456789.987654321", 2, "12345678999", 1);
3270 test_sh("123456789.987654321", 4, "1234567899877", 1);
3271 test_sh("123456789.987654321", 8, "12345678998765432", 1);
3272 test_sh("123456789.987654321", 9, "123456789987654321", 0);
3273 test_sh("123456789.987654321", 10, "123456789.987654321", 2);
3274 test_sh("123456789.987654321", 0, "123456789.987654321", 0);
3275 a.len= sizeof(buf1)/sizeof(dec1);
3276
3277 printf("==== decimal_actual_fraction ====\n");
3278 test_fr("1.123456789000000000", "1.123456789");
3279 test_fr("1.12345678000000000", "1.12345678");
3280 test_fr("1.1234567000000000", "1.1234567");
3281 test_fr("1.123456000000000", "1.123456");
3282 test_fr("1.12345000000000", "1.12345");
3283 test_fr("1.1234000000000", "1.1234");
3284 test_fr("1.123000000000", "1.123");
3285 test_fr("1.12000000000", "1.12");
3286 test_fr("1.1000000000", "1.1");
3287 test_fr("1.000000000", "1");
3288 test_fr("1.0", "1");
3289 test_fr("10000000000000000000.0", "10000000000000000000");
3290
3291 return 0;
3292}
3293
3294#endif
int store(uint32_t mask, const char *from, uint32_t length, const charset_info_st *charset)
Convert string for decimal when string can be in some multibyte charset.
TODO: Rename this file - func.h is stupid.
int decimal2string(const decimal_t *from, char *to, int *to_len, int fixed_precision, int fixed_decimals, char filler)
Convert decimal to its printable string representation.
int double2decimal(const double from, decimal_t *to)
int class_decimal2string(const type::Decimal *d, uint32_t fixed_dec, String *str)
Converting decimal to string.
static int decimal_shift(decimal_t *dec, int shift)
Shift of decimal digits in given number (with rounding if it need).
void max_decimal(int precision, int frac, decimal_t *to)
Get maximum value for given precision and scale.
int decimal_operation_results(int result)
int decimal_div(const decimal_t *from1, const decimal_t *from2, decimal_t *to, int scale_incr)
division of two decimals
int decimal2double(const decimal_t *from, double *to)
int decimal2bin(const decimal_t *from, unsigned char *to, int precision, int frac)
Convert decimal to its binary fixed-length representation (suitable for comparing with memcmp).
int decimal_bin_size(int precision, int scale)
Returns the size of array to hold a binary representation of a decimal.
int bin2decimal(const unsigned char *from, decimal_t *to, int precision, int scale)
Restores decimal from its binary fixed-length representation.
static int do_div_mod(const decimal_t *from1, const decimal_t *from2, decimal_t *to, decimal_t *mod, int scale_incr)
static void do_mini_left_shift(decimal_t *dec, int shift, int beg, int last)
int decimal_round(const decimal_t *from, decimal_t *to, int scale, decimal_round_mode mode)
Rounds the decimal to "scale" digits.
int decimal_mul(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
multiply two decimals
int decimal_mod(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
modulus
int decimal_actual_fraction(decimal_t *from)
Count actual length of fraction part (without ending zeroes).
static void digits_bounds(decimal_t *from, int *start_result, int *end_result)
Return bounds of decimal digits in the number.
int internal_str2dec(char *from, decimal_t *to, char **end, bool fixed)
Convert string to decimal.
static void do_mini_right_shift(decimal_t *dec, int shift, int beg, int last)
Right shift for alignment of data in buffer.
STL namespace.