Lines Matching refs:dn
358 /* dn is the decNumber to receive the integer */
360 /* returns dn */
364 U_CAPI decNumber * U_EXPORT2 uprv_decNumberFromInt32(decNumber *dn, Int in) {
372 uprv_decNumberFromUInt32(dn, unsig);
373 if (in<0) dn->bits=DECNEG; /* sign needed */
374 return dn;
377 U_CAPI decNumber * U_EXPORT2 uprv_decNumberFromUInt32(decNumber *dn, uInt uin) {
379 uprv_decNumberZero(dn); /* clean */
380 if (uin==0) return dn; /* [or decGetDigits bad call] */
381 for (up=dn->lsu; uin>0; up++) {
385 dn->digits=decGetDigits(dn->lsu, up-dn->lsu);
386 return dn;
392 /* dn is the decNumber to convert */
399 U_CAPI Int U_EXPORT2 uprv_decNumberToInt32(const decNumber *dn, decContext *set) {
401 if (decCheckOperands(DECUNRESU, DECUNUSED, dn, set)) return 0;
405 if (dn->bits&DECSPECIAL || dn->digits>10 || dn->exponent!=0) ; /* bad */
410 up=dn->lsu; /* -> lsu */
418 for (d=DECDPUN; d<dn->digits; up++, d+=DECDPUN) hi+=*up*powers[d-1];
422 if (dn->bits&DECNEG && hi==214748364 && lo==8) return 0x80000000;
427 if (dn->bits&DECNEG) return -i;
435 U_CAPI uInt U_EXPORT2 uprv_decNumberToUInt32(const decNumber *dn, decContext *set) {
437 if (decCheckOperands(DECUNRESU, DECUNUSED, dn, set)) return 0;
440 if (dn->bits&DECSPECIAL || dn->digits>10 || dn->exponent!=0
441 || (dn->bits&DECNEG && !ISZERO(dn))); /* bad */
446 up=dn->lsu; /* -> lsu */
454 for (d=DECDPUN; d<dn->digits; up++, d+=DECDPUN) hi+=*up*powers[d-1];
468 /* decNumberToString(dn, string); */
469 /* decNumberToEngString(dn, string); */
471 /* dn is the decNumber to convert */
474 /* string must be at least dn->digits+14 characters long */
478 U_CAPI char * U_EXPORT2 uprv_decNumberToString(const decNumber *dn, char *string){
479 decToString(dn, string, 0);
483 U_CAPI char * U_EXPORT2 uprv_decNumberToEngString(const decNumber *dn, char *string){
484 decToString(dn, string, 1);
492 /* dn -- the number structure to fill */
508 U_CAPI decNumber * U_EXPORT2 uprv_decNumberFromString(decNumber *dn, const char chars[],
530 return uprv_decNumberZero(dn);
566 uprv_decNumberZero(dn); /* be optimistic */
569 dn->bits=bits | DECINF;
575 dn->bits=bits | DECNAN; /* assume simple NaN */
578 dn->bits=bits | DECSNAN;
607 bits=dn->bits; /* for copy-back */
657 uprv_decNumberZero(dn); /* clean result */
670 if (d<=set->digits) res=dn->lsu; /* fits into supplied decNumber */
711 dn->bits=bits;
712 dn->exponent=exponent;
713 dn->digits=d;
718 decSetCoeff(dn, set, res, d, &residue, &status);
720 decFinalize(dn, set, &residue, &status);
724 if ((dn->exponent-1<set->emin-dn->digits)
725 || (dn->exponent-1>set->emax-set->digits)) {
727 decFinalize(dn, set, &residue, &status);
730 /* decNumberShow(dn); */
734 if (status!=0) decStatus(dn, status, set);
735 return dn;
3207 decNumber dn;
3227 uprv_decNumberZero(&dn); /* make a number with exponent 0 */
3228 uprv_decNumberQuantize(res, rhs, &dn, &workset);
3322 /* dn -- the decNumber to test */
3326 enum decClass uprv_decNumberClass(const decNumber *dn, decContext *set) {
3327 if (decNumberIsSpecial(dn)) {
3328 if (decNumberIsQNaN(dn)) return DEC_CLASS_QNAN;
3329 if (decNumberIsSNaN(dn)) return DEC_CLASS_SNAN;
3331 if (decNumberIsNegative(dn)) return DEC_CLASS_NEG_INF;
3335 if (uprv_decNumberIsNormal(dn, set)) { /* most common */
3336 if (decNumberIsNegative(dn)) return DEC_CLASS_NEG_NORMAL;
3340 if (decNumberIsZero(dn)) { /* most common */
3341 if (decNumberIsNegative(dn)) return DEC_CLASS_NEG_ZERO;
3344 if (decNumberIsNegative(dn)) return DEC_CLASS_NEG_SUBNORMAL;
3476 /* dn is the source decNumber */
3477 /* bcd is the uInt array that will receive dn->digits BCD bytes, */
3481 /* bcd must have at least dn->digits bytes. No error is possible; if */
3482 /* dn is a NaN or Infinite, digits must be 1 and the coefficient 0. */
3484 U_CAPI uByte * U_EXPORT2 uprv_decNumberGetBCD(const decNumber *dn, uByte *bcd) {
3485 uByte *ub=bcd+dn->digits-1; /* -> lsd */
3486 const Unit *up=dn->lsu; /* Unit pointer, -> lsu */
3508 /* dn is the target decNumber */
3512 /* returns dn */
3514 /* dn must have space for at least n digits. No error is possible; */
3515 /* if dn is a NaN, or Infinite, or is to become a zero, n must be 1 */
3518 U_CAPI decNumber * U_EXPORT2 uprv_decNumberSetBCD(decNumber *dn, const uByte *bcd, uInt n) {
3519 Unit *up=dn->lsu+D2U(dn->digits)-1; /* -> msu [target pointer] */
3527 for (;up>=dn->lsu; up--) { /* each Unit from msu */
3533 dn->digits=n; /* set digit count */
3534 return dn;
3539 /* dn is the decNumber to test */
3541 /* returns 1 if |dn| is finite and >=Nmin, 0 otherwise */
3543 Int uprv_decNumberIsNormal(const decNumber *dn, decContext *set) {
3546 if (decCheckOperands(DECUNRESU, DECUNUSED, dn, set)) return 0;
3549 if (decNumberIsSpecial(dn)) return 0; /* not finite */
3550 if (decNumberIsZero(dn)) return 0; /* not non-zero */
3552 ae=dn->exponent+dn->digits-1; /* adjusted exponent */
3559 /* dn is the decNumber to test */
3561 /* returns 1 if |dn| is finite, non-zero, and <Nmin, 0 otherwise */
3563 Int uprv_decNumberIsSubnormal(const decNumber *dn, decContext *set) {
3566 if (decCheckOperands(DECUNRESU, DECUNUSED, dn, set)) return 0;
3569 if (decNumberIsSpecial(dn)) return 0; /* not finite */
3570 if (decNumberIsZero(dn)) return 0; /* not non-zero */
3572 ae=dn->exponent+dn->digits-1; /* adjusted exponent */
3580 /* dn is the number to trim */
3581 /* returns dn */
3587 U_CAPI decNumber * U_EXPORT2 uprv_decNumberTrim(decNumber *dn) {
3591 if (decCheckOperands(DECUNRESU, DECUNUSED, dn, DECUNCONT)) return dn;
3594 return decTrim(dn, &set, 0, 1, &dropped);
3609 /* dn is the number to set, with space for one digit */
3610 /* returns dn */
3615 U_CAPI decNumber * U_EXPORT2 uprv_decNumberZero(decNumber *dn) {
3618 if (decCheckOperands(dn, DECUNUSED, DECUNUSED, DECUNCONT)) return dn;
3621 dn->bits=0;
3622 dn->exponent=0;
3623 dn->digits=1;
3624 dn->lsu[0]=0;
3625 return dn;
3635 /* dn is the number to lay out */
3639 /* string must be at least dn->digits+14 characters long */
3648 static void decToString(const decNumber *dn, char *string, Flag eng) {
3649 Int exp=dn->exponent; /* local copy */
3654 const Unit *up=dn->lsu+D2U(dn->digits)-1; /* -> msu [input pointer] */
3658 if (decCheckOperands(DECUNRESU, dn, DECUNUSED, DECUNCONT)) {
3663 if (decNumberIsNegative(dn)) { /* Negatives get a minus */
3667 if (dn->bits&DECSPECIAL) { /* Is a special value */
3668 if (decNumberIsInfinite(dn)) {
3673 if (dn->bits&DECSNAN) { /* signalling NaN */
3681 if (exp!=0 || (*dn->lsu==0 && dn->digits==1)) return;
3686 cut=MSUDIGITS(dn->digits); /* [faster than remainder] */
3690 for (;up>=dn->lsu; up--) { /* each Unit from msu */
3699 pre=dn->digits+exp; /* digits before '.' */
3702 e=exp+dn->digits-1; /* calculate E value */
3719 if (!ISZERO(dn)) pre+=adj;
3735 if (up==dn->lsu) break; /* out of input digits (pre>digits) */
3742 if (n<dn->digits) { /* more to come, after '.' */
3746 if (up==dn->lsu) break; /* out of input digits */
3762 if (up==dn->lsu) break; /* out of input digits */
6619 /* dn is the number to trim or normalize */
6624 /* returns dn */
6631 static decNumber * decTrim(decNumber *dn, decContext *set, Flag all,
6638 if (decCheckOperands(dn, DECUNUSED, DECUNUSED, DECUNCONT)) return dn;
6642 if ((dn->bits & DECSPECIAL) /* fast exit if special .. */
6643 || (*dn->lsu & 0x01)) return dn; /* .. or odd */
6644 if (ISZERO(dn)) { /* .. or 0 */
6645 dn->exponent=0; /* (sign is preserved) */
6646 return dn;
6650 exp=dn->exponent;
6652 up=dn->lsu; /* -> current Unit */
6653 for (d=0; d<dn->digits-1; d++) { /* [don't strip the final digit] */
6675 if (d==0) return dn; /* none to drop */
6679 Int maxd=set->emax-set->digits+1-dn->exponent;
6680 if (maxd<=0) return dn; /* nothing possible */
6685 decShiftToLeast(dn->lsu, D2U(dn->digits), d);
6686 dn->exponent+=d; /* maintain numerical value */
6687 dn->digits-=d; /* new length */
6689 return dn;
6832 /* dn is the number to round (dn->digits is > set->digits) */
6847 static decNumber *decRoundOperand(const decNumber *dn, decContext *set,
6861 decCopyFit(res, dn, set, &residue, &newstatus);
6893 /* dn is the number whose coefficient array is to be set. */
6896 /* lsu -> lsu of the source coefficient [may be dn->lsu] */
6897 /* len is digits in the source coefficient [may be dn->digits] */
6907 /* dn->lsu and len must == dn->digits. */
6911 /* if dn->lsu==lsu). */
6918 /* dn->digits, dn->lsu (and as required), and dn->exponent are */
6919 /* updated as necessary. dn->bits (sign) is unchanged. */
6929 static void decSetCoeff(decNumber *dn, decContext *set, const Unit *lsu,
6942 if (dn->lsu!=lsu) { /* copy needed */
6946 for (target=dn->lsu; count>0; target++, up++, count-=DECDPUN)
6948 dn->digits=len; /* set the new length */
6950 /* dn->exponent and residue are unchanged, record any inexactitude */
6956 dn->exponent+=discard; /* maintain numerical value */
6971 *dn->lsu=0; /* coefficient will now be 0 */
6972 dn->digits=1; /* .. */
7002 *dn->lsu=0; /* .. result is 0 */
7003 dn->digits=1; /* .. */
7007 dn->digits=count; /* set the new length */
7010 for (target=dn->lsu; count>0; target++, up++, count-=DECDPUN)
7048 *dn->lsu=0; /* .. result is 0 */
7049 dn->digits=1; /* .. */
7053 dn->digits=count; /* set the new length */
7055 for (target=dn->lsu; ; target++) {
7082 /* dn is the number, with space for set->digits digits */
7091 /* is, of the opposite sign to dn. In this case the */
7108 /* All fields in dn are updated as required. */
7111 static void decApplyRound(decNumber *dn, decContext *set, Int residue,
7124 /* positive residue and the lsd of dn is 0 or 5, in which case */
7128 Int lsd5=*dn->lsu%5; /* get lsd and quintate */
7147 if (*dn->lsu & 0x01) bump=1;
7162 if (decNumberIsNegative(dn)) {
7173 if (!decNumberIsNegative(dn)) {
7199 uInt count=dn->digits; /* digits to be checked */
7200 for (up=dn->lsu; ; up++) {
7206 for (up=up-1; up>=dn->lsu; up--) *up=0; /* others all to 0 */
7207 dn->exponent++; /* and bump exponent */
7209 if ((dn->exponent+dn->digits)>set->emax+1) {
7210 decSetOverflow(dn, set, status);
7223 uInt count=dn->digits; /* digits to be checked */
7224 for (up=dn->lsu; ; up++) {
7232 for (up=up-1; up>=dn->lsu; up--) *up=(Unit)powers[DECDPUN]-1;
7233 dn->exponent--; /* and bump exponent */
7239 /* dn->exponent, set->digits); */
7240 if (dn->exponent+1==set->emin-set->digits+1) {
7241 if (count==1 && dn->digits==1) *sup=0; /* here 9 -> 0[.9] */
7244 dn->digits--;
7246 dn->exponent++;
7260 decUnitAddSub(dn->lsu, D2U(dn->digits), uarrone, 1, 0, dn->lsu, bump);
7267 /* dn is the number */
7280 static void decFinish(decNumber *dn, decContext *set, Int *residue,
7283 if ISZERO(dn) { /* value is zero */
7284 dn->exponent=0; /* clean exponent .. */
7285 dn->bits=0; /* .. and sign */
7288 if (dn->exponent>=0) { /* non-negative exponent */
7290 if (set->digits >= (dn->exponent+dn->digits)) {
7291 dn->digits=decShiftToMost(dn->lsu, dn->digits, dn->exponent);
7292 dn->exponent=0;
7297 decFinalize(dn, set, residue, status);
7304 /* dn is the number */
7315 static void decFinalize(decNumber *dn, decContext *set, Int *residue,
7318 Int tinyexp=set->emin-dn->digits+1; /* precalculate subnormal boundary */
7326 if (dn->exponent<=tinyexp) { /* prefilter */
7329 /* A very nasty case here is dn == Nmin and residue<0 */
7330 if (dn->exponent<tinyexp) {
7332 decSetSubnormal(dn, set, residue, status);
7335 /* Equals case: only subnormal if dn=Nmin and negative residue */
7339 comp=decCompare(dn, &nmin, 1); /* (signless compare) */
7344 if (*residue<0 && comp==0) { /* neg residue and dn==Nmin */
7345 decApplyRound(dn, set, *residue, status); /* might force down */
7346 decSetSubnormal(dn, set, residue, status);
7352 if (*residue!=0) decApplyRound(dn, set, *residue, status);
7355 if (dn->exponent<=set->emax-set->digits+1) return; /* neither needed */
7359 if (dn->exponent>set->emax-dn->digits+1) { /* too big */
7360 decSetOverflow(dn, set, status);
7367 shift=dn->exponent-(set->emax-set->digits+1);
7370 if (!ISZERO(dn)) {
7371 dn->digits=decShiftToMost(dn->lsu, dn->digits, shift);
7373 dn->exponent-=shift; /* adjust the exponent to match */
7381 /* dn is the number (used for sign [only] and result) */
7387 /* dn and the rounding mode, following IEEE 754 rules. */
7389 static void decSetOverflow(decNumber *dn, decContext *set, uInt *status) {
7391 uByte sign=dn->bits&DECNEG; /* clean and save sign bit */
7393 if (ISZERO(dn)) { /* zero does not overflow magnitude */
7396 if (dn->exponent>emax) { /* clamp required */
7397 dn->exponent=emax;
7403 uprv_decNumberZero(dn);
7420 decSetMaxValue(dn, set);
7421 dn->bits=sign; /* set sign */
7423 else dn->bits=sign|DECINF; /* Value is +/-Infinity */
7430 /* dn is the number to set */
7435 static void decSetMaxValue(decNumber *dn, decContext *set) {
7438 dn->digits=count;
7440 for (up=dn->lsu; ; up++) {
7448 dn->bits=0; /* + sign */
7449 dn->exponent=set->emax-set->digits+1;
7455 /* dn is the number (used as input as well as output; it may have */
7469 static void decSetSubnormal(decNumber *dn, decContext *set, Int *residue,
7477 uprv_decNumberZero(dn);
7488 if ISZERO(dn) { /* value is zero */
7496 if (dn->exponent<etiny) { /* clamp required */
7497 dn->exponent=etiny;
7504 adjust=etiny-dn->exponent; /* calculate digits to remove */
7516 workset.digits=dn->digits-adjust; /* set requested length */
7519 decSetCoeff(dn, &workset, dn->lsu, dn->digits, residue, status);
7520 decApplyRound(dn, &workset, *residue, status);
7528 if (dn->exponent>etiny) {
7529 dn->digits=decShiftToMost(dn->lsu, dn->digits, 1);
7530 dn->exponent--; /* (re)adjust the exponent. */
7534 if (ISZERO(dn)) *status|=DEC_Clamped;
7571 /* dn is the number [which will not be altered] */
7580 /* The sign can be determined from dn by the caller when BIGEVEN or */
7583 static Int decGetInt(const decNumber *dn) {
7587 Int ilength=dn->digits+dn->exponent; /* integral length */
7588 Flag neg=decNumberIsNegative(dn); /* 1 if -ve */
7598 if (ISZERO(dn)) return 0; /* zeros are OK, with any exponent */
7600 up=dn->lsu; /* ready for lsu */
7602 if (dn->exponent>=0) { /* relatively easy */
7604 got=dn->exponent;
7607 Int count=-dn->exponent; /* digits to discard */
7663 /* dn is the number to be decapitated */
7664 /* drop is the number of digits to be removed from the left of dn; */
7665 /* this must be <= dn->digits (if equal, the coefficient is */
7668 /* Returns dn; dn->digits will be <= the initial digits less drop */
7670 /* which will also be removed). Only dn->lsu and dn->digits change. */
7672 static decNumber *decDecap(decNumber *dn, Int drop) {
7675 if (drop>=dn->digits) { /* losing the whole thing */
7677 if (drop>dn->digits)
7679 (LI)drop, (LI)dn->digits);
7681 dn->lsu[0]=0;
7682 dn->digits=1;
7683 return dn;
7685 msu=dn->lsu+D2U(dn->digits-drop)-1; /* -> likely msu */
7686 cut=MSUDIGITS(dn->digits-drop); /* digits to be in use in msu */
7689 dn->digits=decGetDigits(dn->lsu, msu-dn->lsu+1);
7690 return dn;
7770 /* dn is the number to set if error */
7782 static void decStatus(decNumber *dn, uInt status, decContext *set) {
7787 uprv_decNumberZero(dn); /* other error: clean throughout */
7788 dn->bits=DECNAN; /* and make a quiet NaN */
7848 /* dn is the number to show */
7854 void uprv_decNumberShow(const decNumber *dn) {
7859 if (dn==NULL) {
7862 if (decNumberIsNegative(dn)) isign='-';
7864 if (dn->bits&DECSPECIAL) { /* Is a special value */
7865 if (decNumberIsInfinite(dn)) printf("Infinity");
7867 if (dn->bits&DECSNAN) printf("sNaN"); /* signalling NaN */
7871 if (dn->exponent==0 && dn->digits==1 && *dn->lsu==0) {
7879 up=dn->lsu+D2U(dn->digits)-1; /* msu */
7881 for (up=up-1; up>=dn->lsu; up--) {
7890 if (dn->exponent!=0) {
7892 if (dn->exponent<0) esign='-';
7893 printf(" E%c%ld", esign, (LI)abs(dn->exponent));
7895 printf(" [%ld]\n", (LI)dn->digits);
7991 /* dn is the number to check */
7997 static Flag decCheckNumber(const decNumber *dn) {
8003 if (dn==NULL) { /* hopeless */
8011 if (dn->bits & DECSPECIAL) {
8012 if (dn->exponent!=0) {
8015 (LI)dn->exponent, dn->bits);
8020 if (decNumberIsInfinite(dn)) {
8021 if (dn->digits!=1) {
8023 printf("Digits %ld (not 1) for an infinity.\n", (LI)dn->digits);
8026 if (*dn->lsu!=0) {
8028 printf("LSU %ld (not 0) for an infinity.\n", (LI)*dn->lsu);
8030 decDumpAr('I', dn->lsu, D2U(dn->digits));
8039 if (dn->digits<1 || dn->digits>DECNUMMAXP) {
8041 printf("Digits %ld in number.\n", (LI)dn->digits);
8045 d=dn->digits;
8047 for (up=dn->lsu; d>0; up++) {
8051 if (dn->digits>1 && *up<powers[d-1]) {
8054 uprv_decNumberShow(dn);
8061 (LI)*up, (LI)dn->digits, (LI)(up-dn->lsu), (LI)maxuint);
8070 ae=dn->exponent+dn->digits-1; /* adjusted exponent */
8077 uprv_decNumberShow(dn);
8083 uprv_decNumberShow(dn);
8092 /* dn is the number to check */
8098 static void decCheckInexact(const decNumber *dn, decContext *set) {
8101 && (set->digits!=dn->digits) && !(dn->bits & DECSPECIAL)) {
8104 (LI)dn->digits);
8105 uprv_decNumberShow(dn);
8111 if (dn!=NULL && dn->digits==0) set->status|=DEC_Invalid_operation;