Mercurial > hg > CbC > CbC_gcc
comparison libdecnumber/decBasic.c @ 55:77e2b8dfacca gcc-4.4.5
update it from 4.4.3 to 4.5.0
author | ryoma <e075725@ie.u-ryukyu.ac.jp> |
---|---|
date | Fri, 12 Feb 2010 23:39:51 +0900 |
parents | a06113de4d67 |
children | 04ced10e8804 |
comparison
equal
deleted
inserted
replaced
52:c156f1bd5cd9 | 55:77e2b8dfacca |
---|---|
25 | 25 |
26 /* ------------------------------------------------------------------ */ | 26 /* ------------------------------------------------------------------ */ |
27 /* decBasic.c -- common base code for Basic decimal types */ | 27 /* decBasic.c -- common base code for Basic decimal types */ |
28 /* ------------------------------------------------------------------ */ | 28 /* ------------------------------------------------------------------ */ |
29 /* This module comprises code that is shared between decDouble and */ | 29 /* This module comprises code that is shared between decDouble and */ |
30 /* decQuad (but not decSingle). The main arithmetic operations are */ | 30 /* decQuad (but not decSingle). The main arithmetic operations are */ |
31 /* here (Add, Subtract, Multiply, FMA, and Division operators). */ | 31 /* here (Add, Subtract, Multiply, FMA, and Division operators). */ |
32 /* */ | 32 /* */ |
33 /* Unlike decNumber, parameterization takes place at compile time */ | 33 /* Unlike decNumber, parameterization takes place at compile time */ |
34 /* rather than at runtime. The parameters are set in the decDouble.c */ | 34 /* rather than at runtime. The parameters are set in the decDouble.c */ |
35 /* (etc.) files, which then include this one to produce the compiled */ | 35 /* (etc.) files, which then include this one to produce the compiled */ |
36 /* code. The functions here, therefore, are code shared between */ | 36 /* code. The functions here, therefore, are code shared between */ |
52 | 52 |
53 /* Private constants */ | 53 /* Private constants */ |
54 #define DIVIDE 0x80000000 /* Divide operations [as flags] */ | 54 #define DIVIDE 0x80000000 /* Divide operations [as flags] */ |
55 #define REMAINDER 0x40000000 /* .. */ | 55 #define REMAINDER 0x40000000 /* .. */ |
56 #define DIVIDEINT 0x20000000 /* .. */ | 56 #define DIVIDEINT 0x20000000 /* .. */ |
57 #define REMNEAR 0x10000000 /* .. */ | 57 #define REMNEAR 0x10000000 /* .. */ |
58 | 58 |
59 /* Private functions (local, used only by routines in this module) */ | 59 /* Private functions (local, used only by routines in this module) */ |
60 static decFloat *decDivide(decFloat *, const decFloat *, | 60 static decFloat *decDivide(decFloat *, const decFloat *, |
61 const decFloat *, decContext *, uInt); | 61 const decFloat *, decContext *, uInt); |
62 static decFloat *decCanonical(decFloat *, const decFloat *); | 62 static decFloat *decCanonical(decFloat *, const decFloat *); |
74 | 74 |
75 /* ------------------------------------------------------------------ */ | 75 /* ------------------------------------------------------------------ */ |
76 /* decCanonical -- copy a decFloat, making canonical */ | 76 /* decCanonical -- copy a decFloat, making canonical */ |
77 /* */ | 77 /* */ |
78 /* result gets the canonicalized df */ | 78 /* result gets the canonicalized df */ |
79 /* df is the decFloat to copy and make canonical */ | 79 /* df is the decFloat to copy and make canonical */ |
80 /* returns result */ | 80 /* returns result */ |
81 /* */ | 81 /* */ |
82 /* This is exposed via decFloatCanonical for Double and Quad only. */ | 82 /* This is exposed via decFloatCanonical for Double and Quad only. */ |
83 /* This works on specials, too; no error or exception is possible. */ | 83 /* This works on specials, too; no error or exception is possible. */ |
84 /* ------------------------------------------------------------------ */ | 84 /* ------------------------------------------------------------------ */ |
134 inword--; | 134 inword--; |
135 encode=DFWORD(result, inword); | 135 encode=DFWORD(result, inword); |
136 uoff-=32; | 136 uoff-=32; |
137 dpd|=encode<<(10-uoff); /* get pending bits */ | 137 dpd|=encode<<(10-uoff); /* get pending bits */ |
138 } | 138 } |
139 dpd&=0x3ff; /* clear uninteresting bits */ | 139 dpd&=0x3ff; /* clear uninteresting bits */ |
140 if (dpd<0x16e) continue; /* must be canonical */ | 140 if (dpd<0x16e) continue; /* must be canonical */ |
141 canon=BIN2DPD[DPD2BIN[dpd]]; /* determine canonical declet */ | 141 canon=BIN2DPD[DPD2BIN[dpd]]; /* determine canonical declet */ |
142 if (canon==dpd) continue; /* have canonical declet */ | 142 if (canon==dpd) continue; /* have canonical declet */ |
143 /* need to replace declet */ | 143 /* need to replace declet */ |
144 if (uoff>=10) { /* all within current word */ | 144 if (uoff>=10) { /* all within current word */ |
145 encode&=~(0x3ff<<(uoff-10)); /* clear the 10 bits ready for replace */ | 145 encode&=~(0x3ff<<(uoff-10)); /* clear the 10 bits ready for replace */ |
146 encode|=canon<<(uoff-10); /* insert the canonical form */ | 146 encode|=canon<<(uoff-10); /* insert the canonical form */ |
147 DFWORD(result, inword)=encode; /* .. and save */ | 147 DFWORD(result, inword)=encode; /* .. and save */ |
148 continue; | 148 continue; |
149 } | 149 } |
150 /* straddled words */ | 150 /* straddled words */ |
151 precode=DFWORD(result, inword+1); /* get previous */ | 151 precode=DFWORD(result, inword+1); /* get previous */ |
160 | 160 |
161 /* ------------------------------------------------------------------ */ | 161 /* ------------------------------------------------------------------ */ |
162 /* decDivide -- divide operations */ | 162 /* decDivide -- divide operations */ |
163 /* */ | 163 /* */ |
164 /* result gets the result of dividing dfl by dfr: */ | 164 /* result gets the result of dividing dfl by dfr: */ |
165 /* dfl is the first decFloat (lhs) */ | 165 /* dfl is the first decFloat (lhs) */ |
166 /* dfr is the second decFloat (rhs) */ | 166 /* dfr is the second decFloat (rhs) */ |
167 /* set is the context */ | 167 /* set is the context */ |
168 /* op is the operation selector */ | 168 /* op is the operation selector */ |
169 /* returns result */ | 169 /* returns result */ |
170 /* */ | 170 /* */ |
171 /* op is one of DIVIDE, REMAINDER, DIVIDEINT, or REMNEAR. */ | 171 /* op is one of DIVIDE, REMAINDER, DIVIDEINT, or REMNEAR. */ |
172 /* ------------------------------------------------------------------ */ | 172 /* ------------------------------------------------------------------ */ |
173 #define DIVCOUNT 0 /* 1 to instrument subtractions counter */ | 173 #define DIVCOUNT 0 /* 1 to instrument subtractions counter */ |
174 #define DIVBASE BILLION /* the base used for divide */ | 174 #define DIVBASE ((uInt)BILLION) /* the base used for divide */ |
175 #define DIVOPLEN DECPMAX9 /* operand length ('digits' base 10**9) */ | 175 #define DIVOPLEN DECPMAX9 /* operand length ('digits' base 10**9) */ |
176 #define DIVACCLEN (DIVOPLEN*3) /* accumulator length (ditto) */ | 176 #define DIVACCLEN (DIVOPLEN*3) /* accumulator length (ditto) */ |
177 static decFloat * decDivide(decFloat *result, const decFloat *dfl, | 177 static decFloat * decDivide(decFloat *result, const decFloat *dfl, |
178 const decFloat *dfr, decContext *set, uInt op) { | 178 const decFloat *dfr, decContext *set, uInt op) { |
179 decFloat quotient; /* for remainders */ | 179 decFloat quotient; /* for remainders */ |
180 bcdnum num; /* for final conversion */ | 180 bcdnum num; /* for final conversion */ |
181 uInt acc[DIVACCLEN]; /* coefficent in base-billion .. */ | 181 uInt acc[DIVACCLEN]; /* coefficent in base-billion .. */ |
182 uInt div[DIVOPLEN]; /* divisor in base-billion .. */ | 182 uInt div[DIVOPLEN]; /* divisor in base-billion .. */ |
183 uInt quo[DIVOPLEN+1]; /* quotient in base-billion .. */ | 183 uInt quo[DIVOPLEN+1]; /* quotient in base-billion .. */ |
184 uByte bcdacc[(DIVOPLEN+1)*9+2]; /* for quotient in BCD, +1, +1 */ | 184 uByte bcdacc[(DIVOPLEN+1)*9+2]; /* for quotient in BCD, +1, +1 */ |
185 uInt *msua, *msud, *msuq; /* -> msu of acc, div, and quo */ | 185 uInt *msua, *msud, *msuq; /* -> msu of acc, div, and quo */ |
186 Int divunits, accunits; /* lengths */ | 186 Int divunits, accunits; /* lengths */ |
187 Int quodigits; /* digits in quotient */ | 187 Int quodigits; /* digits in quotient */ |
188 uInt *lsua, *lsuq; /* -> current acc and quo lsus */ | 188 uInt *lsua, *lsuq; /* -> current acc and quo lsus */ |
189 Int length, multiplier; /* work */ | 189 Int length, multiplier; /* work */ |
190 uInt carry, sign; /* .. */ | 190 uInt carry, sign; /* .. */ |
191 uInt *ua, *ud, *uq; /* .. */ | 191 uInt *ua, *ud, *uq; /* .. */ |
192 uByte *ub; /* .. */ | 192 uByte *ub; /* .. */ |
193 uInt uiwork; /* for macros */ | |
193 uInt divtop; /* top unit of div adjusted for estimating */ | 194 uInt divtop; /* top unit of div adjusted for estimating */ |
194 #if DIVCOUNT | 195 #if DIVCOUNT |
195 static uInt maxcount=0; /* worst-seen subtractions count */ | 196 static uInt maxcount=0; /* worst-seen subtractions count */ |
196 uInt divcount=0; /* subtractions count [this divide] */ | 197 uInt divcount=0; /* subtractions count [this divide] */ |
197 #endif | 198 #endif |
228 return result; | 229 return result; |
229 } | 230 } |
230 if (op&(REMAINDER|REMNEAR)) return decInvalid(result, set); /* bad rem */ | 231 if (op&(REMAINDER|REMNEAR)) return decInvalid(result, set); /* bad rem */ |
231 set->status|=DEC_Division_by_zero; | 232 set->status|=DEC_Division_by_zero; |
232 DFWORD(result, 0)=num.sign; | 233 DFWORD(result, 0)=num.sign; |
233 return decInfinity(result, result); /* x/0 -> signed Infinity */ | 234 return decInfinity(result, result); /* x/0 -> signed Infinity */ |
234 } | 235 } |
235 num.exponent=GETEXPUN(dfl)-GETEXPUN(dfr); /* ideal exponent */ | 236 num.exponent=GETEXPUN(dfl)-GETEXPUN(dfr); /* ideal exponent */ |
236 if (DFISZERO(dfl)) { /* 0/x (x!=0) */ | 237 if (DFISZERO(dfl)) { /* 0/x (x!=0) */ |
237 /* if divide, result is 0 with ideal exponent; divideInt has */ | 238 /* if divide, result is 0 with ideal exponent; divideInt has */ |
238 /* exponent=0, remainders give zero with lower exponent */ | 239 /* exponent=0, remainders give zero with lower exponent */ |
239 if (op&DIVIDEINT) { | 240 if (op&DIVIDEINT) { |
240 decFloatZero(result); | 241 decFloatZero(result); |
241 DFWORD(result, 0)|=num.sign; /* add sign */ | 242 DFWORD(result, 0)|=num.sign; /* add sign */ |
242 return result; | 243 return result; |
243 } | 244 } |
244 if (!(op&DIVIDE)) { /* a remainder */ | 245 if (!(op&DIVIDE)) { /* a remainder */ |
245 /* exponent is the minimum of the operands */ | 246 /* exponent is the minimum of the operands */ |
246 num.exponent=MINI(GETEXPUN(dfl), GETEXPUN(dfr)); | 247 num.exponent=MINI(GETEXPUN(dfl), GETEXPUN(dfr)); |
247 /* if the result is zero the sign shall be sign of dfl */ | 248 /* if the result is zero the sign shall be sign of dfl */ |
248 num.sign=DFWORD(dfl, 0)&DECFLOAT_Sign; | 249 num.sign=DFWORD(dfl, 0)&DECFLOAT_Sign; |
249 } | 250 } |
282 #error Unexpected Quad DIVOPLEN | 283 #error Unexpected Quad DIVOPLEN |
283 #endif | 284 #endif |
284 #endif | 285 #endif |
285 | 286 |
286 /* set msu and lsu pointers */ | 287 /* set msu and lsu pointers */ |
287 msua=acc+DIVACCLEN-1; /* [leading zeros removed below] */ | 288 msua=acc+DIVACCLEN-1; /* [leading zeros removed below] */ |
288 msuq=quo+DIVOPLEN; | 289 msuq=quo+DIVOPLEN; |
289 /*[loop for div will terminate because operands are non-zero] */ | 290 /*[loop for div will terminate because operands are non-zero] */ |
290 for (msud=div+DIVOPLEN-1; *msud==0;) msud--; | 291 for (msud=div+DIVOPLEN-1; *msud==0;) msud--; |
291 /* the initial least-significant unit of acc is set so acc appears */ | 292 /* the initial least-significant unit of acc is set so acc appears */ |
292 /* to have the same length as div. */ | 293 /* to have the same length as div. */ |
293 /* This moves one position towards the least possible for each */ | 294 /* This moves one position towards the least possible for each */ |
294 /* iteration */ | 295 /* iteration */ |
295 divunits=(Int)(msud-div+1); /* precalculate */ | 296 divunits=(Int)(msud-div+1); /* precalculate */ |
296 lsua=msua-divunits+1; /* initial working lsu of acc */ | 297 lsua=msua-divunits+1; /* initial working lsu of acc */ |
297 lsuq=msuq; /* and of quo */ | 298 lsuq=msuq; /* and of quo */ |
298 | 299 |
299 /* set up the estimator for the multiplier; this is the msu of div, */ | 300 /* set up the estimator for the multiplier; this is the msu of div, */ |
300 /* plus two bits from the unit below (if any) rounded up by one if */ | 301 /* plus two bits from the unit below (if any) rounded up by one if */ |
301 /* there are any non-zero bits or units below that [the extra two */ | 302 /* there are any non-zero bits or units below that [the extra two */ |
364 /* acc==div: quotient gains 1, set acc=0 */ | 365 /* acc==div: quotient gains 1, set acc=0 */ |
365 /* acc>div: subtraction necessary at this position */ | 366 /* acc>div: subtraction necessary at this position */ |
366 for (ud=msud, ua=msua; ud>div; ud--, ua--) if (*ud!=*ua) break; | 367 for (ud=msud, ua=msua; ud>div; ud--, ua--) if (*ud!=*ua) break; |
367 /* [now at first mismatch or lsu] */ | 368 /* [now at first mismatch or lsu] */ |
368 if (*ud>*ua) break; /* next time... */ | 369 if (*ud>*ua) break; /* next time... */ |
369 if (*ud==*ua) { /* all compared equal */ | 370 if (*ud==*ua) { /* all compared equal */ |
370 *lsuq+=1; /* increment result */ | 371 *lsuq+=1; /* increment result */ |
371 msua=lsua; /* collapse acc units */ | 372 msua=lsua; /* collapse acc units */ |
372 *msua=0; /* .. to a zero */ | 373 *msua=0; /* .. to a zero */ |
373 break; | 374 break; |
374 } | 375 } |
411 + *(msua-2)/DIVLO; | 412 + *(msua-2)/DIVLO; |
412 mul/=(*msud*DIVHI + *(msud-1)/DIVLO +1); | 413 mul/=(*msud*DIVHI + *(msud-1)/DIVLO +1); |
413 } | 414 } |
414 else if (divunits==1) { | 415 else if (divunits==1) { |
415 mul=(uLong)*msua * DIVBASE + *(msua-1); | 416 mul=(uLong)*msua * DIVBASE + *(msua-1); |
416 mul/=*msud; /* no more to the right */ | 417 mul/=*msud; /* no more to the right */ |
417 } | 418 } |
418 else { | 419 else { |
419 mul=(uLong)(*msua) * (uInt)(DIVBASE<<2) + (*(msua-1)<<2); | 420 mul=(uLong)(*msua) * (uInt)(DIVBASE<<2) |
421 + (*(msua-1)<<2); | |
420 mul/=divtop; /* [divtop already allows for sticky bits] */ | 422 mul/=divtop; /* [divtop already allows for sticky bits] */ |
421 } | 423 } |
422 multiplier=(Int)mul; | 424 multiplier=(Int)mul; |
423 #else | 425 #else |
424 multiplier=*msua * ((DIVBASE<<2)/divtop); | 426 multiplier=*msua * ((DIVBASE<<2)/divtop); |
533 | 535 |
534 /* Now convert to BCD for rounding and cleanup, starting from the */ | 536 /* Now convert to BCD for rounding and cleanup, starting from the */ |
535 /* most significant end [offset by one into bcdacc to leave room */ | 537 /* most significant end [offset by one into bcdacc to leave room */ |
536 /* for a possible carry digit if rounding for REMNEAR is needed] */ | 538 /* for a possible carry digit if rounding for REMNEAR is needed] */ |
537 for (uq=msuq, ub=bcdacc+1; uq>=lsuq; uq--, ub+=9) { | 539 for (uq=msuq, ub=bcdacc+1; uq>=lsuq; uq--, ub+=9) { |
538 uInt top, mid, rem; /* work */ | 540 uInt top, mid, rem; /* work */ |
539 if (*uq==0) { /* no split needed */ | 541 if (*uq==0) { /* no split needed */ |
540 UINTAT(ub)=0; /* clear 9 BCD8s */ | 542 UBFROMUI(ub, 0); /* clear 9 BCD8s */ |
541 UINTAT(ub+4)=0; /* .. */ | 543 UBFROMUI(ub+4, 0); /* .. */ |
542 *(ub+8)=0; /* .. */ | 544 *(ub+8)=0; /* .. */ |
543 continue; | 545 continue; |
544 } | 546 } |
545 /* *uq is non-zero -- split the base-billion digit into */ | 547 /* *uq is non-zero -- split the base-billion digit into */ |
546 /* hi, mid, and low three-digits */ | 548 /* hi, mid, and low three-digits */ |
551 top=*uq/divsplit9; | 553 top=*uq/divsplit9; |
552 rem=*uq%divsplit9; | 554 rem=*uq%divsplit9; |
553 mid=rem/divsplit6; | 555 mid=rem/divsplit6; |
554 rem=rem%divsplit6; | 556 rem=rem%divsplit6; |
555 /* lay out the nine BCD digits (plus one unwanted byte) */ | 557 /* lay out the nine BCD digits (plus one unwanted byte) */ |
556 UINTAT(ub) =UINTAT(&BIN2BCD8[top*4]); | 558 UBFROMUI(ub, UBTOUI(&BIN2BCD8[top*4])); |
557 UINTAT(ub+3)=UINTAT(&BIN2BCD8[mid*4]); | 559 UBFROMUI(ub+3, UBTOUI(&BIN2BCD8[mid*4])); |
558 UINTAT(ub+6)=UINTAT(&BIN2BCD8[rem*4]); | 560 UBFROMUI(ub+6, UBTOUI(&BIN2BCD8[rem*4])); |
559 } /* BCD conversion loop */ | 561 } /* BCD conversion loop */ |
560 ub--; /* -> lsu */ | 562 ub--; /* -> lsu */ |
561 | 563 |
562 /* complete the bcdnum; quodigits is correct, so the position of */ | 564 /* complete the bcdnum; quodigits is correct, so the position of */ |
563 /* the first non-zero is known */ | 565 /* the first non-zero is known */ |
564 num.msd=bcdacc+1+(msuq-lsuq+1)*9-quodigits; | 566 num.msd=bcdacc+1+(msuq-lsuq+1)*9-quodigits; |
565 num.lsd=ub; | 567 num.lsd=ub; |
635 if (roundat>num.msd) num.lsd=roundat-1; | 637 if (roundat>num.msd) num.lsd=roundat-1; |
636 else { | 638 else { |
637 num.msd--; /* use the 0 .. */ | 639 num.msd--; /* use the 0 .. */ |
638 num.lsd=num.msd; /* .. at the new MSD place */ | 640 num.lsd=num.msd; /* .. at the new MSD place */ |
639 } | 641 } |
640 if (reround!=0) { /* discarding non-zero */ | 642 if (reround!=0) { /* discarding non-zero */ |
641 uInt bump=0; | 643 uInt bump=0; |
642 /* rounding is DEC_ROUND_HALF_EVEN always */ | 644 /* rounding is DEC_ROUND_HALF_EVEN always */ |
643 if (reround>5) bump=1; /* >0.5 goes up */ | 645 if (reround>5) bump=1; /* >0.5 goes up */ |
644 else if (reround==5) /* exactly 0.5000 .. */ | 646 else if (reround==5) /* exactly 0.5000 .. */ |
645 bump=*(num.lsd) & 0x01; /* .. up iff [new] lsd is odd */ | 647 bump=*(num.lsd) & 0x01; /* .. up iff [new] lsd is odd */ |
646 if (bump!=0) { /* need increment */ | 648 if (bump!=0) { /* need increment */ |
647 /* increment the coefficient; this might end up with 1000... */ | 649 /* increment the coefficient; this might end up with 1000... */ |
648 ub=num.lsd; | 650 ub=num.lsd; |
649 for (; UINTAT(ub-3)==0x09090909; ub-=4) UINTAT(ub-3)=0; | 651 for (; UBTOUI(ub-3)==0x09090909; ub-=4) UBFROMUI(ub-3, 0); |
650 for (; *ub==9; ub--) *ub=0; /* at most 3 more */ | 652 for (; *ub==9; ub--) *ub=0; /* at most 3 more */ |
651 *ub+=1; | 653 *ub+=1; |
652 if (ub<num.msd) num.msd--; /* carried */ | 654 if (ub<num.msd) num.msd--; /* carried */ |
653 } /* bump needed */ | 655 } /* bump needed */ |
654 } /* reround!=0 */ | 656 } /* reround!=0 */ |
673 /* ------------------------------------------------------------------ */ | 675 /* ------------------------------------------------------------------ */ |
674 /* decFiniteMultiply -- multiply two finite decFloats */ | 676 /* decFiniteMultiply -- multiply two finite decFloats */ |
675 /* */ | 677 /* */ |
676 /* num gets the result of multiplying dfl and dfr */ | 678 /* num gets the result of multiplying dfl and dfr */ |
677 /* bcdacc .. with the coefficient in this array */ | 679 /* bcdacc .. with the coefficient in this array */ |
678 /* dfl is the first decFloat (lhs) */ | 680 /* dfl is the first decFloat (lhs) */ |
679 /* dfr is the second decFloat (rhs) */ | 681 /* dfr is the second decFloat (rhs) */ |
680 /* */ | 682 /* */ |
681 /* This effects the multiplication of two decFloats, both known to be */ | 683 /* This effects the multiplication of two decFloats, both known to be */ |
682 /* finite, leaving the result in a bcdnum ready for decFinalize (for */ | 684 /* finite, leaving the result in a bcdnum ready for decFinalize (for */ |
683 /* use in Multiply) or in a following addition (FMA). */ | 685 /* use in Multiply) or in a following addition (FMA). */ |
688 /* This routine has two separate implementations of the core */ | 690 /* This routine has two separate implementations of the core */ |
689 /* multiplication; both using base-billion. One uses only 32-bit */ | 691 /* multiplication; both using base-billion. One uses only 32-bit */ |
690 /* variables (Ints and uInts) or smaller; the other uses uLongs (for */ | 692 /* variables (Ints and uInts) or smaller; the other uses uLongs (for */ |
691 /* multiplication and addition only). Both implementations cover */ | 693 /* multiplication and addition only). Both implementations cover */ |
692 /* both arithmetic sizes (DOUBLE and QUAD) in order to allow timing */ | 694 /* both arithmetic sizes (DOUBLE and QUAD) in order to allow timing */ |
693 /* comparisons. In any one compilation only one implementation for */ | 695 /* comparisons. In any one compilation only one implementation for */ |
694 /* each size can be used, and if DECUSE64 is 0 then use of the 32-bit */ | 696 /* each size can be used, and if DECUSE64 is 0 then use of the 32-bit */ |
695 /* version is forced. */ | 697 /* version is forced. */ |
696 /* */ | 698 /* */ |
697 /* Historical note: an earlier version of this code also supported the */ | 699 /* Historical note: an earlier version of this code also supported the */ |
698 /* 256-bit format and has been preserved. That is somewhat trickier */ | 700 /* 256-bit format and has been preserved. That is somewhat trickier */ |
699 /* during lazy carry splitting because the initial quotient estimate */ | 701 /* during lazy carry splitting because the initial quotient estimate */ |
700 /* (est) can exceed 32 bits. */ | 702 /* (est) can exceed 32 bits. */ |
701 | 703 |
702 #define MULTBASE BILLION /* the base used for multiply */ | 704 #define MULTBASE ((uInt)BILLION) /* the base used for multiply */ |
703 #define MULOPLEN DECPMAX9 /* operand length ('digits' base 10**9) */ | 705 #define MULOPLEN DECPMAX9 /* operand length ('digits' base 10**9) */ |
704 #define MULACCLEN (MULOPLEN*2) /* accumulator length (ditto) */ | 706 #define MULACCLEN (MULOPLEN*2) /* accumulator length (ditto) */ |
705 #define LEADZEROS (MULACCLEN*9 - DECPMAX*2) /* leading zeros always */ | 707 #define LEADZEROS (MULACCLEN*9 - DECPMAX*2) /* leading zeros always */ |
706 | 708 |
707 /* Assertions: exponent not too large and MULACCLEN is a multiple of 4 */ | 709 /* Assertions: exponent not too large and MULACCLEN is a multiple of 4 */ |
716 static void decFiniteMultiply(bcdnum *num, uByte *bcdacc, | 718 static void decFiniteMultiply(bcdnum *num, uByte *bcdacc, |
717 const decFloat *dfl, const decFloat *dfr) { | 719 const decFloat *dfl, const decFloat *dfr) { |
718 uInt bufl[MULOPLEN]; /* left coefficient (base-billion) */ | 720 uInt bufl[MULOPLEN]; /* left coefficient (base-billion) */ |
719 uInt bufr[MULOPLEN]; /* right coefficient (base-billion) */ | 721 uInt bufr[MULOPLEN]; /* right coefficient (base-billion) */ |
720 uInt *ui, *uj; /* work */ | 722 uInt *ui, *uj; /* work */ |
721 uByte *ub; /* .. */ | 723 uByte *ub; /* .. */ |
724 uInt uiwork; /* for macros */ | |
722 | 725 |
723 #if DECUSE64 | 726 #if DECUSE64 |
724 uLong accl[MULACCLEN]; /* lazy accumulator (base-billion+) */ | 727 uLong accl[MULACCLEN]; /* lazy accumulator (base-billion+) */ |
725 uLong *pl; /* work -> lazy accumulator */ | 728 uLong *pl; /* work -> lazy accumulator */ |
726 uInt acc[MULACCLEN]; /* coefficent in base-billion .. */ | 729 uInt acc[MULACCLEN]; /* coefficent in base-billion .. */ |
727 #else | 730 #else |
728 uInt acc[MULACCLEN*2]; /* accumulator in base-billion .. */ | 731 uInt acc[MULACCLEN*2]; /* accumulator in base-billion .. */ |
729 #endif | 732 #endif |
730 uInt *pa; /* work -> accumulator */ | 733 uInt *pa; /* work -> accumulator */ |
753 #if DECUSE64 | 756 #if DECUSE64 |
754 | 757 |
755 /* zero the accumulator */ | 758 /* zero the accumulator */ |
756 #if MULACCLEN==4 | 759 #if MULACCLEN==4 |
757 accl[0]=0; accl[1]=0; accl[2]=0; accl[3]=0; | 760 accl[0]=0; accl[1]=0; accl[2]=0; accl[3]=0; |
758 #else /* use a loop */ | 761 #else /* use a loop */ |
759 /* MULACCLEN is a multiple of four, asserted above */ | 762 /* MULACCLEN is a multiple of four, asserted above */ |
760 for (pl=accl; pl<accl+MULACCLEN; pl+=4) { | 763 for (pl=accl; pl<accl+MULACCLEN; pl+=4) { |
761 *pl=0; *(pl+1)=0; *(pl+2)=0; *(pl+3)=0;/* [reduce overhead] */ | 764 *pl=0; *(pl+1)=0; *(pl+2)=0; *(pl+3)=0;/* [reduce overhead] */ |
762 } /* pl */ | 765 } /* pl */ |
763 #endif | 766 #endif |
805 /* just mentioned while minimizing the maximum error (and hence the */ | 808 /* just mentioned while minimizing the maximum error (and hence the */ |
806 /* maximum correction), as shown in the following table: */ | 809 /* maximum correction), as shown in the following table: */ |
807 /* */ | 810 /* */ |
808 /* Type OPLEN A B maxX maxError maxCorrection */ | 811 /* Type OPLEN A B maxX maxError maxCorrection */ |
809 /* --------------------------------------------------------- */ | 812 /* --------------------------------------------------------- */ |
810 /* DOUBLE 2 29 32 <2*10**18 0.63 1 */ | 813 /* DOUBLE 2 29 32 <2*10**18 0.63 1 */ |
811 /* QUAD 4 30 31 <4*10**18 1.17 2 */ | 814 /* QUAD 4 30 31 <4*10**18 1.17 2 */ |
812 /* */ | 815 /* */ |
813 /* In the OPLEN==2 case there is most choice, but the value for B */ | 816 /* In the OPLEN==2 case there is most choice, but the value for B */ |
814 /* of 32 has a big advantage as then the calculation of the */ | 817 /* of 32 has a big advantage as then the calculation of the */ |
815 /* estimate requires no shifting; the compiler can extract the high */ | 818 /* estimate requires no shifting; the compiler can extract the high */ |
816 /* word directly after multiplying magic*hop. */ | 819 /* word directly after multiplying magic*hop. */ |
833 #endif | 836 #endif |
834 | 837 |
835 for (pl=accl, pa=acc; pl<accl+MULACCLEN; pl++, pa++) { /* each column position */ | 838 for (pl=accl, pa=acc; pl<accl+MULACCLEN; pl++, pa++) { /* each column position */ |
836 uInt lo, hop; /* work */ | 839 uInt lo, hop; /* work */ |
837 uInt est; /* cannot exceed 4E+9 */ | 840 uInt est; /* cannot exceed 4E+9 */ |
838 if (*pl>MULTBASE) { | 841 if (*pl>=MULTBASE) { |
839 /* *pl holds a binary number which needs to be split */ | 842 /* *pl holds a binary number which needs to be split */ |
840 hop=(uInt)(*pl>>MULSHIFTA); | 843 hop=(uInt)(*pl>>MULSHIFTA); |
841 est=(uInt)(((uLong)hop*MULMAGIC)>>MULSHIFTB); | 844 est=(uInt)(((uLong)hop*MULMAGIC)>>MULSHIFTB); |
842 /* the estimate is now in est; now calculate hi:lo-est*10**9; */ | 845 /* the estimate is now in est; now calculate hi:lo-est*10**9; */ |
843 /* happily the top word of the result is irrelevant because it */ | 846 /* happily the top word of the result is irrelevant because it */ |
898 | 901 |
899 /* The 64-bit carries must now be resolved; this means that a */ | 902 /* The 64-bit carries must now be resolved; this means that a */ |
900 /* quotient/remainder has to be calculated for base-billion (1E+9). */ | 903 /* quotient/remainder has to be calculated for base-billion (1E+9). */ |
901 /* For this, Clark & Cowlishaw's quotient estimation approach (also */ | 904 /* For this, Clark & Cowlishaw's quotient estimation approach (also */ |
902 /* used in decNumber) is needed, because 64-bit divide is generally */ | 905 /* used in decNumber) is needed, because 64-bit divide is generally */ |
903 /* extremely slow on 32-bit machines. This algorithm splits X */ | 906 /* extremely slow on 32-bit machines. This algorithm splits X */ |
904 /* using: */ | 907 /* using: */ |
905 /* */ | 908 /* */ |
906 /* magic=2**(A+B)/1E+9; // 'magic number' */ | 909 /* magic=2**(A+B)/1E+9; // 'magic number' */ |
907 /* hop=X/2**A; // high order part of X (by shift) */ | 910 /* hop=X/2**A; // high order part of X (by shift) */ |
908 /* est=magic*hop/2**B // quotient estimate (may be low by 1) */ | 911 /* est=magic*hop/2**B // quotient estimate (may be low by 1) */ |
920 /* just mentioned while minimizing the maximum error (and hence the */ | 923 /* just mentioned while minimizing the maximum error (and hence the */ |
921 /* maximum correction), as shown in the following table: */ | 924 /* maximum correction), as shown in the following table: */ |
922 /* */ | 925 /* */ |
923 /* Type OPLEN A B maxX maxError maxCorrection */ | 926 /* Type OPLEN A B maxX maxError maxCorrection */ |
924 /* --------------------------------------------------------- */ | 927 /* --------------------------------------------------------- */ |
925 /* DOUBLE 2 29 32 <2*10**18 0.63 1 */ | 928 /* DOUBLE 2 29 32 <2*10**18 0.63 1 */ |
926 /* QUAD 4 30 31 <4*10**18 1.17 2 */ | 929 /* QUAD 4 30 31 <4*10**18 1.17 2 */ |
927 /* */ | 930 /* */ |
928 /* In the OPLEN==2 case there is most choice, but the value for B */ | 931 /* In the OPLEN==2 case there is most choice, but the value for B */ |
929 /* of 32 has a big advantage as then the calculation of the */ | 932 /* of 32 has a big advantage as then the calculation of the */ |
930 /* estimate requires no shifting; the high word is simply */ | 933 /* estimate requires no shifting; the high word is simply */ |
931 /* calculated from multiplying magic*hop. */ | 934 /* calculated from multiplying magic*hop. */ |
945 for (pa=acc+MULACCLEN-1; pa>=acc; pa--) | 948 for (pa=acc+MULACCLEN-1; pa>=acc; pa--) |
946 printf(" %08lx:%08lx", (LI)*(pa+MULACCLEN), (LI)*pa); | 949 printf(" %08lx:%08lx", (LI)*(pa+MULACCLEN), (LI)*pa); |
947 printf("\n"); | 950 printf("\n"); |
948 #endif | 951 #endif |
949 | 952 |
950 for (pa=acc;; pa++) { /* each low uInt */ | 953 for (pa=acc;; pa++) { /* each low uInt */ |
951 uInt hi, lo; /* words of exact multiply result */ | 954 uInt hi, lo; /* words of exact multiply result */ |
952 uInt hop, estlo; /* work */ | 955 uInt hop, estlo; /* work */ |
953 #if QUAD | 956 #if QUAD |
954 uInt esthi; /* .. */ | 957 uInt esthi; /* .. */ |
955 #endif | 958 #endif |
956 | 959 |
957 lo=*pa; | 960 lo=*pa; |
958 hi=*(pa+MULACCLEN); /* top 32 bits */ | 961 hi=*(pa+MULACCLEN); /* top 32 bits */ |
959 /* hi and lo now hold a binary number which needs to be split */ | 962 /* hi and lo now hold a binary number which needs to be split */ |
960 | 963 |
961 #if DOUBLE | 964 #if DOUBLE |
962 hop=(hi<<3)+(lo>>MULSHIFTA); /* hi:lo/2**29 */ | 965 hop=(hi<<3)+(lo>>MULSHIFTA); /* hi:lo/2**29 */ |
963 LONGMUL32HI(estlo, hop, MULMAGIC);/* only need the high word */ | 966 LONGMUL32HI(estlo, hop, MULMAGIC);/* only need the high word */ |
1025 for (ub=bcdacc;; pa--, ub+=9) { | 1028 for (ub=bcdacc;; pa--, ub+=9) { |
1026 if (*pa!=0) { /* split(s) needed */ | 1029 if (*pa!=0) { /* split(s) needed */ |
1027 uInt top, mid, rem; /* work */ | 1030 uInt top, mid, rem; /* work */ |
1028 /* *pa is non-zero -- split the base-billion acc digit into */ | 1031 /* *pa is non-zero -- split the base-billion acc digit into */ |
1029 /* hi, mid, and low three-digits */ | 1032 /* hi, mid, and low three-digits */ |
1030 #define mulsplit9 1000000 /* divisor */ | 1033 #define mulsplit9 1000000 /* divisor */ |
1031 #define mulsplit6 1000 /* divisor */ | 1034 #define mulsplit6 1000 /* divisor */ |
1032 /* The splitting is done by simple divides and remainders, */ | 1035 /* The splitting is done by simple divides and remainders, */ |
1033 /* assuming the compiler will optimize these where useful */ | 1036 /* assuming the compiler will optimize these where useful */ |
1034 /* [GCC does] */ | 1037 /* [GCC does] */ |
1035 top=*pa/mulsplit9; | 1038 top=*pa/mulsplit9; |
1036 rem=*pa%mulsplit9; | 1039 rem=*pa%mulsplit9; |
1037 mid=rem/mulsplit6; | 1040 mid=rem/mulsplit6; |
1038 rem=rem%mulsplit6; | 1041 rem=rem%mulsplit6; |
1039 /* lay out the nine BCD digits (plus one unwanted byte) */ | 1042 /* lay out the nine BCD digits (plus one unwanted byte) */ |
1040 UINTAT(ub) =UINTAT(&BIN2BCD8[top*4]); | 1043 UBFROMUI(ub, UBTOUI(&BIN2BCD8[top*4])); |
1041 UINTAT(ub+3)=UINTAT(&BIN2BCD8[mid*4]); | 1044 UBFROMUI(ub+3, UBTOUI(&BIN2BCD8[mid*4])); |
1042 UINTAT(ub+6)=UINTAT(&BIN2BCD8[rem*4]); | 1045 UBFROMUI(ub+6, UBTOUI(&BIN2BCD8[rem*4])); |
1043 } | 1046 } |
1044 else { /* *pa==0 */ | 1047 else { /* *pa==0 */ |
1045 UINTAT(ub)=0; /* clear 9 BCD8s */ | 1048 UBFROMUI(ub, 0); /* clear 9 BCD8s */ |
1046 UINTAT(ub+4)=0; /* .. */ | 1049 UBFROMUI(ub+4, 0); /* .. */ |
1047 *(ub+8)=0; /* .. */ | 1050 *(ub+8)=0; /* .. */ |
1048 } | 1051 } |
1049 if (pa==acc) break; | 1052 if (pa==acc) break; |
1050 } /* BCD conversion loop */ | 1053 } /* BCD conversion loop */ |
1051 | 1054 |
1061 | 1064 |
1062 /* ------------------------------------------------------------------ */ | 1065 /* ------------------------------------------------------------------ */ |
1063 /* decFloatAbs -- absolute value, heeding NaNs, etc. */ | 1066 /* decFloatAbs -- absolute value, heeding NaNs, etc. */ |
1064 /* */ | 1067 /* */ |
1065 /* result gets the canonicalized df with sign 0 */ | 1068 /* result gets the canonicalized df with sign 0 */ |
1066 /* df is the decFloat to abs */ | 1069 /* df is the decFloat to abs */ |
1067 /* set is the context */ | 1070 /* set is the context */ |
1068 /* returns result */ | 1071 /* returns result */ |
1069 /* */ | 1072 /* */ |
1070 /* This has the same effect as decFloatPlus unless df is negative, */ | 1073 /* This has the same effect as decFloatPlus unless df is negative, */ |
1071 /* in which case it has the same effect as decFloatMinus. The */ | 1074 /* in which case it has the same effect as decFloatMinus. The */ |
1083 | 1086 |
1084 /* ------------------------------------------------------------------ */ | 1087 /* ------------------------------------------------------------------ */ |
1085 /* decFloatAdd -- add two decFloats */ | 1088 /* decFloatAdd -- add two decFloats */ |
1086 /* */ | 1089 /* */ |
1087 /* result gets the result of adding dfl and dfr: */ | 1090 /* result gets the result of adding dfl and dfr: */ |
1088 /* dfl is the first decFloat (lhs) */ | 1091 /* dfl is the first decFloat (lhs) */ |
1089 /* dfr is the second decFloat (rhs) */ | 1092 /* dfr is the second decFloat (rhs) */ |
1090 /* set is the context */ | 1093 /* set is the context */ |
1091 /* returns result */ | 1094 /* returns result */ |
1092 /* */ | 1095 /* */ |
1093 /* ------------------------------------------------------------------ */ | 1096 /* ------------------------------------------------------------------ */ |
1097 #if QUAD | |
1098 /* Table for testing MSDs for fastpath elimination; returns the MSD of */ | |
1099 /* a decDouble or decQuad (top 6 bits tested) ignoring the sign. */ | |
1100 /* Infinities return -32 and NaNs return -128 so that summing the two */ | |
1101 /* MSDs also allows rapid tests for the Specials (see code below). */ | |
1102 const Int DECTESTMSD[64]={ | |
1103 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, | |
1104 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 9, 8, 9, -32, -128, | |
1105 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, | |
1106 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 9, 8, 9, -32, -128}; | |
1107 #else | |
1108 /* The table for testing MSDs is shared between the modules */ | |
1109 extern const Int DECTESTMSD[64]; | |
1110 #endif | |
1111 | |
1094 decFloat * decFloatAdd(decFloat *result, | 1112 decFloat * decFloatAdd(decFloat *result, |
1095 const decFloat *dfl, const decFloat *dfr, | 1113 const decFloat *dfl, const decFloat *dfr, |
1096 decContext *set) { | 1114 decContext *set) { |
1097 bcdnum num; /* for final conversion */ | 1115 bcdnum num; /* for final conversion */ |
1098 Int expl, expr; /* left and right exponents */ | 1116 Int bexpl, bexpr; /* left and right biased exponents */ |
1099 uInt *ui, *uj; /* work */ | 1117 uByte *ub, *us, *ut; /* work */ |
1100 uByte *ub; /* .. */ | 1118 uInt uiwork; /* for macros */ |
1119 #if QUAD | |
1120 uShort uswork; /* .. */ | |
1121 #endif | |
1101 | 1122 |
1102 uInt sourhil, sourhir; /* top words from source decFloats */ | 1123 uInt sourhil, sourhir; /* top words from source decFloats */ |
1103 /* [valid only until specials */ | 1124 /* [valid only through end of */ |
1104 /* handled or exponents decoded] */ | 1125 /* fastpath code -- before swap] */ |
1105 uInt diffsign; /* non-zero if signs differ */ | 1126 uInt diffsign; /* non-zero if signs differ */ |
1106 uInt carry; /* carry: 0 or 1 before add loop */ | 1127 uInt carry; /* carry: 0 or 1 before add loop */ |
1107 Int overlap; /* coefficient overlap (if full) */ | 1128 Int overlap; /* coefficient overlap (if full) */ |
1129 Int summ; /* sum of the MSDs */ | |
1108 /* the following buffers hold coefficients with various alignments */ | 1130 /* the following buffers hold coefficients with various alignments */ |
1109 /* (see commentary and diagrams below) */ | 1131 /* (see commentary and diagrams below) */ |
1110 uByte acc[4+2+DECPMAX*3+8]; | 1132 uByte acc[4+2+DECPMAX*3+8]; |
1111 uByte buf[4+2+DECPMAX*2]; | 1133 uByte buf[4+2+DECPMAX*2]; |
1112 uByte *umsd, *ulsd; /* local MSD and LSD pointers */ | 1134 uByte *umsd, *ulsd; /* local MSD and LSD pointers */ |
1113 | 1135 |
1114 #if DECLITEND | 1136 #if DECLITEND |
1115 #define CARRYPAT 0x01000000 /* carry=1 pattern */ | 1137 #define CARRYPAT 0x01000000 /* carry=1 pattern */ |
1116 #else | 1138 #else |
1117 #define CARRYPAT 0x00000001 /* carry=1 pattern */ | 1139 #define CARRYPAT 0x00000001 /* carry=1 pattern */ |
1118 #endif | 1140 #endif |
1119 | 1141 |
1120 /* Start decoding the arguments */ | 1142 /* Start decoding the arguments */ |
1121 /* the initial exponents are placed into the opposite Ints to */ | 1143 /* The initial exponents are placed into the opposite Ints to */ |
1122 /* that which might be expected; there are two sets of data to */ | 1144 /* that which might be expected; there are two sets of data to */ |
1123 /* keep track of (each decFloat and the corresponding exponent), */ | 1145 /* keep track of (each decFloat and the corresponding exponent), */ |
1124 /* and this scheme means that at the swap point (after comparing */ | 1146 /* and this scheme means that at the swap point (after comparing */ |
1125 /* exponents) only one pair of words needs to be swapped */ | 1147 /* exponents) only one pair of words needs to be swapped */ |
1126 /* whichever path is taken (thereby minimising worst-case path) */ | 1148 /* whichever path is taken (thereby minimising worst-case path). */ |
1149 /* The calculated exponents will be nonsense when the arguments are */ | |
1150 /* Special, but are not used in that path */ | |
1127 sourhil=DFWORD(dfl, 0); /* LHS top word */ | 1151 sourhil=DFWORD(dfl, 0); /* LHS top word */ |
1128 expr=DECCOMBEXP[sourhil>>26]; /* get exponent high bits (in place) */ | 1152 summ=DECTESTMSD[sourhil>>26]; /* get first MSD for testing */ |
1153 bexpr=DECCOMBEXP[sourhil>>26]; /* get exponent high bits (in place) */ | |
1154 bexpr+=GETECON(dfl); /* .. + continuation */ | |
1155 | |
1129 sourhir=DFWORD(dfr, 0); /* RHS top word */ | 1156 sourhir=DFWORD(dfr, 0); /* RHS top word */ |
1130 expl=DECCOMBEXP[sourhir>>26]; | 1157 summ+=DECTESTMSD[sourhir>>26]; /* sum MSDs for testing */ |
1158 bexpl=DECCOMBEXP[sourhir>>26]; | |
1159 bexpl+=GETECON(dfr); | |
1160 | |
1161 /* here bexpr has biased exponent from lhs, and vice versa */ | |
1131 | 1162 |
1132 diffsign=(sourhil^sourhir)&DECFLOAT_Sign; | 1163 diffsign=(sourhil^sourhir)&DECFLOAT_Sign; |
1133 | 1164 |
1134 if (EXPISSPECIAL(expl | expr)) { /* either is special? */ | 1165 /* now determine whether to take a fast path or the full-function */ |
1135 if (DFISNAN(dfl) || DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set); | 1166 /* slow path. The slow path must be taken when: */ |
1136 /* one or two infinities */ | 1167 /* -- both numbers are finite, and: */ |
1137 /* two infinities with different signs is invalid */ | 1168 /* the exponents are different, or */ |
1138 if (diffsign && DFISINF(dfl) && DFISINF(dfr)) | 1169 /* the signs are different, or */ |
1139 return decInvalid(result, set); | 1170 /* the sum of the MSDs is >8 (hence might overflow) */ |
1140 if (DFISINF(dfl)) return decInfinity(result, dfl); /* LHS is infinite */ | 1171 /* specialness and the sum of the MSDs can be tested at once using */ |
1141 return decInfinity(result, dfr); /* RHS must be Infinite */ | 1172 /* the summ value just calculated, so the test for specials is no */ |
1142 } | 1173 /* longer on the worst-case path (as of 3.60) */ |
1143 | 1174 |
1144 /* Here when both arguments are finite */ | 1175 if (summ<=8) { /* MSD+MSD is good, or there is a special */ |
1145 | 1176 if (summ<0) { /* there is a special */ |
1146 /* complete exponent gathering (keeping swapped) */ | 1177 /* Inf+Inf would give -64; Inf+finite is -32 or higher */ |
1147 expr+=GETECON(dfl)-DECBIAS; /* .. + continuation and unbias */ | 1178 if (summ<-64) return decNaNs(result, dfl, dfr, set); /* one or two NaNs */ |
1148 expl+=GETECON(dfr)-DECBIAS; | 1179 /* two infinities with different signs is invalid */ |
1149 /* here expr has exponent from lhs, and vice versa */ | 1180 if (summ==-64 && diffsign) return decInvalid(result, set); |
1181 if (DFISINF(dfl)) return decInfinity(result, dfl); /* LHS is infinite */ | |
1182 return decInfinity(result, dfr); /* RHS must be Inf */ | |
1183 } | |
1184 /* Here when both arguments are finite; fast path is possible */ | |
1185 /* (currently only for aligned and same-sign) */ | |
1186 if (bexpr==bexpl && !diffsign) { | |
1187 uInt tac[DECLETS+1]; /* base-1000 coefficient */ | |
1188 uInt encode; /* work */ | |
1189 | |
1190 /* Get one coefficient as base-1000 and add the other */ | |
1191 GETCOEFFTHOU(dfl, tac); /* least-significant goes to [0] */ | |
1192 ADDCOEFFTHOU(dfr, tac); | |
1193 /* here the sum of the MSDs (plus any carry) will be <10 due to */ | |
1194 /* the fastpath test earlier */ | |
1195 | |
1196 /* construct the result; low word is the same for both formats */ | |
1197 encode =BIN2DPD[tac[0]]; | |
1198 encode|=BIN2DPD[tac[1]]<<10; | |
1199 encode|=BIN2DPD[tac[2]]<<20; | |
1200 encode|=BIN2DPD[tac[3]]<<30; | |
1201 DFWORD(result, (DECBYTES/4)-1)=encode; | |
1202 | |
1203 /* collect next two declets (all that remains, for Double) */ | |
1204 encode =BIN2DPD[tac[3]]>>2; | |
1205 encode|=BIN2DPD[tac[4]]<<8; | |
1206 | |
1207 #if QUAD | |
1208 /* complete and lay out middling words */ | |
1209 encode|=BIN2DPD[tac[5]]<<18; | |
1210 encode|=BIN2DPD[tac[6]]<<28; | |
1211 DFWORD(result, 2)=encode; | |
1212 | |
1213 encode =BIN2DPD[tac[6]]>>4; | |
1214 encode|=BIN2DPD[tac[7]]<<6; | |
1215 encode|=BIN2DPD[tac[8]]<<16; | |
1216 encode|=BIN2DPD[tac[9]]<<26; | |
1217 DFWORD(result, 1)=encode; | |
1218 | |
1219 /* and final two declets */ | |
1220 encode =BIN2DPD[tac[9]]>>6; | |
1221 encode|=BIN2DPD[tac[10]]<<4; | |
1222 #endif | |
1223 | |
1224 /* add exponent continuation and sign (from either argument) */ | |
1225 encode|=sourhil & (ECONMASK | DECFLOAT_Sign); | |
1226 | |
1227 /* create lookup index = MSD + top two bits of biased exponent <<4 */ | |
1228 tac[DECLETS]|=(bexpl>>DECECONL)<<4; | |
1229 encode|=DECCOMBFROM[tac[DECLETS]]; /* add constructed combination field */ | |
1230 DFWORD(result, 0)=encode; /* complete */ | |
1231 | |
1232 /* decFloatShow(result, ">"); */ | |
1233 return result; | |
1234 } /* fast path OK */ | |
1235 /* drop through to slow path */ | |
1236 } /* low sum or Special(s) */ | |
1237 | |
1238 /* Slow path required -- arguments are finite and might overflow, */ | |
1239 /* or require alignment, or might have different signs */ | |
1150 | 1240 |
1151 /* now swap either exponents or argument pointers */ | 1241 /* now swap either exponents or argument pointers */ |
1152 if (expl<=expr) { | 1242 if (bexpl<=bexpr) { |
1153 /* original left is bigger */ | 1243 /* original left is bigger */ |
1154 Int expswap=expl; | 1244 Int bexpswap=bexpl; |
1155 expl=expr; | 1245 bexpl=bexpr; |
1156 expr=expswap; | 1246 bexpr=bexpswap; |
1157 /* printf("left bigger\n"); */ | 1247 /* printf("left bigger\n"); */ |
1158 } | 1248 } |
1159 else { | 1249 else { |
1160 const decFloat *dfswap=dfl; | 1250 const decFloat *dfswap=dfl; |
1161 dfl=dfr; | 1251 dfl=dfr; |
1162 dfr=dfswap; | 1252 dfr=dfswap; |
1163 /* printf("right bigger\n"); */ | 1253 /* printf("right bigger\n"); */ |
1164 } | 1254 } |
1165 /* [here dfl and expl refer to the datum with the larger exponent, */ | 1255 /* [here dfl and bexpl refer to the datum with the larger exponent, */ |
1166 /* of if the exponents are equal then the original LHS argument] */ | 1256 /* of if the exponents are equal then the original LHS argument] */ |
1167 | 1257 |
1168 /* if lhs is zero then result will be the rhs (now known to have */ | 1258 /* if lhs is zero then result will be the rhs (now known to have */ |
1169 /* the smaller exponent), which also may need to be tested for zero */ | 1259 /* the smaller exponent), which also may need to be tested for zero */ |
1170 /* for the weird IEEE 754 sign rules */ | 1260 /* for the weird IEEE 754 sign rules */ |
1202 /* aligned; the top word gap is there only in case a carry digit */ | 1292 /* aligned; the top word gap is there only in case a carry digit */ |
1203 /* is prefixed after the add -- it does not need to be zeroed */ | 1293 /* is prefixed after the add -- it does not need to be zeroed */ |
1204 #if DOUBLE | 1294 #if DOUBLE |
1205 #define COFF 4 /* offset into acc */ | 1295 #define COFF 4 /* offset into acc */ |
1206 #elif QUAD | 1296 #elif QUAD |
1207 USHORTAT(acc+4)=0; /* prefix 00 */ | 1297 UBFROMUS(acc+4, 0); /* prefix 00 */ |
1208 #define COFF 6 /* offset into acc */ | 1298 #define COFF 6 /* offset into acc */ |
1209 #endif | 1299 #endif |
1210 | 1300 |
1211 GETCOEFF(dfl, acc+COFF); /* decode from decFloat */ | 1301 GETCOEFF(dfl, acc+COFF); /* decode from decFloat */ |
1212 ulsd=acc+COFF+DECPMAX-1; | 1302 ulsd=acc+COFF+DECPMAX-1; |
1213 umsd=acc+4; /* [having this here avoids */ | 1303 umsd=acc+4; /* [having this here avoids */ |
1214 /* weird GCC optimizer failure] */ | 1304 |
1215 #if DECTRACE | 1305 #if DECTRACE |
1216 {bcdnum tum; | 1306 {bcdnum tum; |
1217 tum.msd=umsd; | 1307 tum.msd=umsd; |
1218 tum.lsd=ulsd; | 1308 tum.lsd=ulsd; |
1219 tum.exponent=expl; | 1309 tum.exponent=bexpl-DECBIAS; |
1220 tum.sign=DFWORD(dfl, 0) & DECFLOAT_Sign; | 1310 tum.sign=DFWORD(dfl, 0) & DECFLOAT_Sign; |
1221 decShowNum(&tum, "dflx");} | 1311 decShowNum(&tum, "dflx");} |
1222 #endif | 1312 #endif |
1223 | 1313 |
1224 /* if signs differ, take ten's complement of lhs (here the */ | 1314 /* if signs differ, take ten's complement of lhs (here the */ |
1228 /* where the lsd is known to be at a 4-byte boundary (so no borrow */ | 1318 /* where the lsd is known to be at a 4-byte boundary (so no borrow */ |
1229 /* possible) */ | 1319 /* possible) */ |
1230 carry=0; /* assume no carry */ | 1320 carry=0; /* assume no carry */ |
1231 if (diffsign) { | 1321 if (diffsign) { |
1232 carry=CARRYPAT; /* for +1 during add */ | 1322 carry=CARRYPAT; /* for +1 during add */ |
1233 UINTAT(acc+ 4)=0x09090909-UINTAT(acc+ 4); | 1323 UBFROMUI(acc+ 4, 0x09090909-UBTOUI(acc+ 4)); |
1234 UINTAT(acc+ 8)=0x09090909-UINTAT(acc+ 8); | 1324 UBFROMUI(acc+ 8, 0x09090909-UBTOUI(acc+ 8)); |
1235 UINTAT(acc+12)=0x09090909-UINTAT(acc+12); | 1325 UBFROMUI(acc+12, 0x09090909-UBTOUI(acc+12)); |
1236 UINTAT(acc+16)=0x09090909-UINTAT(acc+16); | 1326 UBFROMUI(acc+16, 0x09090909-UBTOUI(acc+16)); |
1237 #if QUAD | 1327 #if QUAD |
1238 UINTAT(acc+20)=0x09090909-UINTAT(acc+20); | 1328 UBFROMUI(acc+20, 0x09090909-UBTOUI(acc+20)); |
1239 UINTAT(acc+24)=0x09090909-UINTAT(acc+24); | 1329 UBFROMUI(acc+24, 0x09090909-UBTOUI(acc+24)); |
1240 UINTAT(acc+28)=0x09090909-UINTAT(acc+28); | 1330 UBFROMUI(acc+28, 0x09090909-UBTOUI(acc+28)); |
1241 UINTAT(acc+32)=0x09090909-UINTAT(acc+32); | 1331 UBFROMUI(acc+32, 0x09090909-UBTOUI(acc+32)); |
1242 UINTAT(acc+36)=0x09090909-UINTAT(acc+36); | 1332 UBFROMUI(acc+36, 0x09090909-UBTOUI(acc+36)); |
1243 #endif | 1333 #endif |
1244 } /* diffsign */ | 1334 } /* diffsign */ |
1245 | 1335 |
1246 /* now process the rhs coefficient; if it cannot overlap lhs then */ | 1336 /* now process the rhs coefficient; if it cannot overlap lhs then */ |
1247 /* it can be put straight into acc (with an appropriate gap, if */ | 1337 /* it can be put straight into acc (with an appropriate gap, if */ |
1248 /* needed) because no actual addition will be needed (except */ | 1338 /* needed) because no actual addition will be needed (except */ |
1249 /* possibly to complete ten's complement) */ | 1339 /* possibly to complete ten's complement) */ |
1250 overlap=DECPMAX-(expl-expr); | 1340 overlap=DECPMAX-(bexpl-bexpr); |
1251 #if DECTRACE | 1341 #if DECTRACE |
1252 printf("exps: %ld %ld\n", (LI)expl, (LI)expr); | 1342 printf("exps: %ld %ld\n", (LI)(bexpl-DECBIAS), (LI)(bexpr-DECBIAS)); |
1253 printf("Overlap=%ld carry=%08lx\n", (LI)overlap, (LI)carry); | 1343 printf("Overlap=%ld carry=%08lx\n", (LI)overlap, (LI)carry); |
1254 #endif | 1344 #endif |
1255 | 1345 |
1256 if (overlap<=0) { /* no overlap possible */ | 1346 if (overlap<=0) { /* no overlap possible */ |
1257 uInt gap; /* local work */ | 1347 uInt gap; /* local work */ |
1267 /* rhs will be simply sticky bits. In this case the gap is */ | 1357 /* rhs will be simply sticky bits. In this case the gap is */ |
1268 /* clamped to DECPMAX and the exponent adjusted to suit [this is */ | 1358 /* clamped to DECPMAX and the exponent adjusted to suit [this is */ |
1269 /* safe because the lhs is non-zero]. */ | 1359 /* safe because the lhs is non-zero]. */ |
1270 gap=-overlap; | 1360 gap=-overlap; |
1271 if (gap>DECPMAX) { | 1361 if (gap>DECPMAX) { |
1272 expr+=gap-1; | 1362 bexpr+=gap-1; |
1273 gap=DECPMAX; | 1363 gap=DECPMAX; |
1274 } | 1364 } |
1275 ub=ulsd+gap+1; /* where MSD will go */ | 1365 ub=ulsd+gap+1; /* where MSD will go */ |
1276 /* Fill the gap with 0s; note that there is no addition to do */ | 1366 /* Fill the gap with 0s; note that there is no addition to do */ |
1277 ui=&UINTAT(acc+COFF+DECPMAX); /* start of gap */ | 1367 ut=acc+COFF+DECPMAX; /* start of gap */ |
1278 for (; ui<&UINTAT(ub); ui++) *ui=0; /* mind the gap */ | 1368 for (; ut<ub; ut+=4) UBFROMUI(ut, 0); /* mind the gap */ |
1279 if (overlap<-DECPMAX) { /* gap was > DECPMAX */ | 1369 if (overlap<-DECPMAX) { /* gap was > DECPMAX */ |
1280 *ub=(uByte)(!DFISZERO(dfr)); /* make sticky digit */ | 1370 *ub=(uByte)(!DFISZERO(dfr)); /* make sticky digit */ |
1281 } | 1371 } |
1282 else { /* need full coefficient */ | 1372 else { /* need full coefficient */ |
1283 GETCOEFF(dfr, ub); /* decode from decFloat */ | 1373 GETCOEFF(dfr, ub); /* decode from decFloat */ |
1287 } /* no overlap possible */ | 1377 } /* no overlap possible */ |
1288 | 1378 |
1289 else { /* overlap>0 */ | 1379 else { /* overlap>0 */ |
1290 /* coefficients overlap (perhaps completely, although also */ | 1380 /* coefficients overlap (perhaps completely, although also */ |
1291 /* perhaps only where zeros) */ | 1381 /* perhaps only where zeros) */ |
1292 ub=buf+COFF+DECPMAX-overlap; /* where MSD will go */ | 1382 if (overlap==DECPMAX) { /* aligned */ |
1293 /* Fill the prefix gap with 0s; 8 will cover most common */ | 1383 ub=buf+COFF; /* where msd will go */ |
1294 /* unalignments, so start with direct assignments (a loop is */ | 1384 #if QUAD |
1295 /* then used for any remaining -- the loop (and the one in a */ | 1385 UBFROMUS(buf+4, 0); /* clear quad's 00 */ |
1296 /* moment) is not then on the critical path because the number */ | 1386 #endif |
1297 /* of additions is reduced by (at least) two in this case) */ | 1387 GETCOEFF(dfr, ub); /* decode from decFloat */ |
1298 UINTAT(buf+4)=0; /* [clears decQuad 00 too] */ | 1388 } |
1299 UINTAT(buf+8)=0; | 1389 else { /* unaligned */ |
1300 if (ub>buf+12) { | 1390 ub=buf+COFF+DECPMAX-overlap; /* where MSD will go */ |
1301 ui=&UINTAT(buf+12); /* start of any remaining */ | 1391 /* Fill the prefix gap with 0s; 8 will cover most common */ |
1302 for (; ui<&UINTAT(ub); ui++) *ui=0; /* fill them */ | 1392 /* unalignments, so start with direct assignments (a loop is */ |
1303 } | 1393 /* then used for any remaining -- the loop (and the one in a */ |
1304 GETCOEFF(dfr, ub); /* decode from decFloat */ | 1394 /* moment) is not then on the critical path because the number */ |
1305 | 1395 /* of additions is reduced by (at least) two in this case) */ |
1306 /* now move tail of rhs across to main acc; again use direct */ | 1396 UBFROMUI(buf+4, 0); /* [clears decQuad 00 too] */ |
1307 /* assignment for 8 digits-worth */ | 1397 UBFROMUI(buf+8, 0); |
1308 UINTAT(acc+COFF+DECPMAX)=UINTAT(buf+COFF+DECPMAX); | 1398 if (ub>buf+12) { |
1309 UINTAT(acc+COFF+DECPMAX+4)=UINTAT(buf+COFF+DECPMAX+4); | 1399 ut=buf+12; /* start any remaining */ |
1310 if (buf+COFF+DECPMAX+8<ub+DECPMAX) { | 1400 for (; ut<ub; ut+=4) UBFROMUI(ut, 0); /* fill them */ |
1311 uj=&UINTAT(buf+COFF+DECPMAX+8); /* source */ | 1401 } |
1312 ui=&UINTAT(acc+COFF+DECPMAX+8); /* target */ | 1402 GETCOEFF(dfr, ub); /* decode from decFloat */ |
1313 for (; uj<&UINTAT(ub+DECPMAX); ui++, uj++) *ui=*uj; | 1403 |
1314 } | 1404 /* now move tail of rhs across to main acc; again use direct */ |
1405 /* copies for 8 digits-worth */ | |
1406 UBFROMUI(acc+COFF+DECPMAX, UBTOUI(buf+COFF+DECPMAX)); | |
1407 UBFROMUI(acc+COFF+DECPMAX+4, UBTOUI(buf+COFF+DECPMAX+4)); | |
1408 if (buf+COFF+DECPMAX+8<ub+DECPMAX) { | |
1409 us=buf+COFF+DECPMAX+8; /* source */ | |
1410 ut=acc+COFF+DECPMAX+8; /* target */ | |
1411 for (; us<ub+DECPMAX; us+=4, ut+=4) UBFROMUI(ut, UBTOUI(us)); | |
1412 } | |
1413 } /* unaligned */ | |
1315 | 1414 |
1316 ulsd=acc+(ub-buf+DECPMAX-1); /* update LSD pointer */ | 1415 ulsd=acc+(ub-buf+DECPMAX-1); /* update LSD pointer */ |
1317 | 1416 |
1318 /* now do the add of the non-tail; this is all nicely aligned, */ | 1417 /* Now do the add of the non-tail; this is all nicely aligned, */ |
1319 /* and is over a multiple of four digits (because for Quad two */ | 1418 /* and is over a multiple of four digits (because for Quad two */ |
1320 /* two 0 digits were added on the left); words in both acc and */ | 1419 /* zero digits were added on the left); words in both acc and */ |
1321 /* buf (buf especially) will often be zero */ | 1420 /* buf (buf especially) will often be zero */ |
1322 /* [byte-by-byte add, here, is about 15% slower than the by-fours] */ | 1421 /* [byte-by-byte add, here, is about 15% slower total effect than */ |
1422 /* the by-fours] */ | |
1323 | 1423 |
1324 /* Now effect the add; this is harder on a little-endian */ | 1424 /* Now effect the add; this is harder on a little-endian */ |
1325 /* machine as the inter-digit carry cannot use the usual BCD */ | 1425 /* machine as the inter-digit carry cannot use the usual BCD */ |
1326 /* addition trick because the bytes are loaded in the wrong order */ | 1426 /* addition trick because the bytes are loaded in the wrong order */ |
1327 /* [this loop could be unrolled, but probably scarcely worth it] */ | 1427 /* [this loop could be unrolled, but probably scarcely worth it] */ |
1328 | 1428 |
1329 ui=&UINTAT(acc+COFF+DECPMAX-4); /* target LSW (acc) */ | 1429 ut=acc+COFF+DECPMAX-4; /* target LSW (acc) */ |
1330 uj=&UINTAT(buf+COFF+DECPMAX-4); /* source LSW (buf, to add to acc) */ | 1430 us=buf+COFF+DECPMAX-4; /* source LSW (buf, to add to acc) */ |
1331 | 1431 |
1332 #if !DECLITEND | 1432 #if !DECLITEND |
1333 for (; ui>=&UINTAT(acc+4); ui--, uj--) { | 1433 for (; ut>=acc+4; ut-=4, us-=4) { /* big-endian add loop */ |
1334 /* bcd8 add */ | 1434 /* bcd8 add */ |
1335 carry+=*uj; /* rhs + carry */ | 1435 carry+=UBTOUI(us); /* rhs + carry */ |
1336 if (carry==0) continue; /* no-op */ | 1436 if (carry==0) continue; /* no-op */ |
1337 carry+=*ui; /* lhs */ | 1437 carry+=UBTOUI(ut); /* lhs */ |
1338 /* Big-endian BCD adjust (uses internal carry) */ | 1438 /* Big-endian BCD adjust (uses internal carry) */ |
1339 carry+=0x76f6f6f6; /* note top nibble not all bits */ | 1439 carry+=0x76f6f6f6; /* note top nibble not all bits */ |
1340 *ui=(carry & 0x0f0f0f0f) - ((carry & 0x60606060)>>4); /* BCD adjust */ | 1440 /* apply BCD adjust and save */ |
1441 UBFROMUI(ut, (carry & 0x0f0f0f0f) - ((carry & 0x60606060)>>4)); | |
1341 carry>>=31; /* true carry was at far left */ | 1442 carry>>=31; /* true carry was at far left */ |
1342 } /* add loop */ | 1443 } /* add loop */ |
1343 #else | 1444 #else |
1344 for (; ui>=&UINTAT(acc+4); ui--, uj--) { | 1445 for (; ut>=acc+4; ut-=4, us-=4) { /* little-endian add loop */ |
1345 /* bcd8 add */ | 1446 /* bcd8 add */ |
1346 carry+=*uj; /* rhs + carry */ | 1447 carry+=UBTOUI(us); /* rhs + carry */ |
1347 if (carry==0) continue; /* no-op [common if unaligned] */ | 1448 if (carry==0) continue; /* no-op [common if unaligned] */ |
1348 carry+=*ui; /* lhs */ | 1449 carry+=UBTOUI(ut); /* lhs */ |
1349 /* Little-endian BCD adjust; inter-digit carry must be manual */ | 1450 /* Little-endian BCD adjust; inter-digit carry must be manual */ |
1350 /* because the lsb from the array will be in the most-significant */ | 1451 /* because the lsb from the array will be in the most-significant */ |
1351 /* byte of carry */ | 1452 /* byte of carry */ |
1352 carry+=0x76767676; /* note no inter-byte carries */ | 1453 carry+=0x76767676; /* note no inter-byte carries */ |
1353 carry+=(carry & 0x80000000)>>15; | 1454 carry+=(carry & 0x80000000)>>15; |
1354 carry+=(carry & 0x00800000)>>15; | 1455 carry+=(carry & 0x00800000)>>15; |
1355 carry+=(carry & 0x00008000)>>15; | 1456 carry+=(carry & 0x00008000)>>15; |
1356 carry-=(carry & 0x60606060)>>4; /* BCD adjust back */ | 1457 carry-=(carry & 0x60606060)>>4; /* BCD adjust back */ |
1357 *ui=carry & 0x0f0f0f0f; /* clear debris and save */ | 1458 UBFROMUI(ut, carry & 0x0f0f0f0f); /* clear debris and save */ |
1358 /* here, final carry-out bit is at 0x00000080; move it ready */ | 1459 /* here, final carry-out bit is at 0x00000080; move it ready */ |
1359 /* for next word-add (i.e., to 0x01000000) */ | 1460 /* for next word-add (i.e., to 0x01000000) */ |
1360 carry=(carry & 0x00000080)<<17; | 1461 carry=(carry & 0x00000080)<<17; |
1361 } /* add loop */ | 1462 } /* add loop */ |
1362 #endif | 1463 #endif |
1464 | |
1363 #if DECTRACE | 1465 #if DECTRACE |
1364 {bcdnum tum; | 1466 {bcdnum tum; |
1365 printf("Add done, carry=%08lx, diffsign=%ld\n", (LI)carry, (LI)diffsign); | 1467 printf("Add done, carry=%08lx, diffsign=%ld\n", (LI)carry, (LI)diffsign); |
1366 tum.msd=umsd; /* acc+4; */ | 1468 tum.msd=umsd; /* acc+4; */ |
1367 tum.lsd=ulsd; | 1469 tum.lsd=ulsd; |
1385 /* if big-endian */ | 1487 /* if big-endian */ |
1386 #if !DECLITEND | 1488 #if !DECLITEND |
1387 *(ulsd+1)=0; | 1489 *(ulsd+1)=0; |
1388 #endif | 1490 #endif |
1389 /* there are always at least four coefficient words */ | 1491 /* there are always at least four coefficient words */ |
1390 UINTAT(umsd) =0x09090909-UINTAT(umsd); | 1492 UBFROMUI(umsd, 0x09090909-UBTOUI(umsd)); |
1391 UINTAT(umsd+4) =0x09090909-UINTAT(umsd+4); | 1493 UBFROMUI(umsd+4, 0x09090909-UBTOUI(umsd+4)); |
1392 UINTAT(umsd+8) =0x09090909-UINTAT(umsd+8); | 1494 UBFROMUI(umsd+8, 0x09090909-UBTOUI(umsd+8)); |
1393 UINTAT(umsd+12)=0x09090909-UINTAT(umsd+12); | 1495 UBFROMUI(umsd+12, 0x09090909-UBTOUI(umsd+12)); |
1394 #if DOUBLE | 1496 #if DOUBLE |
1395 #define BNEXT 16 | 1497 #define BNEXT 16 |
1396 #elif QUAD | 1498 #elif QUAD |
1397 UINTAT(umsd+16)=0x09090909-UINTAT(umsd+16); | 1499 UBFROMUI(umsd+16, 0x09090909-UBTOUI(umsd+16)); |
1398 UINTAT(umsd+20)=0x09090909-UINTAT(umsd+20); | 1500 UBFROMUI(umsd+20, 0x09090909-UBTOUI(umsd+20)); |
1399 UINTAT(umsd+24)=0x09090909-UINTAT(umsd+24); | 1501 UBFROMUI(umsd+24, 0x09090909-UBTOUI(umsd+24)); |
1400 UINTAT(umsd+28)=0x09090909-UINTAT(umsd+28); | 1502 UBFROMUI(umsd+28, 0x09090909-UBTOUI(umsd+28)); |
1401 UINTAT(umsd+32)=0x09090909-UINTAT(umsd+32); | 1503 UBFROMUI(umsd+32, 0x09090909-UBTOUI(umsd+32)); |
1402 #define BNEXT 36 | 1504 #define BNEXT 36 |
1403 #endif | 1505 #endif |
1404 if (ulsd>=umsd+BNEXT) { /* unaligned */ | 1506 if (ulsd>=umsd+BNEXT) { /* unaligned */ |
1405 /* eight will handle most unaligments for Double; 16 for Quad */ | 1507 /* eight will handle most unaligments for Double; 16 for Quad */ |
1406 UINTAT(umsd+BNEXT)=0x09090909-UINTAT(umsd+BNEXT); | 1508 UBFROMUI(umsd+BNEXT, 0x09090909-UBTOUI(umsd+BNEXT)); |
1407 UINTAT(umsd+BNEXT+4)=0x09090909-UINTAT(umsd+BNEXT+4); | 1509 UBFROMUI(umsd+BNEXT+4, 0x09090909-UBTOUI(umsd+BNEXT+4)); |
1408 #if DOUBLE | 1510 #if DOUBLE |
1409 #define BNEXTY (BNEXT+8) | 1511 #define BNEXTY (BNEXT+8) |
1410 #elif QUAD | 1512 #elif QUAD |
1411 UINTAT(umsd+BNEXT+8)=0x09090909-UINTAT(umsd+BNEXT+8); | 1513 UBFROMUI(umsd+BNEXT+8, 0x09090909-UBTOUI(umsd+BNEXT+8)); |
1412 UINTAT(umsd+BNEXT+12)=0x09090909-UINTAT(umsd+BNEXT+12); | 1514 UBFROMUI(umsd+BNEXT+12, 0x09090909-UBTOUI(umsd+BNEXT+12)); |
1413 #define BNEXTY (BNEXT+16) | 1515 #define BNEXTY (BNEXT+16) |
1414 #endif | 1516 #endif |
1415 if (ulsd>=umsd+BNEXTY) { /* very unaligned */ | 1517 if (ulsd>=umsd+BNEXTY) { /* very unaligned */ |
1416 ui=&UINTAT(umsd+BNEXTY); /* -> continue */ | 1518 ut=umsd+BNEXTY; /* -> continue */ |
1417 for (;;ui++) { | 1519 for (;;ut+=4) { |
1418 *ui=0x09090909-*ui; /* invert four digits */ | 1520 UBFROMUI(ut, 0x09090909-UBTOUI(ut)); /* invert four digits */ |
1419 if (ui>=&UINTAT(ulsd-3)) break; /* all done */ | 1521 if (ut>=ulsd-3) break; /* all done */ |
1420 } | 1522 } |
1421 } | 1523 } |
1422 } | 1524 } |
1423 /* complete the ten's complement by adding 1 */ | 1525 /* complete the ten's complement by adding 1 */ |
1424 for (ub=ulsd; *ub==9; ub--) *ub=0; | 1526 for (ub=ulsd; *ub==9; ub--) *ub=0; |
1439 /* full-length) */ | 1541 /* full-length) */ |
1440 if (ISCOEFFZERO(acc+COFF)) { | 1542 if (ISCOEFFZERO(acc+COFF)) { |
1441 umsd=acc+COFF+DECPMAX-1; /* so far, so zero */ | 1543 umsd=acc+COFF+DECPMAX-1; /* so far, so zero */ |
1442 if (ulsd>umsd) { /* more to check */ | 1544 if (ulsd>umsd) { /* more to check */ |
1443 umsd++; /* to align after checked area */ | 1545 umsd++; /* to align after checked area */ |
1444 for (; UINTAT(umsd)==0 && umsd+3<ulsd;) umsd+=4; | 1546 for (; UBTOUI(umsd)==0 && umsd+3<ulsd;) umsd+=4; |
1445 for (; *umsd==0 && umsd<ulsd;) umsd++; | 1547 for (; *umsd==0 && umsd<ulsd;) umsd++; |
1446 } | 1548 } |
1447 if (*umsd==0) { /* must be true zero (and diffsign) */ | 1549 if (*umsd==0) { /* must be true zero (and diffsign) */ |
1448 num.sign=0; /* assume + */ | 1550 num.sign=0; /* assume + */ |
1449 if (set->round==DEC_ROUND_FLOOR) num.sign=DECFLOAT_Sign; | 1551 if (set->round==DEC_ROUND_FLOOR) num.sign=DECFLOAT_Sign; |
1450 } | 1552 } |
1451 } | 1553 } |
1452 /* [else was not zero, might still have leading zeros] */ | 1554 /* [else was not zero, might still have leading zeros] */ |
1461 umsd=acc+3; | 1563 umsd=acc+3; |
1462 } | 1564 } |
1463 #endif | 1565 #endif |
1464 } /* same sign */ | 1566 } /* same sign */ |
1465 | 1567 |
1466 num.msd=umsd; /* set MSD .. */ | 1568 num.msd=umsd; /* set MSD .. */ |
1467 num.lsd=ulsd; /* .. and LSD */ | 1569 num.lsd=ulsd; /* .. and LSD */ |
1468 num.exponent=expr; /* set exponent to smaller */ | 1570 num.exponent=bexpr-DECBIAS; /* set exponent to smaller, unbiassed */ |
1469 | 1571 |
1470 #if DECTRACE | 1572 #if DECTRACE |
1471 decFloatShow(dfl, "dfl"); | 1573 decFloatShow(dfl, "dfl"); |
1472 decFloatShow(dfr, "dfr"); | 1574 decFloatShow(dfr, "dfr"); |
1473 decShowNum(&num, "postadd"); | 1575 decShowNum(&num, "postadd"); |
1477 | 1579 |
1478 /* ------------------------------------------------------------------ */ | 1580 /* ------------------------------------------------------------------ */ |
1479 /* decFloatAnd -- logical digitwise AND of two decFloats */ | 1581 /* decFloatAnd -- logical digitwise AND of two decFloats */ |
1480 /* */ | 1582 /* */ |
1481 /* result gets the result of ANDing dfl and dfr */ | 1583 /* result gets the result of ANDing dfl and dfr */ |
1482 /* dfl is the first decFloat (lhs) */ | 1584 /* dfl is the first decFloat (lhs) */ |
1483 /* dfr is the second decFloat (rhs) */ | 1585 /* dfr is the second decFloat (rhs) */ |
1484 /* set is the context */ | 1586 /* set is the context */ |
1485 /* returns result, which will be canonical with sign=0 */ | 1587 /* returns result, which will be canonical with sign=0 */ |
1486 /* */ | 1588 /* */ |
1487 /* The operands must be positive, finite with exponent q=0, and */ | 1589 /* The operands must be positive, finite with exponent q=0, and */ |
1488 /* comprise just zeros and ones; if not, Invalid operation results. */ | 1590 /* comprise just zeros and ones; if not, Invalid operation results. */ |
1489 /* ------------------------------------------------------------------ */ | 1591 /* ------------------------------------------------------------------ */ |
1490 decFloat * decFloatAnd(decFloat *result, | 1592 decFloat * decFloatAnd(decFloat *result, |
1491 const decFloat *dfl, const decFloat *dfr, | 1593 const decFloat *dfl, const decFloat *dfr, |
1492 decContext *set) { | 1594 decContext *set) { |
1509 | 1611 |
1510 /* ------------------------------------------------------------------ */ | 1612 /* ------------------------------------------------------------------ */ |
1511 /* decFloatCanonical -- copy a decFloat, making canonical */ | 1613 /* decFloatCanonical -- copy a decFloat, making canonical */ |
1512 /* */ | 1614 /* */ |
1513 /* result gets the canonicalized df */ | 1615 /* result gets the canonicalized df */ |
1514 /* df is the decFloat to copy and make canonical */ | 1616 /* df is the decFloat to copy and make canonical */ |
1515 /* returns result */ | 1617 /* returns result */ |
1516 /* */ | 1618 /* */ |
1517 /* This works on specials, too; no error or exception is possible. */ | 1619 /* This works on specials, too; no error or exception is possible. */ |
1518 /* ------------------------------------------------------------------ */ | 1620 /* ------------------------------------------------------------------ */ |
1519 decFloat * decFloatCanonical(decFloat *result, const decFloat *df) { | 1621 decFloat * decFloatCanonical(decFloat *result, const decFloat *df) { |
1521 } /* decFloatCanonical */ | 1623 } /* decFloatCanonical */ |
1522 | 1624 |
1523 /* ------------------------------------------------------------------ */ | 1625 /* ------------------------------------------------------------------ */ |
1524 /* decFloatClass -- return the class of a decFloat */ | 1626 /* decFloatClass -- return the class of a decFloat */ |
1525 /* */ | 1627 /* */ |
1526 /* df is the decFloat to test */ | 1628 /* df is the decFloat to test */ |
1527 /* returns the decClass that df falls into */ | 1629 /* returns the decClass that df falls into */ |
1528 /* ------------------------------------------------------------------ */ | 1630 /* ------------------------------------------------------------------ */ |
1529 enum decClass decFloatClass(const decFloat *df) { | 1631 enum decClass decFloatClass(const decFloat *df) { |
1530 Int exp; /* exponent */ | 1632 Int exp; /* exponent */ |
1531 if (DFISSPECIAL(df)) { | 1633 if (DFISSPECIAL(df)) { |
1553 } /* decFloatClass */ | 1655 } /* decFloatClass */ |
1554 | 1656 |
1555 /* ------------------------------------------------------------------ */ | 1657 /* ------------------------------------------------------------------ */ |
1556 /* decFloatClassString -- return the class of a decFloat as a string */ | 1658 /* decFloatClassString -- return the class of a decFloat as a string */ |
1557 /* */ | 1659 /* */ |
1558 /* df is the decFloat to test */ | 1660 /* df is the decFloat to test */ |
1559 /* returns a constant string describing the class df falls into */ | 1661 /* returns a constant string describing the class df falls into */ |
1560 /* ------------------------------------------------------------------ */ | 1662 /* ------------------------------------------------------------------ */ |
1561 const char *decFloatClassString(const decFloat *df) { | 1663 const char *decFloatClassString(const decFloat *df) { |
1562 enum decClass eclass=decFloatClass(df); | 1664 enum decClass eclass=decFloatClass(df); |
1563 if (eclass==DEC_CLASS_POS_NORMAL) return DEC_ClassString_PN; | 1665 if (eclass==DEC_CLASS_POS_NORMAL) return DEC_ClassString_PN; |
1572 if (eclass==DEC_CLASS_SNAN) return DEC_ClassString_SN; | 1674 if (eclass==DEC_CLASS_SNAN) return DEC_ClassString_SN; |
1573 return DEC_ClassString_UN; /* Unknown */ | 1675 return DEC_ClassString_UN; /* Unknown */ |
1574 } /* decFloatClassString */ | 1676 } /* decFloatClassString */ |
1575 | 1677 |
1576 /* ------------------------------------------------------------------ */ | 1678 /* ------------------------------------------------------------------ */ |
1577 /* decFloatCompare -- compare two decFloats; quiet NaNs allowed */ | 1679 /* decFloatCompare -- compare two decFloats; quiet NaNs allowed */ |
1578 /* */ | 1680 /* */ |
1579 /* result gets the result of comparing dfl and dfr */ | 1681 /* result gets the result of comparing dfl and dfr */ |
1580 /* dfl is the first decFloat (lhs) */ | 1682 /* dfl is the first decFloat (lhs) */ |
1581 /* dfr is the second decFloat (rhs) */ | 1683 /* dfr is the second decFloat (rhs) */ |
1582 /* set is the context */ | 1684 /* set is the context */ |
1583 /* returns result, which may be -1, 0, 1, or NaN (Unordered) */ | 1685 /* returns result, which may be -1, 0, 1, or NaN (Unordered) */ |
1584 /* ------------------------------------------------------------------ */ | 1686 /* ------------------------------------------------------------------ */ |
1585 decFloat * decFloatCompare(decFloat *result, | 1687 decFloat * decFloatCompare(decFloat *result, |
1599 | 1701 |
1600 /* ------------------------------------------------------------------ */ | 1702 /* ------------------------------------------------------------------ */ |
1601 /* decFloatCompareSignal -- compare two decFloats; all NaNs signal */ | 1703 /* decFloatCompareSignal -- compare two decFloats; all NaNs signal */ |
1602 /* */ | 1704 /* */ |
1603 /* result gets the result of comparing dfl and dfr */ | 1705 /* result gets the result of comparing dfl and dfr */ |
1604 /* dfl is the first decFloat (lhs) */ | 1706 /* dfl is the first decFloat (lhs) */ |
1605 /* dfr is the second decFloat (rhs) */ | 1707 /* dfr is the second decFloat (rhs) */ |
1606 /* set is the context */ | 1708 /* set is the context */ |
1607 /* returns result, which may be -1, 0, 1, or NaN (Unordered) */ | 1709 /* returns result, which may be -1, 0, 1, or NaN (Unordered) */ |
1608 /* ------------------------------------------------------------------ */ | 1710 /* ------------------------------------------------------------------ */ |
1609 decFloat * decFloatCompareSignal(decFloat *result, | 1711 decFloat * decFloatCompareSignal(decFloat *result, |
1626 | 1728 |
1627 /* ------------------------------------------------------------------ */ | 1729 /* ------------------------------------------------------------------ */ |
1628 /* decFloatCompareTotal -- compare two decFloats with total ordering */ | 1730 /* decFloatCompareTotal -- compare two decFloats with total ordering */ |
1629 /* */ | 1731 /* */ |
1630 /* result gets the result of comparing dfl and dfr */ | 1732 /* result gets the result of comparing dfl and dfr */ |
1631 /* dfl is the first decFloat (lhs) */ | 1733 /* dfl is the first decFloat (lhs) */ |
1632 /* dfr is the second decFloat (rhs) */ | 1734 /* dfr is the second decFloat (rhs) */ |
1633 /* returns result, which may be -1, 0, or 1 */ | 1735 /* returns result, which may be -1, 0, or 1 */ |
1634 /* ------------------------------------------------------------------ */ | 1736 /* ------------------------------------------------------------------ */ |
1635 decFloat * decFloatCompareTotal(decFloat *result, | 1737 decFloat * decFloatCompareTotal(decFloat *result, |
1636 const decFloat *dfl, const decFloat *dfr) { | 1738 const decFloat *dfl, const decFloat *dfr) { |
1637 Int comp; /* work */ | 1739 Int comp; /* work */ |
1740 uInt uiwork; /* for macros */ | |
1741 #if QUAD | |
1742 uShort uswork; /* .. */ | |
1743 #endif | |
1638 if (DFISNAN(dfl) || DFISNAN(dfr)) { | 1744 if (DFISNAN(dfl) || DFISNAN(dfr)) { |
1639 Int nanl, nanr; /* work */ | 1745 Int nanl, nanr; /* work */ |
1640 /* morph NaNs to +/- 1 or 2, leave numbers as 0 */ | 1746 /* morph NaNs to +/- 1 or 2, leave numbers as 0 */ |
1641 nanl=DFISSNAN(dfl)+DFISQNAN(dfl)*2; /* quiet > signalling */ | 1747 nanl=DFISSNAN(dfl)+DFISQNAN(dfl)*2; /* quiet > signalling */ |
1642 if (DFISSIGNED(dfl)) nanl=-nanl; | 1748 if (DFISSIGNED(dfl)) nanl=-nanl; |
1643 nanr=DFISSNAN(dfr)+DFISQNAN(dfr)*2; | 1749 nanr=DFISSNAN(dfr)+DFISQNAN(dfr)*2; |
1644 if (DFISSIGNED(dfr)) nanr=-nanr; | 1750 if (DFISSIGNED(dfr)) nanr=-nanr; |
1645 if (nanl>nanr) comp=+1; | 1751 if (nanl>nanr) comp=+1; |
1646 else if (nanl<nanr) comp=-1; | 1752 else if (nanl<nanr) comp=-1; |
1647 else { /* NaNs are the same type and sign .. must compare payload */ | 1753 else { /* NaNs are the same type and sign .. must compare payload */ |
1648 /* buffers need +2 for QUAD */ | 1754 /* buffers need +2 for QUAD */ |
1649 uByte bufl[DECPMAX+4]; /* for LHS coefficient + foot */ | 1755 uByte bufl[DECPMAX+4]; /* for LHS coefficient + foot */ |
1650 uByte bufr[DECPMAX+4]; /* for RHS coefficient + foot */ | 1756 uByte bufr[DECPMAX+4]; /* for RHS coefficient + foot */ |
1651 uByte *ub, *uc; /* work */ | 1757 uByte *ub, *uc; /* work */ |
1652 Int sigl; /* signum of LHS */ | 1758 Int sigl; /* signum of LHS */ |
1653 sigl=(DFISSIGNED(dfl) ? -1 : +1); | 1759 sigl=(DFISSIGNED(dfl) ? -1 : +1); |
1654 | 1760 |
1655 /* decode the coefficients */ | 1761 /* decode the coefficients */ |
1656 /* (shift both right two if Quad to make a multiple of four) */ | 1762 /* (shift both right two if Quad to make a multiple of four) */ |
1657 #if QUAD | 1763 #if QUAD |
1658 ub = bufl; /* avoid type-pun violation */ | 1764 UBFROMUS(bufl, 0); |
1659 USHORTAT(ub)=0; | 1765 UBFROMUS(bufr, 0); |
1660 uc = bufr; /* avoid type-pun violation */ | |
1661 USHORTAT(uc)=0; | |
1662 #endif | 1766 #endif |
1663 GETCOEFF(dfl, bufl+QUAD*2); /* decode from decFloat */ | 1767 GETCOEFF(dfl, bufl+QUAD*2); /* decode from decFloat */ |
1664 GETCOEFF(dfr, bufr+QUAD*2); /* .. */ | 1768 GETCOEFF(dfr, bufr+QUAD*2); /* .. */ |
1665 /* all multiples of four, here */ | 1769 /* all multiples of four, here */ |
1666 comp=0; /* assume equal */ | 1770 comp=0; /* assume equal */ |
1667 for (ub=bufl, uc=bufr; ub<bufl+DECPMAX+QUAD*2; ub+=4, uc+=4) { | 1771 for (ub=bufl, uc=bufr; ub<bufl+DECPMAX+QUAD*2; ub+=4, uc+=4) { |
1668 if (UINTAT(ub)==UINTAT(uc)) continue; /* so far so same */ | 1772 uInt ui=UBTOUI(ub); |
1773 if (ui==UBTOUI(uc)) continue; /* so far so same */ | |
1669 /* about to find a winner; go by bytes in case little-endian */ | 1774 /* about to find a winner; go by bytes in case little-endian */ |
1670 for (;; ub++, uc++) { | 1775 for (;; ub++, uc++) { |
1671 if (*ub==*uc) continue; | 1776 if (*ub==*uc) continue; |
1672 if (*ub>*uc) comp=sigl; /* difference found */ | 1777 if (*ub>*uc) comp=sigl; /* difference found */ |
1673 else comp=-sigl; /* .. */ | 1778 else comp=-sigl; /* .. */ |
1689 | 1794 |
1690 /* ------------------------------------------------------------------ */ | 1795 /* ------------------------------------------------------------------ */ |
1691 /* decFloatCompareTotalMag -- compare magnitudes with total ordering */ | 1796 /* decFloatCompareTotalMag -- compare magnitudes with total ordering */ |
1692 /* */ | 1797 /* */ |
1693 /* result gets the result of comparing abs(dfl) and abs(dfr) */ | 1798 /* result gets the result of comparing abs(dfl) and abs(dfr) */ |
1694 /* dfl is the first decFloat (lhs) */ | 1799 /* dfl is the first decFloat (lhs) */ |
1695 /* dfr is the second decFloat (rhs) */ | 1800 /* dfr is the second decFloat (rhs) */ |
1696 /* returns result, which may be -1, 0, or 1 */ | 1801 /* returns result, which may be -1, 0, or 1 */ |
1697 /* ------------------------------------------------------------------ */ | 1802 /* ------------------------------------------------------------------ */ |
1698 decFloat * decFloatCompareTotalMag(decFloat *result, | 1803 decFloat * decFloatCompareTotalMag(decFloat *result, |
1699 const decFloat *dfl, const decFloat *dfr) { | 1804 const decFloat *dfl, const decFloat *dfr) { |
1740 } /* decFloatCopyAbs */ | 1845 } /* decFloatCopyAbs */ |
1741 | 1846 |
1742 /* ------------------------------------------------------------------ */ | 1847 /* ------------------------------------------------------------------ */ |
1743 /* decFloatCopyNegate -- copy a decFloat as-is with inverted sign bit */ | 1848 /* decFloatCopyNegate -- copy a decFloat as-is with inverted sign bit */ |
1744 /* */ | 1849 /* */ |
1745 /* result gets the copy of dfl with sign bit inverted */ | 1850 /* result gets the copy of dfl with sign bit inverted */ |
1746 /* dfl is the decFloat to copy */ | 1851 /* dfl is the decFloat to copy */ |
1747 /* returns result */ | 1852 /* returns result */ |
1748 /* */ | 1853 /* */ |
1749 /* This is a bitwise operation; no errors or exceptions are possible. */ | 1854 /* This is a bitwise operation; no errors or exceptions are possible. */ |
1750 /* ------------------------------------------------------------------ */ | 1855 /* ------------------------------------------------------------------ */ |
1753 DFBYTE(result, 0)^=0x80; /* invert sign bit */ | 1858 DFBYTE(result, 0)^=0x80; /* invert sign bit */ |
1754 return result; | 1859 return result; |
1755 } /* decFloatCopyNegate */ | 1860 } /* decFloatCopyNegate */ |
1756 | 1861 |
1757 /* ------------------------------------------------------------------ */ | 1862 /* ------------------------------------------------------------------ */ |
1758 /* decFloatCopySign -- copy a decFloat with the sign of another */ | 1863 /* decFloatCopySign -- copy a decFloat with the sign of another */ |
1759 /* */ | 1864 /* */ |
1760 /* result gets the result of copying dfl with the sign of dfr */ | 1865 /* result gets the result of copying dfl with the sign of dfr */ |
1761 /* dfl is the first decFloat (lhs) */ | 1866 /* dfl is the first decFloat (lhs) */ |
1762 /* dfr is the second decFloat (rhs) */ | 1867 /* dfr is the second decFloat (rhs) */ |
1763 /* returns result */ | 1868 /* returns result */ |
1764 /* */ | 1869 /* */ |
1765 /* This is a bitwise operation; no errors or exceptions are possible. */ | 1870 /* This is a bitwise operation; no errors or exceptions are possible. */ |
1766 /* ------------------------------------------------------------------ */ | 1871 /* ------------------------------------------------------------------ */ |
1788 #define dpdlenchk(n, form) {dpd=(form)&0x3ff; \ | 1893 #define dpdlenchk(n, form) {dpd=(form)&0x3ff; \ |
1789 if (dpd) return (DECPMAX-1-3*(n))-(3-DPD2BCD8[dpd*4+3]);} | 1894 if (dpd) return (DECPMAX-1-3*(n))-(3-DPD2BCD8[dpd*4+3]);} |
1790 /* next one is used when it is known that the declet must be */ | 1895 /* next one is used when it is known that the declet must be */ |
1791 /* non-zero, or is the final zero declet */ | 1896 /* non-zero, or is the final zero declet */ |
1792 #define dpdlendun(n, form) {dpd=(form)&0x3ff; \ | 1897 #define dpdlendun(n, form) {dpd=(form)&0x3ff; \ |
1793 if (dpd==0) return 1; \ | 1898 if (dpd==0) return 1; \ |
1794 return (DECPMAX-1-3*(n))-(3-DPD2BCD8[dpd*4+3]);} | 1899 return (DECPMAX-1-3*(n))-(3-DPD2BCD8[dpd*4+3]);} |
1795 | 1900 |
1796 uInt decFloatDigits(const decFloat *df) { | 1901 uInt decFloatDigits(const decFloat *df) { |
1797 uInt dpd; /* work */ | 1902 uInt dpd; /* work */ |
1798 uInt sourhi=DFWORD(df, 0); /* top word from source decFloat */ | 1903 uInt sourhi=DFWORD(df, 0); /* top word from source decFloat */ |
1812 sourlo=DFWORD(df, 1); | 1917 sourlo=DFWORD(df, 1); |
1813 dpdlendun(1, (sourhi<<2) | (sourlo>>30)); | 1918 dpdlendun(1, (sourhi<<2) | (sourlo>>30)); |
1814 } /* [cannot drop through] */ | 1919 } /* [cannot drop through] */ |
1815 sourlo=DFWORD(df, 1); /* sourhi not involved now */ | 1920 sourlo=DFWORD(df, 1); /* sourhi not involved now */ |
1816 if (sourlo&0xfff00000) { /* in one of first two */ | 1921 if (sourlo&0xfff00000) { /* in one of first two */ |
1817 dpdlenchk(1, sourlo>>30); /* very rare */ | 1922 dpdlenchk(1, sourlo>>30); /* very rare */ |
1818 dpdlendun(2, sourlo>>20); | 1923 dpdlendun(2, sourlo>>20); |
1819 } /* [cannot drop through] */ | 1924 } /* [cannot drop through] */ |
1820 dpdlenchk(3, sourlo>>10); | 1925 dpdlenchk(3, sourlo>>10); |
1821 dpdlendun(4, sourlo); | 1926 dpdlendun(4, sourlo); |
1822 /* [cannot drop through] */ | 1927 /* [cannot drop through] */ |
1843 sourlo=DFWORD(df, 3); | 1948 sourlo=DFWORD(df, 3); |
1844 dpdlendun(7, ((sourml)<<2) | (sourlo>>30)); | 1949 dpdlendun(7, ((sourml)<<2) | (sourlo>>30)); |
1845 } /* [cannot drop through] */ | 1950 } /* [cannot drop through] */ |
1846 sourlo=DFWORD(df, 3); | 1951 sourlo=DFWORD(df, 3); |
1847 if (sourlo&0xfff00000) { /* in one of first two */ | 1952 if (sourlo&0xfff00000) { /* in one of first two */ |
1848 dpdlenchk(7, sourlo>>30); /* very rare */ | 1953 dpdlenchk(7, sourlo>>30); /* very rare */ |
1849 dpdlendun(8, sourlo>>20); | 1954 dpdlendun(8, sourlo>>20); |
1850 } /* [cannot drop through] */ | 1955 } /* [cannot drop through] */ |
1851 dpdlenchk(9, sourlo>>10); | 1956 dpdlenchk(9, sourlo>>10); |
1852 dpdlendun(10, sourlo); | 1957 dpdlendun(10, sourlo); |
1853 /* [cannot drop through] */ | 1958 /* [cannot drop through] */ |
1856 | 1961 |
1857 /* ------------------------------------------------------------------ */ | 1962 /* ------------------------------------------------------------------ */ |
1858 /* decFloatDivide -- divide a decFloat by another */ | 1963 /* decFloatDivide -- divide a decFloat by another */ |
1859 /* */ | 1964 /* */ |
1860 /* result gets the result of dividing dfl by dfr: */ | 1965 /* result gets the result of dividing dfl by dfr: */ |
1861 /* dfl is the first decFloat (lhs) */ | 1966 /* dfl is the first decFloat (lhs) */ |
1862 /* dfr is the second decFloat (rhs) */ | 1967 /* dfr is the second decFloat (rhs) */ |
1863 /* set is the context */ | 1968 /* set is the context */ |
1864 /* returns result */ | 1969 /* returns result */ |
1865 /* */ | 1970 /* */ |
1866 /* ------------------------------------------------------------------ */ | 1971 /* ------------------------------------------------------------------ */ |
1873 | 1978 |
1874 /* ------------------------------------------------------------------ */ | 1979 /* ------------------------------------------------------------------ */ |
1875 /* decFloatDivideInteger -- integer divide a decFloat by another */ | 1980 /* decFloatDivideInteger -- integer divide a decFloat by another */ |
1876 /* */ | 1981 /* */ |
1877 /* result gets the result of dividing dfl by dfr: */ | 1982 /* result gets the result of dividing dfl by dfr: */ |
1878 /* dfl is the first decFloat (lhs) */ | 1983 /* dfl is the first decFloat (lhs) */ |
1879 /* dfr is the second decFloat (rhs) */ | 1984 /* dfr is the second decFloat (rhs) */ |
1880 /* set is the context */ | 1985 /* set is the context */ |
1881 /* returns result */ | 1986 /* returns result */ |
1882 /* */ | 1987 /* */ |
1883 /* ------------------------------------------------------------------ */ | 1988 /* ------------------------------------------------------------------ */ |
1889 | 1994 |
1890 /* ------------------------------------------------------------------ */ | 1995 /* ------------------------------------------------------------------ */ |
1891 /* decFloatFMA -- multiply and add three decFloats, fused */ | 1996 /* decFloatFMA -- multiply and add three decFloats, fused */ |
1892 /* */ | 1997 /* */ |
1893 /* result gets the result of (dfl*dfr)+dff with a single rounding */ | 1998 /* result gets the result of (dfl*dfr)+dff with a single rounding */ |
1894 /* dfl is the first decFloat (lhs) */ | 1999 /* dfl is the first decFloat (lhs) */ |
1895 /* dfr is the second decFloat (rhs) */ | 2000 /* dfr is the second decFloat (rhs) */ |
1896 /* dff is the final decFloat (fhs) */ | 2001 /* dff is the final decFloat (fhs) */ |
1897 /* set is the context */ | 2002 /* set is the context */ |
1898 /* returns result */ | 2003 /* returns result */ |
1899 /* */ | 2004 /* */ |
1900 /* ------------------------------------------------------------------ */ | 2005 /* ------------------------------------------------------------------ */ |
1901 decFloat * decFloatFMA(decFloat *result, const decFloat *dfl, | 2006 decFloat * decFloatFMA(decFloat *result, const decFloat *dfl, |
1902 const decFloat *dfr, const decFloat *dff, | 2007 const decFloat *dfr, const decFloat *dff, |
1903 decContext *set) { | 2008 decContext *set) { |
2009 | |
1904 /* The accumulator has the bytes needed for FiniteMultiply, plus */ | 2010 /* The accumulator has the bytes needed for FiniteMultiply, plus */ |
1905 /* one byte to the left in case of carry, plus DECPMAX+2 to the */ | 2011 /* one byte to the left in case of carry, plus DECPMAX+2 to the */ |
1906 /* right for the final addition (up to full fhs + round & sticky) */ | 2012 /* right for the final addition (up to full fhs + round & sticky) */ |
1907 #define FMALEN (1+ (DECPMAX9*18) +DECPMAX+2) | 2013 #define FMALEN (ROUNDUP4(1+ (DECPMAX9*18+1) +DECPMAX+2)) |
1908 uByte acc[FMALEN]; /* for multiplied coefficient in BCD */ | 2014 uByte acc[FMALEN]; /* for multiplied coefficient in BCD */ |
1909 /* .. and for final result */ | 2015 /* .. and for final result */ |
1910 bcdnum mul; /* for multiplication result */ | 2016 bcdnum mul; /* for multiplication result */ |
1911 bcdnum fin; /* for final operand, expanded */ | 2017 bcdnum fin; /* for final operand, expanded */ |
1912 uByte coe[DECPMAX]; /* dff coefficient in BCD */ | 2018 uByte coe[ROUNDUP4(DECPMAX)]; /* dff coefficient in BCD */ |
1913 bcdnum *hi, *lo; /* bcdnum with higher/lower exponent */ | 2019 bcdnum *hi, *lo; /* bcdnum with higher/lower exponent */ |
1914 uInt diffsign; /* non-zero if signs differ */ | 2020 uInt diffsign; /* non-zero if signs differ */ |
1915 uInt hipad; /* pad digit for hi if needed */ | 2021 uInt hipad; /* pad digit for hi if needed */ |
1916 Int padding; /* excess exponent */ | 2022 Int padding; /* excess exponent */ |
1917 uInt carry; /* +1 for ten's complement and during add */ | 2023 uInt carry; /* +1 for ten's complement and during add */ |
1918 uByte *ub, *uh, *ul; /* work */ | 2024 uByte *ub, *uh, *ul; /* work */ |
2025 uInt uiwork; /* for macros */ | |
1919 | 2026 |
1920 /* handle all the special values [any special operand leads to a */ | 2027 /* handle all the special values [any special operand leads to a */ |
1921 /* special result] */ | 2028 /* special result] */ |
1922 if (DFISSPECIAL(dfl) || DFISSPECIAL(dfr) || DFISSPECIAL(dff)) { | 2029 if (DFISSPECIAL(dfl) || DFISSPECIAL(dfr) || DFISSPECIAL(dff)) { |
1923 decFloat proxy; /* multiplication result proxy */ | 2030 decFloat proxy; /* multiplication result proxy */ |
1964 fin.msd=coe; | 2071 fin.msd=coe; |
1965 fin.lsd=coe+DECPMAX-1; | 2072 fin.lsd=coe+DECPMAX-1; |
1966 GETCOEFF(dff, coe); /* extract the coefficient */ | 2073 GETCOEFF(dff, coe); /* extract the coefficient */ |
1967 | 2074 |
1968 /* now set hi and lo so that hi points to whichever of mul and fin */ | 2075 /* now set hi and lo so that hi points to whichever of mul and fin */ |
1969 /* has the higher exponent and lo point to the other [don't care if */ | 2076 /* has the higher exponent and lo points to the other [don't care, */ |
1970 /* the same] */ | 2077 /* if the same]. One coefficient will be in acc, the other in coe. */ |
1971 if (mul.exponent>=fin.exponent) { | 2078 if (mul.exponent>=fin.exponent) { |
1972 hi=&mul; | 2079 hi=&mul; |
1973 lo=&fin; | 2080 lo=&fin; |
1974 } | 2081 } |
1975 else { | 2082 else { |
1976 hi=&fin; | 2083 hi=&fin; |
1977 lo=&mul; | 2084 lo=&mul; |
1978 } | 2085 } |
1979 | 2086 |
1980 /* remove leading zeros on both operands; this will save time later */ | 2087 /* remove leading zeros on both operands; this will save time later */ |
1981 /* and make testing for zero trivial */ | 2088 /* and make testing for zero trivial (tests are safe because acc */ |
1982 for (; UINTAT(hi->msd)==0 && hi->msd+3<hi->lsd;) hi->msd+=4; | 2089 /* and coe are rounded up to uInts) */ |
2090 for (; UBTOUI(hi->msd)==0 && hi->msd+3<hi->lsd;) hi->msd+=4; | |
1983 for (; *hi->msd==0 && hi->msd<hi->lsd;) hi->msd++; | 2091 for (; *hi->msd==0 && hi->msd<hi->lsd;) hi->msd++; |
1984 for (; UINTAT(lo->msd)==0 && lo->msd+3<lo->lsd;) lo->msd+=4; | 2092 for (; UBTOUI(lo->msd)==0 && lo->msd+3<lo->lsd;) lo->msd+=4; |
1985 for (; *lo->msd==0 && lo->msd<lo->lsd;) lo->msd++; | 2093 for (; *lo->msd==0 && lo->msd<lo->lsd;) lo->msd++; |
1986 | 2094 |
1987 /* if hi is zero then result will be lo (which has the smaller */ | 2095 /* if hi is zero then result will be lo (which has the smaller */ |
1988 /* exponent), which also may need to be tested for zero for the */ | 2096 /* exponent), which also may need to be tested for zero for the */ |
1989 /* weird IEEE 754 sign rules */ | 2097 /* weird IEEE 754 sign rules */ |
1990 if (*hi->msd==0 && hi->msd==hi->lsd) { /* hi is zero */ | 2098 if (*hi->msd==0) { /* hi is zero */ |
1991 /* "When the sum of two operands with opposite signs is */ | 2099 /* "When the sum of two operands with opposite signs is */ |
1992 /* exactly zero, the sign of that sum shall be '+' in all */ | 2100 /* exactly zero, the sign of that sum shall be '+' in all */ |
1993 /* rounding modes except round toward -Infinity, in which */ | 2101 /* rounding modes except round toward -Infinity, in which */ |
1994 /* mode that sign shall be '-'." */ | 2102 /* mode that sign shall be '-'." */ |
1995 if (diffsign) { | 2103 if (diffsign) { |
1996 if (*lo->msd==0 && lo->msd==lo->lsd) { /* lo is zero */ | 2104 if (*lo->msd==0) { /* lo is zero */ |
1997 lo->sign=0; | 2105 lo->sign=0; |
1998 if (set->round==DEC_ROUND_FLOOR) lo->sign=DECFLOAT_Sign; | 2106 if (set->round==DEC_ROUND_FLOOR) lo->sign=DECFLOAT_Sign; |
1999 } /* diffsign && lo=0 */ | 2107 } /* diffsign && lo=0 */ |
2000 } /* diffsign */ | 2108 } /* diffsign */ |
2001 return decFinalize(result, lo, set); /* may need clamping */ | 2109 return decFinalize(result, lo, set); /* may need clamping */ |
2002 } /* numfl is zero */ | 2110 } /* numfl is zero */ |
2003 /* [here, both are minimal length and hi is non-zero] */ | 2111 /* [here, both are minimal length and hi is non-zero] */ |
2112 /* (if lo is zero then padding with zeros may be needed, below) */ | |
2004 | 2113 |
2005 /* if signs differ, take the ten's complement of hi (zeros to the */ | 2114 /* if signs differ, take the ten's complement of hi (zeros to the */ |
2006 /* right do not matter because the complement of zero is zero); */ | 2115 /* right do not matter because the complement of zero is zero); the */ |
2007 /* the +1 is done later, as part of the addition, inserted at the */ | 2116 /* +1 is done later, as part of the addition, inserted at the */ |
2008 /* correct digit */ | 2117 /* correct digit */ |
2009 hipad=0; | 2118 hipad=0; |
2010 carry=0; | 2119 carry=0; |
2011 if (diffsign) { | 2120 if (diffsign) { |
2012 hipad=9; | 2121 hipad=9; |
2013 carry=1; | 2122 carry=1; |
2014 /* exactly the correct number of digits must be inverted */ | 2123 /* exactly the correct number of digits must be inverted */ |
2015 for (uh=hi->msd; uh<hi->lsd-3; uh+=4) UINTAT(uh)=0x09090909-UINTAT(uh); | 2124 for (uh=hi->msd; uh<hi->lsd-3; uh+=4) UBFROMUI(uh, 0x09090909-UBTOUI(uh)); |
2016 for (; uh<=hi->lsd; uh++) *uh=(uByte)(0x09-*uh); | 2125 for (; uh<=hi->lsd; uh++) *uh=(uByte)(0x09-*uh); |
2017 } | 2126 } |
2018 | 2127 |
2019 /* ready to add; note that hi has no leading zeros so gap */ | 2128 /* ready to add; note that hi has no leading zeros so gap */ |
2020 /* calculation does not have to be as pessimistic as in decFloatAdd */ | 2129 /* calculation does not have to be as pessimistic as in decFloatAdd */ |
2025 /* for its lsd to be aligned with the lsd of lo */ | 2134 /* for its lsd to be aligned with the lsd of lo */ |
2026 padding=hi->exponent-lo->exponent; | 2135 padding=hi->exponent-lo->exponent; |
2027 /* printf("FMA pad %ld\n", (LI)padding); */ | 2136 /* printf("FMA pad %ld\n", (LI)padding); */ |
2028 | 2137 |
2029 /* the result of the addition will be built into the accumulator, */ | 2138 /* the result of the addition will be built into the accumulator, */ |
2030 /* starting from the far right; this could be either hi or lo */ | 2139 /* starting from the far right; this could be either hi or lo, and */ |
2140 /* will be aligned */ | |
2031 ub=acc+FMALEN-1; /* where lsd of result will go */ | 2141 ub=acc+FMALEN-1; /* where lsd of result will go */ |
2032 ul=lo->lsd; /* lsd of rhs */ | 2142 ul=lo->lsd; /* lsd of rhs */ |
2033 | 2143 |
2034 if (padding!=0) { /* unaligned */ | 2144 if (padding!=0) { /* unaligned */ |
2035 /* if the msd of lo is more than DECPMAX+2 digits to the right of */ | 2145 /* if the msd of lo is more than DECPMAX+2 digits to the right of */ |
2036 /* the original msd of hi then it can be reduced to a single */ | 2146 /* the original msd of hi then it can be reduced to a single */ |
2037 /* digit at the right place, as it stays clear of hi digits */ | 2147 /* digit at the right place, as it stays clear of hi digits */ |
2038 /* [it must be DECPMAX+2 because during a subtraction the msd */ | 2148 /* [it must be DECPMAX+2 because during a subtraction the msd */ |
2039 /* could become 0 after a borrow from 1.000 to 0.9999...] */ | 2149 /* could become 0 after a borrow from 1.000 to 0.9999...] */ |
2040 Int hilen=(Int)(hi->lsd-hi->msd+1); /* lengths */ | 2150 |
2041 Int lolen=(Int)(lo->lsd-lo->msd+1); /* .. */ | 2151 Int hilen=(Int)(hi->lsd-hi->msd+1); /* length of hi */ |
2042 Int newexp=MINI(hi->exponent, hi->exponent+hilen-DECPMAX)-3; | 2152 Int lolen=(Int)(lo->lsd-lo->msd+1); /* and of lo */ |
2043 Int reduce=newexp-lo->exponent; | 2153 |
2044 if (reduce>0) { /* [= case gives reduce=0 nop] */ | 2154 if (hilen+padding-lolen > DECPMAX+2) { /* can reduce lo to single */ |
2155 /* make sure it is virtually at least DECPMAX from hi->msd, at */ | |
2156 /* least to right of hi->lsd (in case of destructive subtract), */ | |
2157 /* and separated by at least two digits from either of those */ | |
2158 /* (the tricky DOUBLE case is when hi is a 1 that will become a */ | |
2159 /* 0.9999... by subtraction: */ | |
2160 /* hi: 1 E+16 */ | |
2161 /* lo: .................1000000000000000 E-16 */ | |
2162 /* which for the addition pads to: */ | |
2163 /* hi: 1000000000000000000 E-16 */ | |
2164 /* lo: .................1000000000000000 E-16 */ | |
2165 Int newexp=MINI(hi->exponent, hi->exponent+hilen-DECPMAX)-3; | |
2166 | |
2045 /* printf("FMA reduce: %ld\n", (LI)reduce); */ | 2167 /* printf("FMA reduce: %ld\n", (LI)reduce); */ |
2046 if (reduce>=lolen) { /* eating all */ | 2168 lo->lsd=lo->msd; /* to single digit [maybe 0] */ |
2047 lo->lsd=lo->msd; /* reduce to single digit */ | 2169 lo->exponent=newexp; /* new lowest exponent */ |
2048 lo->exponent=newexp; /* [known to be non-zero] */ | 2170 padding=hi->exponent-lo->exponent; /* recalculate */ |
2049 } | 2171 ul=lo->lsd; /* .. and repoint */ |
2050 else { /* < */ | 2172 } |
2051 uByte *up=lo->lsd; | 2173 |
2052 lo->lsd=lo->lsd-reduce; | 2174 /* padding is still > 0, but will fit in acc (less leading carry slot) */ |
2053 if (*lo->lsd==0) /* could need sticky bit */ | |
2054 for (; up>lo->lsd; up--) { /* search discarded digits */ | |
2055 if (*up!=0) { /* found one... */ | |
2056 *lo->lsd=1; /* set sticky bit */ | |
2057 break; | |
2058 } | |
2059 } | |
2060 lo->exponent+=reduce; | |
2061 } | |
2062 padding=hi->exponent-lo->exponent; /* recalculate */ | |
2063 ul=lo->lsd; /* .. */ | |
2064 } /* maybe reduce */ | |
2065 /* padding is now <= DECPMAX+2 but still > 0; tricky DOUBLE case */ | |
2066 /* is when hi is a 1 that will become a 0.9999... by subtraction: */ | |
2067 /* hi: 1 E+16 */ | |
2068 /* lo: .................1000000000000000 E-16 */ | |
2069 /* which for the addition pads and reduces to: */ | |
2070 /* hi: 1000000000000000000 E-2 */ | |
2071 /* lo: .................1 E-2 */ | |
2072 #if DECCHECK | 2175 #if DECCHECK |
2073 if (padding>DECPMAX+2) printf("FMA excess padding: %ld\n", (LI)padding); | |
2074 if (padding<=0) printf("FMA low padding: %ld\n", (LI)padding); | 2176 if (padding<=0) printf("FMA low padding: %ld\n", (LI)padding); |
2177 if (hilen+padding+1>FMALEN) | |
2178 printf("FMA excess hilen+padding: %ld+%ld \n", (LI)hilen, (LI)padding); | |
2075 /* printf("FMA padding: %ld\n", (LI)padding); */ | 2179 /* printf("FMA padding: %ld\n", (LI)padding); */ |
2076 #endif | 2180 #endif |
2181 | |
2077 /* padding digits can now be set in the result; one or more of */ | 2182 /* padding digits can now be set in the result; one or more of */ |
2078 /* these will come from lo; others will be zeros in the gap */ | 2183 /* these will come from lo; others will be zeros in the gap */ |
2184 for (; ul-3>=lo->msd && padding>3; padding-=4, ul-=4, ub-=4) { | |
2185 UBFROMUI(ub-3, UBTOUI(ul-3)); /* [cannot overlap] */ | |
2186 } | |
2079 for (; ul>=lo->msd && padding>0; padding--, ul--, ub--) *ub=*ul; | 2187 for (; ul>=lo->msd && padding>0; padding--, ul--, ub--) *ub=*ul; |
2080 for (;padding>0; padding--, ub--) *ub=0; /* mind the gap */ | 2188 for (;padding>0; padding--, ub--) *ub=0; /* mind the gap */ |
2081 } | 2189 } |
2082 | 2190 |
2083 /* addition now complete to the right of the rightmost digit of hi */ | 2191 /* addition now complete to the right of the rightmost digit of hi */ |
2084 uh=hi->lsd; | 2192 uh=hi->lsd; |
2085 | 2193 |
2086 /* carry was set up depending on ten's complement above; do the add... */ | 2194 /* dow do the add from hi->lsd to the left */ |
2195 /* [bytewise, because either operand can run out at any time] */ | |
2196 /* carry was set up depending on ten's complement above */ | |
2197 /* first assume both operands have some digits */ | |
2087 for (;; ub--) { | 2198 for (;; ub--) { |
2088 uInt hid, lod; | 2199 if (uh<hi->msd || ul<lo->msd) break; |
2089 if (uh<hi->msd) { | 2200 *ub=(uByte)(carry+(*uh--)+(*ul--)); |
2090 if (ul<lo->msd) break; | 2201 carry=0; |
2091 hid=hipad; | 2202 if (*ub<10) continue; |
2092 } | 2203 *ub-=10; |
2093 else hid=*uh--; | 2204 carry=1; |
2094 if (ul<lo->msd) lod=0; | 2205 } /* both loop */ |
2095 else lod=*ul--; | 2206 |
2096 *ub=(uByte)(carry+hid+lod); | 2207 if (ul<lo->msd) { /* to left of lo */ |
2097 if (*ub<10) carry=0; | 2208 for (;; ub--) { |
2098 else { | 2209 if (uh<hi->msd) break; |
2210 *ub=(uByte)(carry+(*uh--)); /* [+0] */ | |
2211 carry=0; | |
2212 if (*ub<10) continue; | |
2099 *ub-=10; | 2213 *ub-=10; |
2100 carry=1; | 2214 carry=1; |
2101 } | 2215 } /* hi loop */ |
2102 } /* addition loop */ | 2216 } |
2217 else { /* to left of hi */ | |
2218 for (;; ub--) { | |
2219 if (ul<lo->msd) break; | |
2220 *ub=(uByte)(carry+hipad+(*ul--)); | |
2221 carry=0; | |
2222 if (*ub<10) continue; | |
2223 *ub-=10; | |
2224 carry=1; | |
2225 } /* lo loop */ | |
2226 } | |
2103 | 2227 |
2104 /* addition complete -- now handle carry, borrow, etc. */ | 2228 /* addition complete -- now handle carry, borrow, etc. */ |
2105 /* use lo to set up the num (its exponent is already correct, and */ | 2229 /* use lo to set up the num (its exponent is already correct, and */ |
2106 /* sign usually is) */ | 2230 /* sign usually is) */ |
2107 lo->msd=ub+1; | 2231 lo->msd=ub+1; |
2115 } /* same sign */ | 2239 } /* same sign */ |
2116 else { /* signs differed (subtraction) */ | 2240 else { /* signs differed (subtraction) */ |
2117 if (!carry) { /* no carry out means hi<lo */ | 2241 if (!carry) { /* no carry out means hi<lo */ |
2118 /* borrowed -- take ten's complement of the right digits */ | 2242 /* borrowed -- take ten's complement of the right digits */ |
2119 lo->sign=hi->sign; /* sign is lhs sign */ | 2243 lo->sign=hi->sign; /* sign is lhs sign */ |
2120 for (ul=lo->msd; ul<lo->lsd-3; ul+=4) UINTAT(ul)=0x09090909-UINTAT(ul); | 2244 for (ul=lo->msd; ul<lo->lsd-3; ul+=4) UBFROMUI(ul, 0x09090909-UBTOUI(ul)); |
2121 for (; ul<=lo->lsd; ul++) *ul=(uByte)(0x09-*ul); /* [leaves ul at lsd+1] */ | 2245 for (; ul<=lo->lsd; ul++) *ul=(uByte)(0x09-*ul); /* [leaves ul at lsd+1] */ |
2122 /* complete the ten's complement by adding 1 [cannot overrun] */ | 2246 /* complete the ten's complement by adding 1 [cannot overrun] */ |
2123 for (ul--; *ul==9; ul--) *ul=0; | 2247 for (ul--; *ul==9; ul--) *ul=0; |
2124 *ul+=1; | 2248 *ul+=1; |
2125 } /* borrowed */ | 2249 } /* borrowed */ |
2126 else { /* carry out means hi>=lo */ | 2250 else { /* carry out means hi>=lo */ |
2127 /* sign to use is lo->sign */ | 2251 /* sign to use is lo->sign */ |
2128 /* all done except for the special IEEE 754 exact-zero-result */ | 2252 /* all done except for the special IEEE 754 exact-zero-result */ |
2129 /* rule (see above); while testing for zero, strip leading */ | 2253 /* rule (see above); while testing for zero, strip leading */ |
2130 /* zeros (which will save decFinalize doing it) */ | 2254 /* zeros (which will save decFinalize doing it) */ |
2131 for (; UINTAT(lo->msd)==0 && lo->msd+3<lo->lsd;) lo->msd+=4; | 2255 for (; UBTOUI(lo->msd)==0 && lo->msd+3<lo->lsd;) lo->msd+=4; |
2132 for (; *lo->msd==0 && lo->msd<lo->lsd;) lo->msd++; | 2256 for (; *lo->msd==0 && lo->msd<lo->lsd;) lo->msd++; |
2133 if (*lo->msd==0) { /* must be true zero (and diffsign) */ | 2257 if (*lo->msd==0) { /* must be true zero (and diffsign) */ |
2134 lo->sign=0; /* assume + */ | 2258 lo->sign=0; /* assume + */ |
2135 if (set->round==DEC_ROUND_FLOOR) lo->sign=DECFLOAT_Sign; | 2259 if (set->round==DEC_ROUND_FLOOR) lo->sign=DECFLOAT_Sign; |
2136 } | 2260 } |
2137 /* [else was not zero, might still have leading zeros] */ | 2261 /* [else was not zero, might still have leading zeros] */ |
2138 } /* subtraction gave positive result */ | 2262 } /* subtraction gave positive result */ |
2139 } /* diffsign */ | 2263 } /* diffsign */ |
2140 | 2264 |
2265 #if DECCHECK | |
2266 /* assert no left underrun */ | |
2267 if (lo->msd<acc) { | |
2268 printf("FMA underrun by %ld \n", (LI)(acc-lo->msd)); | |
2269 } | |
2270 #endif | |
2271 | |
2141 return decFinalize(result, lo, set); /* round, check, and lay out */ | 2272 return decFinalize(result, lo, set); /* round, check, and lay out */ |
2142 } /* decFloatFMA */ | 2273 } /* decFloatFMA */ |
2143 | 2274 |
2144 /* ------------------------------------------------------------------ */ | 2275 /* ------------------------------------------------------------------ */ |
2145 /* decFloatFromInt -- initialise a decFloat from an Int */ | 2276 /* decFloatFromInt -- initialise a decFloat from an Int */ |
2146 /* */ | 2277 /* */ |
2147 /* result gets the converted Int */ | 2278 /* result gets the converted Int */ |
2148 /* n is the Int to convert */ | 2279 /* n is the Int to convert */ |
2149 /* returns result */ | 2280 /* returns result */ |
2150 /* */ | 2281 /* */ |
2206 | 2337 |
2207 /* ------------------------------------------------------------------ */ | 2338 /* ------------------------------------------------------------------ */ |
2208 /* decFloatInvert -- logical digitwise INVERT of a decFloat */ | 2339 /* decFloatInvert -- logical digitwise INVERT of a decFloat */ |
2209 /* */ | 2340 /* */ |
2210 /* result gets the result of INVERTing df */ | 2341 /* result gets the result of INVERTing df */ |
2211 /* df is the decFloat to invert */ | 2342 /* df is the decFloat to invert */ |
2212 /* set is the context */ | 2343 /* set is the context */ |
2213 /* returns result, which will be canonical with sign=0 */ | 2344 /* returns result, which will be canonical with sign=0 */ |
2214 /* */ | 2345 /* */ |
2215 /* The operand must be positive, finite with exponent q=0, and */ | 2346 /* The operand must be positive, finite with exponent q=0, and */ |
2216 /* comprise just zeros and ones; if not, Invalid operation results. */ | 2347 /* comprise just zeros and ones; if not, Invalid operation results. */ |
2234 } /* decFloatInvert */ | 2365 } /* decFloatInvert */ |
2235 | 2366 |
2236 /* ------------------------------------------------------------------ */ | 2367 /* ------------------------------------------------------------------ */ |
2237 /* decFloatIs -- decFloat tests (IsSigned, etc.) */ | 2368 /* decFloatIs -- decFloat tests (IsSigned, etc.) */ |
2238 /* */ | 2369 /* */ |
2239 /* df is the decFloat to test */ | 2370 /* df is the decFloat to test */ |
2240 /* returns 0 or 1 in an int32_t */ | 2371 /* returns 0 or 1 in a uInt */ |
2241 /* */ | 2372 /* */ |
2242 /* Many of these could be macros, but having them as real functions */ | 2373 /* Many of these could be macros, but having them as real functions */ |
2243 /* is a bit cleaner (and they can be referred to here by the generic */ | 2374 /* is a little cleaner (and they can be referred to here by the */ |
2244 /* names) */ | 2375 /* generic names) */ |
2245 /* ------------------------------------------------------------------ */ | 2376 /* ------------------------------------------------------------------ */ |
2246 uInt decFloatIsCanonical(const decFloat *df) { | 2377 uInt decFloatIsCanonical(const decFloat *df) { |
2247 if (DFISSPECIAL(df)) { | 2378 if (DFISSPECIAL(df)) { |
2248 if (DFISINF(df)) { | 2379 if (DFISINF(df)) { |
2249 if (DFWORD(df, 0)&ECONMASK) return 0; /* exponent continuation */ | 2380 if (DFWORD(df, 0)&ECONMASK) return 0; /* exponent continuation */ |
2326 uInt decFloatIsZero(const decFloat *df) { | 2457 uInt decFloatIsZero(const decFloat *df) { |
2327 return DFISZERO(df); | 2458 return DFISZERO(df); |
2328 } /* decFloatIs... */ | 2459 } /* decFloatIs... */ |
2329 | 2460 |
2330 /* ------------------------------------------------------------------ */ | 2461 /* ------------------------------------------------------------------ */ |
2331 /* decFloatLogB -- return adjusted exponent, by 754r rules */ | 2462 /* decFloatLogB -- return adjusted exponent, by 754 rules */ |
2332 /* */ | 2463 /* */ |
2333 /* result gets the adjusted exponent as an integer, or a NaN etc. */ | 2464 /* result gets the adjusted exponent as an integer, or a NaN etc. */ |
2334 /* df is the decFloat to be examined */ | 2465 /* df is the decFloat to be examined */ |
2335 /* set is the context */ | 2466 /* set is the context */ |
2336 /* returns result */ | 2467 /* returns result */ |
2337 /* */ | 2468 /* */ |
2338 /* Notable cases: */ | 2469 /* Notable cases: */ |
2339 /* A<0 -> Use |A| */ | 2470 /* A<0 -> Use |A| */ |
2346 decContext *set) { | 2477 decContext *set) { |
2347 Int ae; /* adjusted exponent */ | 2478 Int ae; /* adjusted exponent */ |
2348 if (DFISNAN(df)) return decNaNs(result, df, NULL, set); | 2479 if (DFISNAN(df)) return decNaNs(result, df, NULL, set); |
2349 if (DFISINF(df)) { | 2480 if (DFISINF(df)) { |
2350 DFWORD(result, 0)=0; /* need +ve */ | 2481 DFWORD(result, 0)=0; /* need +ve */ |
2351 return decInfinity(result, result); /* canonical +Infinity */ | 2482 return decInfinity(result, result); /* canonical +Infinity */ |
2352 } | 2483 } |
2353 if (DFISZERO(df)) { | 2484 if (DFISZERO(df)) { |
2354 set->status|=DEC_Division_by_zero; /* as per 754r */ | 2485 set->status|=DEC_Division_by_zero; /* as per 754 */ |
2355 DFWORD(result, 0)=DECFLOAT_Sign; /* make negative */ | 2486 DFWORD(result, 0)=DECFLOAT_Sign; /* make negative */ |
2356 return decInfinity(result, result); /* canonical -Infinity */ | 2487 return decInfinity(result, result); /* canonical -Infinity */ |
2357 } | 2488 } |
2358 ae=GETEXPUN(df) /* get unbiased exponent .. */ | 2489 ae=GETEXPUN(df) /* get unbiased exponent .. */ |
2359 +decFloatDigits(df)-1; /* .. and make adjusted exponent */ | 2490 +decFloatDigits(df)-1; /* .. and make adjusted exponent */ |
2360 /* ae has limited range (3 digits for DOUBLE and 4 for QUAD), so */ | 2491 /* ae has limited range (3 digits for DOUBLE and 4 for QUAD), so */ |
2361 /* it is worth using a special case of decFloatFromInt32 */ | 2492 /* it is worth using a special case of decFloatFromInt32 */ |
2374 #endif | 2505 #endif |
2375 return result; | 2506 return result; |
2376 } /* decFloatLogB */ | 2507 } /* decFloatLogB */ |
2377 | 2508 |
2378 /* ------------------------------------------------------------------ */ | 2509 /* ------------------------------------------------------------------ */ |
2379 /* decFloatMax -- return maxnum of two operands */ | 2510 /* decFloatMax -- return maxnum of two operands */ |
2380 /* */ | 2511 /* */ |
2381 /* result gets the chosen decFloat */ | 2512 /* result gets the chosen decFloat */ |
2382 /* dfl is the first decFloat (lhs) */ | 2513 /* dfl is the first decFloat (lhs) */ |
2383 /* dfr is the second decFloat (rhs) */ | 2514 /* dfr is the second decFloat (rhs) */ |
2384 /* set is the context */ | 2515 /* set is the context */ |
2385 /* returns result */ | 2516 /* returns result */ |
2386 /* */ | 2517 /* */ |
2387 /* If just one operand is a quiet NaN it is ignored. */ | 2518 /* If just one operand is a quiet NaN it is ignored. */ |
2409 | 2540 |
2410 /* ------------------------------------------------------------------ */ | 2541 /* ------------------------------------------------------------------ */ |
2411 /* decFloatMaxMag -- return maxnummag of two operands */ | 2542 /* decFloatMaxMag -- return maxnummag of two operands */ |
2412 /* */ | 2543 /* */ |
2413 /* result gets the chosen decFloat */ | 2544 /* result gets the chosen decFloat */ |
2414 /* dfl is the first decFloat (lhs) */ | 2545 /* dfl is the first decFloat (lhs) */ |
2415 /* dfr is the second decFloat (rhs) */ | 2546 /* dfr is the second decFloat (rhs) */ |
2416 /* set is the context */ | 2547 /* set is the context */ |
2417 /* returns result */ | 2548 /* returns result */ |
2418 /* */ | 2549 /* */ |
2419 /* Returns according to the magnitude comparisons if both numeric and */ | 2550 /* Returns according to the magnitude comparisons if both numeric and */ |
2433 if (comp<0) return decCanonical(result, dfr); | 2564 if (comp<0) return decCanonical(result, dfr); |
2434 return decFloatMax(result, dfl, dfr, set); | 2565 return decFloatMax(result, dfl, dfr, set); |
2435 } /* decFloatMaxMag */ | 2566 } /* decFloatMaxMag */ |
2436 | 2567 |
2437 /* ------------------------------------------------------------------ */ | 2568 /* ------------------------------------------------------------------ */ |
2438 /* decFloatMin -- return minnum of two operands */ | 2569 /* decFloatMin -- return minnum of two operands */ |
2439 /* */ | 2570 /* */ |
2440 /* result gets the chosen decFloat */ | 2571 /* result gets the chosen decFloat */ |
2441 /* dfl is the first decFloat (lhs) */ | 2572 /* dfl is the first decFloat (lhs) */ |
2442 /* dfr is the second decFloat (rhs) */ | 2573 /* dfr is the second decFloat (rhs) */ |
2443 /* set is the context */ | 2574 /* set is the context */ |
2444 /* returns result */ | 2575 /* returns result */ |
2445 /* */ | 2576 /* */ |
2446 /* If just one operand is a quiet NaN it is ignored. */ | 2577 /* If just one operand is a quiet NaN it is ignored. */ |
2468 | 2599 |
2469 /* ------------------------------------------------------------------ */ | 2600 /* ------------------------------------------------------------------ */ |
2470 /* decFloatMinMag -- return minnummag of two operands */ | 2601 /* decFloatMinMag -- return minnummag of two operands */ |
2471 /* */ | 2602 /* */ |
2472 /* result gets the chosen decFloat */ | 2603 /* result gets the chosen decFloat */ |
2473 /* dfl is the first decFloat (lhs) */ | 2604 /* dfl is the first decFloat (lhs) */ |
2474 /* dfr is the second decFloat (rhs) */ | 2605 /* dfr is the second decFloat (rhs) */ |
2475 /* set is the context */ | 2606 /* set is the context */ |
2476 /* returns result */ | 2607 /* returns result */ |
2477 /* */ | 2608 /* */ |
2478 /* Returns according to the magnitude comparisons if both numeric and */ | 2609 /* Returns according to the magnitude comparisons if both numeric and */ |
2494 } /* decFloatMinMag */ | 2625 } /* decFloatMinMag */ |
2495 | 2626 |
2496 /* ------------------------------------------------------------------ */ | 2627 /* ------------------------------------------------------------------ */ |
2497 /* decFloatMinus -- negate value, heeding NaNs, etc. */ | 2628 /* decFloatMinus -- negate value, heeding NaNs, etc. */ |
2498 /* */ | 2629 /* */ |
2499 /* result gets the canonicalized 0-df */ | 2630 /* result gets the canonicalized 0-df */ |
2500 /* df is the decFloat to minus */ | 2631 /* df is the decFloat to minus */ |
2501 /* set is the context */ | 2632 /* set is the context */ |
2502 /* returns result */ | 2633 /* returns result */ |
2503 /* */ | 2634 /* */ |
2504 /* This has the same effect as 0-df where the exponent of the zero is */ | 2635 /* This has the same effect as 0-df where the exponent of the zero is */ |
2505 /* the same as that of df (if df is finite). */ | 2636 /* the same as that of df (if df is finite). */ |
2517 } /* decFloatMinus */ | 2648 } /* decFloatMinus */ |
2518 | 2649 |
2519 /* ------------------------------------------------------------------ */ | 2650 /* ------------------------------------------------------------------ */ |
2520 /* decFloatMultiply -- multiply two decFloats */ | 2651 /* decFloatMultiply -- multiply two decFloats */ |
2521 /* */ | 2652 /* */ |
2522 /* result gets the result of multiplying dfl and dfr: */ | 2653 /* result gets the result of multiplying dfl and dfr: */ |
2523 /* dfl is the first decFloat (lhs) */ | 2654 /* dfl is the first decFloat (lhs) */ |
2524 /* dfr is the second decFloat (rhs) */ | 2655 /* dfr is the second decFloat (rhs) */ |
2525 /* set is the context */ | 2656 /* set is the context */ |
2526 /* returns result */ | 2657 /* returns result */ |
2527 /* */ | 2658 /* */ |
2528 /* ------------------------------------------------------------------ */ | 2659 /* ------------------------------------------------------------------ */ |
2529 decFloat * decFloatMultiply(decFloat *result, | 2660 decFloat * decFloatMultiply(decFloat *result, |
2530 const decFloat *dfl, const decFloat *dfr, | 2661 const decFloat *dfl, const decFloat *dfr, |
2531 decContext *set) { | 2662 decContext *set) { |
2532 bcdnum num; /* for final conversion */ | 2663 bcdnum num; /* for final conversion */ |
2533 uByte bcdacc[DECPMAX9*18+1]; /* for coefficent in BCD */ | 2664 uByte bcdacc[DECPMAX9*18+1]; /* for coefficent in BCD */ |
2534 | 2665 |
2535 if (DFISSPECIAL(dfl) || DFISSPECIAL(dfr)) { /* either is special? */ | 2666 if (DFISSPECIAL(dfl) || DFISSPECIAL(dfr)) { /* either is special? */ |
2536 /* NaNs are handled as usual */ | 2667 /* NaNs are handled as usual */ |
2537 if (DFISNAN(dfl) || DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set); | 2668 if (DFISNAN(dfl) || DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set); |
2538 /* infinity times zero is bad */ | 2669 /* infinity times zero is bad */ |
2554 /* result gets the next lesser decFloat */ | 2685 /* result gets the next lesser decFloat */ |
2555 /* dfl is the decFloat to start with */ | 2686 /* dfl is the decFloat to start with */ |
2556 /* set is the context */ | 2687 /* set is the context */ |
2557 /* returns result */ | 2688 /* returns result */ |
2558 /* */ | 2689 /* */ |
2559 /* This is 754r nextdown; Invalid is the only status possible (from */ | 2690 /* This is 754 nextdown; Invalid is the only status possible (from */ |
2560 /* an sNaN). */ | 2691 /* an sNaN). */ |
2561 /* ------------------------------------------------------------------ */ | 2692 /* ------------------------------------------------------------------ */ |
2562 decFloat * decFloatNextMinus(decFloat *result, const decFloat *dfl, | 2693 decFloat * decFloatNextMinus(decFloat *result, const decFloat *dfl, |
2563 decContext *set) { | 2694 decContext *set) { |
2564 decFloat delta; /* tiny increment */ | 2695 decFloat delta; /* tiny increment */ |
2573 /* other cases are effected by sutracting a tiny delta -- this */ | 2704 /* other cases are effected by sutracting a tiny delta -- this */ |
2574 /* should be done in a wider format as the delta is unrepresentable */ | 2705 /* should be done in a wider format as the delta is unrepresentable */ |
2575 /* here (but can be done with normal add if the sign of zero is */ | 2706 /* here (but can be done with normal add if the sign of zero is */ |
2576 /* treated carefully, because no Inexactitude is interesting); */ | 2707 /* treated carefully, because no Inexactitude is interesting); */ |
2577 /* rounding to -Infinity then pushes the result to next below */ | 2708 /* rounding to -Infinity then pushes the result to next below */ |
2578 decFloatZero(&delta); /* set up tiny delta */ | 2709 decFloatZero(&delta); /* set up tiny delta */ |
2579 DFWORD(&delta, DECWORDS-1)=1; /* coefficient=1 */ | 2710 DFWORD(&delta, DECWORDS-1)=1; /* coefficient=1 */ |
2580 DFWORD(&delta, 0)=DECFLOAT_Sign; /* Sign=1 + biased exponent=0 */ | 2711 DFWORD(&delta, 0)=DECFLOAT_Sign; /* Sign=1 + biased exponent=0 */ |
2581 /* set up for the directional round */ | 2712 /* set up for the directional round */ |
2582 saveround=set->round; /* save mode */ | 2713 saveround=set->round; /* save mode */ |
2583 set->round=DEC_ROUND_FLOOR; /* .. round towards -Infinity */ | 2714 set->round=DEC_ROUND_FLOOR; /* .. round towards -Infinity */ |
2584 savestat=set->status; /* save status */ | 2715 savestat=set->status; /* save status */ |
2585 decFloatAdd(result, dfl, &delta, set); | 2716 decFloatAdd(result, dfl, &delta, set); |
2586 /* Add rules mess up the sign when going from +Ntiny to 0 */ | 2717 /* Add rules mess up the sign when going from +Ntiny to 0 */ |
2587 if (DFISZERO(result)) DFWORD(result, 0)^=DECFLOAT_Sign; /* correct */ | 2718 if (DFISZERO(result)) DFWORD(result, 0)^=DECFLOAT_Sign; /* correct */ |
2588 set->status&=DEC_Invalid_operation; /* preserve only sNaN status */ | 2719 set->status&=DEC_Invalid_operation; /* preserve only sNaN status */ |
2589 set->status|=savestat; /* restore pending flags */ | 2720 set->status|=savestat; /* restore pending flags */ |
2590 set->round=saveround; /* .. and mode */ | 2721 set->round=saveround; /* .. and mode */ |
2591 return result; | 2722 return result; |
2592 } /* decFloatNextMinus */ | 2723 } /* decFloatNextMinus */ |
2593 | 2724 |
2594 /* ------------------------------------------------------------------ */ | 2725 /* ------------------------------------------------------------------ */ |
2595 /* decFloatNextPlus -- next towards +Infinity */ | 2726 /* decFloatNextPlus -- next towards +Infinity */ |
2597 /* result gets the next larger decFloat */ | 2728 /* result gets the next larger decFloat */ |
2598 /* dfl is the decFloat to start with */ | 2729 /* dfl is the decFloat to start with */ |
2599 /* set is the context */ | 2730 /* set is the context */ |
2600 /* returns result */ | 2731 /* returns result */ |
2601 /* */ | 2732 /* */ |
2602 /* This is 754r nextup; Invalid is the only status possible (from */ | 2733 /* This is 754 nextup; Invalid is the only status possible (from */ |
2603 /* an sNaN). */ | 2734 /* an sNaN). */ |
2604 /* ------------------------------------------------------------------ */ | 2735 /* ------------------------------------------------------------------ */ |
2605 decFloat * decFloatNextPlus(decFloat *result, const decFloat *dfl, | 2736 decFloat * decFloatNextPlus(decFloat *result, const decFloat *dfl, |
2606 decContext *set) { | 2737 decContext *set) { |
2607 uInt savestat; /* saves status */ | 2738 uInt savestat; /* saves status */ |
2617 /* other cases are effected by sutracting a tiny delta -- this */ | 2748 /* other cases are effected by sutracting a tiny delta -- this */ |
2618 /* should be done in a wider format as the delta is unrepresentable */ | 2749 /* should be done in a wider format as the delta is unrepresentable */ |
2619 /* here (but can be done with normal add if the sign of zero is */ | 2750 /* here (but can be done with normal add if the sign of zero is */ |
2620 /* treated carefully, because no Inexactitude is interesting); */ | 2751 /* treated carefully, because no Inexactitude is interesting); */ |
2621 /* rounding to +Infinity then pushes the result to next above */ | 2752 /* rounding to +Infinity then pushes the result to next above */ |
2622 decFloatZero(&delta); /* set up tiny delta */ | 2753 decFloatZero(&delta); /* set up tiny delta */ |
2623 DFWORD(&delta, DECWORDS-1)=1; /* coefficient=1 */ | 2754 DFWORD(&delta, DECWORDS-1)=1; /* coefficient=1 */ |
2624 DFWORD(&delta, 0)=0; /* Sign=0 + biased exponent=0 */ | 2755 DFWORD(&delta, 0)=0; /* Sign=0 + biased exponent=0 */ |
2625 /* set up for the directional round */ | 2756 /* set up for the directional round */ |
2626 saveround=set->round; /* save mode */ | 2757 saveround=set->round; /* save mode */ |
2627 set->round=DEC_ROUND_CEILING; /* .. round towards +Infinity */ | 2758 set->round=DEC_ROUND_CEILING; /* .. round towards +Infinity */ |
2628 savestat=set->status; /* save status */ | 2759 savestat=set->status; /* save status */ |
2629 decFloatAdd(result, dfl, &delta, set); | 2760 decFloatAdd(result, dfl, &delta, set); |
2630 /* Add rules mess up the sign when going from -Ntiny to -0 */ | 2761 /* Add rules mess up the sign when going from -Ntiny to -0 */ |
2631 if (DFISZERO(result)) DFWORD(result, 0)^=DECFLOAT_Sign; /* correct */ | 2762 if (DFISZERO(result)) DFWORD(result, 0)^=DECFLOAT_Sign; /* correct */ |
2632 set->status&=DEC_Invalid_operation; /* preserve only sNaN status */ | 2763 set->status&=DEC_Invalid_operation; /* preserve only sNaN status */ |
2633 set->status|=savestat; /* restore pending flags */ | 2764 set->status|=savestat; /* restore pending flags */ |
2634 set->round=saveround; /* .. and mode */ | 2765 set->round=saveround; /* .. and mode */ |
2635 return result; | 2766 return result; |
2636 } /* decFloatNextPlus */ | 2767 } /* decFloatNextPlus */ |
2637 | 2768 |
2638 /* ------------------------------------------------------------------ */ | 2769 /* ------------------------------------------------------------------ */ |
2639 /* decFloatNextToward -- next towards a decFloat */ | 2770 /* decFloatNextToward -- next towards a decFloat */ |
2642 /* dfl is the decFloat to start with */ | 2773 /* dfl is the decFloat to start with */ |
2643 /* dfr is the decFloat to move toward */ | 2774 /* dfr is the decFloat to move toward */ |
2644 /* set is the context */ | 2775 /* set is the context */ |
2645 /* returns result */ | 2776 /* returns result */ |
2646 /* */ | 2777 /* */ |
2647 /* This is 754r nextafter; status may be set unless the result is a */ | 2778 /* This is 754-1985 nextafter, as modified during revision (dropped */ |
2648 /* normal number. */ | 2779 /* from 754-2008); status may be set unless the result is a normal */ |
2780 /* number. */ | |
2649 /* ------------------------------------------------------------------ */ | 2781 /* ------------------------------------------------------------------ */ |
2650 decFloat * decFloatNextToward(decFloat *result, | 2782 decFloat * decFloatNextToward(decFloat *result, |
2651 const decFloat *dfl, const decFloat *dfr, | 2783 const decFloat *dfl, const decFloat *dfr, |
2652 decContext *set) { | 2784 decContext *set) { |
2653 decFloat delta; /* tiny increment or decrement */ | 2785 decFloat delta; /* tiny increment or decrement */ |
2669 DFWORD(result, 0)|=DECFLOAT_Sign; | 2801 DFWORD(result, 0)|=DECFLOAT_Sign; |
2670 return result; | 2802 return result; |
2671 } | 2803 } |
2672 saveround=set->round; /* save mode */ | 2804 saveround=set->round; /* save mode */ |
2673 set->round=DEC_ROUND_CEILING; /* .. round towards +Infinity */ | 2805 set->round=DEC_ROUND_CEILING; /* .. round towards +Infinity */ |
2674 deltatop=0; /* positive delta */ | 2806 deltatop=0; /* positive delta */ |
2675 } | 2807 } |
2676 else { /* lhs>rhs, do NextMinus, see above for commentary */ | 2808 else { /* lhs>rhs, do NextMinus, see above for commentary */ |
2677 if (DFISINF(dfl) && !DFISSIGNED(dfl)) { /* +Infinity special case */ | 2809 if (DFISINF(dfl) && !DFISSIGNED(dfl)) { /* +Infinity special case */ |
2678 DFSETNMAX(result); | 2810 DFSETNMAX(result); |
2679 return result; | 2811 return result; |
2680 } | 2812 } |
2681 saveround=set->round; /* save mode */ | 2813 saveround=set->round; /* save mode */ |
2682 set->round=DEC_ROUND_FLOOR; /* .. round towards -Infinity */ | 2814 set->round=DEC_ROUND_FLOOR; /* .. round towards -Infinity */ |
2683 deltatop=DECFLOAT_Sign; /* negative delta */ | 2815 deltatop=DECFLOAT_Sign; /* negative delta */ |
2684 } | 2816 } |
2685 savestat=set->status; /* save status */ | 2817 savestat=set->status; /* save status */ |
2686 /* Here, Inexact is needed where appropriate (and hence Underflow, */ | 2818 /* Here, Inexact is needed where appropriate (and hence Underflow, */ |
2687 /* etc.). Therefore the tiny delta which is otherwise */ | 2819 /* etc.). Therefore the tiny delta which is otherwise */ |
2688 /* unrepresentable (see NextPlus and NextMinus) is constructed */ | 2820 /* unrepresentable (see NextPlus and NextMinus) is constructed */ |
2689 /* using the multiplication of FMA. */ | 2821 /* using the multiplication of FMA. */ |
2690 decFloatZero(&delta); /* set up tiny delta */ | 2822 decFloatZero(&delta); /* set up tiny delta */ |
2691 DFWORD(&delta, DECWORDS-1)=1; /* coefficient=1 */ | 2823 DFWORD(&delta, DECWORDS-1)=1; /* coefficient=1 */ |
2692 DFWORD(&delta, 0)=deltatop; /* Sign + biased exponent=0 */ | 2824 DFWORD(&delta, 0)=deltatop; /* Sign + biased exponent=0 */ |
2693 decFloatFromString(&pointone, "1E-1", set); /* set up multiplier */ | 2825 decFloatFromString(&pointone, "1E-1", set); /* set up multiplier */ |
2694 decFloatFMA(result, &delta, &pointone, dfl, set); | 2826 decFloatFMA(result, &delta, &pointone, dfl, set); |
2695 /* [Delta is truly tiny, so no need to correct sign of zero] */ | 2827 /* [Delta is truly tiny, so no need to correct sign of zero] */ |
2696 /* use new status unless the result is normal */ | 2828 /* use new status unless the result is normal */ |
2697 if (decFloatIsNormal(result)) set->status=savestat; /* else goes forward */ | 2829 if (decFloatIsNormal(result)) set->status=savestat; /* else goes forward */ |
2698 set->round=saveround; /* restore mode */ | 2830 set->round=saveround; /* restore mode */ |
2699 return result; | 2831 return result; |
2700 } /* decFloatNextToward */ | 2832 } /* decFloatNextToward */ |
2701 | 2833 |
2702 /* ------------------------------------------------------------------ */ | 2834 /* ------------------------------------------------------------------ */ |
2703 /* decFloatOr -- logical digitwise OR of two decFloats */ | 2835 /* decFloatOr -- logical digitwise OR of two decFloats */ |
2704 /* */ | 2836 /* */ |
2705 /* result gets the result of ORing dfl and dfr */ | 2837 /* result gets the result of ORing dfl and dfr */ |
2706 /* dfl is the first decFloat (lhs) */ | 2838 /* dfl is the first decFloat (lhs) */ |
2707 /* dfr is the second decFloat (rhs) */ | 2839 /* dfr is the second decFloat (rhs) */ |
2708 /* set is the context */ | 2840 /* set is the context */ |
2709 /* returns result, which will be canonical with sign=0 */ | 2841 /* returns result, which will be canonical with sign=0 */ |
2710 /* */ | 2842 /* */ |
2711 /* The operands must be positive, finite with exponent q=0, and */ | 2843 /* The operands must be positive, finite with exponent q=0, and */ |
2712 /* comprise just zeros and ones; if not, Invalid operation results. */ | 2844 /* comprise just zeros and ones; if not, Invalid operation results. */ |
2713 /* ------------------------------------------------------------------ */ | 2845 /* ------------------------------------------------------------------ */ |
2714 decFloat * decFloatOr(decFloat *result, | 2846 decFloat * decFloatOr(decFloat *result, |
2715 const decFloat *dfl, const decFloat *dfr, | 2847 const decFloat *dfl, const decFloat *dfr, |
2716 decContext *set) { | 2848 decContext *set) { |
2732 } /* decFloatOr */ | 2864 } /* decFloatOr */ |
2733 | 2865 |
2734 /* ------------------------------------------------------------------ */ | 2866 /* ------------------------------------------------------------------ */ |
2735 /* decFloatPlus -- add value to 0, heeding NaNs, etc. */ | 2867 /* decFloatPlus -- add value to 0, heeding NaNs, etc. */ |
2736 /* */ | 2868 /* */ |
2737 /* result gets the canonicalized 0+df */ | 2869 /* result gets the canonicalized 0+df */ |
2738 /* df is the decFloat to plus */ | 2870 /* df is the decFloat to plus */ |
2739 /* set is the context */ | 2871 /* set is the context */ |
2740 /* returns result */ | 2872 /* returns result */ |
2741 /* */ | 2873 /* */ |
2742 /* This has the same effect as 0+df where the exponent of the zero is */ | 2874 /* This has the same effect as 0+df where the exponent of the zero is */ |
2743 /* the same as that of df (if df is finite). */ | 2875 /* the same as that of df (if df is finite). */ |
2744 /* The effect is also the same as decFloatCopy except that NaNs */ | 2876 /* The effect is also the same as decFloatCopy except that NaNs */ |
2745 /* are handled normally (the sign of a NaN is not affected, and an */ | 2877 /* are handled normally (the sign of a NaN is not affected, and an */ |
2746 /* sNaN will signal), the result is canonical, and zero gets sign 0. */ | 2878 /* sNaN will signal), the result is canonical, and zero gets sign 0. */ |
2747 /* ------------------------------------------------------------------ */ | 2879 /* ------------------------------------------------------------------ */ |
2748 decFloat * decFloatPlus(decFloat *result, const decFloat *df, | 2880 decFloat * decFloatPlus(decFloat *result, const decFloat *df, |
2749 decContext *set) { | 2881 decContext *set) { |
2755 | 2887 |
2756 /* ------------------------------------------------------------------ */ | 2888 /* ------------------------------------------------------------------ */ |
2757 /* decFloatQuantize -- quantize a decFloat */ | 2889 /* decFloatQuantize -- quantize a decFloat */ |
2758 /* */ | 2890 /* */ |
2759 /* result gets the result of quantizing dfl to match dfr */ | 2891 /* result gets the result of quantizing dfl to match dfr */ |
2760 /* dfl is the first decFloat (lhs) */ | 2892 /* dfl is the first decFloat (lhs) */ |
2761 /* dfr is the second decFloat (rhs), which sets the exponent */ | 2893 /* dfr is the second decFloat (rhs), which sets the exponent */ |
2762 /* set is the context */ | 2894 /* set is the context */ |
2763 /* returns result */ | 2895 /* returns result */ |
2764 /* */ | 2896 /* */ |
2765 /* Unless there is an error or the result is infinite, the exponent */ | 2897 /* Unless there is an error or the result is infinite, the exponent */ |
2768 decFloat * decFloatQuantize(decFloat *result, | 2900 decFloat * decFloatQuantize(decFloat *result, |
2769 const decFloat *dfl, const decFloat *dfr, | 2901 const decFloat *dfl, const decFloat *dfr, |
2770 decContext *set) { | 2902 decContext *set) { |
2771 Int explb, exprb; /* left and right biased exponents */ | 2903 Int explb, exprb; /* left and right biased exponents */ |
2772 uByte *ulsd; /* local LSD pointer */ | 2904 uByte *ulsd; /* local LSD pointer */ |
2773 uInt *ui; /* work */ | 2905 uByte *ub, *uc; /* work */ |
2774 uByte *ub; /* .. */ | |
2775 Int drop; /* .. */ | 2906 Int drop; /* .. */ |
2776 uInt dpd; /* .. */ | 2907 uInt dpd; /* .. */ |
2777 uInt encode; /* encoding accumulator */ | 2908 uInt encode; /* encoding accumulator */ |
2778 uInt sourhil, sourhir; /* top words from source decFloats */ | 2909 uInt sourhil, sourhir; /* top words from source decFloats */ |
2910 uInt uiwork; /* for macros */ | |
2911 #if QUAD | |
2912 uShort uswork; /* .. */ | |
2913 #endif | |
2779 /* the following buffer holds the coefficient for manipulation */ | 2914 /* the following buffer holds the coefficient for manipulation */ |
2780 uByte buf[4+DECPMAX*3]; /* + space for zeros to left or right */ | 2915 uByte buf[4+DECPMAX*3+2*QUAD]; /* + space for zeros to left or right */ |
2781 #if DECTRACE | 2916 #if DECTRACE |
2782 bcdnum num; /* for trace displays */ | 2917 bcdnum num; /* for trace displays */ |
2783 #endif | 2918 #endif |
2784 | 2919 |
2785 /* Start decoding the arguments */ | 2920 /* Start decoding the arguments */ |
2786 sourhil=DFWORD(dfl, 0); /* LHS top word */ | 2921 sourhil=DFWORD(dfl, 0); /* LHS top word */ |
2787 explb=DECCOMBEXP[sourhil>>26]; /* get exponent high bits (in place) */ | 2922 explb=DECCOMBEXP[sourhil>>26]; /* get exponent high bits (in place) */ |
2820 num.exponent=explb-DECBIAS; | 2955 num.exponent=explb-DECBIAS; |
2821 num.sign=sourhil & DECFLOAT_Sign; | 2956 num.sign=sourhil & DECFLOAT_Sign; |
2822 decShowNum(&num, "dfl"); | 2957 decShowNum(&num, "dfl"); |
2823 #endif | 2958 #endif |
2824 | 2959 |
2825 if (drop>0) { /* [most common case] */ | 2960 if (drop>0) { /* [most common case] */ |
2826 /* (this code is very similar to that in decFloatFinalize, but */ | 2961 /* (this code is very similar to that in decFloatFinalize, but */ |
2827 /* has many differences so is duplicated here -- so any changes */ | 2962 /* has many differences so is duplicated here -- so any changes */ |
2828 /* may need to be made there, too) */ | 2963 /* may need to be made there, too) */ |
2829 uByte *roundat; /* -> re-round digit */ | 2964 uByte *roundat; /* -> re-round digit */ |
2830 uByte reround; /* reround value */ | 2965 uByte reround; /* reround value */ |
2831 /* printf("Rounding; drop=%ld\n", (LI)drop); */ | 2966 /* printf("Rounding; drop=%ld\n", (LI)drop); */ |
2832 | 2967 |
2833 /* there is at least one zero needed to the left, in all but one */ | 2968 /* there is at least one zero needed to the left, in all but one */ |
2834 /* exceptional (all-nines) case, so place four zeros now; this is */ | 2969 /* exceptional (all-nines) case, so place four zeros now; this is */ |
2835 /* needed almost always and makes rounding all-nines by fours safe */ | 2970 /* needed almost always and makes rounding all-nines by fours safe */ |
2836 UINTAT(BUFOFF-4)=0; | 2971 UBFROMUI(BUFOFF-4, 0); |
2837 | 2972 |
2838 /* Three cases here: */ | 2973 /* Three cases here: */ |
2839 /* 1. new LSD is in coefficient (almost always) */ | 2974 /* 1. new LSD is in coefficient (almost always) */ |
2840 /* 2. new LSD is digit to left of coefficient (so MSD is */ | 2975 /* 2. new LSD is digit to left of coefficient (so MSD is */ |
2841 /* round-for-reround digit) */ | 2976 /* round-for-reround digit) */ |
2842 /* 3. new LSD is to left of case 2 (whole coefficient is sticky) */ | 2977 /* 3. new LSD is to left of case 2 (whole coefficient is sticky) */ |
2843 /* Note that leading zeros can safely be treated as useful digits */ | 2978 /* Note that leading zeros can safely be treated as useful digits */ |
2844 | 2979 |
2845 /* [duplicate check-stickies code to save a test] */ | 2980 /* [duplicate check-stickies code to save a test] */ |
2846 /* [by-digit check for stickies as runs of zeros are rare] */ | 2981 /* [by-digit check for stickies as runs of zeros are rare] */ |
2847 if (drop<DECPMAX) { /* NB lengths not addresses */ | 2982 if (drop<DECPMAX) { /* NB lengths not addresses */ |
2848 roundat=BUFOFF+DECPMAX-drop; | 2983 roundat=BUFOFF+DECPMAX-drop; |
2849 reround=*roundat; | 2984 reround=*roundat; |
2850 for (ub=roundat+1; ub<BUFOFF+DECPMAX; ub++) { | 2985 for (ub=roundat+1; ub<BUFOFF+DECPMAX; ub++) { |
2851 if (*ub!=0) { /* non-zero to be discarded */ | 2986 if (*ub!=0) { /* non-zero to be discarded */ |
2852 reround=DECSTICKYTAB[reround]; /* apply sticky bit */ | 2987 reround=DECSTICKYTAB[reround]; /* apply sticky bit */ |
2925 | 3060 |
2926 if (bump!=0) { /* need increment */ | 3061 if (bump!=0) { /* need increment */ |
2927 /* increment the coefficient; this could give 1000... (after */ | 3062 /* increment the coefficient; this could give 1000... (after */ |
2928 /* the all nines case) */ | 3063 /* the all nines case) */ |
2929 ub=ulsd; | 3064 ub=ulsd; |
2930 for (; UINTAT(ub-3)==0x09090909; ub-=4) UINTAT(ub-3)=0; | 3065 for (; UBTOUI(ub-3)==0x09090909; ub-=4) UBFROMUI(ub-3, 0); |
2931 /* now at most 3 digits left to non-9 (usually just the one) */ | 3066 /* now at most 3 digits left to non-9 (usually just the one) */ |
2932 for (; *ub==9; ub--) *ub=0; | 3067 for (; *ub==9; ub--) *ub=0; |
2933 *ub+=1; | 3068 *ub+=1; |
2934 /* [the all-nines case will have carried one digit to the */ | 3069 /* [the all-nines case will have carried one digit to the */ |
2935 /* left of the original MSD -- just where it is needed] */ | 3070 /* left of the original MSD -- just where it is needed] */ |
2938 | 3073 |
2939 /* now clear zeros to the left so exactly DECPMAX digits will be */ | 3074 /* now clear zeros to the left so exactly DECPMAX digits will be */ |
2940 /* available in the coefficent -- the first word to the left was */ | 3075 /* available in the coefficent -- the first word to the left was */ |
2941 /* cleared earlier for safe carry; now add any more needed */ | 3076 /* cleared earlier for safe carry; now add any more needed */ |
2942 if (drop>4) { | 3077 if (drop>4) { |
2943 UINTAT(BUFOFF-8)=0; /* must be at least 5 */ | 3078 UBFROMUI(BUFOFF-8, 0); /* must be at least 5 */ |
2944 for (ui=&UINTAT(BUFOFF-12); ui>&UINTAT(ulsd-DECPMAX-3); ui--) *ui=0; | 3079 for (uc=BUFOFF-12; uc>ulsd-DECPMAX-3; uc-=4) UBFROMUI(uc, 0); |
2945 } | 3080 } |
2946 } /* need round (drop>0) */ | 3081 } /* need round (drop>0) */ |
2947 | 3082 |
2948 else { /* drop<0; padding with -drop digits is needed */ | 3083 else { /* drop<0; padding with -drop digits is needed */ |
2949 /* This is the case where an error can occur if the padded */ | 3084 /* This is the case where an error can occur if the padded */ |
2960 #if DECLITEND | 3095 #if DECLITEND |
2961 static const uInt dmask[]={0, 0x000000ff, 0x0000ffff, 0x00ffffff}; | 3096 static const uInt dmask[]={0, 0x000000ff, 0x0000ffff, 0x00ffffff}; |
2962 #else | 3097 #else |
2963 static const uInt dmask[]={0, 0xff000000, 0xffff0000, 0xffffff00}; | 3098 static const uInt dmask[]={0, 0xff000000, 0xffff0000, 0xffffff00}; |
2964 #endif | 3099 #endif |
2965 for (ui=&UINTAT(BUFOFF+DECPMAX);; ui++) { | 3100 /* note that here zeros to the right are added by fours, so in */ |
2966 *ui=0; | 3101 /* the Quad case this could write 36 zeros if the coefficient has */ |
2967 if (UINTAT(&UBYTEAT(ui)-DECPMAX)!=0) { /* could be bad */ | 3102 /* fewer than three significant digits (hence the +2*QUAD for buf) */ |
3103 for (uc=BUFOFF+DECPMAX;; uc+=4) { | |
3104 UBFROMUI(uc, 0); | |
3105 if (UBTOUI(uc-DECPMAX)!=0) { /* could be bad */ | |
2968 /* if all four digits should be zero, definitely bad */ | 3106 /* if all four digits should be zero, definitely bad */ |
2969 if (ui<=&UINTAT(BUFOFF+DECPMAX+(-drop)-4)) | 3107 if (uc<=BUFOFF+DECPMAX+(-drop)-4) |
2970 return decInvalid(result, set); | 3108 return decInvalid(result, set); |
2971 /* must be a 1- to 3-digit sequence; check more carefully */ | 3109 /* must be a 1- to 3-digit sequence; check more carefully */ |
2972 if ((UINTAT(&UBYTEAT(ui)-DECPMAX)&dmask[(-drop)%4])!=0) | 3110 if ((UBTOUI(uc-DECPMAX)&dmask[(-drop)%4])!=0) |
2973 return decInvalid(result, set); | 3111 return decInvalid(result, set); |
2974 break; /* no need for loop end test */ | 3112 break; /* no need for loop end test */ |
2975 } | 3113 } |
2976 if (ui>=&UINTAT(BUFOFF+DECPMAX+(-drop)-4)) break; /* done */ | 3114 if (uc>=BUFOFF+DECPMAX+(-drop)-4) break; /* done */ |
2977 } | 3115 } |
2978 ulsd=BUFOFF+DECPMAX+(-drop)-1; | 3116 ulsd=BUFOFF+DECPMAX+(-drop)-1; |
2979 } /* pad and check leading zeros */ | 3117 } /* pad and check leading zeros */ |
2980 } /* drop<0 */ | 3118 } /* drop<0 */ |
2981 | 3119 |
3038 | 3176 |
3039 /* ------------------------------------------------------------------ */ | 3177 /* ------------------------------------------------------------------ */ |
3040 /* decFloatReduce -- reduce finite coefficient to minimum length */ | 3178 /* decFloatReduce -- reduce finite coefficient to minimum length */ |
3041 /* */ | 3179 /* */ |
3042 /* result gets the reduced decFloat */ | 3180 /* result gets the reduced decFloat */ |
3043 /* df is the source decFloat */ | 3181 /* df is the source decFloat */ |
3044 /* set is the context */ | 3182 /* set is the context */ |
3045 /* returns result, which will be canonical */ | 3183 /* returns result, which will be canonical */ |
3046 /* */ | 3184 /* */ |
3047 /* This removes all possible trailing zeros from the coefficient; */ | 3185 /* This removes all possible trailing zeros from the coefficient; */ |
3048 /* some may remain when the number is very close to Nmax. */ | 3186 /* some may remain when the number is very close to Nmax. */ |
3078 | 3216 |
3079 /* ------------------------------------------------------------------ */ | 3217 /* ------------------------------------------------------------------ */ |
3080 /* decFloatRemainder -- integer divide and return remainder */ | 3218 /* decFloatRemainder -- integer divide and return remainder */ |
3081 /* */ | 3219 /* */ |
3082 /* result gets the remainder of dividing dfl by dfr: */ | 3220 /* result gets the remainder of dividing dfl by dfr: */ |
3083 /* dfl is the first decFloat (lhs) */ | 3221 /* dfl is the first decFloat (lhs) */ |
3084 /* dfr is the second decFloat (rhs) */ | 3222 /* dfr is the second decFloat (rhs) */ |
3085 /* set is the context */ | 3223 /* set is the context */ |
3086 /* returns result */ | 3224 /* returns result */ |
3087 /* */ | 3225 /* */ |
3088 /* ------------------------------------------------------------------ */ | 3226 /* ------------------------------------------------------------------ */ |
3094 | 3232 |
3095 /* ------------------------------------------------------------------ */ | 3233 /* ------------------------------------------------------------------ */ |
3096 /* decFloatRemainderNear -- integer divide to nearest and remainder */ | 3234 /* decFloatRemainderNear -- integer divide to nearest and remainder */ |
3097 /* */ | 3235 /* */ |
3098 /* result gets the remainder of dividing dfl by dfr: */ | 3236 /* result gets the remainder of dividing dfl by dfr: */ |
3099 /* dfl is the first decFloat (lhs) */ | 3237 /* dfl is the first decFloat (lhs) */ |
3100 /* dfr is the second decFloat (rhs) */ | 3238 /* dfr is the second decFloat (rhs) */ |
3101 /* set is the context */ | 3239 /* set is the context */ |
3102 /* returns result */ | 3240 /* returns result */ |
3103 /* */ | 3241 /* */ |
3104 /* This is the IEEE remainder, where the nearest integer is used. */ | 3242 /* This is the IEEE remainder, where the nearest integer is used. */ |
3138 uByte *ub; /* .. */ | 3276 uByte *ub; /* .. */ |
3139 | 3277 |
3140 if (DFISNAN(dfl)||DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set); | 3278 if (DFISNAN(dfl)||DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set); |
3141 if (!DFISINT(dfr)) return decInvalid(result, set); | 3279 if (!DFISINT(dfr)) return decInvalid(result, set); |
3142 digits=decFloatDigits(dfr); /* calculate digits */ | 3280 digits=decFloatDigits(dfr); /* calculate digits */ |
3143 if (digits>2) return decInvalid(result, set); /* definitely out of range */ | 3281 if (digits>2) return decInvalid(result, set); /* definitely out of range */ |
3144 rotate=DPD2BIN[DFWORD(dfr, DECWORDS-1)&0x3ff]; /* is in bottom declet */ | 3282 rotate=DPD2BIN[DFWORD(dfr, DECWORDS-1)&0x3ff]; /* is in bottom declet */ |
3145 if (rotate>DECPMAX) return decInvalid(result, set); /* too big */ | 3283 if (rotate>DECPMAX) return decInvalid(result, set); /* too big */ |
3146 /* [from here on no error or status change is possible] */ | 3284 /* [from here on no error or status change is possible] */ |
3147 if (DFISINF(dfl)) return decInfinity(result, dfl); /* canonical */ | 3285 if (DFISINF(dfl)) return decInfinity(result, dfl); /* canonical */ |
3148 /* handle no-rotate cases */ | 3286 /* handle no-rotate cases */ |
3171 } | 3309 } |
3172 /* fill in rest of num */ | 3310 /* fill in rest of num */ |
3173 num.lsd=num.msd+DECPMAX-1; | 3311 num.lsd=num.msd+DECPMAX-1; |
3174 num.sign=DFWORD(dfl, 0)&DECFLOAT_Sign; | 3312 num.sign=DFWORD(dfl, 0)&DECFLOAT_Sign; |
3175 num.exponent=GETEXPUN(dfl); | 3313 num.exponent=GETEXPUN(dfl); |
3176 savestat=set->status; /* record */ | 3314 savestat=set->status; /* record */ |
3177 decFinalize(result, &num, set); | 3315 decFinalize(result, &num, set); |
3178 set->status=savestat; /* restore */ | 3316 set->status=savestat; /* restore */ |
3179 return result; | 3317 return result; |
3180 } /* decFloatRotate */ | 3318 } /* decFloatRotate */ |
3181 | 3319 |
3182 /* ------------------------------------------------------------------ */ | 3320 /* ------------------------------------------------------------------ */ |
3183 /* decFloatSameQuantum -- test decFloats for same quantum */ | 3321 /* decFloatSameQuantum -- test decFloats for same quantum */ |
3184 /* */ | 3322 /* */ |
3185 /* dfl is the first decFloat (lhs) */ | 3323 /* dfl is the first decFloat (lhs) */ |
3186 /* dfr is the second decFloat (rhs) */ | 3324 /* dfr is the second decFloat (rhs) */ |
3187 /* returns 1 if the operands have the same quantum, 0 otherwise */ | 3325 /* returns 1 if the operands have the same quantum, 0 otherwise */ |
3188 /* */ | 3326 /* */ |
3189 /* No error is possible and no status results. */ | 3327 /* No error is possible and no status results. */ |
3190 /* ------------------------------------------------------------------ */ | 3328 /* ------------------------------------------------------------------ */ |
3197 if (GETEXP(dfl)==GETEXP(dfr)) return 1; /* biased exponents match */ | 3335 if (GETEXP(dfl)==GETEXP(dfr)) return 1; /* biased exponents match */ |
3198 return 0; | 3336 return 0; |
3199 } /* decFloatSameQuantum */ | 3337 } /* decFloatSameQuantum */ |
3200 | 3338 |
3201 /* ------------------------------------------------------------------ */ | 3339 /* ------------------------------------------------------------------ */ |
3202 /* decFloatScaleB -- multiply by a power of 10, as per 754r */ | 3340 /* decFloatScaleB -- multiply by a power of 10, as per 754 */ |
3203 /* */ | 3341 /* */ |
3204 /* result gets the result of the operation */ | 3342 /* result gets the result of the operation */ |
3205 /* dfl is the first decFloat (lhs) */ | 3343 /* dfl is the first decFloat (lhs) */ |
3206 /* dfr is the second decFloat (rhs), am integer (with q=0) */ | 3344 /* dfr is the second decFloat (rhs), am integer (with q=0) */ |
3207 /* set is the context */ | 3345 /* set is the context */ |
3208 /* returns result */ | 3346 /* returns result */ |
3209 /* */ | 3347 /* */ |
3210 /* This computes result=dfl x 10**dfr where dfr is an integer in the */ | 3348 /* This computes result=dfl x 10**dfr where dfr is an integer in the */ |
3211 /* range +/-2*(emax+pmax), typically resulting from LogB. */ | 3349 /* range +/-2*(emax+pmax), typically resulting from LogB. */ |
3222 if (DFISNAN(dfl)||DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set); | 3360 if (DFISNAN(dfl)||DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set); |
3223 if (!DFISINT(dfr)) return decInvalid(result, set); | 3361 if (!DFISINT(dfr)) return decInvalid(result, set); |
3224 digits=decFloatDigits(dfr); /* calculate digits */ | 3362 digits=decFloatDigits(dfr); /* calculate digits */ |
3225 | 3363 |
3226 #if DOUBLE | 3364 #if DOUBLE |
3227 if (digits>3) return decInvalid(result, set); /* definitely out of range */ | 3365 if (digits>3) return decInvalid(result, set); /* definitely out of range */ |
3228 expr=DPD2BIN[DFWORD(dfr, 1)&0x3ff]; /* must be in bottom declet */ | 3366 expr=DPD2BIN[DFWORD(dfr, 1)&0x3ff]; /* must be in bottom declet */ |
3229 #elif QUAD | 3367 #elif QUAD |
3230 if (digits>5) return decInvalid(result, set); /* definitely out of range */ | 3368 if (digits>5) return decInvalid(result, set); /* definitely out of range */ |
3231 expr=DPD2BIN[DFWORD(dfr, 3)&0x3ff] /* in bottom 2 declets .. */ | 3369 expr=DPD2BIN[DFWORD(dfr, 3)&0x3ff] /* in bottom 2 declets .. */ |
3232 +DPD2BIN[(DFWORD(dfr, 3)>>10)&0x3ff]*1000; /* .. */ | 3370 +DPD2BIN[(DFWORD(dfr, 3)>>10)&0x3ff]*1000; /* .. */ |
3233 #endif | 3371 #endif |
3234 if (expr>SCALEBMAX) return decInvalid(result, set); /* oops */ | 3372 if (expr>SCALEBMAX) return decInvalid(result, set); /* oops */ |
3235 /* [from now on no error possible] */ | 3373 /* [from now on no error possible] */ |
3236 if (DFISINF(dfl)) return decInfinity(result, dfl); /* canonical */ | 3374 if (DFISINF(dfl)) return decInfinity(result, dfl); /* canonical */ |
3237 if (DFISSIGNED(dfr)) expr=-expr; | 3375 if (DFISSIGNED(dfr)) expr=-expr; |
3238 /* dfl is finite and expr is valid */ | 3376 /* dfl is finite and expr is valid */ |
3239 *result=*dfl; /* copy to target */ | 3377 *result=*dfl; /* copy to target */ |
3240 return decFloatSetExponent(result, set, GETEXPUN(result)+expr); | 3378 return decFloatSetExponent(result, set, GETEXPUN(result)+expr); |
3241 } /* decFloatScaleB */ | 3379 } /* decFloatScaleB */ |
3242 | 3380 |
3243 /* ------------------------------------------------------------------ */ | 3381 /* ------------------------------------------------------------------ */ |
3244 /* decFloatShift -- shift the coefficient of a decFloat left or right */ | 3382 /* decFloatShift -- shift the coefficient of a decFloat left or right */ |
3259 /* operand is an sNaN. The result is canonical. */ | 3397 /* operand is an sNaN. The result is canonical. */ |
3260 /* ------------------------------------------------------------------ */ | 3398 /* ------------------------------------------------------------------ */ |
3261 decFloat * decFloatShift(decFloat *result, | 3399 decFloat * decFloatShift(decFloat *result, |
3262 const decFloat *dfl, const decFloat *dfr, | 3400 const decFloat *dfl, const decFloat *dfr, |
3263 decContext *set) { | 3401 decContext *set) { |
3264 Int shift; /* dfr as an Int */ | 3402 Int shift; /* dfr as an Int */ |
3265 uByte buf[DECPMAX*2]; /* coefficient + padding */ | 3403 uByte buf[DECPMAX*2]; /* coefficient + padding */ |
3266 uInt digits, savestat; /* work */ | 3404 uInt digits, savestat; /* work */ |
3267 bcdnum num; /* .. */ | 3405 bcdnum num; /* .. */ |
3406 uInt uiwork; /* for macros */ | |
3268 | 3407 |
3269 if (DFISNAN(dfl)||DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set); | 3408 if (DFISNAN(dfl)||DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set); |
3270 if (!DFISINT(dfr)) return decInvalid(result, set); | 3409 if (!DFISINT(dfr)) return decInvalid(result, set); |
3271 digits=decFloatDigits(dfr); /* calculate digits */ | 3410 digits=decFloatDigits(dfr); /* calculate digits */ |
3272 if (digits>2) return decInvalid(result, set); /* definitely out of range */ | 3411 if (digits>2) return decInvalid(result, set); /* definitely out of range */ |
3273 shift=DPD2BIN[DFWORD(dfr, DECWORDS-1)&0x3ff]; /* is in bottom declet */ | 3412 shift=DPD2BIN[DFWORD(dfr, DECWORDS-1)&0x3ff]; /* is in bottom declet */ |
3274 if (shift>DECPMAX) return decInvalid(result, set); /* too big */ | 3413 if (shift>DECPMAX) return decInvalid(result, set); /* too big */ |
3275 /* [from here on no error or status change is possible] */ | 3414 /* [from here on no error or status change is possible] */ |
3276 | 3415 |
3277 if (DFISINF(dfl)) return decInfinity(result, dfl); /* canonical */ | 3416 if (DFISINF(dfl)) return decInfinity(result, dfl); /* canonical */ |
3278 /* handle no-shift and all-shift (clear to zero) cases */ | 3417 /* handle no-shift and all-shift (clear to zero) cases */ |
3279 if (shift==0) return decCanonical(result, dfl); | 3418 if (shift==0) return decCanonical(result, dfl); |
3280 if (shift==DECPMAX) { /* zero with sign */ | 3419 if (shift==DECPMAX) { /* zero with sign */ |
3281 uByte sign=(uByte)(DFBYTE(dfl, 0)&0x80); /* save sign bit */ | 3420 uByte sign=(uByte)(DFBYTE(dfl, 0)&0x80); /* save sign bit */ |
3282 decFloatZero(result); /* make +0 */ | 3421 decFloatZero(result); /* make +0 */ |
3283 DFBYTE(result, 0)=(uByte)(DFBYTE(result, 0)|sign); /* and set sign */ | 3422 DFBYTE(result, 0)=(uByte)(DFBYTE(result, 0)|sign); /* and set sign */ |
3284 /* [cannot safely use CopySign] */ | 3423 /* [cannot safely use CopySign] */ |
3285 return result; | 3424 return result; |
3292 if (DFISSIGNED(dfr)) { /* shift right */ | 3431 if (DFISSIGNED(dfr)) { /* shift right */ |
3293 /* edge cases are taken care of, so this is easy */ | 3432 /* edge cases are taken care of, so this is easy */ |
3294 num.lsd=buf+DECPMAX-shift-1; | 3433 num.lsd=buf+DECPMAX-shift-1; |
3295 } | 3434 } |
3296 else { /* shift left -- zero padding needed to right */ | 3435 else { /* shift left -- zero padding needed to right */ |
3297 UINTAT(buf+DECPMAX)=0; /* 8 will handle most cases */ | 3436 UBFROMUI(buf+DECPMAX, 0); /* 8 will handle most cases */ |
3298 UINTAT(buf+DECPMAX+4)=0; /* .. */ | 3437 UBFROMUI(buf+DECPMAX+4, 0); /* .. */ |
3299 if (shift>8) memset(buf+DECPMAX+8, 0, 8+QUAD*18); /* all other cases */ | 3438 if (shift>8) memset(buf+DECPMAX+8, 0, 8+QUAD*18); /* all other cases */ |
3300 num.msd+=shift; | 3439 num.msd+=shift; |
3301 num.lsd=num.msd+DECPMAX-1; | 3440 num.lsd=num.msd+DECPMAX-1; |
3302 } | 3441 } |
3303 savestat=set->status; /* record */ | 3442 savestat=set->status; /* record */ |
3304 decFinalize(result, &num, set); | 3443 decFinalize(result, &num, set); |
3305 set->status=savestat; /* restore */ | 3444 set->status=savestat; /* restore */ |
3306 return result; | 3445 return result; |
3307 } /* decFloatShift */ | 3446 } /* decFloatShift */ |
3308 | 3447 |
3309 /* ------------------------------------------------------------------ */ | 3448 /* ------------------------------------------------------------------ */ |
3310 /* decFloatSubtract -- subtract a decFloat from another */ | 3449 /* decFloatSubtract -- subtract a decFloat from another */ |
3311 /* */ | 3450 /* */ |
3312 /* result gets the result of subtracting dfr from dfl: */ | 3451 /* result gets the result of subtracting dfr from dfl: */ |
3313 /* dfl is the first decFloat (lhs) */ | 3452 /* dfl is the first decFloat (lhs) */ |
3314 /* dfr is the second decFloat (rhs) */ | 3453 /* dfr is the second decFloat (rhs) */ |
3315 /* set is the context */ | 3454 /* set is the context */ |
3316 /* returns result */ | 3455 /* returns result */ |
3317 /* */ | 3456 /* */ |
3318 /* ------------------------------------------------------------------ */ | 3457 /* ------------------------------------------------------------------ */ |
3326 DFBYTE(&temp, 0)^=0x80; /* flip sign */ | 3465 DFBYTE(&temp, 0)^=0x80; /* flip sign */ |
3327 return decFloatAdd(result, dfl, &temp, set); /* and add to the lhs */ | 3466 return decFloatAdd(result, dfl, &temp, set); /* and add to the lhs */ |
3328 } /* decFloatSubtract */ | 3467 } /* decFloatSubtract */ |
3329 | 3468 |
3330 /* ------------------------------------------------------------------ */ | 3469 /* ------------------------------------------------------------------ */ |
3331 /* decFloatToInt -- round to 32-bit binary integer (4 flavours) */ | 3470 /* decFloatToInt -- round to 32-bit binary integer (4 flavours) */ |
3332 /* */ | 3471 /* */ |
3333 /* df is the decFloat to round */ | 3472 /* df is the decFloat to round */ |
3334 /* set is the context */ | 3473 /* set is the context */ |
3335 /* round is the rounding mode to use */ | 3474 /* round is the rounding mode to use */ |
3336 /* returns a uInt or an Int, rounded according to the name */ | 3475 /* returns a uInt or an Int, rounded according to the name */ |
3337 /* */ | 3476 /* */ |
3338 /* Invalid will always be signaled if df is a NaN, is Infinite, or is */ | 3477 /* Invalid will always be signaled if df is a NaN, is Infinite, or is */ |
3354 Int decFloatToInt32Exact(const decFloat *df, decContext *set, | 3493 Int decFloatToInt32Exact(const decFloat *df, decContext *set, |
3355 enum rounding round) { | 3494 enum rounding round) { |
3356 return (Int)decToInt32(df, set, round, 1, 0);} | 3495 return (Int)decToInt32(df, set, round, 1, 0);} |
3357 | 3496 |
3358 /* ------------------------------------------------------------------ */ | 3497 /* ------------------------------------------------------------------ */ |
3359 /* decFloatToIntegral -- round to integral value (two flavours) */ | 3498 /* decFloatToIntegral -- round to integral value (two flavours) */ |
3360 /* */ | 3499 /* */ |
3361 /* result gets the result */ | 3500 /* result gets the result */ |
3362 /* df is the decFloat to round */ | 3501 /* df is the decFloat to round */ |
3363 /* set is the context */ | 3502 /* set is the context */ |
3364 /* round is the rounding mode to use */ | 3503 /* round is the rounding mode to use */ |
3365 /* returns result */ | 3504 /* returns result */ |
3366 /* */ | 3505 /* */ |
3367 /* No exceptions, even Inexact, are raised except for sNaN input, or */ | 3506 /* No exceptions, even Inexact, are raised except for sNaN input, or */ |
3368 /* if 'Exact' appears in the name. */ | 3507 /* if 'Exact' appears in the name. */ |
3369 /* ------------------------------------------------------------------ */ | 3508 /* ------------------------------------------------------------------ */ |
3377 | 3516 |
3378 /* ------------------------------------------------------------------ */ | 3517 /* ------------------------------------------------------------------ */ |
3379 /* decFloatXor -- logical digitwise XOR of two decFloats */ | 3518 /* decFloatXor -- logical digitwise XOR of two decFloats */ |
3380 /* */ | 3519 /* */ |
3381 /* result gets the result of XORing dfl and dfr */ | 3520 /* result gets the result of XORing dfl and dfr */ |
3382 /* dfl is the first decFloat (lhs) */ | 3521 /* dfl is the first decFloat (lhs) */ |
3383 /* dfr is the second decFloat (rhs) */ | 3522 /* dfr is the second decFloat (rhs) */ |
3384 /* set is the context */ | 3523 /* set is the context */ |
3385 /* returns result, which will be canonical with sign=0 */ | 3524 /* returns result, which will be canonical with sign=0 */ |
3386 /* */ | 3525 /* */ |
3387 /* The operands must be positive, finite with exponent q=0, and */ | 3526 /* The operands must be positive, finite with exponent q=0, and */ |
3388 /* comprise just zeros and ones; if not, Invalid operation results. */ | 3527 /* comprise just zeros and ones; if not, Invalid operation results. */ |
3389 /* ------------------------------------------------------------------ */ | 3528 /* ------------------------------------------------------------------ */ |
3390 decFloat * decFloatXor(decFloat *result, | 3529 decFloat * decFloatXor(decFloat *result, |
3391 const decFloat *dfl, const decFloat *dfr, | 3530 const decFloat *dfl, const decFloat *dfr, |
3392 decContext *set) { | 3531 decContext *set) { |
3425 | 3564 |
3426 /* ------------------------------------------------------------------ */ | 3565 /* ------------------------------------------------------------------ */ |
3427 /* decInfinity -- set canonical Infinity with sign from a decFloat */ | 3566 /* decInfinity -- set canonical Infinity with sign from a decFloat */ |
3428 /* */ | 3567 /* */ |
3429 /* result gets a canonical Infinity */ | 3568 /* result gets a canonical Infinity */ |
3430 /* df is source decFloat (only the sign is used) */ | 3569 /* df is source decFloat (only the sign is used) */ |
3431 /* returns result */ | 3570 /* returns result */ |
3432 /* */ | 3571 /* */ |
3433 /* df may be the same as result */ | 3572 /* df may be the same as result */ |
3434 /* ------------------------------------------------------------------ */ | 3573 /* ------------------------------------------------------------------ */ |
3435 static decFloat *decInfinity(decFloat *result, const decFloat *df) { | 3574 static decFloat *decInfinity(decFloat *result, const decFloat *df) { |
3436 uInt sign=DFWORD(df, 0); /* save source signword */ | 3575 uInt sign=DFWORD(df, 0); /* save source signword */ |
3437 decFloatZero(result); /* clear everything */ | 3576 decFloatZero(result); /* clear everything */ |
3438 DFWORD(result, 0)=DECFLOAT_Inf | (sign & DECFLOAT_Sign); | 3577 DFWORD(result, 0)=DECFLOAT_Inf | (sign & DECFLOAT_Sign); |
3439 return result; | 3578 return result; |
3440 } /* decInfinity */ | 3579 } /* decInfinity */ |
3441 | 3580 |
3442 /* ------------------------------------------------------------------ */ | 3581 /* ------------------------------------------------------------------ */ |
3443 /* decNaNs -- handle NaN argument(s) */ | 3582 /* decNaNs -- handle NaN argument(s) */ |
3444 /* */ | 3583 /* */ |
3445 /* result gets the result of handling dfl and dfr, one or both of */ | 3584 /* result gets the result of handling dfl and dfr, one or both of */ |
3446 /* which is a NaN */ | 3585 /* which is a NaN */ |
3447 /* dfl is the first decFloat (lhs) */ | 3586 /* dfl is the first decFloat (lhs) */ |
3448 /* dfr is the second decFloat (rhs) -- may be NULL for a single- */ | 3587 /* dfr is the second decFloat (rhs) -- may be NULL for a single- */ |
3449 /* operand operation */ | 3588 /* operand operation */ |
3450 /* set is the context */ | 3589 /* set is the context */ |
3451 /* returns result */ | 3590 /* returns result */ |
3452 /* */ | 3591 /* */ |
3469 if (!DFISNAN(dfl)) dfl=dfr; /* RHS must be NaN, use it */ | 3608 if (!DFISNAN(dfl)) dfl=dfr; /* RHS must be NaN, use it */ |
3470 return decCanonical(result, dfl); /* propagate canonical qNaN */ | 3609 return decCanonical(result, dfl); /* propagate canonical qNaN */ |
3471 } /* decNaNs */ | 3610 } /* decNaNs */ |
3472 | 3611 |
3473 /* ------------------------------------------------------------------ */ | 3612 /* ------------------------------------------------------------------ */ |
3474 /* decNumCompare -- numeric comparison of two decFloats */ | 3613 /* decNumCompare -- numeric comparison of two decFloats */ |
3475 /* */ | 3614 /* */ |
3476 /* dfl is the left-hand decFloat, which is not a NaN */ | 3615 /* dfl is the left-hand decFloat, which is not a NaN */ |
3477 /* dfr is the right-hand decFloat, which is not a NaN */ | 3616 /* dfr is the right-hand decFloat, which is not a NaN */ |
3478 /* tot is 1 for total order compare, 0 for simple numeric */ | 3617 /* tot is 1 for total order compare, 0 for simple numeric */ |
3479 /* returns -1, 0, or +1 for dfl<dfr, dfl=dfr, dfl>dfr */ | 3618 /* returns -1, 0, or +1 for dfl<dfr, dfl=dfr, dfl>dfr */ |
3480 /* */ | 3619 /* */ |
3481 /* No error is possible; status and mode are unchanged. */ | 3620 /* No error is possible; status and mode are unchanged. */ |
3482 /* ------------------------------------------------------------------ */ | 3621 /* ------------------------------------------------------------------ */ |
3483 static Int decNumCompare(const decFloat *dfl, const decFloat *dfr, Flag tot) { | 3622 static Int decNumCompare(const decFloat *dfl, const decFloat *dfr, Flag tot) { |
3484 Int sigl, sigr; /* LHS and RHS non-0 signums */ | 3623 Int sigl, sigr; /* LHS and RHS non-0 signums */ |
3485 Int shift; /* shift needed to align operands */ | 3624 Int shift; /* shift needed to align operands */ |
3486 uByte *ub, *uc; /* work */ | 3625 uByte *ub, *uc; /* work */ |
3626 uInt uiwork; /* for macros */ | |
3487 /* buffers +2 if Quad (36 digits), need double plus 4 for safe padding */ | 3627 /* buffers +2 if Quad (36 digits), need double plus 4 for safe padding */ |
3488 uByte bufl[DECPMAX*2+QUAD*2+4]; /* for LHS coefficient + padding */ | 3628 uByte bufl[DECPMAX*2+QUAD*2+4]; /* for LHS coefficient + padding */ |
3489 uByte bufr[DECPMAX*2+QUAD*2+4]; /* for RHS coefficient + padding */ | 3629 uByte bufr[DECPMAX*2+QUAD*2+4]; /* for RHS coefficient + padding */ |
3490 | 3630 |
3491 sigl=1; | 3631 sigl=1; |
3505 | 3645 |
3506 /* signs are the same; operand(s) could be zero */ | 3646 /* signs are the same; operand(s) could be zero */ |
3507 sigr=-sigl; /* sign to return if abs(RHS) wins */ | 3647 sigr=-sigl; /* sign to return if abs(RHS) wins */ |
3508 | 3648 |
3509 if (DFISINF(dfl)) { | 3649 if (DFISINF(dfl)) { |
3510 if (DFISINF(dfr)) return 0; /* both infinite & same sign */ | 3650 if (DFISINF(dfr)) return 0; /* both infinite & same sign */ |
3511 return sigl; /* inf > n */ | 3651 return sigl; /* inf > n */ |
3512 } | 3652 } |
3513 if (DFISINF(dfr)) return sigr; /* n < inf [dfl is finite] */ | 3653 if (DFISINF(dfr)) return sigr; /* n < inf [dfl is finite] */ |
3514 | 3654 |
3515 /* here, both are same sign and finite; calculate their offset */ | 3655 /* here, both are same sign and finite; calculate their offset */ |
3537 } | 3677 } |
3538 | 3678 |
3539 /* decode the coefficients */ | 3679 /* decode the coefficients */ |
3540 /* (shift both right two if Quad to make a multiple of four) */ | 3680 /* (shift both right two if Quad to make a multiple of four) */ |
3541 #if QUAD | 3681 #if QUAD |
3542 ub=bufl; /* avoid type-pun violation */ | 3682 UBFROMUI(bufl, 0); |
3543 UINTAT(ub)=0; | 3683 UBFROMUI(bufr, 0); |
3544 uc=bufr; /* avoid type-pun violation */ | |
3545 UINTAT(uc)=0; | |
3546 #endif | 3684 #endif |
3547 GETCOEFF(dfl, bufl+QUAD*2); /* decode from decFloat */ | 3685 GETCOEFF(dfl, bufl+QUAD*2); /* decode from decFloat */ |
3548 GETCOEFF(dfr, bufr+QUAD*2); /* .. */ | 3686 GETCOEFF(dfr, bufr+QUAD*2); /* .. */ |
3549 if (shift==0) { /* aligned; common and easy */ | 3687 if (shift==0) { /* aligned; common and easy */ |
3550 /* all multiples of four, here */ | 3688 /* all multiples of four, here */ |
3551 for (ub=bufl, uc=bufr; ub<bufl+DECPMAX+QUAD*2; ub+=4, uc+=4) { | 3689 for (ub=bufl, uc=bufr; ub<bufl+DECPMAX+QUAD*2; ub+=4, uc+=4) { |
3552 if (UINTAT(ub)==UINTAT(uc)) continue; /* so far so same */ | 3690 uInt ui=UBTOUI(ub); |
3691 if (ui==UBTOUI(uc)) continue; /* so far so same */ | |
3553 /* about to find a winner; go by bytes in case little-endian */ | 3692 /* about to find a winner; go by bytes in case little-endian */ |
3554 for (;; ub++, uc++) { | 3693 for (;; ub++, uc++) { |
3555 if (*ub>*uc) return sigl; /* difference found */ | 3694 if (*ub>*uc) return sigl; /* difference found */ |
3556 if (*ub<*uc) return sigr; /* .. */ | 3695 if (*ub<*uc) return sigr; /* .. */ |
3557 } | 3696 } |
3558 } | 3697 } |
3559 } /* aligned */ | 3698 } /* aligned */ |
3560 else if (shift>0) { /* lhs to left */ | 3699 else if (shift>0) { /* lhs to left */ |
3561 ub=bufl; /* RHS pointer */ | 3700 ub=bufl; /* RHS pointer */ |
3562 /* pad bufl so right-aligned; most shifts will fit in 8 */ | 3701 /* pad bufl so right-aligned; most shifts will fit in 8 */ |
3563 UINTAT(bufl+DECPMAX+QUAD*2)=0; /* add eight zeros */ | 3702 UBFROMUI(bufl+DECPMAX+QUAD*2, 0); /* add eight zeros */ |
3564 UINTAT(bufl+DECPMAX+QUAD*2+4)=0; /* .. */ | 3703 UBFROMUI(bufl+DECPMAX+QUAD*2+4, 0); /* .. */ |
3565 if (shift>8) { | 3704 if (shift>8) { |
3566 /* more than eight; fill the rest, and also worth doing the */ | 3705 /* more than eight; fill the rest, and also worth doing the */ |
3567 /* lead-in by fours */ | 3706 /* lead-in by fours */ |
3568 uByte *up; /* work */ | 3707 uByte *up; /* work */ |
3569 uByte *upend=bufl+DECPMAX+QUAD*2+shift; | 3708 uByte *upend=bufl+DECPMAX+QUAD*2+shift; |
3570 for (up=bufl+DECPMAX+QUAD*2+8; up<upend; up+=4) UINTAT(up)=0; | 3709 for (up=bufl+DECPMAX+QUAD*2+8; up<upend; up+=4) UBFROMUI(up, 0); |
3571 /* [pads up to 36 in all for Quad] */ | 3710 /* [pads up to 36 in all for Quad] */ |
3572 for (;; ub+=4) { | 3711 for (;; ub+=4) { |
3573 if (UINTAT(ub)!=0) return sigl; | 3712 if (UBTOUI(ub)!=0) return sigl; |
3574 if (ub+4>bufl+shift-4) break; | 3713 if (ub+4>bufl+shift-4) break; |
3575 } | 3714 } |
3576 } | 3715 } |
3577 /* check remaining leading digits */ | 3716 /* check remaining leading digits */ |
3578 for (; ub<bufl+shift; ub++) if (*ub!=0) return sigl; | 3717 for (; ub<bufl+shift; ub++) if (*ub!=0) return sigl; |
3579 /* now start the overlapped part; bufl has been padded, so the */ | 3718 /* now start the overlapped part; bufl has been padded, so the */ |
3580 /* comparison can go for the full length of bufr, which is a */ | 3719 /* comparison can go for the full length of bufr, which is a */ |
3581 /* multiple of 4 bytes */ | 3720 /* multiple of 4 bytes */ |
3582 for (uc=bufr; ; uc+=4, ub+=4) { | 3721 for (uc=bufr; ; uc+=4, ub+=4) { |
3583 if (UINTAT(uc)!=UINTAT(ub)) { /* mismatch found */ | 3722 uInt ui=UBTOUI(ub); |
3723 if (ui!=UBTOUI(uc)) { /* mismatch found */ | |
3584 for (;; uc++, ub++) { /* check from left [little-endian?] */ | 3724 for (;; uc++, ub++) { /* check from left [little-endian?] */ |
3585 if (*ub>*uc) return sigl; /* difference found */ | 3725 if (*ub>*uc) return sigl; /* difference found */ |
3586 if (*ub<*uc) return sigr; /* .. */ | 3726 if (*ub<*uc) return sigr; /* .. */ |
3587 } | 3727 } |
3588 } /* mismatch */ | 3728 } /* mismatch */ |
3591 } /* shift>0 */ | 3731 } /* shift>0 */ |
3592 | 3732 |
3593 else { /* shift<0) .. RHS is to left of LHS; mirror shift>0 */ | 3733 else { /* shift<0) .. RHS is to left of LHS; mirror shift>0 */ |
3594 uc=bufr; /* RHS pointer */ | 3734 uc=bufr; /* RHS pointer */ |
3595 /* pad bufr so right-aligned; most shifts will fit in 8 */ | 3735 /* pad bufr so right-aligned; most shifts will fit in 8 */ |
3596 UINTAT(bufr+DECPMAX+QUAD*2)=0; /* add eight zeros */ | 3736 UBFROMUI(bufr+DECPMAX+QUAD*2, 0); /* add eight zeros */ |
3597 UINTAT(bufr+DECPMAX+QUAD*2+4)=0; /* .. */ | 3737 UBFROMUI(bufr+DECPMAX+QUAD*2+4, 0); /* .. */ |
3598 if (shift<-8) { | 3738 if (shift<-8) { |
3599 /* more than eight; fill the rest, and also worth doing the */ | 3739 /* more than eight; fill the rest, and also worth doing the */ |
3600 /* lead-in by fours */ | 3740 /* lead-in by fours */ |
3601 uByte *up; /* work */ | 3741 uByte *up; /* work */ |
3602 uByte *upend=bufr+DECPMAX+QUAD*2-shift; | 3742 uByte *upend=bufr+DECPMAX+QUAD*2-shift; |
3603 for (up=bufr+DECPMAX+QUAD*2+8; up<upend; up+=4) UINTAT(up)=0; | 3743 for (up=bufr+DECPMAX+QUAD*2+8; up<upend; up+=4) UBFROMUI(up, 0); |
3604 /* [pads up to 36 in all for Quad] */ | 3744 /* [pads up to 36 in all for Quad] */ |
3605 for (;; uc+=4) { | 3745 for (;; uc+=4) { |
3606 if (UINTAT(uc)!=0) return sigr; | 3746 if (UBTOUI(uc)!=0) return sigr; |
3607 if (uc+4>bufr-shift-4) break; | 3747 if (uc+4>bufr-shift-4) break; |
3608 } | 3748 } |
3609 } | 3749 } |
3610 /* check remaining leading digits */ | 3750 /* check remaining leading digits */ |
3611 for (; uc<bufr-shift; uc++) if (*uc!=0) return sigr; | 3751 for (; uc<bufr-shift; uc++) if (*uc!=0) return sigr; |
3612 /* now start the overlapped part; bufr has been padded, so the */ | 3752 /* now start the overlapped part; bufr has been padded, so the */ |
3613 /* comparison can go for the full length of bufl, which is a */ | 3753 /* comparison can go for the full length of bufl, which is a */ |
3614 /* multiple of 4 bytes */ | 3754 /* multiple of 4 bytes */ |
3615 for (ub=bufl; ; ub+=4, uc+=4) { | 3755 for (ub=bufl; ; ub+=4, uc+=4) { |
3616 if (UINTAT(ub)!=UINTAT(uc)) { /* mismatch found */ | 3756 uInt ui=UBTOUI(ub); |
3757 if (ui!=UBTOUI(uc)) { /* mismatch found */ | |
3617 for (;; ub++, uc++) { /* check from left [little-endian?] */ | 3758 for (;; ub++, uc++) { /* check from left [little-endian?] */ |
3618 if (*ub>*uc) return sigl; /* difference found */ | 3759 if (*ub>*uc) return sigl; /* difference found */ |
3619 if (*ub<*uc) return sigr; /* .. */ | 3760 if (*ub<*uc) return sigr; /* .. */ |
3620 } | 3761 } |
3621 } /* mismatch */ | 3762 } /* mismatch */ |
3632 } /* decNumCompare */ | 3773 } /* decNumCompare */ |
3633 | 3774 |
3634 /* ------------------------------------------------------------------ */ | 3775 /* ------------------------------------------------------------------ */ |
3635 /* decToInt32 -- local routine to effect ToInteger conversions */ | 3776 /* decToInt32 -- local routine to effect ToInteger conversions */ |
3636 /* */ | 3777 /* */ |
3637 /* df is the decFloat to convert */ | 3778 /* df is the decFloat to convert */ |
3638 /* set is the context */ | 3779 /* set is the context */ |
3639 /* rmode is the rounding mode to use */ | 3780 /* rmode is the rounding mode to use */ |
3640 /* exact is 1 if Inexact should be signalled */ | 3781 /* exact is 1 if Inexact should be signalled */ |
3641 /* unsign is 1 if the result a uInt, 0 if an Int (cast to uInt) */ | 3782 /* unsign is 1 if the result a uInt, 0 if an Int (cast to uInt) */ |
3642 /* returns 32-bit result as a uInt */ | 3783 /* returns 32-bit result as a uInt */ |
3643 /* */ | 3784 /* */ |
3644 /* Invalid is set is df is a NaN, is infinite, or is out-of-range; in */ | 3785 /* Invalid is set is df is a NaN, is infinite, or is out-of-range; in */ |
3645 /* these cases 0 is returned. */ | 3786 /* these cases 0 is returned. */ |
3646 /* ------------------------------------------------------------------ */ | 3787 /* ------------------------------------------------------------------ */ |
3647 static uInt decToInt32(const decFloat *df, decContext *set, | 3788 static uInt decToInt32(const decFloat *df, decContext *set, |
3648 enum rounding rmode, Flag exact, Flag unsign) { | 3789 enum rounding rmode, Flag exact, Flag unsign) { |
3649 Int exp; /* exponent */ | 3790 Int exp; /* exponent */ |
3650 uInt sourhi, sourpen, sourlo; /* top word from source decFloat .. */ | 3791 uInt sourhi, sourpen, sourlo; /* top word from source decFloat .. */ |
3651 uInt hi, lo; /* .. penultimate, least, etc. */ | 3792 uInt hi, lo; /* .. penultimate, least, etc. */ |
3652 decFloat zero, result; /* work */ | 3793 decFloat zero, result; /* work */ |
3653 Int i; /* .. */ | 3794 Int i; /* .. */ |
3654 | 3795 |
3655 /* Start decoding the argument */ | 3796 /* Start decoding the argument */ |
3656 sourhi=DFWORD(df, 0); /* top word */ | 3797 sourhi=DFWORD(df, 0); /* top word */ |
3657 exp=DECCOMBEXP[sourhi>>26]; /* get exponent high bits (in place) */ | 3798 exp=DECCOMBEXP[sourhi>>26]; /* get exponent high bits (in place) */ |
3658 if (EXPISSPECIAL(exp)) { /* is special? */ | 3799 if (EXPISSPECIAL(exp)) { /* is special? */ |
3659 set->status|=DEC_Invalid_operation; /* signal */ | 3800 set->status|=DEC_Invalid_operation; /* signal */ |
3660 return 0; | 3801 return 0; |
3661 } | 3802 } |
3723 | 3864 |
3724 /* ------------------------------------------------------------------ */ | 3865 /* ------------------------------------------------------------------ */ |
3725 /* decToIntegral -- local routine to effect ToIntegral value */ | 3866 /* decToIntegral -- local routine to effect ToIntegral value */ |
3726 /* */ | 3867 /* */ |
3727 /* result gets the result */ | 3868 /* result gets the result */ |
3728 /* df is the decFloat to round */ | 3869 /* df is the decFloat to round */ |
3729 /* set is the context */ | 3870 /* set is the context */ |
3730 /* rmode is the rounding mode to use */ | 3871 /* rmode is the rounding mode to use */ |
3731 /* exact is 1 if Inexact should be signalled */ | 3872 /* exact is 1 if Inexact should be signalled */ |
3732 /* returns result */ | 3873 /* returns result */ |
3733 /* ------------------------------------------------------------------ */ | 3874 /* ------------------------------------------------------------------ */ |
3734 static decFloat * decToIntegral(decFloat *result, const decFloat *df, | 3875 static decFloat * decToIntegral(decFloat *result, const decFloat *df, |
3735 decContext *set, enum rounding rmode, | 3876 decContext *set, enum rounding rmode, |
3736 Flag exact) { | 3877 Flag exact) { |
3739 enum rounding saveround; /* saver */ | 3880 enum rounding saveround; /* saver */ |
3740 uInt savestatus; /* .. */ | 3881 uInt savestatus; /* .. */ |
3741 decFloat zero; /* work */ | 3882 decFloat zero; /* work */ |
3742 | 3883 |
3743 /* Start decoding the argument */ | 3884 /* Start decoding the argument */ |
3744 sourhi=DFWORD(df, 0); /* top word */ | 3885 sourhi=DFWORD(df, 0); /* top word */ |
3745 exp=DECCOMBEXP[sourhi>>26]; /* get exponent high bits (in place) */ | 3886 exp=DECCOMBEXP[sourhi>>26]; /* get exponent high bits (in place) */ |
3746 | 3887 |
3747 if (EXPISSPECIAL(exp)) { /* is special? */ | 3888 if (EXPISSPECIAL(exp)) { /* is special? */ |
3748 /* NaNs are handled as usual */ | 3889 /* NaNs are handled as usual */ |
3749 if (DFISNAN(df)) return decNaNs(result, df, NULL, set); | 3890 if (DFISNAN(df)) return decNaNs(result, df, NULL, set); |
3755 /* complete extraction of the exponent */ | 3896 /* complete extraction of the exponent */ |
3756 exp+=GETECON(df)-DECBIAS; /* .. + continuation and unbias */ | 3897 exp+=GETECON(df)-DECBIAS; /* .. + continuation and unbias */ |
3757 | 3898 |
3758 if (exp>=0) return decCanonical(result, df); /* already integral */ | 3899 if (exp>=0) return decCanonical(result, df); /* already integral */ |
3759 | 3900 |
3760 saveround=set->round; /* save rounding mode .. */ | 3901 saveround=set->round; /* save rounding mode .. */ |
3761 savestatus=set->status; /* .. and status */ | 3902 savestatus=set->status; /* .. and status */ |
3762 set->round=rmode; /* set mode */ | 3903 set->round=rmode; /* set mode */ |
3763 decFloatZero(&zero); /* make 0E+0 */ | 3904 decFloatZero(&zero); /* make 0E+0 */ |
3764 decFloatQuantize(result, df, &zero, set); /* 'integrate'; cannot fail */ | 3905 decFloatQuantize(result, df, &zero, set); /* 'integrate'; cannot fail */ |
3765 set->round=saveround; /* restore rounding mode .. */ | 3906 set->round=saveround; /* restore rounding mode .. */ |
3766 if (!exact) set->status=savestatus; /* .. and status, unless exact */ | 3907 if (!exact) set->status=savestatus; /* .. and status, unless exact */ |
3767 return result; | 3908 return result; |
3768 } /* decToIntegral */ | 3909 } /* decToIntegral */ |