forked from stoneatom/stonedb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecimal.c
2652 lines (2329 loc) · 70.4 KB
/
decimal.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2004, 2021, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is also distributed with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have included with MySQL.
Without limiting anything contained in the foregoing, this file,
which is part of C Driver for MySQL (Connector/C), is also subject to the
Universal FOSS Exception, version 1.0, a copy of which can be found at
http://oss.oracle.com/licenses/universal-foss-exception.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
/*
=======================================================================
NOTE: this library implements SQL standard "exact numeric" type
and is not at all generic, but rather intentinally crippled to
follow the standard :)
=======================================================================
Quoting the standard
(SQL:2003, Part 2 Foundations, aka ISO/IEC 9075-2:2003)
4.4.2 Characteristics of numbers, page 27:
An exact numeric type has a precision P and a scale S. P is a positive
integer that determines the number of significant digits in a
particular radix R, where R is either 2 or 10. S is a non-negative
integer. Every value of an exact numeric type of scale S is of the
form n*10^{-S}, where n is an integer such that -R^P <= n <= R^P.
[...]
If an assignment of some number would result in a loss of its most
significant digit, an exception condition is raised. If least
significant digits are lost, implementation-defined rounding or
truncating occurs, with no exception condition being raised.
[...]
Whenever an exact or approximate numeric value is assigned to an exact
numeric value site, an approximation of its value that preserves
leading significant digits after rounding or truncating is represented
in the declared type of the target. The value is converted to have the
precision and scale of the target. The choice of whether to truncate
or round is implementation-defined.
[...]
All numeric values between the smallest and the largest value,
inclusive, in a given exact numeric type have an approximation
obtained by rounding or truncation for that type; it is
implementation-defined which other numeric values have such
approximations.
5.3 <literal>, page 143
<exact numeric literal> ::=
<unsigned integer> [ <period> [ <unsigned integer> ] ]
| <period> <unsigned integer>
6.1 <data type>, page 165:
19) The <scale> of an <exact numeric type> shall not be greater than
the <precision> of the <exact numeric type>.
20) For the <exact numeric type>s DECIMAL and NUMERIC:
a) The maximum value of <precision> is implementation-defined.
<precision> shall not be greater than this value.
b) The maximum value of <scale> is implementation-defined. <scale>
shall not be greater than this maximum value.
21) NUMERIC specifies the data type exact numeric, with the decimal
precision and scale specified by the <precision> and <scale>.
22) DECIMAL specifies the data type exact numeric, with the decimal
scale specified by the <scale> and the implementation-defined
decimal precision equal to or greater than the value of the
specified <precision>.
6.26 <numeric value expression>, page 241:
1) If the declared type of both operands of a dyadic arithmetic
operator is exact numeric, then the declared type of the result is
an implementation-defined exact numeric type, with precision and
scale determined as follows:
a) Let S1 and S2 be the scale of the first and second operands
respectively.
b) The precision of the result of addition and subtraction is
implementation-defined, and the scale is the maximum of S1 and S2.
c) The precision of the result of multiplication is
implementation-defined, and the scale is S1 + S2.
d) The precision and scale of the result of division are
implementation-defined.
*/
#include <my_global.h>
#include <m_ctype.h>
#include <myisampack.h>
#include <my_sys.h> /* for my_alloca */
#include <m_string.h>
#include <decimal.h>
/*
Internally decimal numbers are stored base 10^9 (see DIG_BASE below)
So one variable of type decimal_digit_t is limited:
0 < decimal_digit <= DIG_MAX < DIG_BASE
in the struct st_decimal_t:
intg is the number of *decimal* digits (NOT number of decimal_digit_t's !)
before the point
frac - number of decimal digits after the point
buf is an array of decimal_digit_t's
len is the length of buf (length of allocated space) in decimal_digit_t's,
not in bytes
*/
typedef decimal_digit_t dec1;
typedef longlong dec2;
#define DIG_PER_DEC1 9
#define DIG_MASK 100000000
#define DIG_BASE 1000000000
#define DIG_MAX (DIG_BASE-1)
#define DIG_BASE2 ((dec2)DIG_BASE * (dec2)DIG_BASE)
#define ROUND_UP(X) (((X)+DIG_PER_DEC1-1)/DIG_PER_DEC1)
static const dec1 powers10[DIG_PER_DEC1+1]={
1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
static const int dig2bytes[DIG_PER_DEC1+1]={0, 1, 1, 2, 2, 3, 3, 4, 4, 4};
static const dec1 frac_max[DIG_PER_DEC1-1]={
900000000, 990000000, 999000000,
999900000, 999990000, 999999000,
999999900, 999999990 };
#define sanity(d) assert((d)->len >0)
#define FIX_INTG_FRAC_ERROR(len, intg1, frac1, error) \
do \
{ \
if (unlikely(intg1+frac1 > (len))) \
{ \
if (unlikely(intg1 > (len))) \
{ \
intg1=(len); \
frac1=0; \
error=E_DEC_OVERFLOW; \
} \
else \
{ \
frac1=(len)-intg1; \
error=E_DEC_TRUNCATED; \
} \
} \
else \
error=E_DEC_OK; \
} while(0)
#define ADD(to, from1, from2, carry) /* assume carry <= 1 */ \
do \
{ \
dec1 a=(from1)+(from2)+(carry); \
assert((carry) <= 1); \
if (((carry)= a >= DIG_BASE)) /* no division here! */ \
a-=DIG_BASE; \
(to)=a; \
} while(0)
#define ADD2(to, from1, from2, carry) \
do \
{ \
dec2 a=((dec2)(from1))+(from2)+(carry); \
if (((carry)= a >= DIG_BASE)) \
a-=DIG_BASE; \
if (unlikely(a >= DIG_BASE)) \
{ \
a-=DIG_BASE; \
carry++; \
} \
(to)=(dec1) a; \
} while(0)
#define SUB(to, from1, from2, carry) /* to=from1-from2 */ \
do \
{ \
dec1 a=(from1)-(from2)-(carry); \
if (((carry)= a < 0)) \
a+=DIG_BASE; \
(to)=a; \
} while(0)
#define SUB2(to, from1, from2, carry) /* to=from1-from2 */ \
do \
{ \
dec1 a=(from1)-(from2)-(carry); \
if (((carry)= a < 0)) \
a+=DIG_BASE; \
if (unlikely(a < 0)) \
{ \
a+=DIG_BASE; \
carry++; \
} \
(to)=a; \
} while(0)
/*
This is a direct loop unrolling of code that used to look like this:
for (; *buf_beg < powers10[i--]; start++) ;
@param i start index
@param val value to compare against list of powers of 10
@retval Number of leading zeroes that can be removed from fraction.
@note Why unroll? To get rid of lots of compiler warnings [-Warray-bounds]
Nice bonus: unrolled code is significantly faster.
*/
static inline int count_leading_zeroes(int i, dec1 val)
{
int ret= 0;
switch (i)
{
/* @note Intentional fallthrough in all case labels */
case 9: if (val >= 1000000000) break; ++ret; // Fall through.
case 8: if (val >= 100000000) break; ++ret; // Fall through.
case 7: if (val >= 10000000) break; ++ret; // Fall through.
case 6: if (val >= 1000000) break; ++ret; // Fall through.
case 5: if (val >= 100000) break; ++ret; // Fall through.
case 4: if (val >= 10000) break; ++ret; // Fall through.
case 3: if (val >= 1000) break; ++ret; // Fall through.
case 2: if (val >= 100) break; ++ret; // Fall through.
case 1: if (val >= 10) break; ++ret; // Fall through.
case 0: if (val >= 1) break; ++ret; // Fall through.
default: { assert(FALSE); }
}
return ret;
}
/*
This is a direct loop unrolling of code that used to look like this:
for (; *buf_end % powers10[i++] == 0; stop--) ;
@param i start index
@param val value to compare against list of powers of 10
@retval Number of trailing zeroes that can be removed from fraction.
@note Why unroll? To get rid of lots of compiler warnings [-Warray-bounds]
Nice bonus: unrolled code is significantly faster.
*/
static inline int count_trailing_zeroes(int i, dec1 val)
{
int ret= 0;
switch(i)
{
/* @note Intentional fallthrough in all case labels */
case 0: if ((val % 1) != 0) break; ++ret; // Fall through.
case 1: if ((val % 10) != 0) break; ++ret; // Fall through.
case 2: if ((val % 100) != 0) break; ++ret; // Fall through.
case 3: if ((val % 1000) != 0) break; ++ret; // Fall through.
case 4: if ((val % 10000) != 0) break; ++ret; // Fall through.
case 5: if ((val % 100000) != 0) break; ++ret; // Fall through.
case 6: if ((val % 1000000) != 0) break; ++ret; // Fall through.
case 7: if ((val % 10000000) != 0) break; ++ret; // Fall through.
case 8: if ((val % 100000000) != 0) break; ++ret; // Fall through.
case 9: if ((val % 1000000000) != 0) break; ++ret; // Fall through.
default: { assert(FALSE); }
}
return ret;
}
/*
Get maximum value for given precision and scale
SYNOPSIS
max_decimal()
precision/scale - see decimal_bin_size() below
to - decimal where where the result will be stored
to->buf and to->len must be set.
*/
void max_decimal(int precision, int frac, decimal_t *to)
{
int intpart;
dec1 *buf= to->buf;
assert(precision && precision >= frac);
to->sign= 0;
if ((intpart= to->intg= (precision - frac)))
{
int firstdigits= intpart % DIG_PER_DEC1;
if (firstdigits)
*buf++= powers10[firstdigits] - 1; /* get 9 99 999 ... */
for(intpart/= DIG_PER_DEC1; intpart; intpart--)
*buf++= DIG_MAX;
}
if ((to->frac= frac))
{
int lastdigits= frac % DIG_PER_DEC1;
for(frac/= DIG_PER_DEC1; frac; frac--)
*buf++= DIG_MAX;
if (lastdigits)
*buf= frac_max[lastdigits - 1];
}
}
static dec1 *remove_leading_zeroes(const decimal_t *from, int *intg_result)
{
int intg= from->intg, i;
dec1 *buf0= from->buf;
i= ((intg - 1) % DIG_PER_DEC1) + 1;
while (intg > 0 && *buf0 == 0)
{
intg-= i;
i= DIG_PER_DEC1;
buf0++;
}
if (intg > 0)
{
intg-= count_leading_zeroes((intg - 1) % DIG_PER_DEC1, *buf0);
assert(intg > 0);
}
else
intg=0;
*intg_result= intg;
return buf0;
}
/*
Count actual length of fraction part (without ending zeroes)
SYNOPSIS
decimal_actual_fraction()
from number for processing
*/
int decimal_actual_fraction(decimal_t *from)
{
int frac= from->frac, i;
dec1 *buf0= from->buf + ROUND_UP(from->intg) + ROUND_UP(frac) - 1;
if (frac == 0)
return 0;
i= ((frac - 1) % DIG_PER_DEC1 + 1);
while (frac > 0 && *buf0 == 0)
{
frac-= i;
i= DIG_PER_DEC1;
buf0--;
}
if (frac > 0)
{
frac-=
count_trailing_zeroes(DIG_PER_DEC1 - ((frac - 1) % DIG_PER_DEC1), *buf0);
}
return frac;
}
/*
Convert decimal to its printable string representation
SYNOPSIS
decimal2string()
from - value to convert
to - points to buffer where string representation
should be stored
*to_len - in: size of to buffer (incl. terminating '\0')
out: length of the actually written string (excl. '\0')
fixed_precision - 0 if representation can be variable length and
fixed_decimals will not be checked in this case.
Put number as with fixed point position with this
number of digits (sign counted and decimal point is
counted)
fixed_decimals - number digits after point.
filler - character to fill gaps in case of fixed_precision > 0
RETURN VALUE
E_DEC_OK/E_DEC_TRUNCATED/E_DEC_OVERFLOW
*/
int decimal2string(const decimal_t *from, char *to, int *to_len,
int fixed_precision, int fixed_decimals,
char filler)
{
/* {intg_len, frac_len} output widths; {intg, frac} places in input */
int len, intg, frac= from->frac, i, intg_len, frac_len, fill;
/* number digits before decimal point */
int fixed_intg= (fixed_precision ?
(fixed_precision - fixed_decimals) : 0);
int error=E_DEC_OK;
char *s=to;
dec1 *buf, *buf0=from->buf, tmp;
assert(*to_len >= 2+from->sign);
/* removing leading zeroes */
buf0= remove_leading_zeroes(from, &intg);
if (unlikely(intg+frac==0))
{
intg=1;
tmp=0;
buf0=&tmp;
}
if (!(intg_len= fixed_precision ? fixed_intg : intg))
intg_len= 1;
frac_len= fixed_precision ? fixed_decimals : frac;
len= from->sign + intg_len + MY_TEST(frac) + frac_len;
if (fixed_precision)
{
if (frac > fixed_decimals)
{
error= E_DEC_TRUNCATED;
frac= fixed_decimals;
}
if (intg > fixed_intg)
{
error= E_DEC_OVERFLOW;
intg= fixed_intg;
}
}
else if (unlikely(len > --*to_len)) /* reserve one byte for \0 */
{
int j= len - *to_len; /* excess printable chars */
error= (frac && j <= frac + 1) ? E_DEC_TRUNCATED : E_DEC_OVERFLOW;
/*
If we need to cut more places than frac is wide, we'll end up
dropping the decimal point as well. Account for this.
*/
if (frac && j >= frac + 1)
j--;
if (j > frac)
{
intg_len= intg-= j-frac;
frac= 0;
}
else
frac-=j;
frac_len= frac;
len= from->sign + intg_len + MY_TEST(frac) + frac_len;
}
*to_len= len;
s[len]= 0;
if (from->sign)
*s++='-';
if (frac)
{
char *s1= s + intg_len;
fill= frac_len - frac;
buf=buf0+ROUND_UP(intg);
*s1++='.';
for (; frac>0; frac-=DIG_PER_DEC1)
{
dec1 x=*buf++;
for (i= MY_MIN(frac, DIG_PER_DEC1); i; i--)
{
dec1 y=x/DIG_MASK;
*s1++='0'+(uchar)y;
x-=y*DIG_MASK;
x*=10;
}
}
for(; fill > 0; fill--)
*s1++=filler;
}
fill= intg_len - intg;
if (intg == 0)
fill--; /* symbol 0 before digital point */
for(; fill > 0; fill--)
*s++=filler;
if (intg)
{
s+=intg;
for (buf=buf0+ROUND_UP(intg); intg>0; intg-=DIG_PER_DEC1)
{
dec1 x=*--buf;
for (i= MY_MIN(intg, DIG_PER_DEC1); i; i--)
{
dec1 y=x/10;
*--s='0'+(uchar)(x-y*10);
x=y;
}
}
}
else
*s= '0';
return error;
}
/*
Return bounds of decimal digits in the number
SYNOPSIS
digits_bounds()
from - decimal number for processing
start_result - index (from 0 ) of first decimal digits will
be written by this address
end_result - index of position just after last decimal digit
be written by this address
*/
static void digits_bounds(decimal_t *from, int *start_result, int *end_result)
{
int start, stop, i;
dec1 *buf_beg= from->buf;
dec1 *end= from->buf + ROUND_UP(from->intg) + ROUND_UP(from->frac);
dec1 *buf_end= end - 1;
/* find non-zero digit from number begining */
while (buf_beg < end && *buf_beg == 0)
buf_beg++;
if (buf_beg >= end)
{
/* it is zero */
*start_result= *end_result= 0;
return;
}
/* find non-zero decimal digit from number begining */
if (buf_beg == from->buf && from->intg)
{
start= DIG_PER_DEC1 - (i= ((from->intg-1) % DIG_PER_DEC1 + 1));
i--;
}
else
{
i= DIG_PER_DEC1 - 1;
start= (int) ((buf_beg - from->buf) * DIG_PER_DEC1);
}
if (buf_beg < end)
start+= count_leading_zeroes(i, *buf_beg);
*start_result= start; /* index of first decimal digit (from 0) */
/* find non-zero digit at the end */
while (buf_end > buf_beg && *buf_end == 0)
buf_end--;
/* find non-zero decimal digit from the end */
if (buf_end == end - 1 && from->frac)
{
stop= (int) (((buf_end - from->buf) * DIG_PER_DEC1 +
(i= ((from->frac - 1) % DIG_PER_DEC1 + 1))));
i= DIG_PER_DEC1 - i + 1;
}
else
{
stop= (int) ((buf_end - from->buf + 1) * DIG_PER_DEC1);
i= 1;
}
stop-= count_trailing_zeroes(i, *buf_end);
*end_result= stop; /* index of position after last decimal digit (from 0) */
}
/*
Left shift for alignment of data in buffer
SYNOPSIS
do_mini_left_shift()
dec pointer to decimal number which have to be shifted
shift number of decimal digits on which it should be shifted
beg/end bounds of decimal digits (see digits_bounds())
NOTE
Result fitting in the buffer should be garanted.
'shift' have to be from 1 to DIG_PER_DEC1-1 (inclusive)
*/
void do_mini_left_shift(decimal_t *dec, int shift, int beg, int last)
{
dec1 *from= dec->buf + ROUND_UP(beg + 1) - 1;
dec1 *end= dec->buf + ROUND_UP(last) - 1;
int c_shift= DIG_PER_DEC1 - shift;
assert(from >= dec->buf);
assert(end < dec->buf + dec->len);
if (beg % DIG_PER_DEC1 < shift)
*(from - 1)= (*from) / powers10[c_shift];
for(; from < end; from++)
*from= ((*from % powers10[c_shift]) * powers10[shift] +
(*(from + 1)) / powers10[c_shift]);
*from= (*from % powers10[c_shift]) * powers10[shift];
}
/*
Right shift for alignment of data in buffer
SYNOPSIS
do_mini_left_shift()
dec pointer to decimal number which have to be shifted
shift number of decimal digits on which it should be shifted
beg/end bounds of decimal digits (see digits_bounds())
NOTE
Result fitting in the buffer should be garanted.
'shift' have to be from 1 to DIG_PER_DEC1-1 (inclusive)
*/
void do_mini_right_shift(decimal_t *dec, int shift, int beg, int last)
{
dec1 *from= dec->buf + ROUND_UP(last) - 1;
dec1 *end= dec->buf + ROUND_UP(beg + 1) - 1;
int c_shift= DIG_PER_DEC1 - shift;
assert(from < dec->buf + dec->len);
assert(end >= dec->buf);
if (DIG_PER_DEC1 - ((last - 1) % DIG_PER_DEC1 + 1) < shift)
*(from + 1)= (*from % powers10[shift]) * powers10[c_shift];
for(; from > end; from--)
*from= (*from / powers10[shift] +
(*(from - 1) % powers10[shift]) * powers10[c_shift]);
*from= *from / powers10[shift];
}
/*
Shift of decimal digits in given number (with rounding if it need)
SYNOPSIS
decimal_shift()
dec number to be shifted
shift number of decimal positions
shift > 0 means shift to left shift
shift < 0 meand right shift
NOTE
In fact it is multipling on 10^shift.
RETURN
E_DEC_OK OK
E_DEC_OVERFLOW operation lead to overflow, number is untoched
E_DEC_TRUNCATED number was rounded to fit into buffer
*/
int decimal_shift(decimal_t *dec, int shift)
{
/* index of first non zero digit (all indexes from 0) */
int beg;
/* index of position after last decimal digit */
int end;
/* index of digit position just after point */
int point= ROUND_UP(dec->intg) * DIG_PER_DEC1;
/* new point position */
int new_point= point + shift;
/* number of digits in result */
int digits_int, digits_frac;
/* length of result and new fraction in big digits*/
int new_len, new_frac_len;
/* return code */
int err= E_DEC_OK;
int new_front;
if (shift == 0)
return E_DEC_OK;
digits_bounds(dec, &beg, &end);
if (beg == end)
{
decimal_make_zero(dec);
return E_DEC_OK;
}
digits_int= new_point - beg;
set_if_bigger(digits_int, 0);
digits_frac= end - new_point;
set_if_bigger(digits_frac, 0);
if ((new_len= ROUND_UP(digits_int) + (new_frac_len= ROUND_UP(digits_frac))) >
dec->len)
{
int lack= new_len - dec->len;
int diff;
if (new_frac_len < lack)
return E_DEC_OVERFLOW; /* lack more then we have in fraction */
/* cat off fraction part to allow new number to fit in our buffer */
err= E_DEC_TRUNCATED;
new_frac_len-= lack;
diff= digits_frac - (new_frac_len * DIG_PER_DEC1);
/* Make rounding method as parameter? */
decimal_round(dec, dec, end - point - diff, HALF_UP);
end-= diff;
digits_frac= new_frac_len * DIG_PER_DEC1;
if (end <= beg)
{
/*
we lost all digits (they will be shifted out of buffer), so we can
just return 0
*/
decimal_make_zero(dec);
return E_DEC_TRUNCATED;
}
}
if (shift % DIG_PER_DEC1)
{
int l_mini_shift, r_mini_shift, mini_shift;
int do_left;
/*
Calculate left/right shift to align decimal digits inside our bug
digits correctly
*/
if (shift > 0)
{
l_mini_shift= shift % DIG_PER_DEC1;
r_mini_shift= DIG_PER_DEC1 - l_mini_shift;
/*
It is left shift so prefer left shift, but if we have not place from
left, we have to have it from right, because we checked length of
result
*/
do_left= l_mini_shift <= beg;
assert(do_left || (dec->len * DIG_PER_DEC1 - end) >= r_mini_shift);
}
else
{
r_mini_shift= (-shift) % DIG_PER_DEC1;
l_mini_shift= DIG_PER_DEC1 - r_mini_shift;
/* see comment above */
do_left= !((dec->len * DIG_PER_DEC1 - end) >= r_mini_shift);
assert(!do_left || l_mini_shift <= beg);
}
if (do_left)
{
do_mini_left_shift(dec, l_mini_shift, beg, end);
mini_shift= -l_mini_shift;
}
else
{
do_mini_right_shift(dec, r_mini_shift, beg, end);
mini_shift= r_mini_shift;
}
new_point+= mini_shift;
/*
If number is shifted and correctly aligned in buffer we can
finish
*/
if (!(shift+= mini_shift) && (new_point - digits_int) < DIG_PER_DEC1)
{
dec->intg= digits_int;
dec->frac= digits_frac;
return err; /* already shifted as it should be */
}
beg+= mini_shift;
end+= mini_shift;
}
/* if new 'decimal front' is in first digit, we do not need move digits */
if ((new_front= (new_point - digits_int)) >= DIG_PER_DEC1 ||
new_front < 0)
{
/* need to move digits */
int d_shift;
dec1 *to, *barier;
if (new_front > 0)
{
/* move left */
d_shift= new_front / DIG_PER_DEC1;
to= dec->buf + (ROUND_UP(beg + 1) - 1 - d_shift);
barier= dec->buf + (ROUND_UP(end) - 1 - d_shift);
assert(to >= dec->buf);
assert(barier + d_shift < dec->buf + dec->len);
for(; to <= barier; to++)
*to= *(to + d_shift);
for(barier+= d_shift; to <= barier; to++)
*to= 0;
d_shift= -d_shift;
}
else
{
/* move right */
d_shift= (1 - new_front) / DIG_PER_DEC1;
to= dec->buf + ROUND_UP(end) - 1 + d_shift;
barier= dec->buf + ROUND_UP(beg + 1) - 1 + d_shift;
assert(to < dec->buf + dec->len);
assert(barier - d_shift >= dec->buf);
for(; to >= barier; to--)
*to= *(to - d_shift);
for(barier-= d_shift; to >= barier; to--)
*to= 0;
}
d_shift*= DIG_PER_DEC1;
beg+= d_shift;
end+= d_shift;
new_point+= d_shift;
}
/*
If there are gaps then fill ren with 0.
Only one of following 'for' loops will work becouse beg <= end
*/
beg= ROUND_UP(beg + 1) - 1;
end= ROUND_UP(end) - 1;
assert(new_point >= 0);
/* We don't want negative new_point below */
if (new_point != 0)
new_point= ROUND_UP(new_point) - 1;
if (new_point > end)
{
do
{
dec->buf[new_point]=0;
} while (--new_point > end);
}
else
{
for (; new_point < beg; new_point++)
dec->buf[new_point]= 0;
}
dec->intg= digits_int;
dec->frac= digits_frac;
return err;
}
/*
Convert string to decimal
SYNOPSIS
internal_str2decl()
from - value to convert. Doesn't have to be \0 terminated!
to - decimal where where the result will be stored
to->buf and to->len must be set.
end - Pointer to pointer to end of string. Will on return be
set to the char after the last used character
fixed - use to->intg, to->frac as limits for input number
NOTE
to->intg and to->frac can be modified even when fixed=1
(but only decreased, in this case)
RETURN VALUE
E_DEC_OK/E_DEC_TRUNCATED/E_DEC_OVERFLOW/E_DEC_BAD_NUM/E_DEC_OOM
In case of E_DEC_FATAL_ERROR *to is set to decimal zero
(to make error handling easier)
*/
int
internal_str2dec(const char *from, decimal_t *to, char **end, my_bool fixed)
{
const char *s= from, *s1, *endp, *end_of_string= *end;
int i, intg, frac, error, intg1, frac1;
dec1 x,*buf;
sanity(to);
error= E_DEC_BAD_NUM; /* In case of bad number */
while (s < end_of_string && my_isspace(&my_charset_latin1, *s))
s++;
if (s == end_of_string)
goto fatal_error;
if ((to->sign= (*s == '-')))
s++;
else if (*s == '+')
s++;
s1=s;
while (s < end_of_string && my_isdigit(&my_charset_latin1, *s))
s++;
intg= (int) (s-s1);
if (s < end_of_string && *s=='.')
{
endp= s+1;
while (endp < end_of_string && my_isdigit(&my_charset_latin1, *endp))
endp++;
frac= (int) (endp - s - 1);
}
else
{
frac= 0;
endp= s;
}
*end= (char*) endp;
if (frac+intg == 0)
goto fatal_error;
error= 0;
if (fixed)
{
if (frac > to->frac)
{
error=E_DEC_TRUNCATED;
frac=to->frac;
}
if (intg > to->intg)
{
error=E_DEC_OVERFLOW;
intg=to->intg;
}
intg1=ROUND_UP(intg);
frac1=ROUND_UP(frac);
if (intg1+frac1 > to->len)
{
error= E_DEC_OOM;
goto fatal_error;
}
}
else
{
intg1=ROUND_UP(intg);
frac1=ROUND_UP(frac);
FIX_INTG_FRAC_ERROR(to->len, intg1, frac1, error);
if (unlikely(error))
{
frac=frac1*DIG_PER_DEC1;
if (error == E_DEC_OVERFLOW)
intg=intg1*DIG_PER_DEC1;
}
}
/* Error is guranteed to be set here */
to->intg=intg;
to->frac=frac;
buf=to->buf+intg1;
s1=s;
for (x=0, i=0; intg; intg--)
{
x+= (*--s - '0')*powers10[i];
if (unlikely(++i == DIG_PER_DEC1))
{
*--buf=x;
x=0;
i=0;
}
}
if (i)
*--buf=x;
buf=to->buf+intg1;
for (x=0, i=0; frac; frac--)
{
x= (*++s1 - '0') + x*10;
if (unlikely(++i == DIG_PER_DEC1))
{
*buf++=x;
x=0;
i=0;
}
}
if (i)
*buf=x*powers10[DIG_PER_DEC1-i];
/* Handle exponent */
if (endp+1 < end_of_string && (*endp == 'e' || *endp == 'E'))
{
int str_error;
longlong exponent= my_strtoll10(endp+1, (char**) &end_of_string,
&str_error);
if (end_of_string != endp +1) /* If at least one digit */
{
*end= (char*) end_of_string;
if (str_error > 0)
{
error= E_DEC_BAD_NUM;
goto fatal_error;
}
if (exponent > INT_MAX/2 || (str_error == 0 && exponent < 0))
{
error= E_DEC_OVERFLOW;
goto fatal_error;
}