Lines Matching refs:dn

360 /*  dn is the decNumber to receive the integer                        */
362 /* returns dn */
366 U_CAPI decNumber * U_EXPORT2 uprv_decNumberFromInt32(decNumber *dn, Int in) {
374 uprv_decNumberFromUInt32(dn, unsig);
375 if (in<0) dn->bits=DECNEG; /* sign needed */
376 return dn;
379 U_CAPI decNumber * U_EXPORT2 uprv_decNumberFromUInt32(decNumber *dn, uInt uin) {
381 uprv_decNumberZero(dn); /* clean */
382 if (uin==0) return dn; /* [or decGetDigits bad call] */
383 for (up=dn->lsu; uin>0; up++) {
387 dn->digits=decGetDigits(dn->lsu, up-dn->lsu);
388 return dn;
394 /* dn is the decNumber to convert */
401 U_CAPI Int U_EXPORT2 uprv_decNumberToInt32(const decNumber *dn, decContext *set) {
403 if (decCheckOperands(DECUNRESU, DECUNUSED, dn, set)) return 0;
407 if (dn->bits&DECSPECIAL || dn->digits>10 || dn->exponent!=0) ; /* bad */
412 up=dn->lsu; /* -> lsu */
420 for (d=DECDPUN; d<dn->digits; up++, d+=DECDPUN) hi+=*up*powers[d-1];
424 if (dn->bits&DECNEG && hi==214748364 && lo==8) return 0x80000000;
429 if (dn->bits&DECNEG) return -i;
437 U_CAPI uInt U_EXPORT2 uprv_decNumberToUInt32(const decNumber *dn, decContext *set) {
439 if (decCheckOperands(DECUNRESU, DECUNUSED, dn, set)) return 0;
442 if (dn->bits&DECSPECIAL || dn->digits>10 || dn->exponent!=0
443 || (dn->bits&DECNEG && !ISZERO(dn))); /* bad */
448 up=dn->lsu; /* -> lsu */
456 for (d=DECDPUN; d<dn->digits; up++, d+=DECDPUN) hi+=*up*powers[d-1];
470 /* decNumberToString(dn, string); */
471 /* decNumberToEngString(dn, string); */
473 /* dn is the decNumber to convert */
476 /* string must be at least dn->digits+14 characters long */
480 U_CAPI char * U_EXPORT2 uprv_decNumberToString(const decNumber *dn, char *string){
481 decToString(dn, string, 0);
485 U_CAPI char * U_EXPORT2 uprv_decNumberToEngString(const decNumber *dn, char *string){
486 decToString(dn, string, 1);
494 /* dn -- the number structure to fill */
510 U_CAPI decNumber * U_EXPORT2 uprv_decNumberFromString(decNumber *dn, const char chars[],
532 return uprv_decNumberZero(dn);
568 uprv_decNumberZero(dn); /* be optimistic */
571 dn->bits=bits | DECINF;
577 dn->bits=bits | DECNAN; /* assume simple NaN */
580 dn->bits=bits | DECSNAN;
609 bits=dn->bits; /* for copy-back */
659 uprv_decNumberZero(dn); /* clean result */
672 if (d<=set->digits) res=dn->lsu; /* fits into supplied decNumber */
713 dn->bits=bits;
714 dn->exponent=exponent;
715 dn->digits=d;
720 decSetCoeff(dn, set, res, d, &residue, &status);
722 decFinalize(dn, set, &residue, &status);
726 if ((dn->exponent-1<set->emin-dn->digits)
727 || (dn->exponent-1>set->emax-set->digits)) {
729 decFinalize(dn, set, &residue, &status);
732 /* decNumberShow(dn); */
736 if (status!=0) decStatus(dn, status, set);
737 return dn;
3209 decNumber dn;
3229 uprv_decNumberZero(&dn); /* make a number with exponent 0 */
3230 uprv_decNumberQuantize(res, rhs, &dn, &workset);
3324 /* dn -- the decNumber to test */
3328 enum decClass uprv_decNumberClass(const decNumber *dn, decContext *set) {
3329 if (decNumberIsSpecial(dn)) {
3330 if (decNumberIsQNaN(dn)) return DEC_CLASS_QNAN;
3331 if (decNumberIsSNaN(dn)) return DEC_CLASS_SNAN;
3333 if (decNumberIsNegative(dn)) return DEC_CLASS_NEG_INF;
3337 if (uprv_decNumberIsNormal(dn, set)) { /* most common */
3338 if (decNumberIsNegative(dn)) return DEC_CLASS_NEG_NORMAL;
3342 if (decNumberIsZero(dn)) { /* most common */
3343 if (decNumberIsNegative(dn)) return DEC_CLASS_NEG_ZERO;
3346 if (decNumberIsNegative(dn)) return DEC_CLASS_NEG_SUBNORMAL;
3478 /* dn is the source decNumber */
3479 /* bcd is the uInt array that will receive dn->digits BCD bytes, */
3483 /* bcd must have at least dn->digits bytes. No error is possible; if */
3484 /* dn is a NaN or Infinite, digits must be 1 and the coefficient 0. */
3486 U_CAPI uByte * U_EXPORT2 uprv_decNumberGetBCD(const decNumber *dn, uByte *bcd) {
3487 uByte *ub=bcd+dn->digits-1; /* -> lsd */
3488 const Unit *up=dn->lsu; /* Unit pointer, -> lsu */
3510 /* dn is the target decNumber */
3514 /* returns dn */
3516 /* dn must have space for at least n digits. No error is possible; */
3517 /* if dn is a NaN, or Infinite, or is to become a zero, n must be 1 */
3520 U_CAPI decNumber * U_EXPORT2 uprv_decNumberSetBCD(decNumber *dn, const uByte *bcd, uInt n) {
3521 Unit *up=dn->lsu+D2U(dn->digits)-1; /* -> msu [target pointer] */
3529 for (;up>=dn->lsu; up--) { /* each Unit from msu */
3535 dn->digits=n; /* set digit count */
3536 return dn;
3541 /* dn is the decNumber to test */
3543 /* returns 1 if |dn| is finite and >=Nmin, 0 otherwise */
3545 Int uprv_decNumberIsNormal(const decNumber *dn, decContext *set) {
3548 if (decCheckOperands(DECUNRESU, DECUNUSED, dn, set)) return 0;
3551 if (decNumberIsSpecial(dn)) return 0; /* not finite */
3552 if (decNumberIsZero(dn)) return 0; /* not non-zero */
3554 ae=dn->exponent+dn->digits-1; /* adjusted exponent */
3561 /* dn is the decNumber to test */
3563 /* returns 1 if |dn| is finite, non-zero, and <Nmin, 0 otherwise */
3565 Int uprv_decNumberIsSubnormal(const decNumber *dn, decContext *set) {
3568 if (decCheckOperands(DECUNRESU, DECUNUSED, dn, set)) return 0;
3571 if (decNumberIsSpecial(dn)) return 0; /* not finite */
3572 if (decNumberIsZero(dn)) return 0; /* not non-zero */
3574 ae=dn->exponent+dn->digits-1; /* adjusted exponent */
3582 /* dn is the number to trim */
3583 /* returns dn */
3589 U_CAPI decNumber * U_EXPORT2 uprv_decNumberTrim(decNumber *dn) {
3593 if (decCheckOperands(DECUNRESU, DECUNUSED, dn, DECUNCONT)) return dn;
3596 return decTrim(dn, &set, 0, 1, &dropped);
3611 /* dn is the number to set, with space for one digit */
3612 /* returns dn */
3617 U_CAPI decNumber * U_EXPORT2 uprv_decNumberZero(decNumber *dn) {
3620 if (decCheckOperands(dn, DECUNUSED, DECUNUSED, DECUNCONT)) return dn;
3623 dn->bits=0;
3624 dn->exponent=0;
3625 dn->digits=1;
3626 dn->lsu[0]=0;
3627 return dn;
3637 /* dn is the number to lay out */
3641 /* string must be at least dn->digits+14 characters long */
3650 static void decToString(const decNumber *dn, char *string, Flag eng) {
3651 Int exp=dn->exponent; /* local copy */
3656 const Unit *up=dn->lsu+D2U(dn->digits)-1; /* -> msu [input pointer] */
3660 if (decCheckOperands(DECUNRESU, dn, DECUNUSED, DECUNCONT)) {
3665 if (decNumberIsNegative(dn)) { /* Negatives get a minus */
3669 if (dn->bits&DECSPECIAL) { /* Is a special value */
3670 if (decNumberIsInfinite(dn)) {
3675 if (dn->bits&DECSNAN) { /* signalling NaN */
3683 if (exp!=0 || (*dn->lsu==0 && dn->digits==1)) return;
3688 cut=MSUDIGITS(dn->digits); /* [faster than remainder] */
3692 for (;up>=dn->lsu; up--) { /* each Unit from msu */
3701 pre=dn->digits+exp; /* digits before '.' */
3704 e=exp+dn->digits-1; /* calculate E value */
3721 if (!ISZERO(dn)) pre+=adj;
3737 if (up==dn->lsu) break; /* out of input digits (pre>digits) */
3744 if (n<dn->digits) { /* more to come, after '.' */
3748 if (up==dn->lsu) break; /* out of input digits */
3764 if (up==dn->lsu) break; /* out of input digits */
6621 /* dn is the number to trim or normalize */
6626 /* returns dn */
6633 static decNumber * decTrim(decNumber *dn, decContext *set, Flag all,
6640 if (decCheckOperands(dn, DECUNUSED, DECUNUSED, DECUNCONT)) return dn;
6644 if ((dn->bits & DECSPECIAL) /* fast exit if special .. */
6645 || (*dn->lsu & 0x01)) return dn; /* .. or odd */
6646 if (ISZERO(dn)) { /* .. or 0 */
6647 dn->exponent=0; /* (sign is preserved) */
6648 return dn;
6652 exp=dn->exponent;
6654 up=dn->lsu; /* -> current Unit */
6655 for (d=0; d<dn->digits-1; d++) { /* [don't strip the final digit] */
6677 if (d==0) return dn; /* none to drop */
6681 Int maxd=set->emax-set->digits+1-dn->exponent;
6682 if (maxd<=0) return dn; /* nothing possible */
6687 decShiftToLeast(dn->lsu, D2U(dn->digits), d);
6688 dn->exponent+=d; /* maintain numerical value */
6689 dn->digits-=d; /* new length */
6691 return dn;
6834 /* dn is the number to round (dn->digits is > set->digits) */
6849 static decNumber *decRoundOperand(const decNumber *dn, decContext *set,
6863 decCopyFit(res, dn, set, &residue, &newstatus);
6895 /* dn is the number whose coefficient array is to be set. */
6898 /* lsu -> lsu of the source coefficient [may be dn->lsu] */
6899 /* len is digits in the source coefficient [may be dn->digits] */
6909 /* dn->lsu and len must == dn->digits. */
6913 /* if dn->lsu==lsu). */
6920 /* dn->digits, dn->lsu (and as required), and dn->exponent are */
6921 /* updated as necessary. dn->bits (sign) is unchanged. */
6931 static void decSetCoeff(decNumber *dn, decContext *set, const Unit *lsu,
6944 if (dn->lsu!=lsu) { /* copy needed */
6948 for (target=dn->lsu; count>0; target++, up++, count-=DECDPUN)
6950 dn->digits=len; /* set the new length */
6952 /* dn->exponent and residue are unchanged, record any inexactitude */
6958 dn->exponent+=discard; /* maintain numerical value */
6973 *dn->lsu=0; /* coefficient will now be 0 */
6974 dn->digits=1; /* .. */
7004 *dn->lsu=0; /* .. result is 0 */
7005 dn->digits=1; /* .. */
7009 dn->digits=count; /* set the new length */
7012 for (target=dn->lsu; count>0; target++, up++, count-=DECDPUN)
7050 *dn->lsu=0; /* .. result is 0 */
7051 dn->digits=1; /* .. */
7055 dn->digits=count; /* set the new length */
7057 for (target=dn->lsu; ; target++) {
7084 /* dn is the number, with space for set->digits digits */
7093 /* is, of the opposite sign to dn. In this case the */
7110 /* All fields in dn are updated as required. */
7113 static void decApplyRound(decNumber *dn, decContext *set, Int residue,
7126 /* positive residue and the lsd of dn is 0 or 5, in which case */
7130 Int lsd5=*dn->lsu%5; /* get lsd and quintate */
7149 if (*dn->lsu & 0x01) bump=1;
7164 if (decNumberIsNegative(dn)) {
7175 if (!decNumberIsNegative(dn)) {
7201 uInt count=dn->digits; /* digits to be checked */
7202 for (up=dn->lsu; ; up++) {
7208 for (up=up-1; up>=dn->lsu; up--) *up=0; /* others all to 0 */
7209 dn->exponent++; /* and bump exponent */
7211 if ((dn->exponent+dn->digits)>set->emax+1) {
7212 decSetOverflow(dn, set, status);
7225 uInt count=dn->digits; /* digits to be checked */
7226 for (up=dn->lsu; ; up++) {
7234 for (up=up-1; up>=dn->lsu; up--) *up=(Unit)powers[DECDPUN]-1;
7235 dn->exponent--; /* and bump exponent */
7241 /* dn->exponent, set->digits); */
7242 if (dn->exponent+1==set->emin-set->digits+1) {
7243 if (count==1 && dn->digits==1) *sup=0; /* here 9 -> 0[.9] */
7246 dn->digits--;
7248 dn->exponent++;
7262 decUnitAddSub(dn->lsu, D2U(dn->digits), uarrone, 1, 0, dn->lsu, bump);
7269 /* dn is the number */
7282 static void decFinish(decNumber *dn, decContext *set, Int *residue,
7285 if ISZERO(dn) { /* value is zero */
7286 dn->exponent=0; /* clean exponent .. */
7287 dn->bits=0; /* .. and sign */
7290 if (dn->exponent>=0) { /* non-negative exponent */
7292 if (set->digits >= (dn->exponent+dn->digits)) {
7293 dn->digits=decShiftToMost(dn->lsu, dn->digits, dn->exponent);
7294 dn->exponent=0;
7299 decFinalize(dn, set, residue, status);
7306 /* dn is the number */
7317 static void decFinalize(decNumber *dn, decContext *set, Int *residue,
7320 Int tinyexp=set->emin-dn->digits+1; /* precalculate subnormal boundary */
7328 if (dn->exponent<=tinyexp) { /* prefilter */
7331 /* A very nasty case here is dn == Nmin and residue<0 */
7332 if (dn->exponent<tinyexp) {
7334 decSetSubnormal(dn, set, residue, status);
7337 /* Equals case: only subnormal if dn=Nmin and negative residue */
7341 comp=decCompare(dn, &nmin, 1); /* (signless compare) */
7346 if (*residue<0 && comp==0) { /* neg residue and dn==Nmin */
7347 decApplyRound(dn, set, *residue, status); /* might force down */
7348 decSetSubnormal(dn, set, residue, status);
7354 if (*residue!=0) decApplyRound(dn, set, *residue, status);
7357 if (dn->exponent<=set->emax-set->digits+1) return; /* neither needed */
7361 if (dn->exponent>set->emax-dn->digits+1) { /* too big */
7362 decSetOverflow(dn, set, status);
7369 shift=dn->exponent-(set->emax-set->digits+1);
7372 if (!ISZERO(dn)) {
7373 dn->digits=decShiftToMost(dn->lsu, dn->digits, shift);
7375 dn->exponent-=shift; /* adjust the exponent to match */
7383 /* dn is the number (used for sign [only] and result) */
7389 /* dn and the rounding mode, following IEEE 754 rules. */
7391 static void decSetOverflow(decNumber *dn, decContext *set, uInt *status) {
7393 uByte sign=dn->bits&DECNEG; /* clean and save sign bit */
7395 if (ISZERO(dn)) { /* zero does not overflow magnitude */
7398 if (dn->exponent>emax) { /* clamp required */
7399 dn->exponent=emax;
7405 uprv_decNumberZero(dn);
7422 decSetMaxValue(dn, set);
7423 dn->bits=sign; /* set sign */
7425 else dn->bits=sign|DECINF; /* Value is +/-Infinity */
7432 /* dn is the number to set */
7437 static void decSetMaxValue(decNumber *dn, decContext *set) {
7440 dn->digits=count;
7442 for (up=dn->lsu; ; up++) {
7450 dn->bits=0; /* + sign */
7451 dn->exponent=set->emax-set->digits+1;
7457 /* dn is the number (used as input as well as output; it may have */
7471 static void decSetSubnormal(decNumber *dn, decContext *set, Int *residue,
7479 uprv_decNumberZero(dn);
7490 if ISZERO(dn) { /* value is zero */
7498 if (dn->exponent<etiny) { /* clamp required */
7499 dn->exponent=etiny;
7506 adjust=etiny-dn->exponent; /* calculate digits to remove */
7518 workset.digits=dn->digits-adjust; /* set requested length */
7521 decSetCoeff(dn, &workset, dn->lsu, dn->digits, residue, status);
7522 decApplyRound(dn, &workset, *residue, status);
7530 if (dn->exponent>etiny) {
7531 dn->digits=decShiftToMost(dn->lsu, dn->digits, 1);
7532 dn->exponent--; /* (re)adjust the exponent. */
7536 if (ISZERO(dn)) *status|=DEC_Clamped;
7573 /* dn is the number [which will not be altered] */
7582 /* The sign can be determined from dn by the caller when BIGEVEN or */
7585 static Int decGetInt(const decNumber *dn) {
7589 Int ilength=dn->digits+dn->exponent; /* integral length */
7590 Flag neg=decNumberIsNegative(dn); /* 1 if -ve */
7600 if (ISZERO(dn)) return 0; /* zeros are OK, with any exponent */
7602 up=dn->lsu; /* ready for lsu */
7604 if (dn->exponent>=0) { /* relatively easy */
7606 got=dn->exponent;
7609 Int count=-dn->exponent; /* digits to discard */
7665 /* dn is the number to be decapitated */
7666 /* drop is the number of digits to be removed from the left of dn; */
7667 /* this must be <= dn->digits (if equal, the coefficient is */
7670 /* Returns dn; dn->digits will be <= the initial digits less drop */
7672 /* which will also be removed). Only dn->lsu and dn->digits change. */
7674 static decNumber *decDecap(decNumber *dn, Int drop) {
7677 if (drop>=dn->digits) { /* losing the whole thing */
7679 if (drop>dn->digits)
7681 (LI)drop, (LI)dn->digits);
7683 dn->lsu[0]=0;
7684 dn->digits=1;
7685 return dn;
7687 msu=dn->lsu+D2U(dn->digits-drop)-1; /* -> likely msu */
7688 cut=MSUDIGITS(dn->digits-drop); /* digits to be in use in msu */
7691 dn->digits=decGetDigits(dn->lsu, msu-dn->lsu+1);
7692 return dn;
7772 /* dn is the number to set if error */
7784 static void decStatus(decNumber *dn, uInt status, decContext *set) {
7789 uprv_decNumberZero(dn); /* other error: clean throughout */
7790 dn->bits=DECNAN; /* and make a quiet NaN */
7850 /* dn is the number to show */
7856 void uprv_decNumberShow(const decNumber *dn) {
7861 if (dn==NULL) {
7864 if (decNumberIsNegative(dn)) isign='-';
7866 if (dn->bits&DECSPECIAL) { /* Is a special value */
7867 if (decNumberIsInfinite(dn)) printf("Infinity");
7869 if (dn->bits&DECSNAN) printf("sNaN"); /* signalling NaN */
7873 if (dn->exponent==0 && dn->digits==1 && *dn->lsu==0) {
7881 up=dn->lsu+D2U(dn->digits)-1; /* msu */
7883 for (up=up-1; up>=dn->lsu; up--) {
7892 if (dn->exponent!=0) {
7894 if (dn->exponent<0) esign='-';
7895 printf(" E%c%ld", esign, (LI)abs(dn->exponent));
7897 printf(" [%ld]\n", (LI)dn->digits);
7993 /* dn is the number to check */
7999 static Flag decCheckNumber(const decNumber *dn) {
8005 if (dn==NULL) { /* hopeless */
8013 if (dn->bits & DECSPECIAL) {
8014 if (dn->exponent!=0) {
8017 (LI)dn->exponent, dn->bits);
8022 if (decNumberIsInfinite(dn)) {
8023 if (dn->digits!=1) {
8025 printf("Digits %ld (not 1) for an infinity.\n", (LI)dn->digits);
8028 if (*dn->lsu!=0) {
8030 printf("LSU %ld (not 0) for an infinity.\n", (LI)*dn->lsu);
8032 decDumpAr('I', dn->lsu, D2U(dn->digits));
8041 if (dn->digits<1 || dn->digits>DECNUMMAXP) {
8043 printf("Digits %ld in number.\n", (LI)dn->digits);
8047 d=dn->digits;
8049 for (up=dn->lsu; d>0; up++) {
8053 if (dn->digits>1 && *up<powers[d-1]) {
8056 uprv_decNumberShow(dn);
8063 (LI)*up, (LI)dn->digits, (LI)(up-dn->lsu), (LI)maxuint);
8072 ae=dn->exponent+dn->digits-1; /* adjusted exponent */
8079 uprv_decNumberShow(dn);
8085 uprv_decNumberShow(dn);
8094 /* dn is the number to check */
8100 static void decCheckInexact(const decNumber *dn, decContext *set) {
8103 && (set->digits!=dn->digits) && !(dn->bits & DECSPECIAL)) {
8106 (LI)dn->digits);
8107 uprv_decNumberShow(dn);
8113 if (dn!=NULL && dn->digits==0) set->status|=DEC_Invalid_operation;