4 conversions and numerical operations for the c# type System.Decimal
6 Author: Martin Weindel (martin.weindel@t-online.de)
8 (C) 2001 by Martin Weindel
12 * machine dependent configuration for
13 * CSharp value type System.Decimal
16 #include <mono/metadata/exception.h>
23 /* needed for building microsoft dll */
25 #define DECINLINE __inline
30 #define LIT_GUINT32(x) x
31 #define LIT_GUINT64(x) x##LL
34 /* we need a UInt64 type => guint64 */
40 * Deal with anon union support.
43 #define signscale u.signscale
48 #define PRECONDITION(flag) assert(flag)
49 #define POSTCONDITION(flag) assert(flag)
50 #define TEST(flag) assert(flag)
51 #define INVARIANT_TEST(p) assert(p->signscale.scale >= 0 && p->signscale.scale <= DECIMAL_MAX_SCALE \
52 && p->signscale.reserved1 == 0 && p->signscale.reserved2 == 0);
54 #define PRECONDITION(flag)
55 #define POSTCONDITION(flag)
57 #define INVARIANT_TEST(p)
58 #endif /*#ifdef _DEBUG*/
60 #define DECIMAL_MAX_SCALE 28
61 #define DECIMAL_MAX_INTFACTORS 9
63 #define DECIMAL_SUCCESS 0
64 #define DECIMAL_FINISHED 1
65 #define DECIMAL_OVERFLOW 2
66 #define DECIMAL_INVALID_CHARACTER 2
67 #define DECIMAL_INTERNAL_ERROR 3
68 #define DECIMAL_INVALID_BITS 4
69 #define DECIMAL_DIVIDE_BY_ZERO 5
70 #define DECIMAL_BUFFER_OVERFLOW 6
73 #define DECINIT(src) memset(src, 0, sizeof(decimal_repr))
75 #define DECCOPY(dest, src) memcpy(dest, src, sizeof(decimal_repr))
77 #define DECSWAP(p1, p2, h) \
78 h = (p1)->ss32; (p1)->ss32 = (p2)->ss32; (p2)->ss32 = h; \
79 h = (p1)->hi32; (p1)->hi32 = (p2)->hi32; (p2)->hi32 = h; \
80 h = (p1)->mid32; (p1)->mid32 = (p2)->mid32; (p2)->mid32 = h; \
81 h = (p1)->lo32; (p1)->lo32 = (p2)->lo32; (p2)->lo32 = h;
83 #define DECNEGATE(p1) (p1)->signscale.sign = 1 - (p1)->signscale.sign
85 #define LIT_DEC128(hi, mid, lo) { (((guint64)mid)<<32 | lo), hi }
87 #define DECTO128(pd, lo, hi) \
88 lo = (((guint64)(pd)->mid32) << 32) | (pd)->lo32; \
92 #define LIT_GUINT32_HIGHBIT LIT_GUINT32(0x80000000)
93 #define LIT_GUINT64_HIGHBIT LIT_GUINT64(0x8000000000000000)
95 #define DECIMAL_LOG_NEGINF -1000
97 static const guint32 constantsDecadeInt32Factors[DECIMAL_MAX_INTFACTORS+1] = {
98 LIT_GUINT32(1), LIT_GUINT32(10), LIT_GUINT32(100), LIT_GUINT32(1000),
99 LIT_GUINT32(10000), LIT_GUINT32(100000), LIT_GUINT32(1000000),
100 LIT_GUINT32(10000000), LIT_GUINT32(100000000), LIT_GUINT32(1000000000)
108 static const dec128_repr dec128decadeFactors[DECIMAL_MAX_SCALE+1] = {
109 LIT_DEC128( 0, 0, 1u), /* == 1 */
110 LIT_DEC128( 0, 0, 10u), /* == 10 */
111 LIT_DEC128( 0, 0, 100u), /* == 100 */
112 LIT_DEC128( 0, 0, 1000u), /* == 1e3m */
113 LIT_DEC128( 0, 0, 10000u), /* == 1e4m */
114 LIT_DEC128( 0, 0, 100000u), /* == 1e5m */
115 LIT_DEC128( 0, 0, 1000000u), /* == 1e6m */
116 LIT_DEC128( 0, 0, 10000000u), /* == 1e7m */
117 LIT_DEC128( 0, 0, 100000000u), /* == 1e8m */
118 LIT_DEC128( 0, 0, 1000000000u), /* == 1e9m */
119 LIT_DEC128( 0, 2u, 1410065408u), /* == 1e10m */
120 LIT_DEC128( 0, 23u, 1215752192u), /* == 1e11m */
121 LIT_DEC128( 0, 232u, 3567587328u), /* == 1e12m */
122 LIT_DEC128( 0, 2328u, 1316134912u), /* == 1e13m */
123 LIT_DEC128( 0, 23283u, 276447232u), /* == 1e14m */
124 LIT_DEC128( 0, 232830u, 2764472320u), /* == 1e15m */
125 LIT_DEC128( 0, 2328306u, 1874919424u), /* == 1e16m */
126 LIT_DEC128( 0, 23283064u, 1569325056u), /* == 1e17m */
127 LIT_DEC128( 0, 232830643u, 2808348672u), /* == 1e18m */
128 LIT_DEC128( 0, 2328306436u, 2313682944u), /* == 1e19m */
129 LIT_DEC128( 5u, 1808227885u, 1661992960u), /* == 1e20m */
130 LIT_DEC128( 54u, 902409669u, 3735027712u), /* == 1e21m */
131 LIT_DEC128( 542u, 434162106u, 2990538752u), /* == 1e22m */
132 LIT_DEC128( 5421u, 46653770u, 4135583744u), /* == 1e23m */
133 LIT_DEC128( 54210u, 466537709u, 2701131776u), /* == 1e24m */
134 LIT_DEC128( 542101u, 370409800u, 1241513984u), /* == 1e25m */
135 LIT_DEC128( 5421010u, 3704098002u, 3825205248u), /* == 1e26m */
136 LIT_DEC128( 54210108u, 2681241660u, 3892314112u), /* == 1e27m */
137 LIT_DEC128( 542101086u, 1042612833u, 268435456u), /* == 1e28m */
140 /* 192 bit addition: c = a + b
141 addition is modulo 2**128, any carry is lost */
142 DECINLINE static void add128(guint64 alo, guint64 ahi,
143 guint64 blo, guint64 bhi,
144 guint64* pclo, guint64* pchi)
147 if (alo < blo) ahi++; /* carry */
154 /* 128 bit subtraction: c = a - b
155 subtraction is modulo 2**128, any carry is lost */
156 DECINLINE static void sub128(guint64 alo, guint64 ahi,
157 guint64 blo, guint64 bhi,
158 guint64* pclo, guint64* pchi)
164 if (alo < blo) chi--; /* borrow */
170 /* 192 bit addition: c = a + b
171 addition is modulo 2**192, any carry is lost */
172 DECINLINE static void add192(guint64 alo, guint64 ami, guint64 ahi,
173 guint64 blo, guint64 bmi, guint64 bhi,
174 guint64* pclo, guint64* pcmi, guint64* pchi)
177 if (alo < blo) { /* carry low */
179 if (ami == 0) ahi++; /* carry mid */
182 if (ami < bmi) ahi++; /* carry mid */
189 /* 192 bit subtraction: c = a - b
190 subtraction is modulo 2**192, any carry is lost */
191 DECINLINE static void sub192(guint64 alo, guint64 ami, guint64 ahi,
192 guint64 blo, guint64 bmi, guint64 bhi,
193 guint64* pclo, guint64* pcmi, guint64* pchi)
195 guint64 clo, cmi, chi;
201 if (cmi == 0) chi--; /* borrow mid */
202 cmi--; /* borrow low */
204 if (ami < bmi) chi--; /* borrow mid */
210 /* multiplication c(192bit) = a(96bit) * b(96bit) */
211 DECINLINE static void mult96by96to192(guint32 alo, guint32 ami, guint32 ahi,
212 guint32 blo, guint32 bmi, guint32 bhi,
213 guint64* pclo, guint64* pcmi, guint64* pchi)
216 guint32 h0, h1, h2, h3, h4, h5;
219 a = ((guint64)alo) * blo;
222 a >>= 32; carry0 = 0;
223 b = ((guint64)alo) * bmi;
224 c = ((guint64)ami) * blo;
225 a += b; if (a < b) carry0++;
226 a += c; if (a < c) carry0++;
229 a >>= 32; carry1 = 0;
230 b = ((guint64)alo) * bhi;
231 c = ((guint64)ami) * bmi;
232 d = ((guint64)ahi) * blo;
233 a += b; if (a < b) carry1++;
234 a += c; if (a < c) carry1++;
235 a += d; if (a < d) carry1++;
238 a >>= 32; a += carry0; carry0 = 0;
239 b = ((guint64)ami) * bhi;
240 c = ((guint64)ahi) * bmi;
241 a += b; if (a < b) carry0++;
242 a += c; if (a < c) carry0++;
245 a >>= 32; a += carry1;
246 b = ((guint64)ahi) * bhi;
250 a >>= 32; a += carry0;
253 *pclo = ((guint64)h1) << 32 | h0;
254 *pcmi = ((guint64)h3) << 32 | h2;
255 *pchi = ((guint64)h5) << 32 | h4;
258 /* multiplication c(128bit) = a(96bit) * b(32bit) */
259 DECINLINE static void mult96by32to128(guint32 alo, guint32 ami, guint32 ahi,
261 guint64* pclo, guint64* pchi)
266 a = ((guint64)alo) * factor;
270 a += ((guint64)ami) * factor;
274 a += ((guint64)ahi) * factor;
276 *pclo = ((guint64)h1) << 32 | h0;
280 /* multiplication c(128bit) *= b(32bit) */
281 DECINLINE static int mult128by32(guint64* pclo, guint64* pchi, guint32 factor, int roundBit)
286 a = ((guint64)(guint32)(*pclo)) * factor;
287 if (roundBit) a += factor / 2;
291 a += (*pclo >> 32) * factor;
294 *pclo = ((guint64)h1) << 32 | h0;
297 a += ((guint64)(guint32)(*pchi)) * factor;
301 a += (*pchi >> 32) * factor;
304 *pchi = ((guint64)h1) << 32 | h0;
306 return ((a >> 32) == 0) ? DECIMAL_SUCCESS : DECIMAL_OVERFLOW;
309 DECINLINE static int mult128DecadeFactor(guint64* pclo, guint64* pchi, int powerOfTen)
313 while (powerOfTen > 0) {
314 idx = (powerOfTen >= DECIMAL_MAX_INTFACTORS) ? DECIMAL_MAX_INTFACTORS : powerOfTen;
316 rc = mult128by32(pclo, pchi, constantsDecadeInt32Factors[idx], 0);
317 if (rc != DECIMAL_SUCCESS) return rc;
319 return DECIMAL_SUCCESS;
322 /* division: x(128bit) /= factor(32bit)
324 DECINLINE static int div128by32(guint64* plo, guint64* phi, guint32 factor, guint32* pRest)
329 a = (guint32)(h >> 32);
337 *phi = b << 32 | (guint32)c;
340 a |= (guint32)(h >> 32);
347 *plo = b << 32 | (guint32)c;
349 if (pRest) *pRest = (guint32) a;
352 return (a >= factor || (a == factor && (c & 1) == 1)) ? 1 : 0;
355 /* division: x(192bit) /= factor(32bit)
356 no rest and no rounding*/
357 DECINLINE static void div192by32(guint64* plo, guint64* pmi, guint64* phi,
363 a = (guint32)(h >> 32);
371 *phi = b << 32 | (guint32)c;
374 a |= (guint32)(h >> 32);
382 *pmi = b << 32 | (guint32)c;
385 a |= (guint32)(h >> 32);
393 *plo = b << 32 | (guint32)c;
396 /* returns upper 32bit for a(192bit) /= b(32bit)
397 a will contain remainder */
398 static guint32 div192by96to32withRest(guint64* palo, guint64* pami, guint64* pahi,
399 guint32 blo, guint32 bmi, guint32 bhi)
401 guint64 rlo, rmi, rhi; /* remainder */
402 guint64 tlo, thi; /* term */
405 rlo = *palo; rmi = *pami; rhi = *pahi;
406 if (rhi >= (((guint64)bhi) << 32)) {
407 c = LIT_GUINT32(0xFFFFFFFF);
409 c = (guint32) (rhi / bhi);
411 mult96by32to128(blo, bmi, bhi, c, &tlo, &thi);
412 sub192(rlo, rmi, rhi, 0, tlo, thi, &rlo, &rmi, &rhi);
413 while (((gint64)rhi) < 0) {
415 add192(rlo, rmi, rhi, 0, (((guint64)bmi)<<32) | blo, bhi, &rlo, &rmi, &rhi);
417 *palo = rlo ; *pami = rmi ; *pahi = rhi;
419 POSTCONDITION(rhi >> 32 == 0);
424 /* c(128bit) = a(192bit) / b(96bit)
426 static void div192by96to128(guint64 alo, guint64 ami, guint64 ahi,
427 guint32 blo, guint32 bmi, guint32 bhi,
428 guint64* pclo, guint64* pchi)
430 guint64 rlo, rmi, rhi; /* remainder */
433 PRECONDITION(ahi < (((guint64)bhi) << 32 | bmi)
434 || (ahi == (((guint64)bhi) << 32 | bmi) && (ami >> 32) > blo));
437 rlo = alo; rmi = ami; rhi = ahi;
438 h = div192by96to32withRest(&rlo, &rmi, &rhi, blo, bmi, bhi);
441 rhi = (rhi << 32) | (rmi >> 32); rmi = (rmi << 32) | (rlo >> 32); rlo <<= 32;
442 *pchi = (((guint64)h) << 32) | div192by96to32withRest(&rlo, &rmi, &rhi, blo, bmi, bhi);
445 rhi = (rhi << 32) | (rmi >> 32); rmi = (rmi << 32) | (rlo >> 32); rlo <<= 32;
446 h = div192by96to32withRest(&rlo, &rmi, &rhi, blo, bmi, bhi);
448 /* estimate lowest 32 bit (two last bits may be wrong) */
450 c = LIT_GUINT32(0xFFFFFFFF);
453 c = (guint32) (rhi / bhi);
455 *pclo = (((guint64)h) << 32) | c;
458 DECINLINE static void roundUp128(guint64* pclo, guint64* pchi) {
459 if (++(*pclo) == 0) ++(*pchi);
462 static int normalize128(guint64* pclo, guint64* pchi, int* pScale,
463 int roundFlag, int roundBit)
465 guint32 overhang = (guint32)(*pchi >> 32);
469 while (overhang != 0) {
470 for (deltaScale = 1; deltaScale < DECIMAL_MAX_INTFACTORS; deltaScale++)
472 if (overhang < constantsDecadeInt32Factors[deltaScale]) break;
476 if (scale < 0) return DECIMAL_OVERFLOW;
478 roundBit = div128by32(pclo, pchi, constantsDecadeInt32Factors[deltaScale], 0);
480 overhang = (guint32)(*pchi >> 32);
481 if (roundFlag && roundBit && *pclo == (guint64)-1 && (gint32)*pchi == (gint32)-1) {
488 if (roundFlag && roundBit) {
489 roundUp128(pclo, pchi);
490 TEST((*pchi >> 32) == 0);
493 return DECIMAL_SUCCESS;
496 DECINLINE static int maxLeftShift(/*[In, Out]*/decimal_repr* pA)
498 guint64 lo64 = (((guint64)(pA->mid32)) << 32) | pA->lo32;
499 guint32 hi32 = pA->hi32;
502 for (shift = 0; ((gint32)hi32) >= 0 && shift < 96; shift++) {
504 if (((gint64)lo64) < 0) hi32++;
508 pA->lo32 = (guint32) lo64;
509 pA->mid32 = (guint32)(lo64>>32);
515 DECINLINE static void rshift128(guint64* pclo, guint64* pchi)
518 if (*pchi & 1) *pclo |= LIT_GUINT64_HIGHBIT;
522 DECINLINE static void lshift96(guint32* pclo, guint32* pcmid, guint32* pchi)
525 if (*pcmid & LIT_GUINT32_HIGHBIT) (*pchi)++;
527 if (*pclo & LIT_GUINT32_HIGHBIT) (*pcmid)++;
531 DECINLINE static void lshift128(guint64* pclo, guint64* pchi)
534 if (*pclo & LIT_GUINT64_HIGHBIT) (*pchi)++;
538 DECINLINE static void rshift192(guint64* pclo, guint64* pcmi, guint64* pchi)
541 if (*pcmi & 1) *pclo |= LIT_GUINT64_HIGHBIT;
543 if (*pchi & 1) *pcmi |= LIT_GUINT64_HIGHBIT;
547 /* returns log2(a) or DECIMAL_LOG_NEGINF for a = 0 */
548 DECINLINE static int log2_32(guint32 a)
552 if (a == 0) return DECIMAL_LOG_NEGINF;
554 if ((a >> 16) != 0) {
579 /* returns log2(a) or DECIMAL_LOG_NEGINF for a = 0 */
580 DECINLINE static int log2_64(guint64 a)
584 if (a == 0) return DECIMAL_LOG_NEGINF;
586 if ((a >> 32) != 0) {
590 if ((a >> 16) != 0) {
615 /* returns log2(a) or DECIMAL_LOG_NEGINF for a = 0 */
616 DECINLINE static int log2_128(guint64 alo, guint64 ahi)
618 if (ahi == 0) return log2_64(alo);
619 else return log2_64(ahi) + 64;
622 /* returns a upper limit for log2(a) considering scale */
623 DECINLINE static int log2withScale_128(guint64 alo, guint64 ahi, int scale)
625 int tlog2 = log2_128(alo, ahi);
626 if (tlog2 < 0) tlog2 = 0;
627 return tlog2 - (scale * 33219) / 10000;
630 DECINLINE static int pack128toDecimal(/*[Out]*/decimal_repr* pA, guint64 alo, guint64 ahi,
633 PRECONDITION((ahi >> 32) == 0);
634 PRECONDITION(sign == 0 || sign == 1);
635 PRECONDITION(scale >= 0 && scale <= DECIMAL_MAX_SCALE);
637 if (scale < 0 || scale > DECIMAL_MAX_SCALE || (ahi >> 32) != 0) {
638 return DECIMAL_OVERFLOW;
641 pA->lo32 = (guint32) alo;
642 pA->mid32 = (guint32) (alo >> 32);
643 pA->hi32 = (guint32) ahi;
644 pA->signscale.sign = sign;
645 pA->signscale.scale = scale;
647 return DECIMAL_SUCCESS;
650 DECINLINE static int adjustScale128(guint64* palo, guint64* pahi, int deltaScale)
654 if (deltaScale < 0) {
656 if (deltaScale > DECIMAL_MAX_SCALE) return DECIMAL_INTERNAL_ERROR;
657 while (deltaScale > 0) {
658 idx = (deltaScale > DECIMAL_MAX_INTFACTORS) ? DECIMAL_MAX_INTFACTORS : deltaScale;
660 div128by32(palo, pahi, constantsDecadeInt32Factors[idx], 0);
662 } else if (deltaScale > 0) {
663 if (deltaScale > DECIMAL_MAX_SCALE) return DECIMAL_INTERNAL_ERROR;
664 while (deltaScale > 0) {
665 idx = (deltaScale > DECIMAL_MAX_INTFACTORS) ? DECIMAL_MAX_INTFACTORS : deltaScale;
667 rc = mult128by32(palo, pahi, constantsDecadeInt32Factors[idx], 0);
668 if (rc != DECIMAL_SUCCESS) return rc;
672 return DECIMAL_SUCCESS;
675 /* input: c * 10^-(*pScale) * 2^-exp
676 output: c * 10^-(*pScale) with
677 minScale <= *pScale <= maxScale and (chi >> 32) == 0 */
678 DECINLINE static int rescale128(guint64* pclo, guint64* pchi, int* pScale, int texp,
679 int minScale, int maxScale, int roundFlag)
681 guint32 factor, overhang;
682 int scale, i, rc, roundBit = 0;
684 PRECONDITION(texp >= 0);
690 while (texp > 0 && scale <= maxScale) {
691 overhang = (guint32)(*pchi >> 32);
692 while (texp > 0 && ((*pclo & 1) == 0 || overhang > (2<<DECIMAL_MAX_INTFACTORS))) {
693 if (--texp == 0) roundBit = (int)(*pclo & 1);
694 rshift128(pclo, pchi);
695 overhang = (guint32)(*pchi >> 32);
698 if (texp > DECIMAL_MAX_INTFACTORS) i = DECIMAL_MAX_INTFACTORS;
700 if (scale + i > maxScale) i = maxScale - scale;
704 factor = constantsDecadeInt32Factors[i] >> i; /* 10^i/2^i=5^i */
705 mult128by32(pclo, pchi, factor, 0);
706 /*printf("3: %.17e\n", (((double)chi) * pow(2,64) + clo) * pow(10, -scale) * pow(2, -texp));*/
710 if (--texp == 0) roundBit = (int)(*pclo & 1);
711 rshift128(pclo, pchi);
717 while (scale > maxScale) {
718 i = scale - maxScale;
719 if (i > DECIMAL_MAX_INTFACTORS) i = DECIMAL_MAX_INTFACTORS;
721 roundBit = div128by32(pclo, pchi, constantsDecadeInt32Factors[i], 0);
724 while (scale < minScale) {
725 if (!roundFlag) roundBit = 0;
726 i = minScale - scale;
727 if (i > DECIMAL_MAX_INTFACTORS) i = DECIMAL_MAX_INTFACTORS;
729 rc = mult128by32(pclo, pchi, constantsDecadeInt32Factors[i], roundBit);
730 if (rc != DECIMAL_SUCCESS) return rc;
734 TEST(scale >= 0 && scale <= DECIMAL_MAX_SCALE);
738 return normalize128(pclo, pchi, pScale, roundFlag, roundBit);
741 /* performs a += b */
742 gint32 mono_decimalIncr(/*[In, Out]*/decimal_repr* pA, /*[In]*/decimal_repr* pB)
744 guint64 alo, ahi, blo, bhi;
745 int log2A, log2B, log2Result, log10Result, rc;
746 int subFlag, sign, scaleA, scaleB;
750 DECTO128(pA, alo, ahi);
751 DECTO128(pB, blo, bhi);
753 sign = pA->signscale.sign;
754 subFlag = sign - (int)pB->signscale.sign;
755 scaleA = pA->signscale.scale;
756 scaleB = pB->signscale.scale;
757 if (scaleA == scaleB) {
758 /* same scale, that's easy */
760 sub128(alo, ahi, blo, bhi, &alo, &ahi);
761 if (ahi & LIT_GUINT64_HIGHBIT) {
769 add128(alo, ahi, blo, bhi, &alo, &ahi);
771 rc = normalize128(&alo, &ahi, &scaleA, 1, 0);
773 /* scales must be adjusted */
774 /* Estimate log10 and scale of result for adjusting scales */
775 log2A = log2withScale_128(alo, ahi, scaleA);
776 log2B = log2withScale_128(blo, bhi, scaleB);
777 log2Result = (log2A >= log2B) ? log2A : log2B;
778 if (!subFlag) log2Result++; /* result can have one bit more */
779 log10Result = (log2Result * 1000) / 3322 + 1;
780 /* we will calculate in 128bit, so we may need to adjust scale */
781 if (scaleB > scaleA) scaleA = scaleB;
782 if (scaleA + log10Result > DECIMAL_MAX_SCALE + 7) {
783 /* this may not fit in 128bit, so limit it */
784 scaleA = DECIMAL_MAX_SCALE + 7 - log10Result;
787 rc = adjustScale128(&alo, &ahi, scaleA - (int)pA->signscale.scale);
788 if (rc != DECIMAL_SUCCESS) return rc;
789 rc = adjustScale128(&blo, &bhi, scaleA - scaleB);
790 if (rc != DECIMAL_SUCCESS) return rc;
793 sub128(alo, ahi, blo, bhi, &alo, &ahi);
794 if (ahi & LIT_GUINT64_HIGHBIT) {
802 add128(alo, ahi, blo, bhi, &alo, &ahi);
805 if (rc != DECIMAL_SUCCESS) return rc;
807 rc = rescale128(&alo, &ahi,&scaleA, 0, 0, DECIMAL_MAX_SCALE, 1);
810 if (rc != DECIMAL_SUCCESS) return rc;
812 return pack128toDecimal(pA, alo, ahi, scaleA, sign);
815 /* performs a += factor * constants[idx] */
816 static int incMultConstant128(guint64* palo, guint64* pahi, int idx, int factor)
820 PRECONDITION(idx >= 0 && idx <= DECIMAL_MAX_SCALE);
821 PRECONDITION(factor > 0 && factor <= 9);
823 blo = dec128decadeFactors[idx].lo;
824 h = bhi = dec128decadeFactors[idx].hi;
826 mult128by32(&blo, &bhi, factor, 0);
827 if (h > bhi) return DECIMAL_OVERFLOW;
830 add128(*palo, *pahi, blo, bhi, palo, pahi);
831 if (h > *pahi) return DECIMAL_OVERFLOW;
832 return DECIMAL_SUCCESS;
835 DECINLINE static void div128DecadeFactor(guint64* palo, guint64* pahi, int powerOfTen)
837 int idx, roundBit = 0;
839 while (powerOfTen > 0) {
840 idx = (powerOfTen > DECIMAL_MAX_INTFACTORS) ? DECIMAL_MAX_INTFACTORS : powerOfTen;
842 roundBit = div128by32(palo, pahi, constantsDecadeInt32Factors[idx], 0);
845 if (roundBit) roundUp128(palo, pahi);
848 /* calc significant digits of mantisse */
849 DECINLINE static int calcDigits(guint64 alo, guint64 ahi)
856 return 0; /* zero has no signficant digits */
858 tlog2 = log2_64(alo);
861 tlog2 = 64 + log2_64(ahi);
864 tlog10 = (tlog2 * 1000) / 3322;
865 /* we need an exact floor value of log10(a) */
866 if (dec128decadeFactors[tlog10].hi > ahi
867 || (dec128decadeFactors[tlog10].hi == ahi
868 && dec128decadeFactors[tlog10].lo > alo)) {
874 gint32 mono_double2decimal(/*[Out]*/decimal_repr* pA, double val, gint32 digits)
877 guint64* p = (guint64*)(&val);
878 int sigDigits, sign, texp, rc, scale;
881 PRECONDITION(digits <= 15);
883 sign = ((*p & LIT_GUINT64_HIGHBIT) != 0) ? 1 : 0;
884 k = ((guint16)((*p) >> 52)) & 0x7FF;
885 alo = (*p & LIT_GUINT64(0xFFFFFFFFFFFFF)) | LIT_GUINT64(0x10000000000000);
888 texp = (k & 0x7FF) - 0x3FF;
889 if (k == 0x7FF || texp >= 96) return DECIMAL_OVERFLOW; /* NaNs, SNaNs, Infinities or >= 2^96 */
890 if (k == 0 || texp <= -94) { /* Subnormals, Zeros or < 2^-94 */
891 DECINIT(pA); /* return zero */
892 return DECIMAL_SUCCESS;
897 for (; texp > 0; texp--) {
898 lshift128(&alo, &ahi);
903 rc = rescale128(&alo, &ahi, &scale, -texp, 0, DECIMAL_MAX_SCALE, 0);
904 if (rc != DECIMAL_SUCCESS) return rc;
906 sigDigits = calcDigits(alo, ahi);
907 /* too much digits, then round */
908 if (sigDigits > digits) {
909 div128DecadeFactor(&alo, &ahi, sigDigits - digits);
910 scale -= sigDigits - digits;
911 /* check value, may be 10^(digits+1) caused by rounding */
912 if (ahi == dec128decadeFactors[digits].hi
913 && alo == dec128decadeFactors[digits].lo) {
914 div128by32(&alo, &ahi, 10, 0);
918 rc = mult128DecadeFactor(&alo, &ahi, -scale);
919 if (rc != DECIMAL_SUCCESS) return rc;
924 return pack128toDecimal(pA, alo, ahi, scale, sign);
928 * mono_string2decimal:
934 * converts a digit string to decimal
935 * The significant digits must be passed as an integer in buf !
938 * if you want to convert the number 123.456789012345678901234 to decimal
939 * buf := "123456789012345678901234"
944 * you want to convert -79228162514264337593543950335 to decimal
945 * buf := "79228162514264337593543950335"
950 * you want to convert -7922816251426433759354395033.250000000000001 to decimal
951 * buf := "7922816251426433759354395033250000000000001"
954 * returns (decimal)-7922816251426433759354395033.3
957 * you want to convert -7922816251426433759354395033.250000000000000 to decimal
958 * buf := "7922816251426433759354395033250000000000000"
961 * returns (decimal)-7922816251426433759354395033.2
964 * you want to convert -7922816251426433759354395033.150000000000000 to decimal
965 * buf := "7922816251426433759354395033150000000000000"
968 * returns (decimal)-7922816251426433759354395033.2
970 * Uses banker's rule for rounding if there are more digits than can be
971 * represented by the significant
973 gint32 mono_string2decimal(/*[Out]*/decimal_repr* pA, MonoString* str, gint32 decrDecimal, gint32 sign)
975 gushort *buf = mono_string_chars(str);
978 int n, rc, i, len, sigLen = -1, firstNonZero;
979 int scale, roundBit = 0;
984 for (p = buf, len = 0; *p != 0; len++, p++) { }
986 for (p = buf, i = 0; *p != 0; i++, p++) {
988 if (n < 0 || n > 9) {
989 return DECIMAL_INVALID_CHARACTER;
994 sigLen = (len - firstNonZero > DECIMAL_MAX_SCALE+1)
995 ? DECIMAL_MAX_SCALE+1+firstNonZero : len;
996 if (decrDecimal > sigLen+1) return DECIMAL_OVERFLOW;
998 if (i >= sigLen) break;
999 rc = incMultConstant128(&alo, &ahi, sigLen - 1 - i, n);
1000 if (rc != DECIMAL_SUCCESS) {
1006 scale = sigLen - decrDecimal;
1008 if (i < len) { /* too much digits, we must round */
1010 if (n < 0 || n > 9) {
1011 return DECIMAL_INVALID_CHARACTER;
1013 if (n > 5) roundBit = 1;
1014 else if (n == 5) { /* we must take a nearer look */
1016 for (++i; i < len; ++i) {
1017 if (buf[i] != '0') break; /* we are greater than .5 */
1019 if (i < len /* greater than exactly .5 */
1020 || n % 2 == 1) { /* exactly .5, use banker's rule for rounding */
1027 rc = normalize128(&alo, &ahi, &scale, 1, roundBit);
1028 if (rc != DECIMAL_SUCCESS) return rc;
1031 if (alo == 0 && ahi == 0) {
1033 return DECIMAL_SUCCESS;
1035 return pack128toDecimal(pA, alo, ahi, sigLen - decrDecimal, sign);
1040 * mono_decimal2string:
1042 * returns minimal number of digit string to represent decimal
1043 * No leading or trailing zeros !
1045 * *pA == 0 => buf = "", *pDecPos = 1, *pSign = 0
1046 * *pA == 12.34 => buf = "1234", *pDecPos = 2, *pSign = 0
1047 * *pA == -1000.0000 => buf = "1", *pDecPos = 4, *pSign = 1
1048 * *pA == -0.00000076 => buf = "76", *pDecPos = -6, *pSign = 0
1051 * pA decimal instance to convert
1052 * digits < 0: use decimals instead
1053 * = 0: gets mantisse as integer
1054 * > 0: gets at most <digits> digits, rounded according to banker's rule if necessary
1055 * decimals only used if digits < 0
1056 * >= 0: number of decimal places
1057 * buf pointer to result buffer
1058 * bufSize size of buffer
1059 * pDecPos receives insert position of decimal point relative to start of buffer
1060 * pSign receives sign
1062 gint32 mono_decimal2string(/*[In]*/decimal_repr* pA, gint32 digits, gint32 decimals,
1063 MonoArray* pArray, gint32 bufSize, gint32* pDecPos, gint32* pSign)
1066 guint16 *buf = (guint16*) mono_array_addr(pArray, guint16, 0);
1067 guint16 *q, *p = tmp;
1071 gint32 sigDigits, d;
1074 MONO_ARCH_SAVE_REGS;
1076 scale = pA->signscale.scale;
1077 DECTO128(pA, alo, ahi);
1078 sigDigits = calcDigits(alo, ahi); /* significant digits */
1080 /* calc needed digits (without leading or trailing zeros) */
1081 d = (digits == 0) ? sigDigits : digits;
1082 if (d < 0) { /* use decimals ? */
1083 if (0 <= decimals && decimals < scale) {
1084 d = sigDigits - scale + decimals;
1086 d = sigDigits; /* use all you can get */
1090 if (sigDigits > d) { /* we need to round decimal number */
1092 aa.signscale.scale = DECIMAL_MAX_SCALE;
1093 mono_decimalRound(&aa, DECIMAL_MAX_SCALE - sigDigits + d);
1094 DECTO128(&aa, alo, ahi);
1095 sigDigits += calcDigits(alo, ahi) - d;
1100 /* get digits starting from the tail */
1101 for (; (alo != 0 || ahi != 0) && len < 40; len++) {
1102 div128by32(&alo, &ahi, 10, &rest);
1103 *p++ = '0' + (char) rest;
1108 if (len >= bufSize) return DECIMAL_BUFFER_OVERFLOW;
1110 /* now we have the minimal count of digits,
1111 extend to wished count of digits or decimals */
1113 if (digits >= 0) { /* count digits */
1114 if (digits >= bufSize) return DECIMAL_BUFFER_OVERFLOW;
1116 /* zero or rounded to zero */
1119 /* copy significant digits */
1120 for (i = 0; i < len; i++) {
1123 *pDecPos = sigDigits - scale;
1125 /* add trailing zeros */
1126 for (i = len; i < digits; i++) {
1129 } else { /* count decimals */
1130 if (scale >= sigDigits) { /* add leading zeros */
1131 if (decimals+2 >= bufSize) return DECIMAL_BUFFER_OVERFLOW;
1133 for (i = 0; i <= scale - sigDigits; i++) {
1137 if (sigDigits - scale + decimals+1 >= bufSize) return DECIMAL_BUFFER_OVERFLOW;
1138 *pDecPos = sigDigits - scale;
1140 /* copy significant digits */
1141 for (i = 0; i < len; i++) {
1144 /* add trailing zeros */
1145 for (i = scale; i < decimals; i++) {
1151 *pSign = (sigDigits > 0) ? pA->signscale.sign : 0; /* zero has positive sign */
1153 return DECIMAL_SUCCESS;
1157 * mono_decimal2UInt64:
1160 * converts a decimal to an UInt64 without rounding
1162 gint32 mono_decimal2UInt64(/*[In]*/decimal_repr* pA, guint64* pResult)
1167 MONO_ARCH_SAVE_REGS;
1169 DECTO128(pA, alo, ahi);
1170 scale = pA->signscale.scale;
1172 div128DecadeFactor(&alo, &ahi, scale);
1175 /* overflow if integer too large or < 0 */
1176 if (ahi != 0 || (alo != 0 && pA->signscale.sign)) return DECIMAL_OVERFLOW;
1179 return DECIMAL_SUCCESS;
1183 * mono_decimal2Int64:
1186 * converts a decimal to an Int64 without rounding
1188 gint32 mono_decimal2Int64(/*[In]*/decimal_repr* pA, gint64* pResult)
1193 MONO_ARCH_SAVE_REGS;
1195 DECTO128(pA, alo, ahi);
1196 scale = pA->signscale.scale;
1198 div128DecadeFactor(&alo, &ahi, scale);
1201 if (ahi != 0) return DECIMAL_OVERFLOW;
1203 sign = pA->signscale.sign;
1204 if (sign && alo != 0) {
1205 if (alo > LIT_GUINT64_HIGHBIT) return DECIMAL_OVERFLOW;
1206 *pResult = (gint64) ~(alo-1);
1208 if (alo & LIT_GUINT64_HIGHBIT) return DECIMAL_OVERFLOW;
1209 *pResult = (gint64) alo;
1212 return DECIMAL_SUCCESS;
1215 void mono_decimalFloorAndTrunc(/*[In, Out]*/decimal_repr* pA, gint32 floorFlag)
1218 guint32 factor, rest;
1219 int scale, sign, idx;
1222 MONO_ARCH_SAVE_REGS;
1224 scale = pA->signscale.scale;
1225 if (scale == 0) return; /* nothing to do */
1227 DECTO128(pA, alo, ahi);
1228 sign = pA->signscale.sign;
1231 idx = (scale > DECIMAL_MAX_INTFACTORS) ? DECIMAL_MAX_INTFACTORS : scale;
1232 factor = constantsDecadeInt32Factors[idx];
1234 div128by32(&alo, &ahi, factor, &rest);
1235 hasRest = hasRest || (rest != 0);
1238 if (floorFlag && hasRest && sign) { /* floor: if negative, we must round up */
1239 roundUp128(&alo, &ahi);
1242 pack128toDecimal(pA, alo, ahi, 0, sign);
1245 void mono_decimalRound(/*[In, Out]*/decimal_repr* pA, gint32 decimals)
1250 MONO_ARCH_SAVE_REGS;
1252 DECTO128(pA, alo, ahi);
1253 scale = pA->signscale.scale;
1254 sign = pA->signscale.sign;
1255 if (scale > decimals) {
1256 div128DecadeFactor(&alo, &ahi, scale - decimals);
1260 pack128toDecimal(pA, alo, ahi, scale, sign);
1263 gint32 mono_decimalMult(/*[In, Out]*/decimal_repr* pA, /*[In]*/decimal_repr* pB)
1265 guint64 low, mid, high;
1267 int scale, sign, rc;
1269 MONO_ARCH_SAVE_REGS;
1271 mult96by96to192(pA->lo32, pA->mid32, pA->hi32, pB->lo32, pB->mid32, pB->hi32,
1274 /* adjust scale and sign */
1275 scale = (int)pA->signscale.scale + (int)pB->signscale.scale;
1276 sign = pA->signscale.sign ^ pB->signscale.sign;
1278 /* first scaling step */
1279 factor = constantsDecadeInt32Factors[DECIMAL_MAX_INTFACTORS];
1280 while (high != 0 || (mid>>32) >= factor) {
1282 factor /= 1000; /* we need some digits for final rounding */
1283 scale -= DECIMAL_MAX_INTFACTORS - 3;
1285 scale -= DECIMAL_MAX_INTFACTORS;
1288 div192by32(&low, &mid, &high, factor);
1291 /* second and final scaling */
1292 rc = rescale128(&low, &mid, &scale, 0, 0, DECIMAL_MAX_SCALE, 1);
1293 if (rc != DECIMAL_SUCCESS) return rc;
1295 return pack128toDecimal(pA, low, mid, scale, sign);
1298 static int decimalDivSub(/*[In]*/decimal_repr* pA, /*[In]*/decimal_repr* pB,
1299 guint64* pclo, guint64* pchi, int* pExp)
1301 guint64 alo, ami, ahi;
1302 guint64 tlo, tmi, thi;
1303 guint32 blo, bmi, bhi;
1304 int ashift, bshift, extraBit, texp;
1306 ahi = (((guint64)(pA->hi32)) << 32) | pA->mid32;
1307 ami = ((guint64)(pA->lo32)) << 32;
1313 if (blo == 0 && bmi == 0 && bhi == 0) {
1314 return DECIMAL_DIVIDE_BY_ZERO;
1317 if (ami == 0 && ahi == 0) {
1319 return DECIMAL_FINISHED;
1322 /* enlarge dividend to get maximal precision */
1323 for (ashift = 0; (ahi & LIT_GUINT64_HIGHBIT) == 0; ++ashift) {
1324 lshift128(&ami, &ahi);
1327 /* ensure that divisor is at least 2^95 */
1328 for (bshift = 0; (bhi & LIT_GUINT32_HIGHBIT) == 0; ++bshift) {
1329 lshift96(&blo, &bmi, &bhi);
1332 thi = ((guint64)bhi)<<32 | bmi;
1333 tmi = ((guint64)blo)<<32;
1335 if (ahi > thi || (ahi == thi && ami >= tmi)) {
1336 sub192(alo, ami, ahi, tlo, tmi, thi, &alo, &ami, &ahi);
1342 div192by96to128(alo, ami, ahi, blo, bmi, bhi, pclo, pchi);
1343 texp = 128 + ashift - bshift;
1346 rshift128(pclo, pchi);
1347 *pchi += LIT_GUINT64_HIGHBIT;
1351 /* try loss free right shift */
1352 while (texp > 0 && (*pclo & 1) == 0) {
1354 rshift128(pclo, pchi);
1360 return DECIMAL_SUCCESS;
1363 gint32 mono_decimalDiv(/*[Out]*/decimal_repr* pC, /*[In]*/decimal_repr* pA, /*[In]*/decimal_repr* pB)
1365 guint64 clo, chi; /* result */
1366 int scale, texp, rc;
1368 MONO_ARCH_SAVE_REGS;
1370 rc = decimalDivSub(pA, pB, &clo, &chi, &texp);
1371 if (rc != DECIMAL_SUCCESS) {
1372 if (rc == DECIMAL_FINISHED) rc = DECIMAL_SUCCESS;
1376 /* adjust scale and sign */
1377 scale = (int)pA->signscale.scale - (int)pB->signscale.scale;
1379 /*test: printf("0: %.17e\n", (((double)chi) * pow(2,64) + clo) * pow(10, -scale) * pow(2, -exp));*/
1380 rc = rescale128(&clo, &chi, &scale, texp, 0, DECIMAL_MAX_SCALE, 1);
1381 if (rc != DECIMAL_SUCCESS) return rc;
1383 return pack128toDecimal(pC, clo, chi, scale, pA->signscale.sign ^ pB->signscale.sign);
1386 gint32 mono_decimalIntDiv(/*[Out]*/decimal_repr* pC, /*[In]*/decimal_repr* pA, /*[In]*/decimal_repr* pB)
1388 guint64 clo, chi; /* result */
1389 int scale, texp, rc;
1391 MONO_ARCH_SAVE_REGS;
1393 rc = decimalDivSub(pA, pB, &clo, &chi, &texp);
1394 if (rc != DECIMAL_SUCCESS) {
1395 if (rc == DECIMAL_FINISHED) rc = DECIMAL_SUCCESS;
1400 scale = (int)pA->signscale.scale - (int)pB->signscale.scale;
1402 /* truncate result to integer */
1403 rc = rescale128(&clo, &chi, &scale, texp, 0, 0, 0);
1404 if (rc != DECIMAL_SUCCESS) return rc;
1406 return pack128toDecimal(pC, clo, chi, scale, pA->signscale.sign);
1409 /* approximation for log2 of a
1410 If q is the exact value for log2(a), then q <= decimalLog2(a) <= q+1 */
1411 DECINLINE static int decimalLog2(/*[In]*/decimal_repr* pA)
1414 int scale = pA->signscale.scale;
1416 if (pA->hi32 != 0) tlog2 = 64 + log2_32(pA->hi32);
1417 else if (pA->mid32 != 0) tlog2 = 32 + log2_32(pA->mid32);
1418 else tlog2 = log2_32(pA->lo32);
1420 if (tlog2 != DECIMAL_LOG_NEGINF) {
1421 tlog2 -= (scale * 33219) / 10000;
1427 DECINLINE static int decimalIsZero(/*[In]*/decimal_repr* pA)
1429 return (pA->lo32 == 0 && pA->mid32 == 0 && pA->hi32 == 0);
1432 gint32 mono_decimalCompare(/*[In]*/decimal_repr* pA, /*[In]*/decimal_repr* pB)
1434 int log2a, log2b, delta, sign;
1437 MONO_ARCH_SAVE_REGS;
1439 sign = (pA->signscale.sign) ? -1 : 1;
1441 if (pA->signscale.sign ^ pB->signscale.sign) {
1442 return (decimalIsZero(pA) && decimalIsZero(pB)) ? 0 : sign;
1445 /* try fast comparison via log2 */
1446 log2a = decimalLog2(pA);
1447 log2b = decimalLog2(pB);
1448 delta = log2a - log2b;
1449 /* decimalLog2 is not exact, so we can say nothing
1450 if abs(delta) <= 1 */
1451 if (delta < -1) return -sign;
1452 if (delta > 1) return sign;
1456 mono_decimalIncr(&aa, pB);
1458 if (decimalIsZero(&aa)) return 0;
1460 return (aa.signscale.sign) ? 1 : -1;
1463 /* d=(-1)^sign * n * 2^(k-52) with sign (1bit), k(11bit), n-2^52(52bit) */
1464 DECINLINE static void buildIEEE754Double(double* pd, int sign, int texp, guint64 mantisse)
1466 guint64* p = (guint64*) pd;
1468 PRECONDITION(sign == 0 || sign == 1);
1469 *p = (((guint64)sign) << 63) | (((guint64)((1023+texp)&0x7ff)) << 52) | mantisse;
1472 double mono_decimal2double(/*[In]*/decimal_repr* pA)
1475 guint64 alo, ahi, mantisse;
1476 guint32 overhang, factor, roundBits;
1477 int scale, texp, log5, i;
1479 MONO_ARCH_SAVE_REGS;
1481 ahi = (((guint64)(pA->hi32)) << 32) | pA->mid32;
1482 alo = ((guint64)(pA->lo32)) << 32;
1484 /* special case zero */
1485 if (ahi == 0 && alo == 0) return 0.0;
1488 scale = pA->signscale.scale;
1490 /* transform n * 10^-scale and exp = 0 => m * 2^-exp and scale = 0 */
1492 while ((ahi & LIT_GUINT64_HIGHBIT) == 0) {
1493 lshift128(&alo, &ahi);
1497 overhang = (guint32) (ahi >> 32);
1498 if (overhang >= 5) {
1500 log5 = (log2_32(overhang) * 1000) / 2322; /* ln(5)/ln(2) = 2.3219... */
1501 if (log5 < DECIMAL_MAX_INTFACTORS) {
1502 /* get maximal factor=5^i, so that overhang / factor >= 1 */
1503 factor = constantsDecadeInt32Factors[log5] >> log5; /* 5^n = 10^n/2^n */
1504 i = log5 + overhang / factor;
1506 i = DECIMAL_MAX_INTFACTORS; /* we have only constants up to 10^DECIMAL_MAX_INTFACTORS */
1508 if (i > scale) i = scale;
1509 factor = constantsDecadeInt32Factors[i] >> i; /* 5^n = 10^n/2^n */
1510 /* n * 10^-scale * 2^-exp => m * 10^-(scale-i) * 2^-(exp+i) with m = n * 5^-i */
1511 div128by32(&alo, &ahi, factor, 0);
1517 /* normalize significand (highest bit should be 1) */
1518 while ((ahi & LIT_GUINT64_HIGHBIT) == 0) {
1519 lshift128(&alo, &ahi);
1523 /* round to nearest even */
1524 roundBits = (guint32)ahi & 0x7ff;
1526 if ((ahi & LIT_GUINT64_HIGHBIT) == 0) { /* overflow ? */
1529 } else if ((roundBits & 0x400) == 0) ahi &= ~1;
1531 /* 96 bit => 1 implizit bit and 52 explicit bits */
1532 mantisse = (ahi & ~LIT_GUINT64_HIGHBIT) >> 11;
1534 buildIEEE754Double(&d, pA->signscale.sign, -texp+95, mantisse);
1540 gint32 mono_decimalSetExponent(/*[In, Out]*/decimal_repr* pA, gint32 texp)
1544 int scale = pA->signscale.scale;
1546 MONO_ARCH_SAVE_REGS;
1550 if (scale < 0 || scale > DECIMAL_MAX_SCALE) {
1551 DECTO128(pA, alo, ahi);
1552 rc = rescale128(&alo, &ahi, &scale, 0, 0, DECIMAL_MAX_SCALE, 1);
1553 if (rc != DECIMAL_SUCCESS) return rc;
1554 return pack128toDecimal(pA, alo, ahi, scale, pA->signscale.sign);
1556 pA->signscale.scale = scale;
1557 return DECIMAL_SUCCESS;