OLD | NEW |
---|---|
1 /* ------------------------------------------------------------------ */ | 1 /* ------------------------------------------------------------------ */ |
2 /* Decimal Number arithmetic module */ | 2 /* Decimal Number arithmetic module */ |
3 /* ------------------------------------------------------------------ */ | 3 /* ------------------------------------------------------------------ */ |
4 /* Copyright (c) IBM Corporation, 2000-2010. All rights reserved. */ | 4 /* Copyright (c) IBM Corporation, 2000-2010. All rights reserved. */ |
5 /* */ | 5 /* */ |
6 /* This software is made available under the terms of the */ | 6 /* This software is made available under the terms of the */ |
7 /* ICU License -- ICU 1.8.1 and later. */ | 7 /* ICU License -- ICU 1.8.1 and later. */ |
8 /* */ | 8 /* */ |
9 /* The description and User's Guide ("The decNumber C Library") for */ | 9 /* The description and User's Guide ("The decNumber C Library") for */ |
10 /* this software is called decNumber.pdf. This document is */ | 10 /* this software is called decNumber.pdf. This document is */ |
(...skipping 1484 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1495 needbytes=sizeof(decNumber)+(D2U(p)-1)*sizeof(Unit); | 1495 needbytes=sizeof(decNumber)+(D2U(p)-1)*sizeof(Unit); |
1496 if (needbytes>sizeof(bufb)) { /* need malloc space */ | 1496 if (needbytes>sizeof(bufb)) { /* need malloc space */ |
1497 allocbufb=(decNumber *)malloc(needbytes); | 1497 allocbufb=(decNumber *)malloc(needbytes); |
1498 if (allocbufb==NULL) { /* hopeless -- abandon */ | 1498 if (allocbufb==NULL) { /* hopeless -- abandon */ |
1499 status|=DEC_Insufficient_storage; | 1499 status|=DEC_Insufficient_storage; |
1500 break;} | 1500 break;} |
1501 b=allocbufb; /* use the allocated space */ | 1501 b=allocbufb; /* use the allocated space */ |
1502 } | 1502 } |
1503 uprv_decNumberZero(w); /* set up 10... */ | 1503 uprv_decNumberZero(w); /* set up 10... */ |
1504 #if DECDPUN==1 | 1504 #if DECDPUN==1 |
1505 #ifdef __clang__ | |
Nico
2012/02/17 00:29:37
You don't need the #ifdef, compilers are supposed
| |
1506 #pragma clang diagnostic push | |
1507 #pragma clang diagnostic ignored "-Warray-bounds" | |
1508 #endif | |
1505 w->lsu[1]=1; w->lsu[0]=0; /* .. */ | 1509 w->lsu[1]=1; w->lsu[0]=0; /* .. */ |
1510 #ifdef __clang__ | |
1511 #pragma clang diagnostic pop | |
1512 #endif | |
1506 #else | 1513 #else |
1507 w->lsu[0]=10; /* .. */ | 1514 w->lsu[0]=10; /* .. */ |
1508 #endif | 1515 #endif |
1509 w->digits=2; /* .. */ | 1516 w->digits=2; /* .. */ |
1510 | 1517 |
1511 aset.digits=p; | 1518 aset.digits=p; |
1512 decLnOp(b, w, &aset, &ignore); /* b=ln(10) */ | 1519 decLnOp(b, w, &aset, &ignore); /* b=ln(10) */ |
1513 | 1520 |
1514 aset.digits=set->digits; /* for final divide */ | 1521 aset.digits=set->digits; /* for final divide */ |
1515 decDivideOp(res, a, b, &aset, DIVIDE, &status); /* into result */ | 1522 decDivideOp(res, a, b, &aset, DIVIDE, &status); /* into result */ |
(...skipping 1411 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2927 /* (Rounded, etc.) should be ignored, not accumulated.] */ | 2934 /* (Rounded, etc.) should be ignored, not accumulated.] */ |
2928 | 2935 |
2929 /* Calculate initial approximation, and allow for odd exponent */ | 2936 /* Calculate initial approximation, and allow for odd exponent */ |
2930 workset.digits=workp; /* p for initial calculation */ | 2937 workset.digits=workp; /* p for initial calculation */ |
2931 t->bits=0; t->digits=3; | 2938 t->bits=0; t->digits=3; |
2932 a->bits=0; a->digits=3; | 2939 a->bits=0; a->digits=3; |
2933 if ((exp & 1)==0) { /* even exponent */ | 2940 if ((exp & 1)==0) { /* even exponent */ |
2934 /* Set t=0.259, a=0.819 */ | 2941 /* Set t=0.259, a=0.819 */ |
2935 t->exponent=-3; | 2942 t->exponent=-3; |
2936 a->exponent=-3; | 2943 a->exponent=-3; |
2944 #ifdef __clang__ | |
2945 #pragma clang diagnostic push | |
2946 #pragma clang diagnostic ignored "-Warray-bounds" | |
2947 #endif | |
2937 #if DECDPUN>=3 | 2948 #if DECDPUN>=3 |
2938 t->lsu[0]=259; | 2949 t->lsu[0]=259; |
2939 a->lsu[0]=819; | 2950 a->lsu[0]=819; |
2940 #elif DECDPUN==2 | 2951 #elif DECDPUN==2 |
2941 t->lsu[0]=59; t->lsu[1]=2; | 2952 t->lsu[0]=59; t->lsu[1]=2; |
2942 a->lsu[0]=19; a->lsu[1]=8; | 2953 a->lsu[0]=19; a->lsu[1]=8; |
2943 #else | 2954 #else |
2944 t->lsu[0]=9; t->lsu[1]=5; t->lsu[2]=2; | 2955 t->lsu[0]=9; t->lsu[1]=5; t->lsu[2]=2; |
2945 a->lsu[0]=9; a->lsu[1]=1; a->lsu[2]=8; | 2956 a->lsu[0]=9; a->lsu[1]=1; a->lsu[2]=8; |
2946 #endif | 2957 #endif |
2958 #ifdef __clang__ | |
2959 #pragma clang diagnostic pop | |
2960 #endif | |
2947 } | 2961 } |
2948 else { /* odd exponent */ | 2962 else { /* odd exponent */ |
2949 /* Set t=0.0819, a=2.59 */ | 2963 /* Set t=0.0819, a=2.59 */ |
2950 f->exponent--; /* f=f/10 */ | 2964 f->exponent--; /* f=f/10 */ |
2951 exp++; /* e=e+1 */ | 2965 exp++; /* e=e+1 */ |
2952 t->exponent=-4; | 2966 t->exponent=-4; |
2953 a->exponent=-2; | 2967 a->exponent=-2; |
2968 #ifdef __clang__ | |
2969 #pragma clang diagnostic push | |
2970 #pragma clang diagnostic ignored "-Warray-bounds" | |
2971 #endif | |
2954 #if DECDPUN>=3 | 2972 #if DECDPUN>=3 |
2955 t->lsu[0]=819; | 2973 t->lsu[0]=819; |
2956 a->lsu[0]=259; | 2974 a->lsu[0]=259; |
2957 #elif DECDPUN==2 | 2975 #elif DECDPUN==2 |
2958 t->lsu[0]=19; t->lsu[1]=8; | 2976 t->lsu[0]=19; t->lsu[1]=8; |
2959 a->lsu[0]=59; a->lsu[1]=2; | 2977 a->lsu[0]=59; a->lsu[1]=2; |
2960 #else | 2978 #else |
2961 t->lsu[0]=9; t->lsu[1]=1; t->lsu[2]=8; | 2979 t->lsu[0]=9; t->lsu[1]=1; t->lsu[2]=8; |
2962 a->lsu[0]=9; a->lsu[1]=5; a->lsu[2]=2; | 2980 a->lsu[0]=9; a->lsu[1]=5; a->lsu[2]=2; |
2963 #endif | 2981 #endif |
2982 #ifdef __clang__ | |
2983 #pragma clang diagnostic pop | |
2984 #endif | |
2964 } | 2985 } |
2965 | 2986 |
2966 decMultiplyOp(a, a, f, &workset, &ignore); /* a=a*f */ | 2987 decMultiplyOp(a, a, f, &workset, &ignore); /* a=a*f */ |
2967 decAddOp(a, a, t, &workset, 0, &ignore); /* ..+t */ | 2988 decAddOp(a, a, t, &workset, 0, &ignore); /* ..+t */ |
2968 /* [a is now the initial approximation for sqrt(f), calculated with */ | 2989 /* [a is now the initial approximation for sqrt(f), calculated with */ |
2969 /* currentprecision, which is also a's precision.] */ | 2990 /* currentprecision, which is also a's precision.] */ |
2970 | 2991 |
2971 /* the main calculation loop */ | 2992 /* the main calculation loop */ |
2972 uprv_decNumberZero(&dzero); /* make 0 */ | 2993 uprv_decNumberZero(&dzero); /* make 0 */ |
2973 uprv_decNumberZero(t); /* set t = 0.5 */ | 2994 uprv_decNumberZero(t); /* set t = 0.5 */ |
(...skipping 2656 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
5630 /* Non-zero negatives are bad... */ | 5651 /* Non-zero negatives are bad... */ |
5631 if (decNumberIsNegative(rhs)) { /* -x -> error */ | 5652 if (decNumberIsNegative(rhs)) { /* -x -> error */ |
5632 *status|=DEC_Invalid_operation; | 5653 *status|=DEC_Invalid_operation; |
5633 break;} | 5654 break;} |
5634 | 5655 |
5635 /* Here, rhs is positive, finite, and in range */ | 5656 /* Here, rhs is positive, finite, and in range */ |
5636 | 5657 |
5637 /* lookaside fastpath code for ln(2) and ln(10) at common lengths */ | 5658 /* lookaside fastpath code for ln(2) and ln(10) at common lengths */ |
5638 if (rhs->exponent==0 && set->digits<=40) { | 5659 if (rhs->exponent==0 && set->digits<=40) { |
5639 #if DECDPUN==1 | 5660 #if DECDPUN==1 |
5661 #ifdef __clang__ | |
5662 #pragma clang diagnostic push | |
5663 #pragma clang diagnostic ignored "-Warray-bounds" | |
5664 #endif | |
5640 if (rhs->lsu[0]==0 && rhs->lsu[1]==1 && rhs->digits==2) { /* ln(10) */ | 5665 if (rhs->lsu[0]==0 && rhs->lsu[1]==1 && rhs->digits==2) { /* ln(10) */ |
5666 #ifdef __clang__ | |
5667 #pragma clang diagnostic pop | |
5668 #endif | |
5641 #else | 5669 #else |
5642 if (rhs->lsu[0]==10 && rhs->digits==2) { /* ln(10) */ | 5670 if (rhs->lsu[0]==10 && rhs->digits==2) { /* ln(10) */ |
5643 #endif | 5671 #endif |
5644 aset=*set; aset.round=DEC_ROUND_HALF_EVEN; | 5672 aset=*set; aset.round=DEC_ROUND_HALF_EVEN; |
5645 #define LN10 "2.302585092994045684017991454684364207601" | 5673 #define LN10 "2.302585092994045684017991454684364207601" |
5646 uprv_decNumberFromString(res, LN10, &aset); | 5674 uprv_decNumberFromString(res, LN10, &aset); |
5647 *status|=(DEC_Inexact | DEC_Rounded); /* is inexact */ | 5675 *status|=(DEC_Inexact | DEC_Rounded); /* is inexact */ |
5648 break;} | 5676 break;} |
5649 if (rhs->lsu[0]==2 && rhs->digits==1) { /* ln(2) */ | 5677 if (rhs->lsu[0]==2 && rhs->digits==1) { /* ln(2) */ |
5650 aset=*set; aset.round=DEC_ROUND_HALF_EVEN; | 5678 aset=*set; aset.round=DEC_ROUND_HALF_EVEN; |
(...skipping 2483 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
8134 for (b=b0+n+8; b<b0+n+12; b++) if (*b!=DECFENCE) | 8162 for (b=b0+n+8; b<b0+n+12; b++) if (*b!=DECFENCE) |
8135 printf("=== Corrupt byte [%02x] at offset +%d from %ld, n=%ld ===\n", *b, | 8163 printf("=== Corrupt byte [%02x] at offset +%d from %ld, n=%ld ===\n", *b, |
8136 b-b0-8, (LI)b0, (LI)n); | 8164 b-b0-8, (LI)b0, (LI)n); |
8137 free(b0); /* drop the storage */ | 8165 free(b0); /* drop the storage */ |
8138 decAllocBytes-=n; /* account for storage */ | 8166 decAllocBytes-=n; /* account for storage */ |
8139 /* printf(" free -- dAB: %d (%d)\n", decAllocBytes, -n); */ | 8167 /* printf(" free -- dAB: %d (%d)\n", decAllocBytes, -n); */ |
8140 } /* decFree */ | 8168 } /* decFree */ |
8141 #define malloc(a) decMalloc(a) | 8169 #define malloc(a) decMalloc(a) |
8142 #define free(a) decFree(a) | 8170 #define free(a) decFree(a) |
8143 #endif | 8171 #endif |
OLD | NEW |