Lines Matching defs:dn

339 /*  dn is the decNumber to receive the integer			      */
341 /* returns dn */
345 decNumber * decNumberFromInt32(decNumber *dn, Int in) {
353 decNumberFromUInt32(dn, unsig);
354 if (in<0) dn->bits=DECNEG; /* sign needed */
355 return dn;
358 decNumber * decNumberFromUInt32(decNumber *dn, uInt uin) {
360 decNumberZero(dn); /* clean */
361 if (uin==0) return dn; /* [or decGetDigits bad call] */
362 for (up=dn->lsu; uin>0; up++) {
366 dn->digits=decGetDigits(dn->lsu, up-dn->lsu);
367 return dn;
373 /* dn is the decNumber to convert */
380 Int decNumberToInt32(const decNumber *dn, decContext *set) {
382 if (decCheckOperands(DECUNRESU, DECUNUSED, dn, set)) return 0;
386 if (dn->bits&DECSPECIAL || dn->digits>10 || dn->exponent!=0) ; /* bad */
391 up=dn->lsu; /* -> lsu */
399 for (d=DECDPUN; d<dn->digits; up++, d+=DECDPUN) hi+=*up*powers[d-1];
403 if (dn->bits&DECNEG && hi==214748364 && lo==8) return 0x80000000;
408 if (dn->bits&DECNEG) return -i;
416 uInt decNumberToUInt32(const decNumber *dn, decContext *set) {
418 if (decCheckOperands(DECUNRESU, DECUNUSED, dn, set)) return 0;
421 if (dn->bits&DECSPECIAL || dn->digits>10 || dn->exponent!=0
422 || (dn->bits&DECNEG && !ISZERO(dn))); /* bad */
427 up=dn->lsu; /* -> lsu */
435 for (d=DECDPUN; d<dn->digits; up++, d+=DECDPUN) hi+=*up*powers[d-1];
449 /* decNumberToString(dn, string); */
450 /* decNumberToEngString(dn, string); */
452 /* dn is the decNumber to convert */
455 /* string must be at least dn->digits+14 characters long */
459 char * decNumberToString(const decNumber *dn, char *string){
460 decToString(dn, string, 0);
464 char * decNumberToEngString(const decNumber *dn, char *string){
465 decToString(dn, string, 1);
473 /* dn -- the number structure to fill */
489 decNumber * decNumberFromString(decNumber *dn, const char chars[],
511 return decNumberZero(dn);
547 decNumberZero(dn); /* be optimistic */
550 dn->bits=bits | DECINF;
556 dn->bits=bits | DECNAN; /* assume simple NaN */
559 dn->bits=bits | DECSNAN;
588 bits=dn->bits; /* for copy-back */
638 decNumberZero(dn); /* clean result */
651 if (d<=set->digits) res=dn->lsu; /* fits into supplied decNumber */
692 dn->bits=bits;
693 dn->exponent=exponent;
694 dn->digits=d;
699 decSetCoeff(dn, set, res, d, &residue, &status);
701 decFinalize(dn, set, &residue, &status);
705 if ((dn->exponent-1<set->emin-dn->digits)
706 || (dn->exponent-1>set->emax-set->digits)) {
708 decFinalize(dn, set, &residue, &status);
711 /* decNumberShow(dn); */
715 if (status!=0) decStatus(dn, status, set);
716 return dn;
3174 decNumber dn;
3194 decNumberZero(&dn); /* make a number with exponent 0 */
3195 decNumberQuantize(res, rhs, &dn, &workset);
3289 /* dn -- the decNumber to test */
3293 enum decClass decNumberClass(const decNumber *dn, decContext *set) {
3294 if (decNumberIsSpecial(dn)) {
3295 if (decNumberIsQNaN(dn)) return DEC_CLASS_QNAN;
3296 if (decNumberIsSNaN(dn)) return DEC_CLASS_SNAN;
3298 if (decNumberIsNegative(dn)) return DEC_CLASS_NEG_INF;
3302 if (decNumberIsNormal(dn, set)) { /* most common */
3303 if (decNumberIsNegative(dn)) return DEC_CLASS_NEG_NORMAL;
3307 if (decNumberIsZero(dn)) { /* most common */
3308 if (decNumberIsNegative(dn)) return DEC_CLASS_NEG_ZERO;
3311 if (decNumberIsNegative(dn)) return DEC_CLASS_NEG_SUBNORMAL;
3443 /* dn is the source decNumber */
3444 /* bcd is the uInt array that will receive dn->digits BCD bytes, */
3448 /* bcd must have at least dn->digits bytes. No error is possible; if */
3449 /* dn is a NaN or Infinite, digits must be 1 and the coefficient 0. */
3451 uByte * decNumberGetBCD(const decNumber *dn, uByte *bcd) {
3452 uByte *ub=bcd+dn->digits-1; /* -> lsd */
3453 const Unit *up=dn->lsu; /* Unit pointer, -> lsu */
3475 /* dn is the target decNumber */
3479 /* returns dn */
3481 /* dn must have space for at least n digits. No error is possible; */
3482 /* if dn is a NaN, or Infinite, or is to become a zero, n must be 1 */
3485 decNumber * decNumberSetBCD(decNumber *dn, const uByte *bcd, uInt n) {
3486 Unit *up=dn->lsu+D2U(dn->digits)-1; /* -> msu [target pointer] */
3494 for (;up>=dn->lsu; up--) { /* each Unit from msu */
3500 dn->digits=n; /* set digit count */
3501 return dn;
3506 /* dn is the decNumber to test */
3508 /* returns 1 if |dn| is finite and >=Nmin, 0 otherwise */
3510 Int decNumberIsNormal(const decNumber *dn, decContext *set) {
3513 if (decCheckOperands(DECUNRESU, DECUNUSED, dn, set)) return 0;
3516 if (decNumberIsSpecial(dn)) return 0; /* not finite */
3517 if (decNumberIsZero(dn)) return 0; /* not non-zero */
3519 ae=dn->exponent+dn->digits-1; /* adjusted exponent */
3526 /* dn is the decNumber to test */
3528 /* returns 1 if |dn| is finite, non-zero, and <Nmin, 0 otherwise */
3530 Int decNumberIsSubnormal(const decNumber *dn, decContext *set) {
3533 if (decCheckOperands(DECUNRESU, DECUNUSED, dn, set)) return 0;
3536 if (decNumberIsSpecial(dn)) return 0; /* not finite */
3537 if (decNumberIsZero(dn)) return 0; /* not non-zero */
3539 ae=dn->exponent+dn->digits-1; /* adjusted exponent */
3547 /* dn is the number to trim */
3548 /* returns dn */
3554 decNumber * decNumberTrim(decNumber *dn) {
3558 if (decCheckOperands(DECUNRESU, DECUNUSED, dn, DECUNCONT)) return dn;
3561 return decTrim(dn, &set, 0, 1, &dropped);
3576 /* dn is the number to set, with space for one digit */
3577 /* returns dn */
3582 decNumber * decNumberZero(decNumber *dn) {
3585 if (decCheckOperands(dn, DECUNUSED, DECUNUSED, DECUNCONT)) return dn;
3588 dn->bits=0;
3589 dn->exponent=0;
3590 dn->digits=1;
3591 dn->lsu[0]=0;
3592 return dn;
3602 /* dn is the number to lay out */
3606 /* string must be at least dn->digits+14 characters long */
3615 static void decToString(const decNumber *dn, char *string, Flag eng) {
3616 Int exp=dn->exponent; /* local copy */
3621 const Unit *up=dn->lsu+D2U(dn->digits)-1; /* -> msu [input pointer] */
3625 if (decCheckOperands(DECUNRESU, dn, DECUNUSED, DECUNCONT)) {
3630 if (decNumberIsNegative(dn)) { /* Negatives get a minus */
3634 if (dn->bits&DECSPECIAL) { /* Is a special value */
3635 if (decNumberIsInfinite(dn)) {
3640 if (dn->bits&DECSNAN) { /* signalling NaN */
3648 if (exp!=0 || (*dn->lsu==0 && dn->digits==1)) return;
3653 cut=MSUDIGITS(dn->digits); /* [faster than remainder] */
3657 for (;up>=dn->lsu; up--) { /* each Unit from msu */
3666 pre=dn->digits+exp; /* digits before '.' */
3669 e=exp+dn->digits-1; /* calculate E value */
3686 if (!ISZERO(dn)) pre+=adj;
3702 if (up==dn->lsu) break; /* out of input digits (pre>digits) */
3709 if (n<dn->digits) { /* more to come, after '.' */
3713 if (up==dn->lsu) break; /* out of input digits */
3729 if (up==dn->lsu) break; /* out of input digits */
6573 /* dn is the number to trim or normalize */
6578 /* returns dn */
6585 static decNumber * decTrim(decNumber *dn, decContext *set, Flag all,
6592 if (decCheckOperands(dn, DECUNUSED, DECUNUSED, DECUNCONT)) return dn;
6596 if ((dn->bits & DECSPECIAL) /* fast exit if special .. */
6597 || (*dn->lsu & 0x01)) return dn; /* .. or odd */
6598 if (ISZERO(dn)) { /* .. or 0 */
6599 dn->exponent=0; /* (sign is preserved) */
6600 return dn;
6604 exp=dn->exponent;
6606 up=dn->lsu; /* -> current Unit */
6607 for (d=0; d<dn->digits-1; d++) { /* [don't strip the final digit] */
6629 if (d==0) return dn; /* none to drop */
6633 Int maxd=set->emax-set->digits+1-dn->exponent;
6634 if (maxd<=0) return dn; /* nothing possible */
6639 decShiftToLeast(dn->lsu, D2U(dn->digits), d);
6640 dn->exponent+=d; /* maintain numerical value */
6641 dn->digits-=d; /* new length */
6643 return dn;
6786 /* dn is the number to round (dn->digits is > set->digits) */
6801 static decNumber *decRoundOperand(const decNumber *dn, decContext *set,
6815 decCopyFit(res, dn, set, &residue, &newstatus);
6847 /* dn is the number whose coefficient array is to be set. */
6850 /* lsu -> lsu of the source coefficient [may be dn->lsu] */
6851 /* len is digits in the source coefficient [may be dn->digits] */
6861 /* dn->lsu and len must == dn->digits. */
6865 /* if dn->lsu==lsu). */
6872 /* dn->digits, dn->lsu (and as required), and dn->exponent are */
6873 /* updated as necessary. dn->bits (sign) is unchanged. */
6883 static void decSetCoeff(decNumber *dn, decContext *set, const Unit *lsu,
6896 if (dn->lsu!=lsu) { /* copy needed */
6900 for (target=dn->lsu; count>0; target++, up++, count-=DECDPUN)
6902 dn->digits=len; /* set the new length */
6904 /* dn->exponent and residue are unchanged, record any inexactitude */
6910 dn->exponent+=discard; /* maintain numerical value */
6925 *dn->lsu=0; /* coefficient will now be 0 */
6926 dn->digits=1; /* .. */
6956 *dn->lsu=0; /* .. result is 0 */
6957 dn->digits=1; /* .. */
6961 dn->digits=count; /* set the new length */
6964 for (target=dn->lsu; count>0; target++, up++, count-=DECDPUN)
7001 *dn->lsu=0; /* .. result is 0 */
7002 dn->digits=1; /* .. */
7006 dn->digits=count; /* set the new length */
7008 for (target=dn->lsu; ; target++) {
7035 /* dn is the number, with space for set->digits digits */
7044 /* is, of the opposite sign to dn. In this case the */
7061 /* All fields in dn are updated as required. */
7064 static void decApplyRound(decNumber *dn, decContext *set, Int residue,
7077 /* positive residue and the lsd of dn is 0 or 5, in which case */
7081 Int lsd5=*dn->lsu%5; /* get lsd and quintate */
7100 if (*dn->lsu & 0x01) bump=1;
7115 if (decNumberIsNegative(dn)) {
7126 if (!decNumberIsNegative(dn)) {
7152 uInt count=dn->digits; /* digits to be checked */
7153 for (up=dn->lsu; ; up++) {
7159 for (up=up-1; up>=dn->lsu; up--) *up=0; /* others all to 0 */
7160 dn->exponent++; /* and bump exponent */
7162 if ((dn->exponent+dn->digits)>set->emax+1) {
7163 decSetOverflow(dn, set, status);
7176 uInt count=dn->digits; /* digits to be checked */
7177 for (up=dn->lsu; ; up++) {
7185 for (up=up-1; up>=dn->lsu; up--) *up=(Unit)powers[DECDPUN]-1;
7186 dn->exponent--; /* and bump exponent */
7192 /* dn->exponent, set->digits); */
7193 if (dn->exponent+1==set->emin-set->digits+1) {
7194 if (count==1 && dn->digits==1) *sup=0; /* here 9 -> 0[.9] */
7197 dn->digits--;
7199 dn->exponent++;
7213 decUnitAddSub(dn->lsu, D2U(dn->digits), uarrone, 1, 0, dn->lsu, bump);
7220 /* dn is the number */
7233 static void decFinish(decNumber *dn, decContext *set, Int *residue,
7236 if ISZERO(dn) { /* value is zero */
7237 dn->exponent=0; /* clean exponent .. */
7238 dn->bits=0; /* .. and sign */
7241 if (dn->exponent>=0) { /* non-negative exponent */
7243 if (set->digits >= (dn->exponent+dn->digits)) {
7244 dn->digits=decShiftToMost(dn->lsu, dn->digits, dn->exponent);
7245 dn->exponent=0;
7250 decFinalize(dn, set, residue, status);
7257 /* dn is the number */
7268 static void decFinalize(decNumber *dn, decContext *set, Int *residue,
7271 Int tinyexp=set->emin-dn->digits+1; /* precalculate subnormal boundary */
7279 if (dn->exponent<=tinyexp) { /* prefilter */
7282 /* A very nasty case here is dn == Nmin and residue<0 */
7283 if (dn->exponent<tinyexp) {
7285 decSetSubnormal(dn, set, residue, status);
7288 /* Equals case: only subnormal if dn=Nmin and negative residue */
7292 comp=decCompare(dn, &nmin, 1); /* (signless compare) */
7297 if (*residue<0 && comp==0) { /* neg residue and dn==Nmin */
7298 decApplyRound(dn, set, *residue, status); /* might force down */
7299 decSetSubnormal(dn, set, residue, status);
7305 if (*residue!=0) decApplyRound(dn, set, *residue, status);
7308 if (dn->exponent<=set->emax-set->digits+1) return; /* neither needed */
7312 if (dn->exponent>set->emax-dn->digits+1) { /* too big */
7313 decSetOverflow(dn, set, status);
7320 shift=dn->exponent-(set->emax-set->digits+1);
7323 if (!ISZERO(dn)) {
7324 dn->digits=decShiftToMost(dn->lsu, dn->digits, shift);
7326 dn->exponent-=shift; /* adjust the exponent to match */
7334 /* dn is the number (used for sign [only] and result) */
7340 /* dn and the rounding mode, following IEEE 754 rules. */
7342 static void decSetOverflow(decNumber *dn, decContext *set, uInt *status) {
7344 uByte sign=dn->bits&DECNEG; /* clean and save sign bit */
7346 if (ISZERO(dn)) { /* zero does not overflow magnitude */
7349 if (dn->exponent>emax) { /* clamp required */
7350 dn->exponent=emax;
7356 decNumberZero(dn);
7373 decSetMaxValue(dn, set);
7374 dn->bits=sign; /* set sign */
7376 else dn->bits=sign|DECINF; /* Value is +/-Infinity */
7383 /* dn is the number to set */
7388 static void decSetMaxValue(decNumber *dn, decContext *set) {
7391 dn->digits=count;
7393 for (up=dn->lsu; ; up++) {
7401 dn->bits=0; /* + sign */
7402 dn->exponent=set->emax-set->digits+1;
7408 /* dn is the number (used as input as well as output; it may have */
7422 static void decSetSubnormal(decNumber *dn, decContext *set, Int *residue,
7430 decNumberZero(dn);
7441 if ISZERO(dn) { /* value is zero */
7449 if (dn->exponent<etiny) { /* clamp required */
7450 dn->exponent=etiny;
7457 adjust=etiny-dn->exponent; /* calculate digits to remove */
7469 workset.digits=dn->digits-adjust; /* set requested length */
7472 decSetCoeff(dn, &workset, dn->lsu, dn->digits, residue, status);
7473 decApplyRound(dn, &workset, *residue, status);
7481 if (dn->exponent>etiny) {
7482 dn->digits=decShiftToMost(dn->lsu, dn->digits, 1);
7483 dn->exponent--; /* (re)adjust the exponent. */
7487 if (ISZERO(dn)) *status|=DEC_Clamped;
7524 /* dn is the number [which will not be altered] */
7533 /* The sign can be determined from dn by the caller when BIGEVEN or */
7536 static Int decGetInt(const decNumber *dn) {
7540 Int ilength=dn->digits+dn->exponent; /* integral length */
7541 Flag neg=decNumberIsNegative(dn); /* 1 if -ve */
7551 if (ISZERO(dn)) return 0; /* zeros are OK, with any exponent */
7553 up=dn->lsu; /* ready for lsu */
7555 if (dn->exponent>=0) { /* relatively easy */
7557 got=dn->exponent;
7560 Int count=-dn->exponent; /* digits to discard */
7616 /* dn is the number to be decapitated */
7617 /* drop is the number of digits to be removed from the left of dn; */
7618 /* this must be <= dn->digits (if equal, the coefficient is */
7621 /* Returns dn; dn->digits will be <= the initial digits less drop */
7623 /* which will also be removed). Only dn->lsu and dn->digits change. */
7625 static decNumber *decDecap(decNumber *dn, Int drop) {
7628 if (drop>=dn->digits) { /* losing the whole thing */
7630 if (drop>dn->digits)
7632 (LI)drop, (LI)dn->digits);
7634 dn->lsu[0]=0;
7635 dn->digits=1;
7636 return dn;
7638 msu=dn->lsu+D2U(dn->digits-drop)-1; /* -> likely msu */
7639 cut=MSUDIGITS(dn->digits-drop); /* digits to be in use in msu */
7642 dn->digits=decGetDigits(dn->lsu, msu-dn->lsu+1);
7643 return dn;
7723 /* dn is the number to set if error */
7735 static void decStatus(decNumber *dn, uInt status, decContext *set) {
7740 decNumberZero(dn); /* other error: clean throughout */
7741 dn->bits=DECNAN; /* and make a quiet NaN */
7801 /* dn is the number to show */
7807 void decNumberShow(const decNumber *dn) {
7812 if (dn==NULL) {
7815 if (decNumberIsNegative(dn)) isign='-';
7817 if (dn->bits&DECSPECIAL) { /* Is a special value */
7818 if (decNumberIsInfinite(dn)) printf("Infinity");
7820 if (dn->bits&DECSNAN) printf("sNaN"); /* signalling NaN */
7824 if (dn->exponent==0 && dn->digits==1 && *dn->lsu==0) {
7832 up=dn->lsu+D2U(dn->digits)-1; /* msu */
7834 for (up=up-1; up>=dn->lsu; up--) {
7843 if (dn->exponent!=0) {
7845 if (dn->exponent<0) esign='-';
7846 printf(" E%c%ld", esign, (LI)abs(dn->exponent));
7848 printf(" [%ld]\n", (LI)dn->digits);
7944 /* dn is the number to check */
7950 static Flag decCheckNumber(const decNumber *dn) {
7956 if (dn==NULL) { /* hopeless */
7964 if (dn->bits & DECSPECIAL) {
7965 if (dn->exponent!=0) {
7968 (LI)dn->exponent, dn->bits);
7973 if (decNumberIsInfinite(dn)) {
7974 if (dn->digits!=1) {
7976 printf("Digits %ld (not 1) for an infinity.\n", (LI)dn->digits);
7979 if (*dn->lsu!=0) {
7981 printf("LSU %ld (not 0) for an infinity.\n", (LI)*dn->lsu);
7983 decDumpAr('I', dn->lsu, D2U(dn->digits));
7992 if (dn->digits<1 || dn->digits>DECNUMMAXP) {
7994 printf("Digits %ld in number.\n", (LI)dn->digits);
7998 d=dn->digits;
8000 for (up=dn->lsu; d>0; up++) {
8004 if (dn->digits>1 && *up<powers[d-1]) {
8007 decNumberShow(dn);
8014 (LI)*up, (LI)dn->digits, (LI)(up-dn->lsu), (LI)maxuint);
8023 ae=dn->exponent+dn->digits-1; /* adjusted exponent */
8030 decNumberShow(dn);
8036 decNumberShow(dn);
8045 /* dn is the number to check */
8051 static void decCheckInexact(const decNumber *dn, decContext *set) {
8054 && (set->digits!=dn->digits) && !(dn->bits & DECSPECIAL)) {
8057 (LI)dn->digits);
8058 decNumberShow(dn);
8064 if (dn!=NULL && dn->digits==0) set->status|=DEC_Invalid_operation;