Skip to content
Myr

decBasic.c

This file

Path

vendor/decNumber/decBasic.c

3,908 lines · 179.5 KiB · C

Last changed by

Not known.

History of this file

Take it

1/* ------------------------------------------------------------------ */
2/* decBasic.c -- common base code for Basic decimal types             */
3/* ------------------------------------------------------------------ */
4/* Copyright (c) IBM Corporation, 2000, 2010.  All rights reserved.   */
5/*                                                                    */
6/* This software is made available under the terms of the             */
7/* ICU License -- ICU 1.8.1 and later.                                */
8/*                                                                    */
9/* The description and User's Guide ("The decNumber C Library") for   */
10/* this software is included in the package as decNumber.pdf.  This   */
11/* document is also available in HTML, together with specifications,  */
12/* testcases, and Web links, on the General Decimal Arithmetic page.  */
13/*                                                                    */
14/* Please send comments, suggestions, and corrections to the author:  */
15/*   mfc@uk.ibm.com                                                   */
16/*   Mike Cowlishaw, IBM Fellow                                       */
17/*   IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK         */
18/* ------------------------------------------------------------------ */
19/* This module comprises code that is shared between decDouble and    */
20/* decQuad (but not decSingle).  The main arithmetic operations are   */
21/* here (Add, Subtract, Multiply, FMA, and Division operators).       */
22/*                                                                    */
23/* Unlike decNumber, parameterization takes place at compile time     */
24/* rather than at runtime.  The parameters are set in the decDouble.c */
25/* (etc.) files, which then include this one to produce the compiled  */
26/* code.  The functions here, therefore, are code shared between      */
27/* multiple formats.                                                  */
28/*                                                                    */
29/* This must be included after decCommon.c.                           */
30/* ------------------------------------------------------------------ */
31// Names here refer to decFloat rather than to decDouble, etc., and
32// the functions are in strict alphabetical order.
33
34// The compile-time flags SINGLE, DOUBLE, and QUAD are set up in
35// decCommon.c
36#if !defined(QUAD)
37  #error decBasic.c must be included after decCommon.c
38#endif
39#if SINGLE
40  #error Routines in decBasic.c are for decDouble and decQuad only
41#endif
42
43/* Private constants */
44#define DIVIDE      0x80000000     // Divide operations [as flags]
45#define REMAINDER   0x40000000     // ..
46#define DIVIDEINT   0x20000000     // ..
47#define REMNEAR     0x10000000     // ..
48
49/* Private functions (local, used only by routines in this module) */
50static decFloat *decDivide(decFloat *, const decFloat *,
51                              const decFloat *, decContext *, uInt);
52static decFloat *decCanonical(decFloat *, const decFloat *);
53static void      decFiniteMultiply(bcdnum *, uByte *, const decFloat *,
54                              const decFloat *);
55static decFloat *decInfinity(decFloat *, const decFloat *);
56static decFloat *decInvalid(decFloat *, decContext *);
57static decFloat *decNaNs(decFloat *, const decFloat *, const decFloat *,
58                              decContext *);
59static Int       decNumCompare(const decFloat *, const decFloat *, Flag);
60static decFloat *decToIntegral(decFloat *, const decFloat *, decContext *,
61                              enum rounding, Flag);
62static uInt      decToInt32(const decFloat *, decContext *, enum rounding,
63                              Flag, Flag);
64
65/* ------------------------------------------------------------------ */
66/* decCanonical -- copy a decFloat, making canonical                  */
67/*                                                                    */
68/*   result gets the canonicalized df                                 */
69/*   df     is the decFloat to copy and make canonical                */
70/*   returns result                                                   */
71/*                                                                    */
72/* This is exposed via decFloatCanonical for Double and Quad only.    */
73/* This works on specials, too; no error or exception is possible.    */
74/* ------------------------------------------------------------------ */
75static decFloat * decCanonical(decFloat *result, const decFloat *df) {
76  uInt encode, precode, dpd;       // work
77  uInt inword, uoff, canon;        // ..
78  Int  n;                          // counter (down)
79  if (df!=result) *result=*df;     // effect copy if needed
80  if (DFISSPECIAL(result)) {
81    if (DFISINF(result)) return decInfinity(result, df); // clean Infinity
82    // is a NaN
83    DFWORD(result, 0)&=~ECONNANMASK;    // clear ECON except selector
84    if (DFISCCZERO(df)) return result;  // coefficient continuation is 0
85    // drop through to check payload
86    }
87  // return quickly if the coefficient continuation is canonical
88  { // declare block
89  #if DOUBLE
90    uInt sourhi=DFWORD(df, 0);
91    uInt sourlo=DFWORD(df, 1);
92    if (CANONDPDOFF(sourhi, 8)
93     && CANONDPDTWO(sourhi, sourlo, 30)
94     && CANONDPDOFF(sourlo, 20)
95     && CANONDPDOFF(sourlo, 10)
96     && CANONDPDOFF(sourlo, 0)) return result;
97  #elif QUAD
98    uInt sourhi=DFWORD(df, 0);
99    uInt sourmh=DFWORD(df, 1);
100    uInt sourml=DFWORD(df, 2);
101    uInt sourlo=DFWORD(df, 3);
102    if (CANONDPDOFF(sourhi, 4)
103     && CANONDPDTWO(sourhi, sourmh, 26)
104     && CANONDPDOFF(sourmh, 16)
105     && CANONDPDOFF(sourmh, 6)
106     && CANONDPDTWO(sourmh, sourml, 28)
107     && CANONDPDOFF(sourml, 18)
108     && CANONDPDOFF(sourml, 8)
109     && CANONDPDTWO(sourml, sourlo, 30)
110     && CANONDPDOFF(sourlo, 20)
111     && CANONDPDOFF(sourlo, 10)
112     && CANONDPDOFF(sourlo, 0)) return result;
113  #endif
114  } // block
115
116  // Loop to repair a non-canonical coefficent, as needed
117  inword=DECWORDS-1;               // current input word
118  uoff=0;                          // bit offset of declet
119  encode=DFWORD(result, inword);
120  for (n=DECLETS-1; n>=0; n--) {   // count down declets of 10 bits
121    dpd=encode>>uoff;
122    uoff+=10;
123    if (uoff>32) {                 // crossed uInt boundary
124      inword--;
125      encode=DFWORD(result, inword);
126      uoff-=32;
127      dpd|=encode<<(10-uoff);      // get pending bits
128      }
129    dpd&=0x3ff;                    // clear uninteresting bits
130    if (dpd<0x16e) continue;       // must be canonical
131    canon=BIN2DPD[DPD2BIN[dpd]];   // determine canonical declet
132    if (canon==dpd) continue;      // have canonical declet
133    // need to replace declet
134    if (uoff>=10) {                // all within current word
135      encode&=~(0x3ff<<(uoff-10)); // clear the 10 bits ready for replace
136      encode|=canon<<(uoff-10);    // insert the canonical form
137      DFWORD(result, inword)=encode;    // .. and save
138      continue;
139      }
140    // straddled words
141    precode=DFWORD(result, inword+1);   // get previous
142    precode&=0xffffffff>>(10-uoff);     // clear top bits
143    DFWORD(result, inword+1)=precode|(canon<<(32-(10-uoff)));
144    encode&=0xffffffff<<uoff;           // clear bottom bits
145    encode|=canon>>(10-uoff);           // insert canonical
146    DFWORD(result, inword)=encode;      // .. and save
147    } // n
148  return result;
149  } // decCanonical
150
151/* ------------------------------------------------------------------ */
152/* decDivide -- divide operations                                     */
153/*                                                                    */
154/*   result gets the result of dividing dfl by dfr:                   */
155/*   dfl    is the first decFloat (lhs)                               */
156/*   dfr    is the second decFloat (rhs)                              */
157/*   set    is the context                                            */
158/*   op     is the operation selector                                 */
159/*   returns result                                                   */
160/*                                                                    */
161/* op is one of DIVIDE, REMAINDER, DIVIDEINT, or REMNEAR.             */
162/* ------------------------------------------------------------------ */
163#define DIVCOUNT  0                // 1 to instrument subtractions counter
164#define DIVBASE   ((uInt)BILLION)  // the base used for divide
165#define DIVOPLEN  DECPMAX9         // operand length ('digits' base 10**9)
166#define DIVACCLEN (DIVOPLEN*3)     // accumulator length (ditto)
167static decFloat * decDivide(decFloat *result, const decFloat *dfl,
168                            const decFloat *dfr, decContext *set, uInt op) {
169  decFloat quotient;               // for remainders
170  bcdnum num;                      // for final conversion
171  uInt   acc[DIVACCLEN];           // coefficent in base-billion ..
172  uInt   div[DIVOPLEN];            // divisor in base-billion ..
173  uInt   quo[DIVOPLEN+1];          // quotient in base-billion ..
174  uByte  bcdacc[(DIVOPLEN+1)*9+2]; // for quotient in BCD, +1, +1
175  uInt   *msua, *msud, *msuq;      // -> msu of acc, div, and quo
176  Int    divunits, accunits;       // lengths
177  Int    quodigits;                // digits in quotient
178  uInt   *lsua, *lsuq;             // -> current acc and quo lsus
179  Int    length, multiplier;       // work
180  uInt   carry, sign;              // ..
181  uInt   *ua, *ud, *uq;            // ..
182  uByte  *ub;                      // ..
183  uInt   uiwork;                   // for macros
184  uInt   divtop;                   // top unit of div adjusted for estimating
185  #if DIVCOUNT
186  static uInt maxcount=0;          // worst-seen subtractions count
187  uInt   divcount=0;               // subtractions count [this divide]
188  #endif
189
190  // calculate sign
191  num.sign=(DFWORD(dfl, 0)^DFWORD(dfr, 0)) & DECFLOAT_Sign;
192
193  if (DFISSPECIAL(dfl) || DFISSPECIAL(dfr)) { // either is special?
194    // NaNs are handled as usual
195    if (DFISNAN(dfl) || DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set);
196    // one or two infinities
197    if (DFISINF(dfl)) {
198      if (DFISINF(dfr)) return decInvalid(result, set); // Two infinities bad
199      if (op&(REMAINDER|REMNEAR)) return decInvalid(result, set); // as is rem
200      // Infinity/x is infinite and quiet, even if x=0
201      DFWORD(result, 0)=num.sign;
202      return decInfinity(result, result);
203      }
204    // must be x/Infinity -- remainders are lhs
205    if (op&(REMAINDER|REMNEAR)) return decCanonical(result, dfl);
206    // divides: return zero with correct sign and exponent depending
207    // on op (Etiny for divide, 0 for divideInt)
208    decFloatZero(result);
209    if (op==DIVIDEINT) DFWORD(result, 0)|=num.sign; // add sign
210     else DFWORD(result, 0)=num.sign;        // zeros the exponent, too
211    return result;
212    }
213  // next, handle zero operands (x/0 and 0/x)
214  if (DFISZERO(dfr)) {                       // x/0
215    if (DFISZERO(dfl)) {                     // 0/0 is undefined
216      decFloatZero(result);
217      DFWORD(result, 0)=DECFLOAT_qNaN;
218      set->status|=DEC_Division_undefined;
219      return result;
220      }
221    if (op&(REMAINDER|REMNEAR)) return decInvalid(result, set); // bad rem
222    set->status|=DEC_Division_by_zero;
223    DFWORD(result, 0)=num.sign;
224    return decInfinity(result, result);      // x/0 -> signed Infinity
225    }
226  num.exponent=GETEXPUN(dfl)-GETEXPUN(dfr);  // ideal exponent
227  if (DFISZERO(dfl)) {                       // 0/x (x!=0)
228    // if divide, result is 0 with ideal exponent; divideInt has
229    // exponent=0, remainders give zero with lower exponent
230    if (op&DIVIDEINT) {
231      decFloatZero(result);
232      DFWORD(result, 0)|=num.sign;           // add sign
233      return result;
234      }
235    if (!(op&DIVIDE)) {                      // a remainder
236      // exponent is the minimum of the operands
237      num.exponent=MINI(GETEXPUN(dfl), GETEXPUN(dfr));
238      // if the result is zero the sign shall be sign of dfl
239      num.sign=DFWORD(dfl, 0)&DECFLOAT_Sign;
240      }
241    bcdacc[0]=0;
242    num.msd=bcdacc;                          // -> 0
243    num.lsd=bcdacc;                          // ..
244    return decFinalize(result, &num, set);   // [divide may clamp exponent]
245    } // 0/x
246  // [here, both operands are known to be finite and non-zero]
247
248  // extract the operand coefficents into 'units' which are
249  // base-billion; the lhs is high-aligned in acc and the msu of both
250  // acc and div is at the right-hand end of array (offset length-1);
251  // the quotient can need one more unit than the operands as digits
252  // in it are not necessarily aligned neatly; further, the quotient
253  // may not start accumulating until after the end of the initial
254  // operand in acc if that is small (e.g., 1) so the accumulator
255  // must have at least that number of units extra (at the ls end)
256  GETCOEFFBILL(dfl, acc+DIVACCLEN-DIVOPLEN);
257  GETCOEFFBILL(dfr, div);
258  // zero the low uInts of acc
259  acc[0]=0;
260  acc[1]=0;
261  acc[2]=0;
262  acc[3]=0;
263  #if DOUBLE
264    #if DIVOPLEN!=2
265      #error Unexpected Double DIVOPLEN
266    #endif
267  #elif QUAD
268  acc[4]=0;
269  acc[5]=0;
270  acc[6]=0;
271  acc[7]=0;
272    #if DIVOPLEN!=4
273      #error Unexpected Quad DIVOPLEN
274    #endif
275  #endif
276
277  // set msu and lsu pointers
278  msua=acc+DIVACCLEN-1;       // [leading zeros removed below]
279  msuq=quo+DIVOPLEN;
280  //[loop for div will terminate because operands are non-zero]
281  for (msud=div+DIVOPLEN-1; *msud==0;) msud--;
282  // the initial least-significant unit of acc is set so acc appears
283  // to have the same length as div.
284  // This moves one position towards the least possible for each
285  // iteration
286  divunits=(Int)(msud-div+1); // precalculate
287  lsua=msua-divunits+1;       // initial working lsu of acc
288  lsuq=msuq;                  // and of quo
289
290  // set up the estimator for the multiplier; this is the msu of div,
291  // plus two bits from the unit below (if any) rounded up by one if
292  // there are any non-zero bits or units below that [the extra two
293  // bits makes for a much better estimate when the top unit is small]
294  divtop=*msud<<2;
295  if (divunits>1) {
296    uInt *um=msud-1;
297    uInt d=*um;
298    if (d>=750000000) {divtop+=3; d-=750000000;}
299     else if (d>=500000000) {divtop+=2; d-=500000000;}
300     else if (d>=250000000) {divtop++; d-=250000000;}
301    if (d) divtop++;
302     else for (um--; um>=div; um--) if (*um) {
303      divtop++;
304      break;
305      }
306    } // >1 unit
307
308  #if DECTRACE
309  {Int i;
310  printf("----- div=");
311  for (i=divunits-1; i>=0; i--) printf("%09ld ", (LI)div[i]);
312  printf("\n");}
313  #endif
314
315  // now collect up to DECPMAX+1 digits in the quotient (this may
316  // need OPLEN+1 uInts if unaligned)
317  quodigits=0;                // no digits yet
318  for (;; lsua--) {           // outer loop -- each input position
319    #if DECCHECK
320    if (lsua<acc) {
321      printf("Acc underrun...\n");
322      break;
323      }
324    #endif
325    #if DECTRACE
326    printf("Outer: quodigits=%ld acc=", (LI)quodigits);
327    for (ua=msua; ua>=lsua; ua--) printf("%09ld ", (LI)*ua);
328    printf("\n");
329    #endif
330    *lsuq=0;                  // default unit result is 0
331    for (;;) {                // inner loop -- calculate quotient unit
332      // strip leading zero units from acc (either there initially or
333      // from subtraction below); this may strip all if exactly 0
334      for (; *msua==0 && msua>=lsua;) msua--;
335      accunits=(Int)(msua-lsua+1);                // [maybe 0]
336      // subtraction is only necessary and possible if there are as
337      // least as many units remaining in acc for this iteration as
338      // there are in div
339      if (accunits<divunits) {
340        if (accunits==0) msua++;                  // restore
341        break;
342        }
343
344      // If acc is longer than div then subtraction is definitely
345      // possible (as msu of both is non-zero), but if they are the
346      // same length a comparison is needed.
347      // If a subtraction is needed then a good estimate of the
348      // multiplier for the subtraction is also needed in order to
349      // minimise the iterations of this inner loop because the
350      // subtractions needed dominate division performance.
351      if (accunits==divunits) {
352        // compare the high divunits of acc and div:
353        // acc<div:  this quotient unit is unchanged; subtraction
354        //           will be possible on the next iteration
355        // acc==div: quotient gains 1, set acc=0
356        // acc>div:  subtraction necessary at this position
357        for (ud=msud, ua=msua; ud>div; ud--, ua--) if (*ud!=*ua) break;
358        // [now at first mismatch or lsu]
359        if (*ud>*ua) break;                       // next time...
360        if (*ud==*ua) {                           // all compared equal
361          *lsuq+=1;                               // increment result
362          msua=lsua;                              // collapse acc units
363          *msua=0;                                // .. to a zero
364          break;
365          }
366
367        // subtraction necessary; estimate multiplier [see above]
368        // if both *msud and *msua are small it is cost-effective to
369        // bring in part of the following units (if any) to get a
370        // better estimate (assume some other non-zero in div)
371        #define DIVLO 1000000U
372        #define DIVHI (DIVBASE/DIVLO)
373        #if DECUSE64
374          if (divunits>1) {
375            // there cannot be a *(msud-2) for DECDOUBLE so next is
376            // an exact calculation unless DECQUAD (which needs to
377            // assume bits out there if divunits>2)
378            uLong mul=(uLong)*msua * DIVBASE + *(msua-1);
379            uLong div=(uLong)*msud * DIVBASE + *(msud-1);
380            #if QUAD
381            if (divunits>2) div++;
382            #endif
383            mul/=div;
384            multiplier=(Int)mul;
385            }
386           else multiplier=*msua/(*msud);
387        #else
388          if (divunits>1 && *msua<DIVLO && *msud<DIVLO) {
389            multiplier=(*msua*DIVHI + *(msua-1)/DIVLO)
390                      /(*msud*DIVHI + *(msud-1)/DIVLO +1);
391            }
392           else multiplier=(*msua<<2)/divtop;
393        #endif
394        }
395       else {                                     // accunits>divunits
396        // msud is one unit 'lower' than msua, so estimate differently
397        #if DECUSE64
398          uLong mul;
399          // as before, bring in extra digits if possible
400          if (divunits>1 && *msua<DIVLO && *msud<DIVLO) {
401            mul=((uLong)*msua * DIVHI * DIVBASE) + *(msua-1) * DIVHI
402               + *(msua-2)/DIVLO;
403            mul/=(*msud*DIVHI + *(msud-1)/DIVLO +1);
404            }
405           else if (divunits==1) {
406            mul=(uLong)*msua * DIVBASE + *(msua-1);
407            mul/=*msud;       // no more to the right
408            }
409           else {
410            mul=(uLong)(*msua) * (uInt)(DIVBASE<<2)
411                + (*(msua-1)<<2);
412            mul/=divtop;      // [divtop already allows for sticky bits]
413            }
414          multiplier=(Int)mul;
415        #else
416          multiplier=*msua * ((DIVBASE<<2)/divtop);
417        #endif
418        }
419      if (multiplier==0) multiplier=1;            // marginal case
420      *lsuq+=multiplier;
421
422      #if DIVCOUNT
423      // printf("Multiplier: %ld\n", (LI)multiplier);
424      divcount++;
425      #endif
426
427      // Carry out the subtraction  acc-(div*multiplier); for each
428      // unit in div, do the multiply, split to units (see
429      // decFloatMultiply for the algorithm), and subtract from acc
430      #define DIVMAGIC  2305843009U               // 2**61/10**9
431      #define DIVSHIFTA 29
432      #define DIVSHIFTB 32
433      carry=0;
434      for (ud=div, ua=lsua; ud<=msud; ud++, ua++) {
435        uInt lo, hop;
436        #if DECUSE64
437          uLong sub=(uLong)multiplier*(*ud)+carry;
438          if (sub<DIVBASE) {
439            carry=0;
440            lo=(uInt)sub;
441            }
442           else {
443            hop=(uInt)(sub>>DIVSHIFTA);
444            carry=(uInt)(((uLong)hop*DIVMAGIC)>>DIVSHIFTB);
445            // the estimate is now in hi; now calculate sub-hi*10**9
446            // to get the remainder (which will be <DIVBASE))
447            lo=(uInt)sub;
448            lo-=carry*DIVBASE;                    // low word of result
449            if (lo>=DIVBASE) {
450              lo-=DIVBASE;                        // correct by +1
451              carry++;
452              }
453            }
454        #else // 32-bit
455          uInt hi;
456          // calculate multiplier*(*ud) into hi and lo
457          LONGMUL32HI(hi, *ud, multiplier);       // get the high word
458          lo=multiplier*(*ud);                    // .. and the low
459          lo+=carry;                              // add the old hi
460          carry=hi+(lo<carry);                    // .. with any carry
461          if (carry || lo>=DIVBASE) {             // split is needed
462            hop=(carry<<3)+(lo>>DIVSHIFTA);       // hi:lo/2**29
463            LONGMUL32HI(carry, hop, DIVMAGIC);    // only need the high word
464            // [DIVSHIFTB is 32, so carry can be used directly]
465            // the estimate is now in carry; now calculate hi:lo-est*10**9;
466            // happily the top word of the result is irrelevant because it
467            // will always be zero so this needs only one multiplication
468            lo-=(carry*DIVBASE);
469            // the correction here will be at most +1; do it
470            if (lo>=DIVBASE) {
471              lo-=DIVBASE;
472              carry++;
473              }
474            }
475        #endif
476        if (lo>*ua) {              // borrow needed
477          *ua+=DIVBASE;
478          carry++;
479          }
480        *ua-=lo;
481        } // ud loop
482      if (carry) *ua-=carry;       // accdigits>divdigits [cannot borrow]
483      } // inner loop
484
485    // the outer loop terminates when there is either an exact result
486    // or enough digits; first update the quotient digit count and
487    // pointer (if any significant digits)
488    #if DECTRACE
489    if (*lsuq || quodigits) printf("*lsuq=%09ld\n", (LI)*lsuq);
490    #endif
491    if (quodigits) {
492      quodigits+=9;                // had leading unit earlier
493      lsuq--;
494      if (quodigits>DECPMAX+1) break;   // have enough
495      }
496     else if (*lsuq) {             // first quotient digits
497      const uInt *pow;
498      for (pow=DECPOWERS; *lsuq>=*pow; pow++) quodigits++;
499      lsuq--;
500      // [cannot have >DECPMAX+1 on first unit]
501      }
502
503    if (*msua!=0) continue;        // not an exact result
504    // acc is zero iff used all of original units and zero down to lsua
505    // (must also continue to original lsu for correct quotient length)
506    if (lsua>acc+DIVACCLEN-DIVOPLEN) continue;
507    for (; msua>lsua && *msua==0;) msua--;
508    if (*msua==0 && msua==lsua) break;
509    } // outer loop
510
511  // all of the original operand in acc has been covered at this point
512  // quotient now has at least DECPMAX+2 digits
513  // *msua is now non-0 if inexact and sticky bits
514  // lsuq is one below the last uint of the quotient
515  lsuq++;                          // set -> true lsu of quo
516  if (*msua) *lsuq|=1;             // apply sticky bit
517
518  // quo now holds the (unrounded) quotient in base-billion; one
519  // base-billion 'digit' per uInt.
520  #if DECTRACE
521  printf("DivQuo:");
522  for (uq=msuq; uq>=lsuq; uq--) printf(" %09ld", (LI)*uq);
523  printf("\n");
524  #endif
525
526  // Now convert to BCD for rounding and cleanup, starting from the
527  // most significant end [offset by one into bcdacc to leave room
528  // for a possible carry digit if rounding for REMNEAR is needed]
529  for (uq=msuq, ub=bcdacc+1; uq>=lsuq; uq--, ub+=9) {
530    uInt top, mid, rem;                 // work
531    if (*uq==0) {                       // no split needed
532      UBFROMUI(ub, 0);                  // clear 9 BCD8s
533      UBFROMUI(ub+4, 0);                // ..
534      *(ub+8)=0;                        // ..
535      continue;
536      }
537    // *uq is non-zero -- split the base-billion digit into
538    // hi, mid, and low three-digits
539    #define divsplit9 1000000           // divisor
540    #define divsplit6 1000              // divisor
541    // The splitting is done by simple divides and remainders,
542    // assuming the compiler will optimize these [GCC does]
543    top=*uq/divsplit9;
544    rem=*uq%divsplit9;
545    mid=rem/divsplit6;
546    rem=rem%divsplit6;
547    // lay out the nine BCD digits (plus one unwanted byte)
548    UBFROMUI(ub,   UBTOUI(&BIN2BCD8[top*4]));
549    UBFROMUI(ub+3, UBTOUI(&BIN2BCD8[mid*4]));
550    UBFROMUI(ub+6, UBTOUI(&BIN2BCD8[rem*4]));
551    } // BCD conversion loop
552  ub--;                                 // -> lsu
553
554  // complete the bcdnum; quodigits is correct, so the position of
555  // the first non-zero is known
556  num.msd=bcdacc+1+(msuq-lsuq+1)*9-quodigits;
557  num.lsd=ub;
558
559  // make exponent adjustments, etc
560  if (lsua<acc+DIVACCLEN-DIVOPLEN) {    // used extra digits
561    num.exponent-=(Int)((acc+DIVACCLEN-DIVOPLEN-lsua)*9);
562    // if the result was exact then there may be up to 8 extra
563    // trailing zeros in the overflowed quotient final unit
564    if (*msua==0) {
565      for (; *ub==0;) ub--;             // drop zeros
566      num.exponent+=(Int)(num.lsd-ub);  // and adjust exponent
567      num.lsd=ub;
568      }
569    } // adjustment needed
570
571  #if DIVCOUNT
572  if (divcount>maxcount) {              // new high-water nark
573    maxcount=divcount;
574    printf("DivNewMaxCount: %ld\n", (LI)maxcount);
575    }
576  #endif
577
578  if (op&DIVIDE) return decFinalize(result, &num, set); // all done
579
580  // Is DIVIDEINT or a remainder; there is more to do -- first form
581  // the integer (this is done 'after the fact', unlike as in
582  // decNumber, so as not to tax DIVIDE)
583
584  // The first non-zero digit will be in the first 9 digits, known
585  // from quodigits and num.msd, so there is always space for DECPMAX
586  // digits
587
588  length=(Int)(num.lsd-num.msd+1);
589  //printf("Length exp: %ld %ld\n", (LI)length, (LI)num.exponent);
590
591  if (length+num.exponent>DECPMAX) { // cannot fit
592    decFloatZero(result);
593    DFWORD(result, 0)=DECFLOAT_qNaN;
594    set->status|=DEC_Division_impossible;
595    return result;
596    }
597
598  if (num.exponent>=0) {           // already an int, or need pad zeros
599    for (ub=num.lsd+1; ub<=num.lsd+num.exponent; ub++) *ub=0;
600    num.lsd+=num.exponent;
601    }
602   else {                          // too long: round or truncate needed
603    Int drop=-num.exponent;
604    if (!(op&REMNEAR)) {           // simple truncate
605      num.lsd-=drop;
606      if (num.lsd<num.msd) {       // truncated all
607        num.lsd=num.msd;           // make 0
608        *num.lsd=0;                // .. [sign still relevant]
609        }
610      }
611     else {                        // round to nearest even [sigh]
612      // round-to-nearest, in-place; msd is at or to right of bcdacc+1
613      // (this is a special case of Quantize -- q.v. for commentary)
614      uByte *roundat;              // -> re-round digit
615      uByte reround;               // reround value
616      *(num.msd-1)=0;              // in case of left carry, or make 0
617      if (drop<length) roundat=num.lsd-drop+1;
618       else if (drop==length) roundat=num.msd;
619       else roundat=num.msd-1;     // [-> 0]
620      reround=*roundat;
621      for (ub=roundat+1; ub<=num.lsd; ub++) {
622        if (*ub!=0) {
623          reround=DECSTICKYTAB[reround];
624          break;
625          }
626        } // check stickies
627      if (roundat>num.msd) num.lsd=roundat-1;
628       else {
629        num.msd--;                           // use the 0 ..
630        num.lsd=num.msd;                     // .. at the new MSD place
631        }
632      if (reround!=0) {                      // discarding non-zero
633        uInt bump=0;
634        // rounding is DEC_ROUND_HALF_EVEN always
635        if (reround>5) bump=1;               // >0.5 goes up
636         else if (reround==5)                // exactly 0.5000 ..
637          bump=*(num.lsd) & 0x01;            // .. up iff [new] lsd is odd
638        if (bump!=0) {                       // need increment
639          // increment the coefficient; this might end up with 1000...
640          ub=num.lsd;
641          for (; UBTOUI(ub-3)==0x09090909; ub-=4) UBFROMUI(ub-3, 0);
642          for (; *ub==9; ub--) *ub=0;        // at most 3 more
643          *ub+=1;
644          if (ub<num.msd) num.msd--;         // carried
645          } // bump needed
646        } // reround!=0
647      } // remnear
648    } // round or truncate needed
649  num.exponent=0;                            // all paths
650  //decShowNum(&num, "int");
651
652  if (op&DIVIDEINT) return decFinalize(result, &num, set); // all done
653
654  // Have a remainder to calculate
655  decFinalize(&quotient, &num, set);         // lay out the integer so far
656  DFWORD(&quotient, 0)^=DECFLOAT_Sign;       // negate it
657  sign=DFWORD(dfl, 0);                       // save sign of dfl
658  decFloatFMA(result, &quotient, dfr, dfl, set);
659  if (!DFISZERO(result)) return result;
660  // if the result is zero the sign shall be sign of dfl
661  DFWORD(&quotient, 0)=sign;                 // construct decFloat of sign
662  return decFloatCopySign(result, result, &quotient);
663  } // decDivide
664
665/* ------------------------------------------------------------------ */
666/* decFiniteMultiply -- multiply two finite decFloats                 */
667/*                                                                    */
668/*   num    gets the result of multiplying dfl and dfr                */
669/*   bcdacc .. with the coefficient in this array                     */
670/*   dfl    is the first decFloat (lhs)                               */
671/*   dfr    is the second decFloat (rhs)                              */
672/*                                                                    */
673/* This effects the multiplication of two decFloats, both known to be */
674/* finite, leaving the result in a bcdnum ready for decFinalize (for  */
675/* use in Multiply) or in a following addition (FMA).                 */
676/*                                                                    */
677/* bcdacc must have space for at least DECPMAX9*18+1 bytes.           */
678/* No error is possible and no status is set.                         */
679/* ------------------------------------------------------------------ */
680// This routine has two separate implementations of the core
681// multiplication; both using base-billion.  One uses only 32-bit
682// variables (Ints and uInts) or smaller; the other uses uLongs (for
683// multiplication and addition only).  Both implementations cover
684// both arithmetic sizes (DOUBLE and QUAD) in order to allow timing
685// comparisons.  In any one compilation only one implementation for
686// each size can be used, and if DECUSE64 is 0 then use of the 32-bit
687// version is forced.
688//
689// Historical note: an earlier version of this code also supported the
690// 256-bit format and has been preserved.  That is somewhat trickier
691// during lazy carry splitting because the initial quotient estimate
692// (est) can exceed 32 bits.
693
694#define MULTBASE  ((uInt)BILLION)  // the base used for multiply
695#define MULOPLEN  DECPMAX9         // operand length ('digits' base 10**9)
696#define MULACCLEN (MULOPLEN*2)              // accumulator length (ditto)
697#define LEADZEROS (MULACCLEN*9 - DECPMAX*2) // leading zeros always
698
699// Assertions: exponent not too large and MULACCLEN is a multiple of 4
700#if DECEMAXD>9
701  #error Exponent may overflow when doubled for Multiply
702#endif
703#if MULACCLEN!=(MULACCLEN/4)*4
704  // This assumption is used below only for initialization
705  #error MULACCLEN is not a multiple of 4
706#endif
707
708static void decFiniteMultiply(bcdnum *num, uByte *bcdacc,
709                              const decFloat *dfl, const decFloat *dfr) {
710  uInt   bufl[MULOPLEN];           // left  coefficient (base-billion)
711  uInt   bufr[MULOPLEN];           // right coefficient (base-billion)
712  uInt   *ui, *uj;                 // work
713  uByte  *ub;                      // ..
714  uInt   uiwork;                   // for macros
715
716  #if DECUSE64
717  uLong  accl[MULACCLEN];          // lazy accumulator (base-billion+)
718  uLong  *pl;                      // work -> lazy accumulator
719  uInt   acc[MULACCLEN];           // coefficent in base-billion ..
720  #else
721  uInt   acc[MULACCLEN*2];         // accumulator in base-billion ..
722  #endif
723  uInt   *pa;                      // work -> accumulator
724  //printf("Base10**9: OpLen=%d MulAcclen=%d\n", OPLEN, MULACCLEN);
725
726  /* Calculate sign and exponent */
727  num->sign=(DFWORD(dfl, 0)^DFWORD(dfr, 0)) & DECFLOAT_Sign;
728  num->exponent=GETEXPUN(dfl)+GETEXPUN(dfr); // [see assertion above]
729
730  /* Extract the coefficients and prepare the accumulator */
731  // the coefficients of the operands are decoded into base-billion
732  // numbers in uInt arrays (bufl and bufr, LSD at offset 0) of the
733  // appropriate size.
734  GETCOEFFBILL(dfl, bufl);
735  GETCOEFFBILL(dfr, bufr);
736  #if DECTRACE && 0
737    printf("CoeffbL:");
738    for (ui=bufl+MULOPLEN-1; ui>=bufl; ui--) printf(" %08lx", (LI)*ui);
739    printf("\n");
740    printf("CoeffbR:");
741    for (uj=bufr+MULOPLEN-1; uj>=bufr; uj--) printf(" %08lx", (LI)*uj);
742    printf("\n");
743  #endif
744
745  // start the 64-bit/32-bit differing paths...
746#if DECUSE64
747
748  // zero the accumulator
749  #if MULACCLEN==4
750    accl[0]=0; accl[1]=0; accl[2]=0; accl[3]=0;
751  #else                                      // use a loop
752    // MULACCLEN is a multiple of four, asserted above
753    for (pl=accl; pl<accl+MULACCLEN; pl+=4) {
754      *pl=0; *(pl+1)=0; *(pl+2)=0; *(pl+3)=0;// [reduce overhead]
755      } // pl
756  #endif
757
758  /* Effect the multiplication */
759  // The multiplication proceeds using MFC's lazy-carry resolution
760  // algorithm from decNumber.  First, the multiplication is
761  // effected, allowing accumulation of the partial products (which
762  // are in base-billion at each column position) into 64 bits
763  // without resolving back to base=billion after each addition.
764  // These 64-bit numbers (which may contain up to 19 decimal digits)
765  // are then split using the Clark & Cowlishaw algorithm (see below).
766  // [Testing for 0 in the inner loop is not really a 'win']
767  for (ui=bufr; ui<bufr+MULOPLEN; ui++) { // over each item in rhs
768    if (*ui==0) continue;                 // product cannot affect result
769    pl=accl+(ui-bufr);                    // where to add the lhs
770    for (uj=bufl; uj<bufl+MULOPLEN; uj++, pl++) { // over each item in lhs
771      // if (*uj==0) continue;            // product cannot affect result
772      *pl+=((uLong)*ui)*(*uj);
773      } // uj
774    } // ui
775
776  // The 64-bit carries must now be resolved; this means that a
777  // quotient/remainder has to be calculated for base-billion (1E+9).
778  // For this, Clark & Cowlishaw's quotient estimation approach (also
779  // used in decNumber) is needed, because 64-bit divide is generally
780  // extremely slow on 32-bit machines, and may be slower than this
781  // approach even on 64-bit machines.  This algorithm splits X
782  // using:
783  //
784  //   magic=2**(A+B)/1E+9;   // 'magic number'
785  //   hop=X/2**A;            // high order part of X (by shift)
786  //   est=magic*hop/2**B     // quotient estimate (may be low by 1)
787  //
788  // A and B are quite constrained; hop and magic must fit in 32 bits,
789  // and 2**(A+B) must be as large as possible (which is 2**61 if
790  // magic is to fit).  Further, maxX increases with the length of
791  // the operands (and hence the number of partial products
792  // accumulated); maxX is OPLEN*(10**18), which is up to 19 digits.
793  //
794  // It can be shown that when OPLEN is 2 then the maximum error in
795  // the estimated quotient is <1, but for larger maximum x the
796  // maximum error is above 1 so a correction that is >1 may be
797  // needed.  Values of A and B are chosen to satisfy the constraints
798  // just mentioned while minimizing the maximum error (and hence the
799  // maximum correction), as shown in the following table:
800  //
801  //   Type    OPLEN   A   B     maxX    maxError  maxCorrection
802  //   ---------------------------------------------------------
803  //   DOUBLE    2    29  32  <2*10**18    0.63       1
804  //   QUAD      4    30  31  <4*10**18    1.17       2
805  //
806  // In the OPLEN==2 case there is most choice, but the value for B
807  // of 32 has a big advantage as then the calculation of the
808  // estimate requires no shifting; the compiler can extract the high
809  // word directly after multiplying magic*hop.
810  #define MULMAGIC 2305843009U          // 2**61/10**9  [both cases]
811  #if DOUBLE
812    #define MULSHIFTA 29
813    #define MULSHIFTB 32
814  #elif QUAD
815    #define MULSHIFTA 30
816    #define MULSHIFTB 31
817  #else
818    #error Unexpected type
819  #endif
820
821  #if DECTRACE
822  printf("MulAccl:");
823  for (pl=accl+MULACCLEN-1; pl>=accl; pl--)
824    printf(" %08lx:%08lx", (LI)(*pl>>32), (LI)(*pl&0xffffffff));
825  printf("\n");
826  #endif
827
828  for (pl=accl, pa=acc; pl<accl+MULACCLEN; pl++, pa++) { // each column position
829    uInt lo, hop;                       // work
830    uInt est;                           // cannot exceed 4E+9
831    if (*pl>=MULTBASE) {
832      // *pl holds a binary number which needs to be split
833      hop=(uInt)(*pl>>MULSHIFTA);
834      est=(uInt)(((uLong)hop*MULMAGIC)>>MULSHIFTB);
835      // the estimate is now in est; now calculate hi:lo-est*10**9;
836      // happily the top word of the result is irrelevant because it
837      // will always be zero so this needs only one multiplication
838      lo=(uInt)(*pl-((uLong)est*MULTBASE));  // low word of result
839      // If QUAD, the correction here could be +2
840      if (lo>=MULTBASE) {
841        lo-=MULTBASE;                   // correct by +1
842        est++;
843        #if QUAD
844        // may need to correct by +2
845        if (lo>=MULTBASE) {
846          lo-=MULTBASE;
847          est++;
848          }
849        #endif
850        }
851      // finally place lo as the new coefficient 'digit' and add est to
852      // the next place up [this is safe because this path is never
853      // taken on the final iteration as *pl will fit]
854      *pa=lo;
855      *(pl+1)+=est;
856      } // *pl needed split
857     else {                             // *pl<MULTBASE
858      *pa=(uInt)*pl;                    // just copy across
859      }
860    } // pl loop
861
862#else  // 32-bit
863  for (pa=acc;; pa+=4) {                     // zero the accumulator
864    *pa=0; *(pa+1)=0; *(pa+2)=0; *(pa+3)=0;  // [reduce overhead]
865    if (pa==acc+MULACCLEN*2-4) break;        // multiple of 4 asserted
866    } // pa
867
868  /* Effect the multiplication */
869  // uLongs are not available (and in particular, there is no uLong
870  // divide) but it is still possible to use MFC's lazy-carry
871  // resolution algorithm from decNumber.  First, the multiplication
872  // is effected, allowing accumulation of the partial products
873  // (which are in base-billion at each column position) into 64 bits
874  // [with the high-order 32 bits in each position being held at
875  // offset +ACCLEN from the low-order 32 bits in the accumulator].
876  // These 64-bit numbers (which may contain up to 19 decimal digits)
877  // are then split using the Clark & Cowlishaw algorithm (see
878  // below).
879  for (ui=bufr;; ui++) {                // over each item in rhs
880    uInt hi, lo;                        // words of exact multiply result
881    pa=acc+(ui-bufr);                   // where to add the lhs
882    for (uj=bufl;; uj++, pa++) {        // over each item in lhs
883      LONGMUL32HI(hi, *ui, *uj);        // calculate product of digits
884      lo=(*ui)*(*uj);                   // ..
885      *pa+=lo;                          // accumulate low bits and ..
886      *(pa+MULACCLEN)+=hi+(*pa<lo);     // .. high bits with any carry
887      if (uj==bufl+MULOPLEN-1) break;
888      }
889    if (ui==bufr+MULOPLEN-1) break;
890    }
891
892  // The 64-bit carries must now be resolved; this means that a
893  // quotient/remainder has to be calculated for base-billion (1E+9).
894  // For this, Clark & Cowlishaw's quotient estimation approach (also
895  // used in decNumber) is needed, because 64-bit divide is generally
896  // extremely slow on 32-bit machines.  This algorithm splits X
897  // using:
898  //
899  //   magic=2**(A+B)/1E+9;   // 'magic number'
900  //   hop=X/2**A;            // high order part of X (by shift)
901  //   est=magic*hop/2**B     // quotient estimate (may be low by 1)
902  //
903  // A and B are quite constrained; hop and magic must fit in 32 bits,
904  // and 2**(A+B) must be as large as possible (which is 2**61 if
905  // magic is to fit).  Further, maxX increases with the length of
906  // the operands (and hence the number of partial products
907  // accumulated); maxX is OPLEN*(10**18), which is up to 19 digits.
908  //
909  // It can be shown that when OPLEN is 2 then the maximum error in
910  // the estimated quotient is <1, but for larger maximum x the
911  // maximum error is above 1 so a correction that is >1 may be
912  // needed.  Values of A and B are chosen to satisfy the constraints
913  // just mentioned while minimizing the maximum error (and hence the
914  // maximum correction), as shown in the following table:
915  //
916  //   Type    OPLEN   A   B     maxX    maxError  maxCorrection
917  //   ---------------------------------------------------------
918  //   DOUBLE    2    29  32  <2*10**18    0.63       1
919  //   QUAD      4    30  31  <4*10**18    1.17       2
920  //
921  // In the OPLEN==2 case there is most choice, but the value for B
922  // of 32 has a big advantage as then the calculation of the
923  // estimate requires no shifting; the high word is simply
924  // calculated from multiplying magic*hop.
925  #define MULMAGIC 2305843009U          // 2**61/10**9  [both cases]
926  #if DOUBLE
927    #define MULSHIFTA 29
928    #define MULSHIFTB 32
929  #elif QUAD
930    #define MULSHIFTA 30
931    #define MULSHIFTB 31
932  #else
933    #error Unexpected type
934  #endif
935
936  #if DECTRACE
937  printf("MulHiLo:");
938  for (pa=acc+MULACCLEN-1; pa>=acc; pa--)
939    printf(" %08lx:%08lx", (LI)*(pa+MULACCLEN), (LI)*pa);
940  printf("\n");
941  #endif
942
943  for (pa=acc;; pa++) {                 // each low uInt
944    uInt hi, lo;                        // words of exact multiply result
945    uInt hop, estlo;                    // work
946    #if QUAD
947    uInt esthi;                         // ..
948    #endif
949
950    lo=*pa;
951    hi=*(pa+MULACCLEN);                 // top 32 bits
952    // hi and lo now hold a binary number which needs to be split
953
954    #if DOUBLE
955      hop=(hi<<3)+(lo>>MULSHIFTA);      // hi:lo/2**29
956      LONGMUL32HI(estlo, hop, MULMAGIC);// only need the high word
957      // [MULSHIFTB is 32, so estlo can be used directly]
958      // the estimate is now in estlo; now calculate hi:lo-est*10**9;
959      // happily the top word of the result is irrelevant because it
960      // will always be zero so this needs only one multiplication
961      lo-=(estlo*MULTBASE);
962      // esthi=0;                       // high word is ignored below
963      // the correction here will be at most +1; do it
964      if (lo>=MULTBASE) {
965        lo-=MULTBASE;
966        estlo++;
967        }
968    #elif QUAD
969      hop=(hi<<2)+(lo>>MULSHIFTA);      // hi:lo/2**30
970      LONGMUL32HI(esthi, hop, MULMAGIC);// shift will be 31 ..
971      estlo=hop*MULMAGIC;               // .. so low word needed
972      estlo=(esthi<<1)+(estlo>>MULSHIFTB); // [just the top bit]
973      // esthi=0;                       // high word is ignored below
974      lo-=(estlo*MULTBASE);             // as above
975      // the correction here could be +1 or +2
976      if (lo>=MULTBASE) {
977        lo-=MULTBASE;
978        estlo++;
979        }
980      if (lo>=MULTBASE) {
981        lo-=MULTBASE;
982        estlo++;
983        }
984    #else
985      #error Unexpected type
986    #endif
987
988    // finally place lo as the new accumulator digit and add est to
989    // the next place up; this latter add could cause a carry of 1
990    // to the high word of the next place
991    *pa=lo;
992    *(pa+1)+=estlo;
993    // esthi is always 0 for DOUBLE and QUAD so this is skipped
994    // *(pa+1+MULACCLEN)+=esthi;
995    if (*(pa+1)<estlo) *(pa+1+MULACCLEN)+=1; // carry
996    if (pa==acc+MULACCLEN-2) break;          // [MULACCLEN-1 will never need split]
997    } // pa loop
998#endif
999
1000  // At this point, whether using the 64-bit or the 32-bit paths, the
1001  // accumulator now holds the (unrounded) result in base-billion;
1002  // one base-billion 'digit' per uInt.
1003  #if DECTRACE
1004  printf("MultAcc:");
1005  for (pa=acc+MULACCLEN-1; pa>=acc; pa--) printf(" %09ld", (LI)*pa);
1006  printf("\n");
1007  #endif
1008
1009  // Now convert to BCD for rounding and cleanup, starting from the
1010  // most significant end
1011  pa=acc+MULACCLEN-1;
1012  if (*pa!=0) num->msd=bcdacc+LEADZEROS;// drop known lead zeros
1013   else {                               // >=1 word of leading zeros
1014    num->msd=bcdacc;                    // known leading zeros are gone
1015    pa--;                               // skip first word ..
1016    for (; *pa==0; pa--) if (pa==acc) break; // .. and any more leading 0s
1017    }
1018  for (ub=bcdacc;; pa--, ub+=9) {
1019    if (*pa!=0) {                       // split(s) needed
1020      uInt top, mid, rem;               // work
1021      // *pa is non-zero -- split the base-billion acc digit into
1022      // hi, mid, and low three-digits
1023      #define mulsplit9 1000000         // divisor
1024      #define mulsplit6 1000            // divisor
1025      // The splitting is done by simple divides and remainders,
1026      // assuming the compiler will optimize these where useful
1027      // [GCC does]
1028      top=*pa/mulsplit9;
1029      rem=*pa%mulsplit9;
1030      mid=rem/mulsplit6;
1031      rem=rem%mulsplit6;
1032      // lay out the nine BCD digits (plus one unwanted byte)
1033      UBFROMUI(ub,   UBTOUI(&BIN2BCD8[top*4]));
1034      UBFROMUI(ub+3, UBTOUI(&BIN2BCD8[mid*4]));
1035      UBFROMUI(ub+6, UBTOUI(&BIN2BCD8[rem*4]));
1036      }
1037     else {                             // *pa==0
1038      UBFROMUI(ub, 0);                  // clear 9 BCD8s
1039      UBFROMUI(ub+4, 0);                // ..
1040      *(ub+8)=0;                        // ..
1041      }
1042    if (pa==acc) break;
1043    } // BCD conversion loop
1044
1045  num->lsd=ub+8;                        // complete the bcdnum ..
1046
1047  #if DECTRACE
1048  decShowNum(num, "postmult");
1049  decFloatShow(dfl, "dfl");
1050  decFloatShow(dfr, "dfr");
1051  #endif
1052  return;
1053  } // decFiniteMultiply
1054
1055/* ------------------------------------------------------------------ */
1056/* decFloatAbs -- absolute value, heeding NaNs, etc.                  */
1057/*                                                                    */
1058/*   result gets the canonicalized df with sign 0                     */
1059/*   df     is the decFloat to abs                                    */
1060/*   set    is the context                                            */
1061/*   returns result                                                   */
1062/*                                                                    */
1063/* This has the same effect as decFloatPlus unless df is negative,    */
1064/* in which case it has the same effect as decFloatMinus.  The        */
1065/* effect is also the same as decFloatCopyAbs except that NaNs are    */
1066/* handled normally (the sign of a NaN is not affected, and an sNaN   */
1067/* will signal) and the result will be canonical.                     */
1068/* ------------------------------------------------------------------ */
1069decFloat * decFloatAbs(decFloat *result, const decFloat *df,
1070                       decContext *set) {
1071  if (DFISNAN(df)) return decNaNs(result, df, NULL, set);
1072  decCanonical(result, df);             // copy and check
1073  DFBYTE(result, 0)&=~0x80;             // zero sign bit
1074  return result;
1075  } // decFloatAbs
1076
1077/* ------------------------------------------------------------------ */
1078/* decFloatAdd -- add two decFloats                                   */
1079/*                                                                    */
1080/*   result gets the result of adding dfl and dfr:                    */
1081/*   dfl    is the first decFloat (lhs)                               */
1082/*   dfr    is the second decFloat (rhs)                              */
1083/*   set    is the context                                            */
1084/*   returns result                                                   */
1085/*                                                                    */
1086/* ------------------------------------------------------------------ */
1087#if QUAD
1088// Table for testing MSDs for fastpath elimination; returns the MSD of
1089// a decDouble or decQuad (top 6 bits tested) ignoring the sign.
1090// Infinities return -32 and NaNs return -128 so that summing the two
1091// MSDs also allows rapid tests for the Specials (see code below).
1092const Int DECTESTMSD[64]={
1093  0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5,   6,    7,
1094  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 9, 8, 9, -32, -128,
1095  0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5,   6,    7,
1096  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 9, 8, 9, -32, -128};
1097#else
1098// The table for testing MSDs is shared between the modules
1099extern const Int DECTESTMSD[64];
1100#endif
1101
1102decFloat * decFloatAdd(decFloat *result,
1103                       const decFloat *dfl, const decFloat *dfr,
1104                       decContext *set) {
1105  bcdnum num;                      // for final conversion
1106  Int    bexpl, bexpr;             // left and right biased exponents
1107  uByte  *ub, *us, *ut;            // work
1108  uInt   uiwork;                   // for macros
1109  #if QUAD
1110  uShort uswork;                   // ..
1111  #endif
1112
1113  uInt sourhil, sourhir;           // top words from source decFloats
1114                                   // [valid only through end of
1115                                   // fastpath code -- before swap]
1116  uInt diffsign;                   // non-zero if signs differ
1117  uInt carry;                      // carry: 0 or 1 before add loop
1118  Int  overlap;                    // coefficient overlap (if full)
1119  Int  summ;                       // sum of the MSDs
1120  // the following buffers hold coefficients with various alignments
1121  // (see commentary and diagrams below)
1122  uByte acc[4+2+DECPMAX*3+8];
1123  uByte buf[4+2+DECPMAX*2];
1124  uByte *umsd, *ulsd;              // local MSD and LSD pointers
1125
1126  #if DECLITEND
1127    #define CARRYPAT 0x01000000    // carry=1 pattern
1128  #else
1129    #define CARRYPAT 0x00000001    // carry=1 pattern
1130  #endif
1131
1132  /* Start decoding the arguments */
1133  // The initial exponents are placed into the opposite Ints to
1134  // that which might be expected; there are two sets of data to
1135  // keep track of (each decFloat and the corresponding exponent),
1136  // and this scheme means that at the swap point (after comparing
1137  // exponents) only one pair of words needs to be swapped
1138  // whichever path is taken (thereby minimising worst-case path).
1139  // The calculated exponents will be nonsense when the arguments are
1140  // Special, but are not used in that path
1141  sourhil=DFWORD(dfl, 0);          // LHS top word
1142  summ=DECTESTMSD[sourhil>>26];    // get first MSD for testing
1143  bexpr=DECCOMBEXP[sourhil>>26];   // get exponent high bits (in place)
1144  bexpr+=GETECON(dfl);             // .. + continuation
1145
1146  sourhir=DFWORD(dfr, 0);          // RHS top word
1147  summ+=DECTESTMSD[sourhir>>26];   // sum MSDs for testing
1148  bexpl=DECCOMBEXP[sourhir>>26];
1149  bexpl+=GETECON(dfr);
1150
1151  // here bexpr has biased exponent from lhs, and vice versa
1152
1153  diffsign=(sourhil^sourhir)&DECFLOAT_Sign;
1154
1155  // now determine whether to take a fast path or the full-function
1156  // slow path.  The slow path must be taken when:
1157  //   -- both numbers are finite, and:
1158  //         the exponents are different, or
1159  //         the signs are different, or
1160  //         the sum of the MSDs is >8 (hence might overflow)
1161  // specialness and the sum of the MSDs can be tested at once using
1162  // the summ value just calculated, so the test for specials is no
1163  // longer on the worst-case path (as of 3.60)
1164
1165  if (summ<=8) {                   // MSD+MSD is good, or there is a special
1166    if (summ<0) {                  // there is a special
1167      // Inf+Inf would give -64; Inf+finite is -32 or higher
1168      if (summ<-64) return decNaNs(result, dfl, dfr, set);  // one or two NaNs
1169      // two infinities with different signs is invalid
1170      if (summ==-64 && diffsign) return decInvalid(result, set);
1171      if (DFISINF(dfl)) return decInfinity(result, dfl);    // LHS is infinite
1172      return decInfinity(result, dfr);                      // RHS must be Inf
1173      }
1174    // Here when both arguments are finite; fast path is possible
1175    // (currently only for aligned and same-sign)
1176    if (bexpr==bexpl && !diffsign) {
1177      uInt tac[DECLETS+1];              // base-1000 coefficient
1178      uInt encode;                      // work
1179
1180      // Get one coefficient as base-1000 and add the other
1181      GETCOEFFTHOU(dfl, tac);           // least-significant goes to [0]
1182      ADDCOEFFTHOU(dfr, tac);
1183      // here the sum of the MSDs (plus any carry) will be <10 due to
1184      // the fastpath test earlier
1185
1186      // construct the result; low word is the same for both formats
1187      encode =BIN2DPD[tac[0]];
1188      encode|=BIN2DPD[tac[1]]<<10;
1189      encode|=BIN2DPD[tac[2]]<<20;
1190      encode|=BIN2DPD[tac[3]]<<30;
1191      DFWORD(result, (DECBYTES/4)-1)=encode;
1192
1193      // collect next two declets (all that remains, for Double)
1194      encode =BIN2DPD[tac[3]]>>2;
1195      encode|=BIN2DPD[tac[4]]<<8;
1196
1197      #if QUAD
1198      // complete and lay out middling words
1199      encode|=BIN2DPD[tac[5]]<<18;
1200      encode|=BIN2DPD[tac[6]]<<28;
1201      DFWORD(result, 2)=encode;
1202
1203      encode =BIN2DPD[tac[6]]>>4;
1204      encode|=BIN2DPD[tac[7]]<<6;
1205      encode|=BIN2DPD[tac[8]]<<16;
1206      encode|=BIN2DPD[tac[9]]<<26;
1207      DFWORD(result, 1)=encode;
1208
1209      // and final two declets
1210      encode =BIN2DPD[tac[9]]>>6;
1211      encode|=BIN2DPD[tac[10]]<<4;
1212      #endif
1213
1214      // add exponent continuation and sign (from either argument)
1215      encode|=sourhil & (ECONMASK | DECFLOAT_Sign);
1216
1217      // create lookup index = MSD + top two bits of biased exponent <<4
1218      tac[DECLETS]|=(bexpl>>DECECONL)<<4;
1219      encode|=DECCOMBFROM[tac[DECLETS]]; // add constructed combination field
1220      DFWORD(result, 0)=encode;          // complete
1221
1222      // decFloatShow(result, ">");
1223      return result;
1224      } // fast path OK
1225    // drop through to slow path
1226    } // low sum or Special(s)
1227
1228  /* Slow path required -- arguments are finite and might overflow,   */
1229  /* or require alignment, or might have different signs              */
1230
1231  // now swap either exponents or argument pointers
1232  if (bexpl<=bexpr) {
1233    // original left is bigger
1234    Int bexpswap=bexpl;
1235    bexpl=bexpr;
1236    bexpr=bexpswap;
1237    // printf("left bigger\n");
1238    }
1239   else {
1240    const decFloat *dfswap=dfl;
1241    dfl=dfr;
1242    dfr=dfswap;
1243    // printf("right bigger\n");
1244    }
1245  // [here dfl and bexpl refer to the datum with the larger exponent,
1246  // of if the exponents are equal then the original LHS argument]
1247
1248  // if lhs is zero then result will be the rhs (now known to have
1249  // the smaller exponent), which also may need to be tested for zero
1250  // for the weird IEEE 754 sign rules
1251  if (DFISZERO(dfl)) {
1252    decCanonical(result, dfr);               // clean copy
1253    // "When the sum of two operands with opposite signs is
1254    // exactly zero, the sign of that sum shall be '+' in all
1255    // rounding modes except round toward -Infinity, in which
1256    // mode that sign shall be '-'."
1257    if (diffsign && DFISZERO(result)) {
1258      DFWORD(result, 0)&=~DECFLOAT_Sign;     // assume sign 0
1259      if (set->round==DEC_ROUND_FLOOR) DFWORD(result, 0)|=DECFLOAT_Sign;
1260      }
1261    return result;
1262    } // numfl is zero
1263  // [here, LHS is non-zero; code below assumes that]
1264
1265  // Coefficients layout during the calculations to follow:
1266  //
1267  //       Overlap case:
1268  //       +------------------------------------------------+
1269  // acc:  |0000|      coeffa      | tail B |               |
1270  //       +------------------------------------------------+
1271  // buf:  |0000| pad0s |      coeffb       |               |
1272  //       +------------------------------------------------+
1273  //
1274  //       Touching coefficients or gap:
1275  //       +------------------------------------------------+
1276  // acc:  |0000|      coeffa      | gap |      coeffb      |
1277  //       +------------------------------------------------+
1278  //       [buf not used or needed; gap clamped to Pmax]
1279
1280  // lay out lhs coefficient into accumulator; this starts at acc+4
1281  // for decDouble or acc+6 for decQuad so the LSD is word-
1282  // aligned; the top word gap is there only in case a carry digit
1283  // is prefixed after the add -- it does not need to be zeroed
1284  #if DOUBLE
1285    #define COFF 4                      // offset into acc
1286  #elif QUAD
1287    UBFROMUS(acc+4, 0);                 // prefix 00
1288    #define COFF 6                      // offset into acc
1289  #endif
1290
1291  GETCOEFF(dfl, acc+COFF);              // decode from decFloat
1292  ulsd=acc+COFF+DECPMAX-1;
1293  umsd=acc+4;                           // [having this here avoids
1294
1295  #if DECTRACE
1296  {bcdnum tum;
1297  tum.msd=umsd;
1298  tum.lsd=ulsd;
1299  tum.exponent=bexpl-DECBIAS;
1300  tum.sign=DFWORD(dfl, 0) & DECFLOAT_Sign;
1301  decShowNum(&tum, "dflx");}
1302  #endif
1303
1304  // if signs differ, take ten's complement of lhs (here the
1305  // coefficient is subtracted from all-nines; the 1 is added during
1306  // the later add cycle -- zeros to the right do not matter because
1307  // the complement of zero is zero); these are fixed-length inverts
1308  // where the lsd is known to be at a 4-byte boundary (so no borrow
1309  // possible)
1310  carry=0;                              // assume no carry
1311  if (diffsign) {
1312    carry=CARRYPAT;                     // for +1 during add
1313    UBFROMUI(acc+ 4, 0x09090909-UBTOUI(acc+ 4));
1314    UBFROMUI(acc+ 8, 0x09090909-UBTOUI(acc+ 8));
1315    UBFROMUI(acc+12, 0x09090909-UBTOUI(acc+12));
1316    UBFROMUI(acc+16, 0x09090909-UBTOUI(acc+16));
1317    #if QUAD
1318    UBFROMUI(acc+20, 0x09090909-UBTOUI(acc+20));
1319    UBFROMUI(acc+24, 0x09090909-UBTOUI(acc+24));
1320    UBFROMUI(acc+28, 0x09090909-UBTOUI(acc+28));
1321    UBFROMUI(acc+32, 0x09090909-UBTOUI(acc+32));
1322    UBFROMUI(acc+36, 0x09090909-UBTOUI(acc+36));
1323    #endif
1324    } // diffsign
1325
1326  // now process the rhs coefficient; if it cannot overlap lhs then
1327  // it can be put straight into acc (with an appropriate gap, if
1328  // needed) because no actual addition will be needed (except
1329  // possibly to complete ten's complement)
1330  overlap=DECPMAX-(bexpl-bexpr);
1331  #if DECTRACE
1332  printf("exps: %ld %ld\n", (LI)(bexpl-DECBIAS), (LI)(bexpr-DECBIAS));
1333  printf("Overlap=%ld carry=%08lx\n", (LI)overlap, (LI)carry);
1334  #endif
1335
1336  if (overlap<=0) {                     // no overlap possible
1337    uInt gap;                           // local work
1338    // since a full addition is not needed, a ten's complement
1339    // calculation started above may need to be completed
1340    if (carry) {
1341      for (ub=ulsd; *ub==9; ub--) *ub=0;
1342      *ub+=1;
1343      carry=0;                          // taken care of
1344      }
1345    // up to DECPMAX-1 digits of the final result can extend down
1346    // below the LSD of the lhs, so if the gap is >DECPMAX then the
1347    // rhs will be simply sticky bits.  In this case the gap is
1348    // clamped to DECPMAX and the exponent adjusted to suit [this is
1349    // safe because the lhs is non-zero].
1350    gap=-overlap;
1351    if (gap>DECPMAX) {
1352      bexpr+=gap-1;
1353      gap=DECPMAX;
1354      }
1355    ub=ulsd+gap+1;                      // where MSD will go
1356    // Fill the gap with 0s; note that there is no addition to do
1357    ut=acc+COFF+DECPMAX;                // start of gap
1358    for (; ut<ub; ut+=4) UBFROMUI(ut, 0); // mind the gap
1359    if (overlap<-DECPMAX) {             // gap was > DECPMAX
1360      *ub=(uByte)(!DFISZERO(dfr));      // make sticky digit
1361      }
1362     else {                             // need full coefficient
1363      GETCOEFF(dfr, ub);                // decode from decFloat
1364      ub+=DECPMAX-1;                    // new LSD...
1365      }
1366    ulsd=ub;                            // save new LSD
1367    } // no overlap possible
1368
1369   else {                               // overlap>0
1370    // coefficients overlap (perhaps completely, although also
1371    // perhaps only where zeros)
1372    if (overlap==DECPMAX) {             // aligned
1373      ub=buf+COFF;                      // where msd will go
1374      #if QUAD
1375      UBFROMUS(buf+4, 0);               // clear quad's 00
1376      #endif
1377      GETCOEFF(dfr, ub);                // decode from decFloat
1378      }
1379     else {                             // unaligned
1380      ub=buf+COFF+DECPMAX-overlap;      // where MSD will go
1381      // Fill the prefix gap with 0s; 8 will cover most common
1382      // unalignments, so start with direct assignments (a loop is
1383      // then used for any remaining -- the loop (and the one in a
1384      // moment) is not then on the critical path because the number
1385      // of additions is reduced by (at least) two in this case)
1386      UBFROMUI(buf+4, 0);               // [clears decQuad 00 too]
1387      UBFROMUI(buf+8, 0);
1388      if (ub>buf+12) {
1389        ut=buf+12;                      // start any remaining
1390        for (; ut<ub; ut+=4) UBFROMUI(ut, 0); // fill them
1391        }
1392      GETCOEFF(dfr, ub);                // decode from decFloat
1393
1394      // now move tail of rhs across to main acc; again use direct
1395      // copies for 8 digits-worth
1396      UBFROMUI(acc+COFF+DECPMAX,   UBTOUI(buf+COFF+DECPMAX));
1397      UBFROMUI(acc+COFF+DECPMAX+4, UBTOUI(buf+COFF+DECPMAX+4));
1398      if (buf+COFF+DECPMAX+8<ub+DECPMAX) {
1399        us=buf+COFF+DECPMAX+8;          // source
1400        ut=acc+COFF+DECPMAX+8;          // target
1401        for (; us<ub+DECPMAX; us+=4, ut+=4) UBFROMUI(ut, UBTOUI(us));
1402        }
1403      } // unaligned
1404
1405    ulsd=acc+(ub-buf+DECPMAX-1);        // update LSD pointer
1406
1407    // Now do the add of the non-tail; this is all nicely aligned,
1408    // and is over a multiple of four digits (because for Quad two
1409    // zero digits were added on the left); words in both acc and
1410    // buf (buf especially) will often be zero
1411    // [byte-by-byte add, here, is about 15% slower total effect than
1412    // the by-fours]
1413
1414    // Now effect the add; this is harder on a little-endian
1415    // machine as the inter-digit carry cannot use the usual BCD
1416    // addition trick because the bytes are loaded in the wrong order
1417    // [this loop could be unrolled, but probably scarcely worth it]
1418
1419    ut=acc+COFF+DECPMAX-4;              // target LSW (acc)
1420    us=buf+COFF+DECPMAX-4;              // source LSW (buf, to add to acc)
1421
1422    #if !DECLITEND
1423    for (; ut>=acc+4; ut-=4, us-=4) {   // big-endian add loop
1424      // bcd8 add
1425      carry+=UBTOUI(us);                // rhs + carry
1426      if (carry==0) continue;           // no-op
1427      carry+=UBTOUI(ut);                // lhs
1428      // Big-endian BCD adjust (uses internal carry)
1429      carry+=0x76f6f6f6;                // note top nibble not all bits
1430      // apply BCD adjust and save
1431      UBFROMUI(ut, (carry & 0x0f0f0f0f) - ((carry & 0x60606060)>>4));
1432      carry>>=31;                       // true carry was at far left
1433      } // add loop
1434    #else
1435    for (; ut>=acc+4; ut-=4, us-=4) {   // little-endian add loop
1436      // bcd8 add
1437      carry+=UBTOUI(us);                // rhs + carry
1438      if (carry==0) continue;           // no-op [common if unaligned]
1439      carry+=UBTOUI(ut);                // lhs
1440      // Little-endian BCD adjust; inter-digit carry must be manual
1441      // because the lsb from the array will be in the most-significant
1442      // byte of carry
1443      carry+=0x76767676;                // note no inter-byte carries
1444      carry+=(carry & 0x80000000)>>15;
1445      carry+=(carry & 0x00800000)>>15;
1446      carry+=(carry & 0x00008000)>>15;
1447      carry-=(carry & 0x60606060)>>4;   // BCD adjust back
1448      UBFROMUI(ut, carry & 0x0f0f0f0f); // clear debris and save
1449      // here, final carry-out bit is at 0x00000080; move it ready
1450      // for next word-add (i.e., to 0x01000000)
1451      carry=(carry & 0x00000080)<<17;
1452      } // add loop
1453    #endif
1454
1455    #if DECTRACE
1456    {bcdnum tum;
1457    printf("Add done, carry=%08lx, diffsign=%ld\n", (LI)carry, (LI)diffsign);
1458    tum.msd=umsd;  // acc+4;
1459    tum.lsd=ulsd;
1460    tum.exponent=0;
1461    tum.sign=0;
1462    decShowNum(&tum, "dfadd");}
1463    #endif
1464    } // overlap possible
1465
1466  // ordering here is a little strange in order to have slowest path
1467  // first in GCC asm listing
1468  if (diffsign) {                  // subtraction
1469    if (!carry) {                  // no carry out means RHS<LHS
1470      // borrowed -- take ten's complement
1471      // sign is lhs sign
1472      num.sign=DFWORD(dfl, 0) & DECFLOAT_Sign;
1473
1474      // invert the coefficient first by fours, then add one; space
1475      // at the end of the buffer ensures the by-fours is always
1476      // safe, but lsd+1 must be cleared to prevent a borrow
1477      // if big-endian
1478      #if !DECLITEND
1479      *(ulsd+1)=0;
1480      #endif
1481      // there are always at least four coefficient words
1482      UBFROMUI(umsd,    0x09090909-UBTOUI(umsd));
1483      UBFROMUI(umsd+4,  0x09090909-UBTOUI(umsd+4));
1484      UBFROMUI(umsd+8,  0x09090909-UBTOUI(umsd+8));
1485      UBFROMUI(umsd+12, 0x09090909-UBTOUI(umsd+12));
1486      #if DOUBLE
1487        #define BNEXT 16
1488      #elif QUAD
1489        UBFROMUI(umsd+16, 0x09090909-UBTOUI(umsd+16));
1490        UBFROMUI(umsd+20, 0x09090909-UBTOUI(umsd+20));
1491        UBFROMUI(umsd+24, 0x09090909-UBTOUI(umsd+24));
1492        UBFROMUI(umsd+28, 0x09090909-UBTOUI(umsd+28));
1493        UBFROMUI(umsd+32, 0x09090909-UBTOUI(umsd+32));
1494        #define BNEXT 36
1495      #endif
1496      if (ulsd>=umsd+BNEXT) {           // unaligned
1497        // eight will handle most unaligments for Double; 16 for Quad
1498        UBFROMUI(umsd+BNEXT,   0x09090909-UBTOUI(umsd+BNEXT));
1499        UBFROMUI(umsd+BNEXT+4, 0x09090909-UBTOUI(umsd+BNEXT+4));
1500        #if DOUBLE
1501        #define BNEXTY (BNEXT+8)
1502        #elif QUAD
1503        UBFROMUI(umsd+BNEXT+8,  0x09090909-UBTOUI(umsd+BNEXT+8));
1504        UBFROMUI(umsd+BNEXT+12, 0x09090909-UBTOUI(umsd+BNEXT+12));
1505        #define BNEXTY (BNEXT+16)
1506        #endif
1507        if (ulsd>=umsd+BNEXTY) {        // very unaligned
1508          ut=umsd+BNEXTY;               // -> continue
1509          for (;;ut+=4) {
1510            UBFROMUI(ut, 0x09090909-UBTOUI(ut)); // invert four digits
1511            if (ut>=ulsd-3) break;      // all done
1512            }
1513          }
1514        }
1515      // complete the ten's complement by adding 1
1516      for (ub=ulsd; *ub==9; ub--) *ub=0;
1517      *ub+=1;
1518      } // borrowed
1519
1520     else {                        // carry out means RHS>=LHS
1521      num.sign=DFWORD(dfr, 0) & DECFLOAT_Sign;
1522      // all done except for the special IEEE 754 exact-zero-result
1523      // rule (see above); while testing for zero, strip leading
1524      // zeros (which will save decFinalize doing it) (this is in
1525      // diffsign path, so carry impossible and true umsd is
1526      // acc+COFF)
1527
1528      // Check the initial coefficient area using the fast macro;
1529      // this will often be all that needs to be done (as on the
1530      // worst-case path when the subtraction was aligned and
1531      // full-length)
1532      if (ISCOEFFZERO(acc+COFF)) {
1533        umsd=acc+COFF+DECPMAX-1;   // so far, so zero
1534        if (ulsd>umsd) {           // more to check
1535          umsd++;                  // to align after checked area
1536          for (; UBTOUI(umsd)==0 && umsd+3<ulsd;) umsd+=4;
1537          for (; *umsd==0 && umsd<ulsd;) umsd++;
1538          }
1539        if (*umsd==0) {            // must be true zero (and diffsign)
1540          num.sign=0;              // assume +
1541          if (set->round==DEC_ROUND_FLOOR) num.sign=DECFLOAT_Sign;
1542          }
1543        }
1544      // [else was not zero, might still have leading zeros]
1545      } // subtraction gave positive result
1546    } // diffsign
1547
1548   else { // same-sign addition
1549    num.sign=DFWORD(dfl, 0)&DECFLOAT_Sign;
1550    #if DOUBLE
1551    if (carry) {                   // only possible with decDouble
1552      *(acc+3)=1;                  // [Quad has leading 00]
1553      umsd=acc+3;
1554      }
1555    #endif
1556    } // same sign
1557
1558  num.msd=umsd;                    // set MSD ..
1559  num.lsd=ulsd;                    // .. and LSD
1560  num.exponent=bexpr-DECBIAS;      // set exponent to smaller, unbiassed
1561
1562  #if DECTRACE
1563  decFloatShow(dfl, "dfl");
1564  decFloatShow(dfr, "dfr");
1565  decShowNum(&num, "postadd");
1566  #endif
1567  return decFinalize(result, &num, set); // round, check, and lay out
1568  } // decFloatAdd
1569
1570/* ------------------------------------------------------------------ */
1571/* decFloatAnd -- logical digitwise AND of two decFloats              */
1572/*                                                                    */
1573/*   result gets the result of ANDing dfl and dfr                     */
1574/*   dfl    is the first decFloat (lhs)                               */
1575/*   dfr    is the second decFloat (rhs)                              */
1576/*   set    is the context                                            */
1577/*   returns result, which will be canonical with sign=0              */
1578/*                                                                    */
1579/* The operands must be positive, finite with exponent q=0, and       */
1580/* comprise just zeros and ones; if not, Invalid operation results.   */
1581/* ------------------------------------------------------------------ */
1582decFloat * decFloatAnd(decFloat *result,
1583                       const decFloat *dfl, const decFloat *dfr,
1584                       decContext *set) {
1585  if (!DFISUINT01(dfl) || !DFISUINT01(dfr)
1586   || !DFISCC01(dfl)   || !DFISCC01(dfr)) return decInvalid(result, set);
1587  // the operands are positive finite integers (q=0) with just 0s and 1s
1588  #if DOUBLE
1589   DFWORD(result, 0)=ZEROWORD
1590                   |((DFWORD(dfl, 0) & DFWORD(dfr, 0))&0x04009124);
1591   DFWORD(result, 1)=(DFWORD(dfl, 1) & DFWORD(dfr, 1))&0x49124491;
1592  #elif QUAD
1593   DFWORD(result, 0)=ZEROWORD
1594                   |((DFWORD(dfl, 0) & DFWORD(dfr, 0))&0x04000912);
1595   DFWORD(result, 1)=(DFWORD(dfl, 1) & DFWORD(dfr, 1))&0x44912449;
1596   DFWORD(result, 2)=(DFWORD(dfl, 2) & DFWORD(dfr, 2))&0x12449124;
1597   DFWORD(result, 3)=(DFWORD(dfl, 3) & DFWORD(dfr, 3))&0x49124491;
1598  #endif
1599  return result;
1600  } // decFloatAnd
1601
1602/* ------------------------------------------------------------------ */
1603/* decFloatCanonical -- copy a decFloat, making canonical             */
1604/*                                                                    */
1605/*   result gets the canonicalized df                                 */
1606/*   df     is the decFloat to copy and make canonical                */
1607/*   returns result                                                   */
1608/*                                                                    */
1609/* This works on specials, too; no error or exception is possible.    */
1610/* ------------------------------------------------------------------ */
1611decFloat * decFloatCanonical(decFloat *result, const decFloat *df) {
1612  return decCanonical(result, df);
1613  } // decFloatCanonical
1614
1615/* ------------------------------------------------------------------ */
1616/* decFloatClass -- return the class of a decFloat                    */
1617/*                                                                    */
1618/*   df is the decFloat to test                                       */
1619/*   returns the decClass that df falls into                          */
1620/* ------------------------------------------------------------------ */
1621enum decClass decFloatClass(const decFloat *df) {
1622  Int exp;                         // exponent
1623  if (DFISSPECIAL(df)) {
1624    if (DFISQNAN(df)) return DEC_CLASS_QNAN;
1625    if (DFISSNAN(df)) return DEC_CLASS_SNAN;
1626    // must be an infinity
1627    if (DFISSIGNED(df)) return DEC_CLASS_NEG_INF;
1628    return DEC_CLASS_POS_INF;
1629    }
1630  if (DFISZERO(df)) {              // quite common
1631    if (DFISSIGNED(df)) return DEC_CLASS_NEG_ZERO;
1632    return DEC_CLASS_POS_ZERO;
1633    }
1634  // is finite and non-zero; similar code to decFloatIsNormal, here
1635  // [this could be speeded up slightly by in-lining decFloatDigits]
1636  exp=GETEXPUN(df)                 // get unbiased exponent ..
1637     +decFloatDigits(df)-1;        // .. and make adjusted exponent
1638  if (exp>=DECEMIN) {              // is normal
1639    if (DFISSIGNED(df)) return DEC_CLASS_NEG_NORMAL;
1640    return DEC_CLASS_POS_NORMAL;
1641    }
1642  // is subnormal
1643  if (DFISSIGNED(df)) return DEC_CLASS_NEG_SUBNORMAL;
1644  return DEC_CLASS_POS_SUBNORMAL;
1645  } // decFloatClass
1646
1647/* ------------------------------------------------------------------ */
1648/* decFloatClassString -- return the class of a decFloat as a string  */
1649/*                                                                    */
1650/*   df is the decFloat to test                                       */
1651/*   returns a constant string describing the class df falls into     */
1652/* ------------------------------------------------------------------ */
1653const char *decFloatClassString(const decFloat *df) {
1654  enum decClass eclass=decFloatClass(df);
1655  if (eclass==DEC_CLASS_POS_NORMAL)    return DEC_ClassString_PN;
1656  if (eclass==DEC_CLASS_NEG_NORMAL)    return DEC_ClassString_NN;
1657  if (eclass==DEC_CLASS_POS_ZERO)      return DEC_ClassString_PZ;
1658  if (eclass==DEC_CLASS_NEG_ZERO)      return DEC_ClassString_NZ;
1659  if (eclass==DEC_CLASS_POS_SUBNORMAL) return DEC_ClassString_PS;
1660  if (eclass==DEC_CLASS_NEG_SUBNORMAL) return DEC_ClassString_NS;
1661  if (eclass==DEC_CLASS_POS_INF)       return DEC_ClassString_PI;
1662  if (eclass==DEC_CLASS_NEG_INF)       return DEC_ClassString_NI;
1663  if (eclass==DEC_CLASS_QNAN)          return DEC_ClassString_QN;
1664  if (eclass==DEC_CLASS_SNAN)          return DEC_ClassString_SN;
1665  return DEC_ClassString_UN;           // Unknown
1666  } // decFloatClassString
1667
1668/* ------------------------------------------------------------------ */
1669/* decFloatCompare -- compare two decFloats; quiet NaNs allowed       */
1670/*                                                                    */
1671/*   result gets the result of comparing dfl and dfr                  */
1672/*   dfl    is the first decFloat (lhs)                               */
1673/*   dfr    is the second decFloat (rhs)                              */
1674/*   set    is the context                                            */
1675/*   returns result, which may be -1, 0, 1, or NaN (Unordered)        */
1676/* ------------------------------------------------------------------ */
1677decFloat * decFloatCompare(decFloat *result,
1678                           const decFloat *dfl, const decFloat *dfr,
1679                           decContext *set) {
1680  Int comp;                                  // work
1681  // NaNs are handled as usual
1682  if (DFISNAN(dfl) || DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set);
1683  // numeric comparison needed
1684  comp=decNumCompare(dfl, dfr, 0);
1685  decFloatZero(result);
1686  if (comp==0) return result;
1687  DFBYTE(result, DECBYTES-1)=0x01;      // LSD=1
1688  if (comp<0) DFBYTE(result, 0)|=0x80;  // set sign bit
1689  return result;
1690  } // decFloatCompare
1691
1692/* ------------------------------------------------------------------ */
1693/* decFloatCompareSignal -- compare two decFloats; all NaNs signal    */
1694/*                                                                    */
1695/*   result gets the result of comparing dfl and dfr                  */
1696/*   dfl    is the first decFloat (lhs)                               */
1697/*   dfr    is the second decFloat (rhs)                              */
1698/*   set    is the context                                            */
1699/*   returns result, which may be -1, 0, 1, or NaN (Unordered)        */
1700/* ------------------------------------------------------------------ */
1701decFloat * decFloatCompareSignal(decFloat *result,
1702                                 const decFloat *dfl, const decFloat *dfr,
1703                                 decContext *set) {
1704  Int comp;                                  // work
1705  // NaNs are handled as usual, except that all NaNs signal
1706  if (DFISNAN(dfl) || DFISNAN(dfr)) {
1707    set->status|=DEC_Invalid_operation;
1708    return decNaNs(result, dfl, dfr, set);
1709    }
1710  // numeric comparison needed
1711  comp=decNumCompare(dfl, dfr, 0);
1712  decFloatZero(result);
1713  if (comp==0) return result;
1714  DFBYTE(result, DECBYTES-1)=0x01;      // LSD=1
1715  if (comp<0) DFBYTE(result, 0)|=0x80;  // set sign bit
1716  return result;
1717  } // decFloatCompareSignal
1718
1719/* ------------------------------------------------------------------ */
1720/* decFloatCompareTotal -- compare two decFloats with total ordering  */
1721/*                                                                    */
1722/*   result gets the result of comparing dfl and dfr                  */
1723/*   dfl    is the first decFloat (lhs)                               */
1724/*   dfr    is the second decFloat (rhs)                              */
1725/*   returns result, which may be -1, 0, or 1                         */
1726/* ------------------------------------------------------------------ */
1727decFloat * decFloatCompareTotal(decFloat *result,
1728                                const decFloat *dfl, const decFloat *dfr) {
1729  Int  comp;                                 // work
1730  uInt uiwork;                               // for macros
1731  #if QUAD
1732  uShort uswork;                             // ..
1733  #endif
1734  if (DFISNAN(dfl) || DFISNAN(dfr)) {
1735    Int nanl, nanr;                          // work
1736    // morph NaNs to +/- 1 or 2, leave numbers as 0
1737    nanl=DFISSNAN(dfl)+DFISQNAN(dfl)*2;      // quiet > signalling
1738    if (DFISSIGNED(dfl)) nanl=-nanl;
1739    nanr=DFISSNAN(dfr)+DFISQNAN(dfr)*2;
1740    if (DFISSIGNED(dfr)) nanr=-nanr;
1741    if (nanl>nanr) comp=+1;
1742     else if (nanl<nanr) comp=-1;
1743     else { // NaNs are the same type and sign .. must compare payload
1744      // buffers need +2 for QUAD
1745      uByte bufl[DECPMAX+4];                 // for LHS coefficient + foot
1746      uByte bufr[DECPMAX+4];                 // for RHS coefficient + foot
1747      uByte *ub, *uc;                        // work
1748      Int sigl;                              // signum of LHS
1749      sigl=(DFISSIGNED(dfl) ? -1 : +1);
1750
1751      // decode the coefficients
1752      // (shift both right two if Quad to make a multiple of four)
1753      #if QUAD
1754        UBFROMUS(bufl, 0);
1755        UBFROMUS(bufr, 0);
1756      #endif
1757      GETCOEFF(dfl, bufl+QUAD*2);            // decode from decFloat
1758      GETCOEFF(dfr, bufr+QUAD*2);            // ..
1759      // all multiples of four, here
1760      comp=0;                                // assume equal
1761      for (ub=bufl, uc=bufr; ub<bufl+DECPMAX+QUAD*2; ub+=4, uc+=4) {
1762        uInt ui=UBTOUI(ub);
1763        if (ui==UBTOUI(uc)) continue; // so far so same
1764        // about to find a winner; go by bytes in case little-endian
1765        for (;; ub++, uc++) {
1766          if (*ub==*uc) continue;
1767          if (*ub>*uc) comp=sigl;            // difference found
1768           else comp=-sigl;                  // ..
1769           break;
1770          }
1771        }
1772      } // same NaN type and sign
1773    }
1774   else {
1775    // numeric comparison needed
1776    comp=decNumCompare(dfl, dfr, 1);    // total ordering
1777    }
1778  decFloatZero(result);
1779  if (comp==0) return result;
1780  DFBYTE(result, DECBYTES-1)=0x01;      // LSD=1
1781  if (comp<0) DFBYTE(result, 0)|=0x80;  // set sign bit
1782  return result;
1783  } // decFloatCompareTotal
1784
1785/* ------------------------------------------------------------------ */
1786/* decFloatCompareTotalMag -- compare magnitudes with total ordering  */
1787/*                                                                    */
1788/*   result gets the result of comparing abs(dfl) and abs(dfr)        */
1789/*   dfl    is the first decFloat (lhs)                               */
1790/*   dfr    is the second decFloat (rhs)                              */
1791/*   returns result, which may be -1, 0, or 1                         */
1792/* ------------------------------------------------------------------ */
1793decFloat * decFloatCompareTotalMag(decFloat *result,
1794                                const decFloat *dfl, const decFloat *dfr) {
1795  decFloat a, b;                        // for copy if needed
1796  // copy and redirect signed operand(s)
1797  if (DFISSIGNED(dfl)) {
1798    decFloatCopyAbs(&a, dfl);
1799    dfl=&a;
1800    }
1801  if (DFISSIGNED(dfr)) {
1802    decFloatCopyAbs(&b, dfr);
1803    dfr=&b;
1804    }
1805  return decFloatCompareTotal(result, dfl, dfr);
1806  } // decFloatCompareTotalMag
1807
1808/* ------------------------------------------------------------------ */
1809/* decFloatCopy -- copy a decFloat as-is                              */
1810/*                                                                    */
1811/*   result gets the copy of dfl                                      */
1812/*   dfl    is the decFloat to copy                                   */
1813/*   returns result                                                   */
1814/*                                                                    */
1815/* This is a bitwise operation; no errors or exceptions are possible. */
1816/* ------------------------------------------------------------------ */
1817decFloat * decFloatCopy(decFloat *result, const decFloat *dfl) {
1818  if (dfl!=result) *result=*dfl;             // copy needed
1819  return result;
1820  } // decFloatCopy
1821
1822/* ------------------------------------------------------------------ */
1823/* decFloatCopyAbs -- copy a decFloat as-is and set sign bit to 0     */
1824/*                                                                    */
1825/*   result gets the copy of dfl with sign bit 0                      */
1826/*   dfl    is the decFloat to copy                                   */
1827/*   returns result                                                   */
1828/*                                                                    */
1829/* This is a bitwise operation; no errors or exceptions are possible. */
1830/* ------------------------------------------------------------------ */
1831decFloat * decFloatCopyAbs(decFloat *result, const decFloat *dfl) {
1832  if (dfl!=result) *result=*dfl;        // copy needed
1833  DFBYTE(result, 0)&=~0x80;             // zero sign bit
1834  return result;
1835  } // decFloatCopyAbs
1836
1837/* ------------------------------------------------------------------ */
1838/* decFloatCopyNegate -- copy a decFloat as-is with inverted sign bit */
1839/*                                                                    */
1840/*   result gets the copy of dfl with sign bit inverted               */
1841/*   dfl    is the decFloat to copy                                   */
1842/*   returns result                                                   */
1843/*                                                                    */
1844/* This is a bitwise operation; no errors or exceptions are possible. */
1845/* ------------------------------------------------------------------ */
1846decFloat * decFloatCopyNegate(decFloat *result, const decFloat *dfl) {
1847  if (dfl!=result) *result=*dfl;        // copy needed
1848  DFBYTE(result, 0)^=0x80;              // invert sign bit
1849  return result;
1850  } // decFloatCopyNegate
1851
1852/* ------------------------------------------------------------------ */
1853/* decFloatCopySign -- copy a decFloat with the sign of another       */
1854/*                                                                    */
1855/*   result gets the result of copying dfl with the sign of dfr       */
1856/*   dfl    is the first decFloat (lhs)                               */
1857/*   dfr    is the second decFloat (rhs)                              */
1858/*   returns result                                                   */
1859/*                                                                    */
1860/* This is a bitwise operation; no errors or exceptions are possible. */
1861/* ------------------------------------------------------------------ */
1862decFloat * decFloatCopySign(decFloat *result,
1863                            const decFloat *dfl, const decFloat *dfr) {
1864  uByte sign=(uByte)(DFBYTE(dfr, 0)&0x80);   // save sign bit
1865  if (dfl!=result) *result=*dfl;             // copy needed
1866  DFBYTE(result, 0)&=~0x80;                  // clear sign ..
1867  DFBYTE(result, 0)=(uByte)(DFBYTE(result, 0)|sign); // .. and set saved
1868  return result;
1869  } // decFloatCopySign
1870
1871/* ------------------------------------------------------------------ */
1872/* decFloatDigits -- return the number of digits in a decFloat        */
1873/*                                                                    */
1874/*   df is the decFloat to investigate                                */
1875/*   returns the number of significant digits in the decFloat; a      */
1876/*     zero coefficient returns 1 as does an infinity (a NaN returns  */
1877/*     the number of digits in the payload)                           */
1878/* ------------------------------------------------------------------ */
1879// private macro to extract a declet according to provided formula
1880// (form), and if it is non-zero then return the calculated digits
1881// depending on the declet number (n), where n=0 for the most
1882// significant declet; uses uInt dpd for work
1883#define dpdlenchk(n, form)  dpd=(form)&0x3ff;     \
1884  if (dpd) return (DECPMAX-1-3*(n))-(3-DPD2BCD8[dpd*4+3])
1885// next one is used when it is known that the declet must be
1886// non-zero, or is the final zero declet
1887#define dpdlendun(n, form)  dpd=(form)&0x3ff;     \
1888  if (dpd==0) return 1;                           \
1889  return (DECPMAX-1-3*(n))-(3-DPD2BCD8[dpd*4+3])
1890
1891uInt decFloatDigits(const decFloat *df) {
1892  uInt dpd;                        // work
1893  uInt sourhi=DFWORD(df, 0);       // top word from source decFloat
1894  #if QUAD
1895  uInt sourmh, sourml;
1896  #endif
1897  uInt sourlo;
1898
1899  if (DFISINF(df)) return 1;
1900  // A NaN effectively has an MSD of 0; otherwise if non-zero MSD
1901  // then the coefficient is full-length
1902  if (!DFISNAN(df) && DECCOMBMSD[sourhi>>26]) return DECPMAX;
1903
1904  #if DOUBLE
1905    if (sourhi&0x0003ffff) {     // ends in first
1906      dpdlenchk(0, sourhi>>8);
1907      sourlo=DFWORD(df, 1);
1908      dpdlendun(1, (sourhi<<2) | (sourlo>>30));
1909      } // [cannot drop through]
1910    sourlo=DFWORD(df, 1);  // sourhi not involved now
1911    if (sourlo&0xfff00000) {     // in one of first two
1912      dpdlenchk(1, sourlo>>30);  // very rare
1913      dpdlendun(2, sourlo>>20);
1914      } // [cannot drop through]
1915    dpdlenchk(3, sourlo>>10);
1916    dpdlendun(4, sourlo);
1917    // [cannot drop through]
1918
1919  #elif QUAD
1920    if (sourhi&0x00003fff) {     // ends in first
1921      dpdlenchk(0, sourhi>>4);
1922      sourmh=DFWORD(df, 1);
1923      dpdlendun(1, ((sourhi)<<6) | (sourmh>>26));
1924      } // [cannot drop through]
1925    sourmh=DFWORD(df, 1);
1926    if (sourmh) {
1927      dpdlenchk(1, sourmh>>26);
1928      dpdlenchk(2, sourmh>>16);
1929      dpdlenchk(3, sourmh>>6);
1930      sourml=DFWORD(df, 2);
1931      dpdlendun(4, ((sourmh)<<4) | (sourml>>28));
1932      } // [cannot drop through]
1933    sourml=DFWORD(df, 2);
1934    if (sourml) {
1935      dpdlenchk(4, sourml>>28);
1936      dpdlenchk(5, sourml>>18);
1937      dpdlenchk(6, sourml>>8);
1938      sourlo=DFWORD(df, 3);
1939      dpdlendun(7, ((sourml)<<2) | (sourlo>>30));
1940      } // [cannot drop through]
1941    sourlo=DFWORD(df, 3);
1942    if (sourlo&0xfff00000) {     // in one of first two
1943      dpdlenchk(7, sourlo>>30);  // very rare
1944      dpdlendun(8, sourlo>>20);
1945      } // [cannot drop through]
1946    dpdlenchk(9, sourlo>>10);
1947    dpdlendun(10, sourlo);
1948    // [cannot drop through]
1949  #endif
1950  } // decFloatDigits
1951
1952/* ------------------------------------------------------------------ */
1953/* decFloatDivide -- divide a decFloat by another                     */
1954/*                                                                    */
1955/*   result gets the result of dividing dfl by dfr:                   */
1956/*   dfl    is the first decFloat (lhs)                               */
1957/*   dfr    is the second decFloat (rhs)                              */
1958/*   set    is the context                                            */
1959/*   returns result                                                   */
1960/*                                                                    */
1961/* ------------------------------------------------------------------ */
1962// This is just a wrapper.
1963decFloat * decFloatDivide(decFloat *result,
1964                          const decFloat *dfl, const decFloat *dfr,
1965                          decContext *set) {
1966  return decDivide(result, dfl, dfr, set, DIVIDE);
1967  } // decFloatDivide
1968
1969/* ------------------------------------------------------------------ */
1970/* decFloatDivideInteger -- integer divide a decFloat by another      */
1971/*                                                                    */
1972/*   result gets the result of dividing dfl by dfr:                   */
1973/*   dfl    is the first decFloat (lhs)                               */
1974/*   dfr    is the second decFloat (rhs)                              */
1975/*   set    is the context                                            */
1976/*   returns result                                                   */
1977/*                                                                    */
1978/* ------------------------------------------------------------------ */
1979decFloat * decFloatDivideInteger(decFloat *result,
1980                             const decFloat *dfl, const decFloat *dfr,
1981                             decContext *set) {
1982  return decDivide(result, dfl, dfr, set, DIVIDEINT);
1983  } // decFloatDivideInteger
1984
1985/* ------------------------------------------------------------------ */
1986/* decFloatFMA -- multiply and add three decFloats, fused             */
1987/*                                                                    */
1988/*   result gets the result of (dfl*dfr)+dff with a single rounding   */
1989/*   dfl    is the first decFloat (lhs)                               */
1990/*   dfr    is the second decFloat (rhs)                              */
1991/*   dff    is the final decFloat (fhs)                               */
1992/*   set    is the context                                            */
1993/*   returns result                                                   */
1994/*                                                                    */
1995/* ------------------------------------------------------------------ */
1996decFloat * decFloatFMA(decFloat *result, const decFloat *dfl,
1997                       const decFloat *dfr, const decFloat *dff,
1998                       decContext *set) {
1999
2000  // The accumulator has the bytes needed for FiniteMultiply, plus

The first 2,000 of 3,908 lines.