-
-
Notifications
You must be signed in to change notification settings - Fork 214
/
decCommon.c
1835 lines (1712 loc) · 83.1 KB
/
decCommon.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
/* ------------------------------------------------------------------ */
/* decCommon.c -- common code for all three fixed-size types */
/* ------------------------------------------------------------------ */
/* Copyright (c) IBM Corporation, 2000, 2010. All rights reserved. */
/* */
/* This software is made available under the terms of the */
/* ICU License -- ICU 1.8.1 and later. */
/* */
/* The description and User's Guide ("The decNumber C Library") for */
/* this software is included in the package as decNumber.pdf. This */
/* document is also available in HTML, together with specifications, */
/* testcases, and Web links, on the General Decimal Arithmetic page. */
/* */
/* Please send comments, suggestions, and corrections to the author: */
/* mfc@uk.ibm.com */
/* Mike Cowlishaw, IBM Fellow */
/* IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK */
/* ------------------------------------------------------------------ */
/* This module comprises code that is shared between all the formats */
/* (decSingle, decDouble, and decQuad); it includes set and extract */
/* of format components, widening, narrowing, and string conversions. */
/* */
/* Unlike decNumber, parameterization takes place at compile time */
/* rather than at runtime. The parameters are set in the decDouble.c */
/* (etc.) files, which then include this one to produce the compiled */
/* code. The functions here, therefore, are code shared between */
/* multiple formats. */
/* ------------------------------------------------------------------ */
// Names here refer to decFloat rather than to decDouble, etc., and
// the functions are in strict alphabetical order.
// Constants, tables, and debug function(s) are included only for QUAD
// (which will always be compiled if DOUBLE or SINGLE are used).
//
// Whenever a decContext is used, only the status may be set (using
// OR) or the rounding mode read; all other fields are ignored and
// untouched.
// names for simpler testing and default context
#if DECPMAX==7
#define SINGLE 1
#define DOUBLE 0
#define QUAD 0
#define DEFCONTEXT DEC_INIT_DECIMAL32
#elif DECPMAX==16
#define SINGLE 0
#define DOUBLE 1
#define QUAD 0
#define DEFCONTEXT DEC_INIT_DECIMAL64
#elif DECPMAX==34
#define SINGLE 0
#define DOUBLE 0
#define QUAD 1
#define DEFCONTEXT DEC_INIT_DECIMAL128
#else
#error Unexpected DECPMAX value
#endif
/* Assertions */
#if DECPMAX!=7 && DECPMAX!=16 && DECPMAX!=34
#error Unexpected Pmax (DECPMAX) value for this module
#endif
// Assert facts about digit characters, etc.
#if ('9'&0x0f)!=9
#error This module assumes characters are of the form 0b....nnnn
// where .... are don't care 4 bits and nnnn is 0000 through 1001
#endif
#if ('9'&0xf0)==('.'&0xf0)
#error This module assumes '.' has a different mask than a digit
#endif
// Assert ToString lay-out conditions
#if DECSTRING<DECPMAX+9
#error ToString needs at least 8 characters for lead-in and dot
#endif
#if DECPMAX+DECEMAXD+5 > DECSTRING
#error Exponent form can be too long for ToString to lay out safely
#endif
#if DECEMAXD > 4
#error Exponent form is too long for ToString to lay out
// Note: code for up to 9 digits exists in archives [decOct]
#endif
/* Private functions used here and possibly in decBasic.c, etc. */
static decFloat * decFinalize(decFloat *, bcdnum *, decContext *);
static Flag decBiStr(const char *, const char *, const char *);
/* Macros and private tables; those which are not format-dependent */
/* are only included if decQuad is being built. */
/* ------------------------------------------------------------------ */
/* Combination field lookup tables (uInts to save measurable work) */
/* */
/* DECCOMBEXP - 2 most-significant-bits of exponent (00, 01, or */
/* 10), shifted left for format, or DECFLOAT_Inf/NaN */
/* DECCOMBWEXP - The same, for the next-wider format (unless QUAD) */
/* DECCOMBMSD - 4-bit most-significant-digit */
/* [0 if the index is a special (Infinity or NaN)] */
/* DECCOMBFROM - 5-bit combination field from EXP top bits and MSD */
/* (placed in uInt so no shift is needed) */
/* */
/* DECCOMBEXP, DECCOMBWEXP, and DECCOMBMSD are indexed by the sign */
/* and 5-bit combination field (0-63, the second half of the table */
/* identical to the first half) */
/* DECCOMBFROM is indexed by expTopTwoBits*16 + msd */
/* */
/* DECCOMBMSD and DECCOMBFROM are not format-dependent and so are */
/* only included once, when QUAD is being built */
/* ------------------------------------------------------------------ */
static const uInt DECCOMBEXP[64]={
0, 0, 0, 0, 0, 0, 0, 0,
1<<DECECONL, 1<<DECECONL, 1<<DECECONL, 1<<DECECONL,
1<<DECECONL, 1<<DECECONL, 1<<DECECONL, 1<<DECECONL,
2<<DECECONL, 2<<DECECONL, 2<<DECECONL, 2<<DECECONL,
2<<DECECONL, 2<<DECECONL, 2<<DECECONL, 2<<DECECONL,
0, 0, 1<<DECECONL, 1<<DECECONL,
2<<DECECONL, 2<<DECECONL, DECFLOAT_Inf, DECFLOAT_NaN,
0, 0, 0, 0, 0, 0, 0, 0,
1<<DECECONL, 1<<DECECONL, 1<<DECECONL, 1<<DECECONL,
1<<DECECONL, 1<<DECECONL, 1<<DECECONL, 1<<DECECONL,
2<<DECECONL, 2<<DECECONL, 2<<DECECONL, 2<<DECECONL,
2<<DECECONL, 2<<DECECONL, 2<<DECECONL, 2<<DECECONL,
0, 0, 1<<DECECONL, 1<<DECECONL,
2<<DECECONL, 2<<DECECONL, DECFLOAT_Inf, DECFLOAT_NaN};
#if !QUAD
static const uInt DECCOMBWEXP[64]={
0, 0, 0, 0, 0, 0, 0, 0,
1<<DECWECONL, 1<<DECWECONL, 1<<DECWECONL, 1<<DECWECONL,
1<<DECWECONL, 1<<DECWECONL, 1<<DECWECONL, 1<<DECWECONL,
2<<DECWECONL, 2<<DECWECONL, 2<<DECWECONL, 2<<DECWECONL,
2<<DECWECONL, 2<<DECWECONL, 2<<DECWECONL, 2<<DECWECONL,
0, 0, 1<<DECWECONL, 1<<DECWECONL,
2<<DECWECONL, 2<<DECWECONL, DECFLOAT_Inf, DECFLOAT_NaN,
0, 0, 0, 0, 0, 0, 0, 0,
1<<DECWECONL, 1<<DECWECONL, 1<<DECWECONL, 1<<DECWECONL,
1<<DECWECONL, 1<<DECWECONL, 1<<DECWECONL, 1<<DECWECONL,
2<<DECWECONL, 2<<DECWECONL, 2<<DECWECONL, 2<<DECWECONL,
2<<DECWECONL, 2<<DECWECONL, 2<<DECWECONL, 2<<DECWECONL,
0, 0, 1<<DECWECONL, 1<<DECWECONL,
2<<DECWECONL, 2<<DECWECONL, DECFLOAT_Inf, DECFLOAT_NaN};
#endif
#if QUAD
const uInt DECCOMBMSD[64]={
0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 9, 8, 9, 0, 0,
0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 9, 8, 9, 0, 0};
const uInt DECCOMBFROM[48]={
0x00000000, 0x04000000, 0x08000000, 0x0C000000, 0x10000000, 0x14000000,
0x18000000, 0x1C000000, 0x60000000, 0x64000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x20000000, 0x24000000,
0x28000000, 0x2C000000, 0x30000000, 0x34000000, 0x38000000, 0x3C000000,
0x68000000, 0x6C000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x40000000, 0x44000000, 0x48000000, 0x4C000000,
0x50000000, 0x54000000, 0x58000000, 0x5C000000, 0x70000000, 0x74000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000};
/* ------------------------------------------------------------------ */
/* Request and include the tables to use for conversions */
/* ------------------------------------------------------------------ */
#define DEC_BCD2DPD 1 // 0-0x999 -> DPD
#define DEC_BIN2DPD 1 // 0-999 -> DPD
#define DEC_BIN2BCD8 1 // 0-999 -> ddd, len
#define DEC_DPD2BCD8 1 // DPD -> ddd, len
#define DEC_DPD2BIN 1 // DPD -> 0-999
#define DEC_DPD2BINK 1 // DPD -> 0-999000
#define DEC_DPD2BINM 1 // DPD -> 0-999000000
#include "decDPD.h" // source of the lookup tables
#endif
/* ----------------------------------------------------------------- */
/* decBiStr -- compare string with pairwise options */
/* */
/* targ is the string to compare */
/* str1 is one of the strings to compare against (length may be 0) */
/* str2 is the other; it must be the same length as str1 */
/* */
/* returns 1 if strings compare equal, (that is, targ is the same */
/* length as str1 and str2, and each character of targ is in one */
/* of str1 or str2 in the corresponding position), or 0 otherwise */
/* */
/* This is used for generic caseless compare, including the awkward */
/* case of the Turkish dotted and dotless Is. Use as (for example): */
/* if (decBiStr(test, "mike", "MIKE")) ... */
/* ----------------------------------------------------------------- */
static Flag decBiStr(const char *targ, const char *str1, const char *str2) {
for (;;targ++, str1++, str2++) {
if (*targ!=*str1 && *targ!=*str2) return 0;
// *targ has a match in one (or both, if terminator)
if (*targ=='\0') break;
} // forever
return 1;
} // decBiStr
/* ------------------------------------------------------------------ */
/* decFinalize -- adjust and store a final result */
/* */
/* df is the decFloat format number which gets the final result */
/* num is the descriptor of the number to be checked and encoded */
/* [its values, including the coefficient, may be modified] */
/* set is the context to use */
/* returns df */
/* */
/* The num descriptor may point to a bcd8 string of any length; this */
/* string may have leading insignificant zeros. If it has more than */
/* DECPMAX digits then the final digit can be a round-for-reround */
/* digit (i.e., it may include a sticky bit residue). */
/* */
/* The exponent (q) may be one of the codes for a special value and */
/* can be up to 999999999 for conversion from string. */
/* */
/* No error is possible, but Inexact, Underflow, and/or Overflow may */
/* be set. */
/* ------------------------------------------------------------------ */
// Constant whose size varies with format; also the check for surprises
static uByte allnines[DECPMAX]=
#if SINGLE
{9, 9, 9, 9, 9, 9, 9};
#elif DOUBLE
{9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9};
#elif QUAD
{9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9};
#endif
static decFloat * decFinalize(decFloat *df, bcdnum *num,
decContext *set) {
uByte *ub; // work
uInt dpd; // ..
uInt uiwork; // for macros
uByte *umsd=num->msd; // local copy
uByte *ulsd=num->lsd; // ..
uInt encode; // encoding accumulator
Int length; // coefficient length
#if DECCHECK
Int clen=ulsd-umsd+1;
#if QUAD
#define COEXTRA 2 // extra-long coefficent
#else
#define COEXTRA 0
#endif
if (clen<1 || clen>DECPMAX*3+2+COEXTRA)
printf("decFinalize: suspect coefficient [length=%ld]\n", (LI)clen);
if (num->sign!=0 && num->sign!=DECFLOAT_Sign)
printf("decFinalize: bad sign [%08lx]\n", (LI)num->sign);
if (!EXPISSPECIAL(num->exponent)
&& (num->exponent>1999999999 || num->exponent<-1999999999))
printf("decFinalize: improbable exponent [%ld]\n", (LI)num->exponent);
// decShowNum(num, "final");
#endif
// A special will have an 'exponent' which is very positive and a
// coefficient < DECPMAX
length=(uInt)(ulsd-umsd+1); // coefficient length
if (!NUMISSPECIAL(num)) {
Int drop; // digits to be dropped
// skip leading insignificant zeros to calculate an exact length
// [this is quite expensive]
if (*umsd==0) {
for (; umsd+3<ulsd && UBTOUI(umsd)==0;) umsd+=4;
for (; *umsd==0 && umsd<ulsd;) umsd++;
length=ulsd-umsd+1; // recalculate
}
drop=MAXI(length-DECPMAX, DECQTINY-num->exponent);
// drop can now be > digits for bottom-clamp (subnormal) cases
if (drop>0) { // rounding needed
// (decFloatQuantize has very similar code to this, so any
// changes may need to be made there, too)
uByte *roundat; // -> re-round digit
uByte reround; // reround value
// printf("Rounding; drop=%ld\n", (LI)drop);
num->exponent+=drop; // always update exponent
// Three cases here:
// 1. new LSD is in coefficient (almost always)
// 2. new LSD is digit to left of coefficient (so MSD is
// round-for-reround digit)
// 3. new LSD is to left of case 2 (whole coefficient is sticky)
// [duplicate check-stickies code to save a test]
// [by-digit check for stickies as runs of zeros are rare]
if (drop<length) { // NB lengths not addresses
roundat=umsd+length-drop;
reround=*roundat;
for (ub=roundat+1; ub<=ulsd; ub++) {
if (*ub!=0) { // non-zero to be discarded
reround=DECSTICKYTAB[reround]; // apply sticky bit
break; // [remainder don't-care]
}
} // check stickies
ulsd=roundat-1; // new LSD
}
else { // edge case
if (drop==length) {
roundat=umsd;
reround=*roundat;
}
else {
roundat=umsd-1;
reround=0;
}
for (ub=roundat+1; ub<=ulsd; ub++) {
if (*ub!=0) { // non-zero to be discarded
reround=DECSTICKYTAB[reround]; // apply sticky bit
break; // [remainder don't-care]
}
} // check stickies
*umsd=0; // coefficient is a 0
ulsd=umsd; // ..
}
if (reround!=0) { // discarding non-zero
uInt bump=0;
set->status|=DEC_Inexact;
// if adjusted exponent [exp+digits-1] is < EMIN then num is
// subnormal -- so raise Underflow
if (num->exponent<DECEMIN && (num->exponent+(ulsd-umsd+1)-1)<DECEMIN)
set->status|=DEC_Underflow;
// next decide whether increment of the coefficient is needed
if (set->round==DEC_ROUND_HALF_EVEN) { // fastpath slowest case
if (reround>5) bump=1; // >0.5 goes up
else if (reround==5) // exactly 0.5000 ..
bump=*ulsd & 0x01; // .. up iff [new] lsd is odd
} // r-h-e
else switch (set->round) {
case DEC_ROUND_DOWN: {
// no change
break;} // r-d
case DEC_ROUND_HALF_DOWN: {
if (reround>5) bump=1;
break;} // r-h-d
case DEC_ROUND_HALF_UP: {
if (reround>=5) bump=1;
break;} // r-h-u
case DEC_ROUND_UP: {
if (reround>0) bump=1;
break;} // r-u
case DEC_ROUND_CEILING: {
// same as _UP for positive numbers, and as _DOWN for negatives
if (!num->sign && reround>0) bump=1;
break;} // r-c
case DEC_ROUND_FLOOR: {
// same as _UP for negative numbers, and as _DOWN for positive
// [negative reround cannot occur on 0]
if (num->sign && reround>0) bump=1;
break;} // r-f
case DEC_ROUND_05UP: {
if (reround>0) { // anything out there is 'sticky'
// bump iff lsd=0 or 5; this cannot carry so it could be
// effected immediately with no bump -- but the code
// is clearer if this is done the same way as the others
if (*ulsd==0 || *ulsd==5) bump=1;
}
break;} // r-r
default: { // e.g., DEC_ROUND_MAX
set->status|=DEC_Invalid_context;
#if DECCHECK
printf("Unknown rounding mode: %ld\n", (LI)set->round);
#endif
break;}
} // switch (not r-h-e)
// printf("ReRound: %ld bump: %ld\n", (LI)reround, (LI)bump);
if (bump!=0) { // need increment
// increment the coefficient; this might end up with 1000...
// (after the all nines case)
ub=ulsd;
for(; ub-3>=umsd && UBTOUI(ub-3)==0x09090909; ub-=4) {
UBFROMUI(ub-3, 0); // to 00000000
}
// [note ub could now be to left of msd, and it is not safe
// to write to the the left of the msd]
// now at most 3 digits left to non-9 (usually just the one)
for (; ub>=umsd; *ub=0, ub--) {
if (*ub==9) continue; // carry
*ub+=1;
break;
}
if (ub<umsd) { // had all-nines
*umsd=1; // coefficient to 1000...
// usually the 1000... coefficient can be used as-is
if ((ulsd-umsd+1)==DECPMAX) {
num->exponent++;
}
else {
// if coefficient is shorter than Pmax then num is
// subnormal, so extend it; this is safe as drop>0
// (or, if the coefficient was supplied above, it could
// not be 9); this may make the result normal.
ulsd++;
*ulsd=0;
// [exponent unchanged]
#if DECCHECK
if (num->exponent!=DECQTINY) // sanity check
printf("decFinalize: bad all-nines extend [^%ld, %ld]\n",
(LI)num->exponent, (LI)(ulsd-umsd+1));
#endif
} // subnormal extend
} // had all-nines
} // bump needed
} // inexact rounding
length=ulsd-umsd+1; // recalculate (may be <DECPMAX)
} // need round (drop>0)
// The coefficient will now fit and has final length unless overflow
// decShowNum(num, "rounded");
// if exponent is >=emax may have to clamp, overflow, or fold-down
if (num->exponent>DECEMAX-(DECPMAX-1)) { // is edge case
// printf("overflow checks...\n");
if (*ulsd==0 && ulsd==umsd) { // have zero
num->exponent=DECEMAX-(DECPMAX-1); // clamp to max
}
else if ((num->exponent+length-1)>DECEMAX) { // > Nmax
// Overflow -- these could go straight to encoding, here, but
// instead num is adjusted to keep the code cleaner
Flag needmax=0; // 1 for finite result
set->status|=(DEC_Overflow | DEC_Inexact);
switch (set->round) {
case DEC_ROUND_DOWN: {
needmax=1; // never Infinity
break;} // r-d
case DEC_ROUND_05UP: {
needmax=1; // never Infinity
break;} // r-05
case DEC_ROUND_CEILING: {
if (num->sign) needmax=1; // Infinity iff non-negative
break;} // r-c
case DEC_ROUND_FLOOR: {
if (!num->sign) needmax=1; // Infinity iff negative
break;} // r-f
default: break; // Infinity in all other cases
}
if (!needmax) { // easy .. set Infinity
num->exponent=DECFLOAT_Inf;
*umsd=0; // be clean: coefficient to 0
ulsd=umsd; // ..
}
else { // return Nmax
umsd=allnines; // use constant array
ulsd=allnines+DECPMAX-1;
num->exponent=DECEMAX-(DECPMAX-1);
}
}
else { // no overflow but non-zero and may have to fold-down
Int shift=num->exponent-(DECEMAX-(DECPMAX-1));
if (shift>0) { // fold-down needed
// fold down needed; must copy to buffer in order to pad
// with zeros safely; fortunately this is not the worst case
// path because cannot have had a round
uByte buffer[ROUNDUP(DECPMAX+3, 4)]; // [+3 allows uInt padding]
uByte *s=umsd; // source
uByte *t=buffer; // safe target
uByte *tlsd=buffer+(ulsd-umsd)+shift; // target LSD
// printf("folddown shift=%ld\n", (LI)shift);
for (; s<=ulsd; s+=4, t+=4) UBFROMUI(t, UBTOUI(s));
for (t=tlsd-shift+1; t<=tlsd; t+=4) UBFROMUI(t, 0); // pad 0s
num->exponent-=shift;
umsd=buffer;
ulsd=tlsd;
}
} // fold-down?
length=ulsd-umsd+1; // recalculate length
} // high-end edge case
} // finite number
/*------------------------------------------------------------------*/
/* At this point the result will properly fit the decFloat */
/* encoding, and it can be encoded with no possibility of error */
/*------------------------------------------------------------------*/
// Following code does not alter coefficient (could be allnines array)
// fast path possible when DECPMAX digits
if (length==DECPMAX) {
return decFloatFromBCD(df, num->exponent, umsd, num->sign);
} // full-length
// slower path when not a full-length number; must care about length
// [coefficient length here will be < DECPMAX]
if (!NUMISSPECIAL(num)) { // is still finite
// encode the combination field and exponent continuation
uInt uexp=(uInt)(num->exponent+DECBIAS); // biased exponent
uInt code=(uexp>>DECECONL)<<4; // top two bits of exp
// [msd==0]
// look up the combination field and make high word
encode=DECCOMBFROM[code]; // indexed by (0-2)*16+msd
encode|=(uexp<<(32-6-DECECONL)) & 0x03ffffff; // exponent continuation
}
else encode=num->exponent; // special [already in word]
encode|=num->sign; // add sign
// private macro to extract a declet, n (where 0<=n<DECLETS and 0
// refers to the declet from the least significant three digits)
// and put the corresponding DPD code into dpd. Access to umsd and
// ulsd (pointers to the most and least significant digit of the
// variable-length coefficient) is assumed, along with use of a
// working pointer, uInt *ub.
// As not full-length then chances are there are many leading zeros
// [and there may be a partial triad]
#define getDPDt(dpd, n) ub=ulsd-(3*(n))-2; \
if (ub<umsd-2) dpd=0; \
else if (ub>=umsd) dpd=BCD2DPD[(*ub*256)+(*(ub+1)*16)+*(ub+2)]; \
else {dpd=*(ub+2); if (ub+1==umsd) dpd+=*(ub+1)*16; dpd=BCD2DPD[dpd];}
// place the declets in the encoding words and copy to result (df),
// according to endianness; in all cases complete the sign word
// first
#if DECPMAX==7
getDPDt(dpd, 1);
encode|=dpd<<10;
getDPDt(dpd, 0);
encode|=dpd;
DFWORD(df, 0)=encode; // just the one word
#elif DECPMAX==16
getDPDt(dpd, 4); encode|=dpd<<8;
getDPDt(dpd, 3); encode|=dpd>>2;
DFWORD(df, 0)=encode;
encode=dpd<<30;
getDPDt(dpd, 2); encode|=dpd<<20;
getDPDt(dpd, 1); encode|=dpd<<10;
getDPDt(dpd, 0); encode|=dpd;
DFWORD(df, 1)=encode;
#elif DECPMAX==34
getDPDt(dpd,10); encode|=dpd<<4;
getDPDt(dpd, 9); encode|=dpd>>6;
DFWORD(df, 0)=encode;
encode=dpd<<26;
getDPDt(dpd, 8); encode|=dpd<<16;
getDPDt(dpd, 7); encode|=dpd<<6;
getDPDt(dpd, 6); encode|=dpd>>4;
DFWORD(df, 1)=encode;
encode=dpd<<28;
getDPDt(dpd, 5); encode|=dpd<<18;
getDPDt(dpd, 4); encode|=dpd<<8;
getDPDt(dpd, 3); encode|=dpd>>2;
DFWORD(df, 2)=encode;
encode=dpd<<30;
getDPDt(dpd, 2); encode|=dpd<<20;
getDPDt(dpd, 1); encode|=dpd<<10;
getDPDt(dpd, 0); encode|=dpd;
DFWORD(df, 3)=encode;
#endif
// printf("Status: %08lx\n", (LI)set->status);
// decFloatShow(df, "final2");
return df;
} // decFinalize
/* ------------------------------------------------------------------ */
/* decFloatFromBCD -- set decFloat from exponent, BCD8, and sign */
/* */
/* df is the target decFloat */
/* exp is the in-range unbiased exponent, q, or a special value in */
/* the form returned by decFloatGetExponent */
/* bcdar holds DECPMAX digits to set the coefficient from, one */
/* digit in each byte (BCD8 encoding); the first (MSD) is ignored */
/* if df is a NaN; all are ignored if df is infinite. */
/* All bytes must be in 0-9; results are undefined otherwise. */
/* sig is DECFLOAT_Sign to set the sign bit, 0 otherwise */
/* returns df, which will be canonical */
/* */
/* No error is possible, and no status will be set. */
/* ------------------------------------------------------------------ */
decFloat * decFloatFromBCD(decFloat *df, Int exp, const uByte *bcdar,
Int sig) {
uInt encode, dpd; // work
const uByte *ub; // ..
if (EXPISSPECIAL(exp)) encode=exp|sig;// specials already encoded
else { // is finite
// encode the combination field and exponent continuation
uInt uexp=(uInt)(exp+DECBIAS); // biased exponent
uInt code=(uexp>>DECECONL)<<4; // top two bits of exp
code+=bcdar[0]; // add msd
// look up the combination field and make high word
encode=DECCOMBFROM[code]|sig; // indexed by (0-2)*16+msd
encode|=(uexp<<(32-6-DECECONL)) & 0x03ffffff; // exponent continuation
}
// private macro to extract a declet, n (where 0<=n<DECLETS and 0
// refers to the declet from the least significant three digits)
// and put the corresponding DPD code into dpd.
// Use of a working pointer, uInt *ub, is assumed.
#define getDPDb(dpd, n) ub=bcdar+DECPMAX-1-(3*(n))-2; \
dpd=BCD2DPD[(*ub*256)+(*(ub+1)*16)+*(ub+2)];
// place the declets in the encoding words and copy to result (df),
// according to endianness; in all cases complete the sign word
// first
#if DECPMAX==7
getDPDb(dpd, 1);
encode|=dpd<<10;
getDPDb(dpd, 0);
encode|=dpd;
DFWORD(df, 0)=encode; // just the one word
#elif DECPMAX==16
getDPDb(dpd, 4); encode|=dpd<<8;
getDPDb(dpd, 3); encode|=dpd>>2;
DFWORD(df, 0)=encode;
encode=dpd<<30;
getDPDb(dpd, 2); encode|=dpd<<20;
getDPDb(dpd, 1); encode|=dpd<<10;
getDPDb(dpd, 0); encode|=dpd;
DFWORD(df, 1)=encode;
#elif DECPMAX==34
getDPDb(dpd,10); encode|=dpd<<4;
getDPDb(dpd, 9); encode|=dpd>>6;
DFWORD(df, 0)=encode;
encode=dpd<<26;
getDPDb(dpd, 8); encode|=dpd<<16;
getDPDb(dpd, 7); encode|=dpd<<6;
getDPDb(dpd, 6); encode|=dpd>>4;
DFWORD(df, 1)=encode;
encode=dpd<<28;
getDPDb(dpd, 5); encode|=dpd<<18;
getDPDb(dpd, 4); encode|=dpd<<8;
getDPDb(dpd, 3); encode|=dpd>>2;
DFWORD(df, 2)=encode;
encode=dpd<<30;
getDPDb(dpd, 2); encode|=dpd<<20;
getDPDb(dpd, 1); encode|=dpd<<10;
getDPDb(dpd, 0); encode|=dpd;
DFWORD(df, 3)=encode;
#endif
// decFloatShow(df, "fromB");
return df;
} // decFloatFromBCD
/* ------------------------------------------------------------------ */
/* decFloatFromPacked -- set decFloat from exponent and packed BCD */
/* */
/* df is the target decFloat */
/* exp is the in-range unbiased exponent, q, or a special value in */
/* the form returned by decFloatGetExponent */
/* packed holds DECPMAX packed decimal digits plus a sign nibble */
/* (all 6 codes are OK); the first (MSD) is ignored if df is a NaN */
/* and all except sign are ignored if df is infinite. For DOUBLE */
/* and QUAD the first (pad) nibble is also ignored in all cases. */
/* All coefficient nibbles must be in 0-9 and sign in A-F; results */
/* are undefined otherwise. */
/* returns df, which will be canonical */
/* */
/* No error is possible, and no status will be set. */
/* ------------------------------------------------------------------ */
decFloat * decFloatFromPacked(decFloat *df, Int exp, const uByte *packed) {
uByte bcdar[DECPMAX+2]; // work [+1 for pad, +1 for sign]
const uByte *ip; // ..
uByte *op; // ..
Int sig=0; // sign
// expand coefficient and sign to BCDAR
#if SINGLE
op=bcdar+1; // no pad digit
#else
op=bcdar; // first (pad) digit ignored
#endif
for (ip=packed; ip<packed+((DECPMAX+2)/2); ip++) {
*op++=*ip>>4;
*op++=(uByte)(*ip&0x0f); // [final nibble is sign]
}
op--; // -> sign byte
if (*op==DECPMINUS || *op==DECPMINUSALT) sig=DECFLOAT_Sign;
if (EXPISSPECIAL(exp)) { // Infinity or NaN
if (!EXPISINF(exp)) bcdar[1]=0; // a NaN: ignore MSD
else memset(bcdar+1, 0, DECPMAX); // Infinite: coefficient to 0
}
return decFloatFromBCD(df, exp, bcdar+1, sig);
} // decFloatFromPacked
/* ------------------------------------------------------------------ */
/* decFloatFromPackedChecked -- set from exponent and packed; checked */
/* */
/* df is the target decFloat */
/* exp is the in-range unbiased exponent, q, or a special value in */
/* the form returned by decFloatGetExponent */
/* packed holds DECPMAX packed decimal digits plus a sign nibble */
/* (all 6 codes are OK); the first (MSD) must be 0 if df is a NaN */
/* and all digits must be 0 if df is infinite. For DOUBLE and */
/* QUAD the first (pad) nibble must be 0. */
/* All coefficient nibbles must be in 0-9 and sign in A-F. */
/* returns df, which will be canonical or NULL if any of the */
/* requirements are not met (if this case df is unchanged); that */
/* is, the input data must be as returned by decFloatToPacked, */
/* except that all six sign codes are acccepted. */
/* */
/* No status will be set. */
/* ------------------------------------------------------------------ */
decFloat * decFloatFromPackedChecked(decFloat *df, Int exp,
const uByte *packed) {
uByte bcdar[DECPMAX+2]; // work [+1 for pad, +1 for sign]
const uByte *ip; // ..
uByte *op; // ..
Int sig=0; // sign
// expand coefficient and sign to BCDAR
#if SINGLE
op=bcdar+1; // no pad digit
#else
op=bcdar; // first (pad) digit here
#endif
for (ip=packed; ip<packed+((DECPMAX+2)/2); ip++) {
*op=*ip>>4;
if (*op>9) return NULL;
op++;
*op=(uByte)(*ip&0x0f); // [final nibble is sign]
if (*op>9 && ip<packed+((DECPMAX+2)/2)-1) return NULL;
op++;
}
op--; // -> sign byte
if (*op<=9) return NULL; // bad sign
if (*op==DECPMINUS || *op==DECPMINUSALT) sig=DECFLOAT_Sign;
#if !SINGLE
if (bcdar[0]!=0) return NULL; // bad pad nibble
#endif
if (EXPISNAN(exp)) { // a NaN
if (bcdar[1]!=0) return NULL; // bad msd
} // NaN
else if (EXPISINF(exp)) { // is infinite
Int i;
for (i=0; i<DECPMAX; i++) {
if (bcdar[i+1]!=0) return NULL; // should be all zeros
}
} // infinity
else { // finite
// check the exponent is in range
if (exp>DECEMAX-DECPMAX+1) return NULL;
if (exp<DECEMIN-DECPMAX+1) return NULL;
}
return decFloatFromBCD(df, exp, bcdar+1, sig);
} // decFloatFromPacked
/* ------------------------------------------------------------------ */
/* decFloatFromString -- conversion from numeric string */
/* */
/* result is the decFloat format number which gets the result of */
/* the conversion */
/* *string is the character string which should contain a valid */
/* number (which may be a special value), \0-terminated */
/* If there are too many significant digits in the */
/* coefficient it will be rounded. */
/* set is the context */
/* returns result */
/* */
/* The length of the coefficient and the size of the exponent are */
/* checked by this routine, so the correct error (Underflow or */
/* Overflow) can be reported or rounding applied, as necessary. */
/* */
/* There is no limit to the coefficient length for finite inputs; */
/* NaN payloads must be integers with no more than DECPMAX-1 digits. */
/* Exponents may have up to nine significant digits. */
/* */
/* If bad syntax is detected, the result will be a quiet NaN. */
/* ------------------------------------------------------------------ */
decFloat * decFloatFromString(decFloat *result, const char *string,
decContext *set) {
Int digits; // count of digits in coefficient
const char *dotchar=NULL; // where dot was found [NULL if none]
const char *cfirst=string; // -> first character of decimal part
const char *c; // work
uByte *ub; // ..
uInt uiwork; // for macros
bcdnum num; // collects data for finishing
uInt error=DEC_Conversion_syntax; // assume the worst
uByte buffer[ROUNDUP(DECSTRING+11, 8)]; // room for most coefficents,
// some common rounding, +3, & pad
#if DECTRACE
// printf("FromString %s ...\n", string);
#endif
for(;;) { // once-only 'loop'
num.sign=0; // assume non-negative
num.msd=buffer; // MSD is here always
// detect and validate the coefficient, including any leading,
// trailing, or embedded '.'
// [could test four-at-a-time here (saving 10% for decQuads),
// but that risks storage violation because the position of the
// terminator is unknown]
for (c=string;; c++) { // -> input character
if (((unsigned)(*c-'0'))<=9) continue; // '0' through '9' is good
if (*c=='\0') break; // most common non-digit
if (*c=='.') {
if (dotchar!=NULL) break; // not first '.'
dotchar=c; // record offset into decimal part
continue;}
if (c==string) { // first in string...
if (*c=='-') { // valid - sign
cfirst++;
num.sign=DECFLOAT_Sign;
continue;}
if (*c=='+') { // valid + sign
cfirst++;
continue;}
}
// *c is not a digit, terminator, or a valid +, -, or '.'
break;
} // c loop
digits=(uInt)(c-cfirst); // digits (+1 if a dot)
if (digits>0) { // had digits and/or dot
const char *clast=c-1; // note last coefficient char position
Int exp=0; // exponent accumulator
if (*c!='\0') { // something follows the coefficient
uInt edig; // unsigned work
// had some digits and more to come; expect E[+|-]nnn now
const char *firstexp; // exponent first non-zero
if (*c!='E' && *c!='e') break;
c++; // to (optional) sign
if (*c=='-' || *c=='+') c++; // step over sign (c=clast+2)
if (*c=='\0') break; // no digits! (e.g., '1.2E')
for (; *c=='0';) c++; // skip leading zeros [even last]
firstexp=c; // remember start [maybe '\0']
// gather exponent digits
edig=(uInt)*c-(uInt)'0';
if (edig<=9) { // [check not bad or terminator]
exp+=edig; // avoid initial X10
c++;
for (;; c++) {
edig=(uInt)*c-(uInt)'0';
if (edig>9) break;
exp=exp*10+edig;
}
}
// if not now on the '\0', *c must not be a digit
if (*c!='\0') break;
// (this next test must be after the syntax checks)
// if definitely more than the possible digits for format then
// the exponent may have wrapped, so simply set it to a certain
// over/underflow value
if (c>firstexp+DECEMAXD) exp=DECEMAX*2;
if (*(clast+2)=='-') exp=-exp; // was negative
} // exponent part
if (dotchar!=NULL) { // had a '.'
digits--; // remove from digits count
if (digits==0) break; // was dot alone: bad syntax
exp-=(Int)(clast-dotchar); // adjust exponent
// [the '.' can now be ignored]
}
num.exponent=exp; // exponent is good; store it
// Here when whole string has been inspected and syntax is good
// cfirst->first digit or dot, clast->last digit or dot
error=0; // no error possible now
// if the number of digits in the coefficient will fit in buffer
// then it can simply be converted to bcd8 and copied -- decFinalize
// will take care of leading zeros and rounding; the buffer is big
// enough for all canonical coefficients, including 0.00000nn...
ub=buffer;
if (digits<=(Int)(sizeof(buffer)-3)) { // [-3 allows by-4s copy]
c=cfirst;
if (dotchar!=NULL) { // a dot to worry about
if (*(c+1)=='.') { // common canonical case
*ub++=(uByte)(*c-'0'); // copy leading digit
c+=2; // prepare to handle rest
}
else for (; c<=clast;) { // '.' could be anywhere
// as usual, go by fours when safe; NB it has been asserted
// that a '.' does not have the same mask as a digit
if (c<=clast-3 // safe for four
&& (UBTOUI(c)&0xf0f0f0f0)==CHARMASK) { // test four
UBFROMUI(ub, UBTOUI(c)&0x0f0f0f0f); // to BCD8
ub+=4;
c+=4;
continue;
}
if (*c=='.') { // found the dot
c++; // step over it ..
break; // .. and handle the rest
}
*ub++=(uByte)(*c++-'0');
}
} // had dot
// Now no dot; do this by fours (where safe)
for (; c<=clast-3; c+=4, ub+=4) UBFROMUI(ub, UBTOUI(c)&0x0f0f0f0f);
for (; c<=clast; c++, ub++) *ub=(uByte)(*c-'0');
num.lsd=buffer+digits-1; // record new LSD
} // fits
else { // too long for buffer
// [This is a rare and unusual case; arbitrary-length input]
// strip leading zeros [but leave final 0 if all 0's]
if (*cfirst=='.') cfirst++; // step past dot at start
if (*cfirst=='0') { // [cfirst always -> digit]
for (; cfirst<clast; cfirst++) {
if (*cfirst!='0') { // non-zero found
if (*cfirst=='.') continue; // [ignore]
break; // done
}
digits--; // 0 stripped
} // cfirst
} // at least one leading 0
// the coefficient is now as short as possible, but may still
// be too long; copy up to Pmax+1 digits to the buffer, then
// just record any non-zeros (set round-for-reround digit)
for (c=cfirst; c<=clast && ub<=buffer+DECPMAX; c++) {
// (see commentary just above)
if (c<=clast-3 // safe for four
&& (UBTOUI(c)&0xf0f0f0f0)==CHARMASK) { // four digits
UBFROMUI(ub, UBTOUI(c)&0x0f0f0f0f); // to BCD8
ub+=4;
c+=3; // [will become 4]
continue;
}
if (*c=='.') continue; // [ignore]
*ub++=(uByte)(*c-'0');
}
ub--; // -> LSD
for (; c<=clast; c++) { // inspect remaining chars
if (*c!='0') { // sticky bit needed
if (*c=='.') continue; // [ignore]
*ub=DECSTICKYTAB[*ub]; // update round-for-reround
break; // no need to look at more
}
}
num.lsd=ub; // record LSD
// adjust exponent for dropped digits
num.exponent+=digits-(Int)(ub-buffer+1);
} // too long for buffer
} // digits and/or dot
else { // no digits or dot were found
// only Infinities and NaNs are allowed, here
if (*c=='\0') break; // nothing there is bad
buffer[0]=0; // default a coefficient of 0
num.lsd=buffer; // ..
if (decBiStr(c, "infinity", "INFINITY")
|| decBiStr(c, "inf", "INF")) num.exponent=DECFLOAT_Inf;
else { // should be a NaN
num.exponent=DECFLOAT_qNaN; // assume quiet NaN
if (*c=='s' || *c=='S') { // probably an sNaN
num.exponent=DECFLOAT_sNaN; // effect the 's'
c++; // and step over it
}
if (*c!='N' && *c!='n') break; // check caseless "NaN"
c++;
if (*c!='a' && *c!='A') break; // ..
c++;
if (*c!='N' && *c!='n') break; // ..
c++;
// now either nothing, or nnnn payload (no dots), expected
// -> start of integer, and skip leading 0s [including plain 0]
for (cfirst=c; *cfirst=='0';) cfirst++;
if (*cfirst!='\0') { // not empty or all-0, payload
// payload found; check all valid digits and copy to buffer as bcd8
ub=buffer;
for (c=cfirst;; c++, ub++) {
if ((unsigned)(*c-'0')>9) break; // quit if not 0-9
if (c-cfirst==DECPMAX-1) break; // too many digits
*ub=(uByte)(*c-'0'); // good bcd8
}
if (*c!='\0') break; // not all digits, or too many
num.lsd=ub-1; // record new LSD
}
} // NaN or sNaN
error=0; // syntax is OK
} // digits=0 (special expected)
break; // drop out
} // [for(;;) once-loop]
// decShowNum(&num, "fromStr");
if (error!=0) {
set->status|=error;
num.exponent=DECFLOAT_qNaN; // set up quiet NaN
num.sign=0; // .. with 0 sign
buffer[0]=0; // .. and coefficient
num.lsd=buffer; // ..
// decShowNum(&num, "oops");
}
// decShowNum(&num, "dffs");
decFinalize(result, &num, set); // round, check, and lay out
// decFloatShow(result, "fromString");