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
17 #include <mono/metadata/exception.h>
29 #ifndef DISABLE_DECIMAL
31 /* needed for building microsoft dll */
33 #define DECINLINE __inline
38 #define LIT_GUINT32(x) x
39 #define LIT_GUINT64(x) x##LL
42 /* we need a UInt64 type => guint64 */
48 * Deal with anon union support.
51 #define signscale u.signscale
56 #define PRECONDITION(flag) assert(flag)
57 #define POSTCONDITION(flag) assert(flag)
58 #define TEST(flag) assert(flag)
59 #define INVARIANT_TEST(p) assert(p->signscale.scale >= 0 && p->signscale.scale <= DECIMAL_MAX_SCALE \
60 && p->signscale.reserved1 == 0 && p->signscale.reserved2 == 0);
62 #define PRECONDITION(flag)
63 #define POSTCONDITION(flag)
65 #define INVARIANT_TEST(p)
66 #endif /*#ifdef _DEBUG*/
68 #define DECIMAL_MAX_SCALE 28
69 #define DECIMAL_MAX_INTFACTORS 9
71 #define DECIMAL_SUCCESS 0
72 #define DECIMAL_FINISHED 1
73 #define DECIMAL_OVERFLOW 2
74 #define DECIMAL_INVALID_CHARACTER 2
75 #define DECIMAL_INTERNAL_ERROR 3
76 #define DECIMAL_INVALID_BITS 4
77 #define DECIMAL_DIVIDE_BY_ZERO 5
78 #define DECIMAL_BUFFER_OVERFLOW 6
81 #define DECINIT(src) memset(src, 0, sizeof(decimal_repr))
83 #define DECCOPY(dest, src) memcpy(dest, src, sizeof(decimal_repr))
85 #define DECSWAP(p1, p2, h) \
86 h = (p1)->ss32; (p1)->ss32 = (p2)->ss32; (p2)->ss32 = h; \
87 h = (p1)->hi32; (p1)->hi32 = (p2)->hi32; (p2)->hi32 = h; \
88 h = (p1)->mid32; (p1)->mid32 = (p2)->mid32; (p2)->mid32 = h; \
89 h = (p1)->lo32; (p1)->lo32 = (p2)->lo32; (p2)->lo32 = h;
91 #define DECNEGATE(p1) (p1)->signscale.sign = 1 - (p1)->signscale.sign
93 #define LIT_DEC128(hi, mid, lo) { (((guint64)mid)<<32 | lo), hi }
95 #define DECTO128(pd, lo, hi) \
96 lo = (((guint64)(pd)->mid32) << 32) | (pd)->lo32; \
100 #define LIT_GUINT32_HIGHBIT LIT_GUINT32(0x80000000)
101 #define LIT_GUINT64_HIGHBIT LIT_GUINT64(0x8000000000000000)
103 #define DECIMAL_LOG_NEGINF -1000
105 static const guint32 constantsDecadeInt32Factors[DECIMAL_MAX_INTFACTORS+1] = {
106 LIT_GUINT32(1), LIT_GUINT32(10), LIT_GUINT32(100), LIT_GUINT32(1000),
107 LIT_GUINT32(10000), LIT_GUINT32(100000), LIT_GUINT32(1000000),
108 LIT_GUINT32(10000000), LIT_GUINT32(100000000), LIT_GUINT32(1000000000)
116 static const dec128_repr dec128decadeFactors[DECIMAL_MAX_SCALE+1] = {
117 LIT_DEC128( 0, 0, 1u), /* == 1 */
118 LIT_DEC128( 0, 0, 10u), /* == 10 */
119 LIT_DEC128( 0, 0, 100u), /* == 100 */
120 LIT_DEC128( 0, 0, 1000u), /* == 1e3m */
121 LIT_DEC128( 0, 0, 10000u), /* == 1e4m */
122 LIT_DEC128( 0, 0, 100000u), /* == 1e5m */
123 LIT_DEC128( 0, 0, 1000000u), /* == 1e6m */
124 LIT_DEC128( 0, 0, 10000000u), /* == 1e7m */
125 LIT_DEC128( 0, 0, 100000000u), /* == 1e8m */
126 LIT_DEC128( 0, 0, 1000000000u), /* == 1e9m */
127 LIT_DEC128( 0, 2u, 1410065408u), /* == 1e10m */
128 LIT_DEC128( 0, 23u, 1215752192u), /* == 1e11m */
129 LIT_DEC128( 0, 232u, 3567587328u), /* == 1e12m */
130 LIT_DEC128( 0, 2328u, 1316134912u), /* == 1e13m */
131 LIT_DEC128( 0, 23283u, 276447232u), /* == 1e14m */
132 LIT_DEC128( 0, 232830u, 2764472320u), /* == 1e15m */
133 LIT_DEC128( 0, 2328306u, 1874919424u), /* == 1e16m */
134 LIT_DEC128( 0, 23283064u, 1569325056u), /* == 1e17m */
135 LIT_DEC128( 0, 232830643u, 2808348672u), /* == 1e18m */
136 LIT_DEC128( 0, 2328306436u, 2313682944u), /* == 1e19m */
137 LIT_DEC128( 5u, 1808227885u, 1661992960u), /* == 1e20m */
138 LIT_DEC128( 54u, 902409669u, 3735027712u), /* == 1e21m */
139 LIT_DEC128( 542u, 434162106u, 2990538752u), /* == 1e22m */
140 LIT_DEC128( 5421u, 46653770u, 4135583744u), /* == 1e23m */
141 LIT_DEC128( 54210u, 466537709u, 2701131776u), /* == 1e24m */
142 LIT_DEC128( 542101u, 370409800u, 1241513984u), /* == 1e25m */
143 LIT_DEC128( 5421010u, 3704098002u, 3825205248u), /* == 1e26m */
144 LIT_DEC128( 54210108u, 2681241660u, 3892314112u), /* == 1e27m */
145 LIT_DEC128( 542101086u, 1042612833u, 268435456u), /* == 1e28m */
148 /* 192 bit addition: c = a + b
149 addition is modulo 2**128, any carry is lost */
150 DECINLINE static void add128(guint64 alo, guint64 ahi,
151 guint64 blo, guint64 bhi,
152 guint64* pclo, guint64* pchi)
155 if (alo < blo) ahi++; /* carry */
162 /* 128 bit subtraction: c = a - b
163 subtraction is modulo 2**128, any carry is lost */
164 DECINLINE static void sub128(guint64 alo, guint64 ahi,
165 guint64 blo, guint64 bhi,
166 guint64* pclo, guint64* pchi)
172 if (alo < blo) chi--; /* borrow */
178 /* 192 bit addition: c = a + b
179 addition is modulo 2**192, any carry is lost */
180 DECINLINE static void add192(guint64 alo, guint64 ami, guint64 ahi,
181 guint64 blo, guint64 bmi, guint64 bhi,
182 guint64* pclo, guint64* pcmi, guint64* pchi)
185 if (alo < blo) { /* carry low */
187 if (ami == 0) ahi++; /* carry mid */
190 if (ami < bmi) ahi++; /* carry mid */
197 /* 192 bit subtraction: c = a - b
198 subtraction is modulo 2**192, any carry is lost */
199 DECINLINE static void sub192(guint64 alo, guint64 ami, guint64 ahi,
200 guint64 blo, guint64 bmi, guint64 bhi,
201 guint64* pclo, guint64* pcmi, guint64* pchi)
203 guint64 clo, cmi, chi;
209 if (cmi == 0) chi--; /* borrow mid */
210 cmi--; /* borrow low */
212 if (ami < bmi) chi--; /* borrow mid */
218 /* multiplication c(192bit) = a(96bit) * b(96bit) */
219 DECINLINE static void mult96by96to192(guint32 alo, guint32 ami, guint32 ahi,
220 guint32 blo, guint32 bmi, guint32 bhi,
221 guint64* pclo, guint64* pcmi, guint64* pchi)
224 guint32 h0, h1, h2, h3, h4, h5;
227 a = ((guint64)alo) * blo;
230 a >>= 32; carry0 = 0;
231 b = ((guint64)alo) * bmi;
232 c = ((guint64)ami) * blo;
233 a += b; if (a < b) carry0++;
234 a += c; if (a < c) carry0++;
237 a >>= 32; carry1 = 0;
238 b = ((guint64)alo) * bhi;
239 c = ((guint64)ami) * bmi;
240 d = ((guint64)ahi) * blo;
241 a += b; if (a < b) carry1++;
242 a += c; if (a < c) carry1++;
243 a += d; if (a < d) carry1++;
246 a >>= 32; a += carry0; carry0 = 0;
247 b = ((guint64)ami) * bhi;
248 c = ((guint64)ahi) * bmi;
249 a += b; if (a < b) carry0++;
250 a += c; if (a < c) carry0++;
253 a >>= 32; a += carry1;
254 b = ((guint64)ahi) * bhi;
258 a >>= 32; a += carry0;
261 *pclo = ((guint64)h1) << 32 | h0;
262 *pcmi = ((guint64)h3) << 32 | h2;
263 *pchi = ((guint64)h5) << 32 | h4;
266 /* multiplication c(128bit) = a(96bit) * b(32bit) */
267 DECINLINE static void mult96by32to128(guint32 alo, guint32 ami, guint32 ahi,
269 guint64* pclo, guint64* pchi)
274 a = ((guint64)alo) * factor;
278 a += ((guint64)ami) * factor;
282 a += ((guint64)ahi) * factor;
284 *pclo = ((guint64)h1) << 32 | h0;
288 /* multiplication c(128bit) *= b(32bit) */
289 DECINLINE static int mult128by32(guint64* pclo, guint64* pchi, guint32 factor, int roundBit)
294 a = ((guint64)(guint32)(*pclo)) * factor;
295 if (roundBit) a += factor / 2;
299 a += (*pclo >> 32) * factor;
302 *pclo = ((guint64)h1) << 32 | h0;
305 a += ((guint64)(guint32)(*pchi)) * factor;
309 a += (*pchi >> 32) * factor;
312 *pchi = ((guint64)h1) << 32 | h0;
314 return ((a >> 32) == 0) ? DECIMAL_SUCCESS : DECIMAL_OVERFLOW;
317 DECINLINE static int mult128DecadeFactor(guint64* pclo, guint64* pchi, int powerOfTen)
321 while (powerOfTen > 0) {
322 idx = (powerOfTen >= DECIMAL_MAX_INTFACTORS) ? DECIMAL_MAX_INTFACTORS : powerOfTen;
324 rc = mult128by32(pclo, pchi, constantsDecadeInt32Factors[idx], 0);
325 if (rc != DECIMAL_SUCCESS) return rc;
327 return DECIMAL_SUCCESS;
330 /* division: x(128bit) /= factor(32bit)
332 DECINLINE static int div128by32(guint64* plo, guint64* phi, guint32 factor, guint32* pRest)
337 a = (guint32)(h >> 32);
345 *phi = b << 32 | (guint32)c;
348 a |= (guint32)(h >> 32);
355 *plo = b << 32 | (guint32)c;
357 if (pRest) *pRest = (guint32) a;
360 return (a >= factor || (a == factor && (c & 1) == 1)) ? 1 : 0;
363 /* division: x(192bit) /= factor(32bit)
364 no rest and no rounding*/
365 DECINLINE static void div192by32(guint64* plo, guint64* pmi, guint64* phi,
371 a = (guint32)(h >> 32);
379 *phi = b << 32 | (guint32)c;
382 a |= (guint32)(h >> 32);
390 *pmi = b << 32 | (guint32)c;
393 a |= (guint32)(h >> 32);
401 *plo = b << 32 | (guint32)c;
404 /* returns upper 32bit for a(192bit) /= b(32bit)
405 a will contain remainder */
406 DECINLINE static guint32 div192by96to32withRest(guint64* palo, guint64* pami, guint64* pahi,
407 guint32 blo, guint32 bmi, guint32 bhi)
409 guint64 rlo, rmi, rhi; /* remainder */
410 guint64 tlo, thi; /* term */
413 rlo = *palo; rmi = *pami; rhi = *pahi;
414 if (rhi >= (((guint64)bhi) << 32)) {
415 c = LIT_GUINT32(0xFFFFFFFF);
417 c = (guint32) (rhi / bhi);
419 mult96by32to128(blo, bmi, bhi, c, &tlo, &thi);
420 sub192(rlo, rmi, rhi, 0, tlo, thi, &rlo, &rmi, &rhi);
421 while (((gint64)rhi) < 0) {
423 add192(rlo, rmi, rhi, 0, (((guint64)bmi)<<32) | blo, bhi, &rlo, &rmi, &rhi);
425 *palo = rlo ; *pami = rmi ; *pahi = rhi;
427 POSTCONDITION(rhi >> 32 == 0);
432 /* c(128bit) = a(192bit) / b(96bit)
434 DECINLINE static void div192by96to128(guint64 alo, guint64 ami, guint64 ahi,
435 guint32 blo, guint32 bmi, guint32 bhi,
436 guint64* pclo, guint64* pchi)
438 guint64 rlo, rmi, rhi; /* remainder */
441 PRECONDITION(ahi < (((guint64)bhi) << 32 | bmi)
442 || (ahi == (((guint64)bhi) << 32 | bmi) && (ami >> 32) > blo));
445 rlo = alo; rmi = ami; rhi = ahi;
446 h = div192by96to32withRest(&rlo, &rmi, &rhi, blo, bmi, bhi);
449 rhi = (rhi << 32) | (rmi >> 32); rmi = (rmi << 32) | (rlo >> 32); rlo <<= 32;
450 *pchi = (((guint64)h) << 32) | div192by96to32withRest(&rlo, &rmi, &rhi, blo, bmi, bhi);
453 rhi = (rhi << 32) | (rmi >> 32); rmi = (rmi << 32) | (rlo >> 32); rlo <<= 32;
454 h = div192by96to32withRest(&rlo, &rmi, &rhi, blo, bmi, bhi);
456 /* estimate lowest 32 bit (two last bits may be wrong) */
458 c = LIT_GUINT32(0xFFFFFFFF);
461 c = (guint32) (rhi / bhi);
463 *pclo = (((guint64)h) << 32) | c;
466 DECINLINE static void roundUp128(guint64* pclo, guint64* pchi) {
467 if (++(*pclo) == 0) ++(*pchi);
470 DECINLINE static int normalize128(guint64* pclo, guint64* pchi, int* pScale,
471 int roundFlag, int roundBit)
473 guint32 overhang = (guint32)(*pchi >> 32);
477 while (overhang != 0) {
478 for (deltaScale = 1; deltaScale < DECIMAL_MAX_INTFACTORS; deltaScale++)
480 if (overhang < constantsDecadeInt32Factors[deltaScale]) break;
484 if (scale < 0) return DECIMAL_OVERFLOW;
486 roundBit = div128by32(pclo, pchi, constantsDecadeInt32Factors[deltaScale], 0);
488 overhang = (guint32)(*pchi >> 32);
489 if (roundFlag && roundBit && *pclo == (guint64)-1 && (gint32)*pchi == (gint32)-1) {
496 if (roundFlag && roundBit) {
497 roundUp128(pclo, pchi);
498 TEST((*pchi >> 32) == 0);
501 return DECIMAL_SUCCESS;
504 DECINLINE static int maxLeftShift(/*[In, Out]*/decimal_repr* pA)
506 guint64 lo64 = (((guint64)(pA->mid32)) << 32) | pA->lo32;
507 guint32 hi32 = pA->hi32;
510 for (shift = 0; ((gint32)hi32) >= 0 && shift < 96; shift++) {
512 if (((gint64)lo64) < 0) hi32++;
516 pA->lo32 = (guint32) lo64;
517 pA->mid32 = (guint32)(lo64>>32);
523 DECINLINE static void rshift128(guint64* pclo, guint64* pchi)
526 *pclo |= (*pchi & 1) << 63;
530 DECINLINE static void lshift96(guint32* pclo, guint32* pcmid, guint32* pchi)
533 *pchi |= (*pcmid & LIT_GUINT32_HIGHBIT) >> 31;
535 *pcmid |= (*pclo & LIT_GUINT32_HIGHBIT) >> 31;
539 DECINLINE static void lshift128(guint64* pclo, guint64* pchi)
542 *pchi |= (*pclo & LIT_GUINT64_HIGHBIT) >> 63;
546 DECINLINE static void rshift192(guint64* pclo, guint64* pcmi, guint64* pchi)
549 *pclo |= (*pcmi & 1) << 63;
551 *pcmi |= (*pchi & 1) << 63;
556 my_g_bit_nth_msf (gsize mask)
558 /* Mask is expected to be != 0 */
559 #if defined(__i386__) && defined(__GNUC__)
562 __asm__("bsrl %1,%0\n\t"
563 : "=r" (r) : "rm" (mask));
565 #elif defined(__x86_64) && defined(__GNUC__)
568 __asm__("bsrq %1,%0\n\t"
569 : "=r" (r) : "rm" (mask));
571 #elif defined(__i386__) && defined(_MSC_VER)
572 unsigned long bIndex = 0;
573 if (_BitScanReverse (&bIndex, mask))
576 #elif defined(__x86_64__) && defined(_MSC_VER)
577 unsigned long bIndex = 0;
578 if (_BitScanReverse64 (&bIndex, mask))
581 #elif defined(__s390x__) && defined(__NOT_YET)
584 __asm__("\tlrvgr\t%1,%1\n"
589 : "=r" (r) : "r" (mask) : "cc");
593 i = sizeof (gsize) * 8;
596 if (mask & (1UL << i))
603 /* returns log2(a) or DECIMAL_LOG_NEGINF for a = 0 */
604 DECINLINE static int log2_32(guint32 a)
606 if (a == 0) return DECIMAL_LOG_NEGINF;
608 return my_g_bit_nth_msf (a) + 1;
611 /* returns log2(a) or DECIMAL_LOG_NEGINF for a = 0 */
612 DECINLINE static int log2_64(guint64 a)
614 if (a == 0) return DECIMAL_LOG_NEGINF;
616 #if SIZEOF_VOID_P == 8
617 return my_g_bit_nth_msf (a) + 1;
620 return my_g_bit_nth_msf ((guint32)a) + 1;
622 return my_g_bit_nth_msf ((guint32)(a >> 32)) + 1 + 32;
626 /* returns log2(a) or DECIMAL_LOG_NEGINF for a = 0 */
627 DECINLINE static int log2_128(guint64 alo, guint64 ahi)
629 if (ahi == 0) return log2_64(alo);
630 else return log2_64(ahi) + 64;
633 /* returns a upper limit for log2(a) considering scale */
634 DECINLINE static int log2withScale_128(guint64 alo, guint64 ahi, int scale)
636 int tlog2 = log2_128(alo, ahi);
637 if (tlog2 < 0) tlog2 = 0;
638 return tlog2 - (scale * 33219) / 10000;
641 DECINLINE static int pack128toDecimal(/*[Out]*/decimal_repr* pA, guint64 alo, guint64 ahi,
644 PRECONDITION((ahi >> 32) == 0);
645 PRECONDITION(sign == 0 || sign == 1);
646 PRECONDITION(scale >= 0 && scale <= DECIMAL_MAX_SCALE);
648 if (scale < 0 || scale > DECIMAL_MAX_SCALE || (ahi >> 32) != 0) {
649 return DECIMAL_OVERFLOW;
652 pA->lo32 = (guint32) alo;
653 pA->mid32 = (guint32) (alo >> 32);
654 pA->hi32 = (guint32) ahi;
655 pA->signscale.sign = sign;
656 pA->signscale.scale = scale;
658 return DECIMAL_SUCCESS;
661 DECINLINE static int adjustScale128(guint64* palo, guint64* pahi, int deltaScale)
665 if (deltaScale < 0) {
667 if (deltaScale > DECIMAL_MAX_SCALE) return DECIMAL_INTERNAL_ERROR;
668 while (deltaScale > 0) {
669 idx = (deltaScale > DECIMAL_MAX_INTFACTORS) ? DECIMAL_MAX_INTFACTORS : deltaScale;
671 div128by32(palo, pahi, constantsDecadeInt32Factors[idx], 0);
673 } else if (deltaScale > 0) {
674 if (deltaScale > DECIMAL_MAX_SCALE) return DECIMAL_INTERNAL_ERROR;
675 while (deltaScale > 0) {
676 idx = (deltaScale > DECIMAL_MAX_INTFACTORS) ? DECIMAL_MAX_INTFACTORS : deltaScale;
678 rc = mult128by32(palo, pahi, constantsDecadeInt32Factors[idx], 0);
679 if (rc != DECIMAL_SUCCESS) return rc;
683 return DECIMAL_SUCCESS;
686 /* input: c * 10^-(*pScale) * 2^-exp
687 output: c * 10^-(*pScale) with
688 minScale <= *pScale <= maxScale and (chi >> 32) == 0 */
689 DECINLINE static int rescale128(guint64* pclo, guint64* pchi, int* pScale, int texp,
690 int minScale, int maxScale, int roundFlag)
692 guint32 factor, overhang;
693 int scale, i, rc, roundBit = 0;
695 PRECONDITION(texp >= 0);
701 while (texp > 0 && scale <= maxScale) {
702 overhang = (guint32)(*pchi >> 32);
704 /* The original loop was this: */
706 while (texp > 0 && (overhang > (2<<DECIMAL_MAX_INTFACTORS) || (*pclo & 1) == 0)) {
708 roundBit = (int)(*pclo & 1);
709 rshift128(pclo, pchi);
710 overhang = (guint32)(*pchi >> 32);
714 int msf = my_g_bit_nth_msf (overhang);
715 int shift = msf - (DECIMAL_MAX_INTFACTORS + 2);
722 *pclo = (*pclo >> shift) | ((*pchi & ((1 << shift) - 1)) << (64 - shift));
727 g_assert (overhang > (2 << DECIMAL_MAX_INTFACTORS));
730 while (texp > 0 && (overhang > (2<<DECIMAL_MAX_INTFACTORS) || (*pclo & 1) == 0)) {
731 if (--texp == 0) roundBit = (int)(*pclo & 1);
732 rshift128(pclo, pchi);
736 if (texp > DECIMAL_MAX_INTFACTORS) i = DECIMAL_MAX_INTFACTORS;
738 if (scale + i > maxScale) i = maxScale - scale;
742 factor = constantsDecadeInt32Factors[i] >> i; /* 10^i/2^i=5^i */
743 mult128by32(pclo, pchi, factor, 0);
744 /*printf("3: %.17e\n", (((double)chi) * pow(2,64) + clo) * pow(10, -scale) * pow(2, -texp));*/
748 if (--texp == 0) roundBit = (int)(*pclo & 1);
749 rshift128(pclo, pchi);
755 while (scale > maxScale) {
756 i = scale - maxScale;
757 if (i > DECIMAL_MAX_INTFACTORS) i = DECIMAL_MAX_INTFACTORS;
759 roundBit = div128by32(pclo, pchi, constantsDecadeInt32Factors[i], 0);
762 while (scale < minScale) {
763 if (!roundFlag) roundBit = 0;
764 i = minScale - scale;
765 if (i > DECIMAL_MAX_INTFACTORS) i = DECIMAL_MAX_INTFACTORS;
767 rc = mult128by32(pclo, pchi, constantsDecadeInt32Factors[i], roundBit);
768 if (rc != DECIMAL_SUCCESS) return rc;
772 TEST(scale >= 0 && scale <= DECIMAL_MAX_SCALE);
776 return normalize128(pclo, pchi, pScale, roundFlag, roundBit);
780 static void trimExcessScale(guint64* pclo, guint64* pchi, int* pScale)
782 guint64 ilo = *pclo, lastlo;
783 guint64 ihi = *pchi, lasthi;
793 roundBit = div128by32(&ilo, &ihi, 10, &rest);
807 /* performs a += b */
808 gint32 mono_decimalIncr(/*[In, Out]*/decimal_repr* pA, /*[In]*/decimal_repr* pB)
810 guint64 alo, ahi, blo, bhi;
811 int log2A, log2B, log2Result, log10Result, rc;
812 int subFlag, sign, scaleA, scaleB;
816 DECTO128(pA, alo, ahi);
817 DECTO128(pB, blo, bhi);
819 sign = pA->signscale.sign;
820 subFlag = sign - (int)pB->signscale.sign;
821 scaleA = pA->signscale.scale;
822 scaleB = pB->signscale.scale;
823 if (scaleA == scaleB) {
824 /* same scale, that's easy */
826 sub128(alo, ahi, blo, bhi, &alo, &ahi);
827 if (ahi & LIT_GUINT64_HIGHBIT) {
835 add128(alo, ahi, blo, bhi, &alo, &ahi);
837 rc = normalize128(&alo, &ahi, &scaleA, 1, 0);
839 /* scales must be adjusted */
840 /* Estimate log10 and scale of result for adjusting scales */
841 log2A = log2withScale_128(alo, ahi, scaleA);
842 log2B = log2withScale_128(blo, bhi, scaleB);
843 log2Result = MAX (log2A, log2B);
844 if (!subFlag) log2Result++; /* result can have one bit more */
845 log10Result = (log2Result * 1000) / 3322 + 1;
846 /* we will calculate in 128bit, so we may need to adjust scale */
847 if (scaleB > scaleA) scaleA = scaleB;
848 if (scaleA + log10Result > DECIMAL_MAX_SCALE + 7) {
849 /* this may not fit in 128bit, so limit it */
850 scaleA = DECIMAL_MAX_SCALE + 7 - log10Result;
853 rc = adjustScale128(&alo, &ahi, scaleA - (int)pA->signscale.scale);
854 if (rc != DECIMAL_SUCCESS) return rc;
855 rc = adjustScale128(&blo, &bhi, scaleA - scaleB);
856 if (rc != DECIMAL_SUCCESS) return rc;
859 sub128(alo, ahi, blo, bhi, &alo, &ahi);
860 if (ahi & LIT_GUINT64_HIGHBIT) {
868 add128(alo, ahi, blo, bhi, &alo, &ahi);
871 rc = rescale128(&alo, &ahi,&scaleA, 0, 0, DECIMAL_MAX_SCALE, 1);
874 if (rc != DECIMAL_SUCCESS) return rc;
876 return pack128toDecimal(pA, alo, ahi, scaleA, sign);
879 /* performs a += factor * constants[idx] */
880 static int incMultConstant128(guint64* palo, guint64* pahi, int idx, int factor)
884 PRECONDITION(idx >= 0 && idx <= DECIMAL_MAX_SCALE);
885 PRECONDITION(factor > 0 && factor <= 9);
887 blo = dec128decadeFactors[idx].lo;
888 h = bhi = dec128decadeFactors[idx].hi;
890 mult128by32(&blo, &bhi, factor, 0);
891 if (h > bhi) return DECIMAL_OVERFLOW;
894 add128(*palo, *pahi, blo, bhi, palo, pahi);
895 if (h > *pahi) return DECIMAL_OVERFLOW;
896 return DECIMAL_SUCCESS;
899 DECINLINE static void div128DecadeFactor(guint64* palo, guint64* pahi, int powerOfTen)
901 int idx, roundBit = 0;
903 while (powerOfTen > 0) {
904 idx = (powerOfTen > DECIMAL_MAX_INTFACTORS) ? DECIMAL_MAX_INTFACTORS : powerOfTen;
906 roundBit = div128by32(palo, pahi, constantsDecadeInt32Factors[idx], 0);
909 if (roundBit) roundUp128(palo, pahi);
912 /* calc significant digits of mantisse */
913 DECINLINE static int calcDigits(guint64 alo, guint64 ahi)
920 return 0; /* zero has no signficant digits */
922 tlog2 = log2_64(alo);
925 tlog2 = 64 + log2_64(ahi);
928 tlog10 = (tlog2 * 1000) / 3322;
929 /* we need an exact floor value of log10(a) */
930 if (dec128decadeFactors[tlog10].hi > ahi
931 || (dec128decadeFactors[tlog10].hi == ahi
932 && dec128decadeFactors[tlog10].lo > alo)) {
938 gint32 mono_double2decimal(/*[Out]*/decimal_repr* pA, double val, gint32 digits)
941 guint64* p = (guint64*)(&val);
942 int sigDigits, sign, texp, rc, scale;
945 PRECONDITION(digits <= 15);
947 sign = ((*p & LIT_GUINT64_HIGHBIT) != 0) ? 1 : 0;
950 k = ((guint16)((*p) >> 52)) & 0x7FF;
952 // 1-bit followed by the fraction component from the float
953 alo = (*p & LIT_GUINT64(0xFFFFFFFFFFFFF)) | LIT_GUINT64(0x10000000000000);
956 texp = (k & 0x7FF) - 0x3FF;
957 if (k == 0x7FF || texp >= 96) return DECIMAL_OVERFLOW; /* NaNs, SNaNs, Infinities or >= 2^96 */
958 if (k == 0 || texp <= -94) { /* Subnormals, Zeros or < 2^-94 */
959 DECINIT(pA); /* return zero */
960 return DECIMAL_SUCCESS;
965 for (; texp > 0; texp--) {
966 lshift128(&alo, &ahi);
971 rc = rescale128(&alo, &ahi, &scale, -texp, 0, DECIMAL_MAX_SCALE, 1);
972 if (rc != DECIMAL_SUCCESS) return rc;
974 sigDigits = calcDigits(alo, ahi);
975 /* too much digits, then round */
976 if (sigDigits > digits) {
977 div128DecadeFactor(&alo, &ahi, sigDigits - digits);
978 scale -= sigDigits - digits;
979 /* check value, may be 10^(digits+1) caused by rounding */
980 if (ahi == dec128decadeFactors[digits].hi
981 && alo == dec128decadeFactors[digits].lo) {
982 div128by32(&alo, &ahi, 10, 0);
986 rc = mult128DecadeFactor(&alo, &ahi, -scale);
987 if (rc != DECIMAL_SUCCESS) return rc;
993 // Turn the double 0.6 which at this point is:
994 // 0.6000000000000000
998 trimExcessScale (&alo, &ahi, &scale);
1000 return pack128toDecimal(pA, alo, ahi, scale, sign);
1004 * mono_string2decimal:
1010 * converts a digit string to decimal
1011 * The significant digits must be passed as an integer in buf !
1014 * if you want to convert the number 123.456789012345678901234 to decimal
1015 * buf := "123456789012345678901234"
1020 * you want to convert -79228162514264337593543950335 to decimal
1021 * buf := "79228162514264337593543950335"
1026 * you want to convert -7922816251426433759354395033.250000000000001 to decimal
1027 * buf := "7922816251426433759354395033250000000000001"
1030 * returns (decimal)-7922816251426433759354395033.3
1033 * you want to convert -7922816251426433759354395033.250000000000000 to decimal
1034 * buf := "7922816251426433759354395033250000000000000"
1037 * returns (decimal)-7922816251426433759354395033.2
1040 * you want to convert -7922816251426433759354395033.150000000000000 to decimal
1041 * buf := "7922816251426433759354395033150000000000000"
1044 * returns (decimal)-7922816251426433759354395033.2
1046 * Uses banker's rule for rounding if there are more digits than can be
1047 * represented by the significant
1049 gint32 mono_string2decimal(/*[Out]*/decimal_repr* pA, MonoString* str, gint32 decrDecimal, gint32 sign)
1051 gushort *buf = mono_string_chars(str);
1054 int n, rc, i, len, sigLen = -1, firstNonZero;
1055 int scale, roundBit = 0;
1060 for (p = buf, len = 0; *p != 0; len++, p++) { }
1062 for (p = buf, i = 0; *p != 0; i++, p++) {
1064 if (n < 0 || n > 9) {
1065 return DECIMAL_INVALID_CHARACTER;
1070 sigLen = (len - firstNonZero > DECIMAL_MAX_SCALE+1)
1071 ? DECIMAL_MAX_SCALE+1+firstNonZero : len;
1072 if (decrDecimal > sigLen+1) return DECIMAL_OVERFLOW;
1074 if (i >= sigLen) break;
1075 rc = incMultConstant128(&alo, &ahi, sigLen - 1 - i, n);
1076 if (rc != DECIMAL_SUCCESS) {
1082 scale = sigLen - decrDecimal;
1084 if (i < len) { /* too much digits, we must round */
1086 if (n < 0 || n > 9) {
1087 return DECIMAL_INVALID_CHARACTER;
1089 if (n > 5) roundBit = 1;
1090 else if (n == 5) { /* we must take a nearer look */
1092 for (++i; i < len; ++i) {
1093 if (buf[i] != '0') break; /* we are greater than .5 */
1095 if (i < len /* greater than exactly .5 */
1096 || n % 2 == 1) { /* exactly .5, use banker's rule for rounding */
1103 rc = normalize128(&alo, &ahi, &scale, 1, roundBit);
1104 if (rc != DECIMAL_SUCCESS) return rc;
1107 if (alo == 0 && ahi == 0) {
1109 return DECIMAL_SUCCESS;
1111 return pack128toDecimal(pA, alo, ahi, sigLen - decrDecimal, sign);
1116 * mono_decimal2string:
1118 * returns minimal number of digit string to represent decimal
1119 * No leading or trailing zeros !
1121 * *pA == 0 => buf = "", *pDecPos = 1, *pSign = 0
1122 * *pA == 12.34 => buf = "1234", *pDecPos = 2, *pSign = 0
1123 * *pA == -1000.0000 => buf = "1", *pDecPos = 4, *pSign = 1
1124 * *pA == -0.00000076 => buf = "76", *pDecPos = -6, *pSign = 0
1127 * pA decimal instance to convert
1128 * digits < 0: use decimals instead
1129 * = 0: gets mantisse as integer
1130 * > 0: gets at most <digits> digits, rounded according to banker's rule if necessary
1131 * decimals only used if digits < 0
1132 * >= 0: number of decimal places
1133 * buf pointer to result buffer
1134 * bufSize size of buffer
1135 * pDecPos receives insert position of decimal point relative to start of buffer
1136 * pSign receives sign
1138 gint32 mono_decimal2string(/*[In]*/decimal_repr* pA, gint32 digits, gint32 decimals,
1139 MonoArray* pArray, gint32 bufSize, gint32* pDecPos, gint32* pSign)
1142 guint16 *buf = (guint16*) mono_array_addr(pArray, guint16, 0);
1143 guint16 *q, *p = tmp;
1147 gint32 sigDigits, d;
1150 MONO_ARCH_SAVE_REGS;
1152 scale = pA->signscale.scale;
1153 DECTO128(pA, alo, ahi);
1154 sigDigits = calcDigits(alo, ahi); /* significant digits */
1156 /* calc needed digits (without leading or trailing zeros) */
1157 d = (digits == 0) ? sigDigits : digits;
1158 if (d < 0) { /* use decimals ? */
1159 if (0 <= decimals && decimals < scale) {
1160 d = sigDigits - scale + decimals;
1162 d = sigDigits; /* use all you can get */
1166 if (sigDigits > d) { /* we need to round decimal number */
1168 aa.signscale.scale = DECIMAL_MAX_SCALE;
1169 mono_decimalRound(&aa, DECIMAL_MAX_SCALE - sigDigits + d);
1170 DECTO128(&aa, alo, ahi);
1171 sigDigits += calcDigits(alo, ahi) - d;
1176 /* get digits starting from the tail */
1177 for (; (alo != 0 || ahi != 0) && len < 40; len++) {
1178 div128by32(&alo, &ahi, 10, &rest);
1179 *p++ = '0' + (char) rest;
1184 if (len >= bufSize) return DECIMAL_BUFFER_OVERFLOW;
1186 /* now we have the minimal count of digits,
1187 extend to wished count of digits or decimals */
1189 if (digits >= 0) { /* count digits */
1190 if (digits >= bufSize) return DECIMAL_BUFFER_OVERFLOW;
1192 /* zero or rounded to zero */
1195 /* copy significant digits */
1196 for (i = 0; i < len; i++) {
1199 *pDecPos = sigDigits - scale;
1201 /* add trailing zeros */
1202 for (i = len; i < digits; i++) {
1205 } else { /* count decimals */
1206 if (scale >= sigDigits) { /* add leading zeros */
1207 if (decimals+2 >= bufSize) return DECIMAL_BUFFER_OVERFLOW;
1209 for (i = 0; i <= scale - sigDigits; i++) {
1213 if (sigDigits - scale + decimals+1 >= bufSize) return DECIMAL_BUFFER_OVERFLOW;
1214 *pDecPos = sigDigits - scale;
1216 /* copy significant digits */
1217 for (i = 0; i < len; i++) {
1220 /* add trailing zeros */
1221 for (i = scale; i < decimals; i++) {
1227 *pSign = (sigDigits > 0) ? pA->signscale.sign : 0; /* zero has positive sign */
1229 return DECIMAL_SUCCESS;
1233 * mono_decimal2UInt64:
1236 * converts a decimal to an UInt64 without rounding
1238 gint32 mono_decimal2UInt64(/*[In]*/decimal_repr* pA, guint64* pResult)
1243 MONO_ARCH_SAVE_REGS;
1245 DECTO128(pA, alo, ahi);
1246 scale = pA->signscale.scale;
1248 div128DecadeFactor(&alo, &ahi, scale);
1251 /* overflow if integer too large or < 0 */
1252 if (ahi != 0 || (alo != 0 && pA->signscale.sign)) return DECIMAL_OVERFLOW;
1255 return DECIMAL_SUCCESS;
1259 * mono_decimal2Int64:
1262 * converts a decimal to an Int64 without rounding
1264 gint32 mono_decimal2Int64(/*[In]*/decimal_repr* pA, gint64* pResult)
1269 MONO_ARCH_SAVE_REGS;
1271 DECTO128(pA, alo, ahi);
1272 scale = pA->signscale.scale;
1274 div128DecadeFactor(&alo, &ahi, scale);
1277 if (ahi != 0) return DECIMAL_OVERFLOW;
1279 sign = pA->signscale.sign;
1280 if (sign && alo != 0) {
1281 if (alo > LIT_GUINT64_HIGHBIT) return DECIMAL_OVERFLOW;
1282 *pResult = (gint64) ~(alo-1);
1284 if (alo & LIT_GUINT64_HIGHBIT) return DECIMAL_OVERFLOW;
1285 *pResult = (gint64) alo;
1288 return DECIMAL_SUCCESS;
1291 void mono_decimalFloorAndTrunc(/*[In, Out]*/decimal_repr* pA, gint32 floorFlag)
1294 guint32 factor, rest;
1295 int scale, sign, idx;
1298 MONO_ARCH_SAVE_REGS;
1300 scale = pA->signscale.scale;
1301 if (scale == 0) return; /* nothing to do */
1303 DECTO128(pA, alo, ahi);
1304 sign = pA->signscale.sign;
1307 idx = (scale > DECIMAL_MAX_INTFACTORS) ? DECIMAL_MAX_INTFACTORS : scale;
1308 factor = constantsDecadeInt32Factors[idx];
1310 div128by32(&alo, &ahi, factor, &rest);
1311 hasRest = hasRest || (rest != 0);
1314 if (floorFlag && hasRest && sign) { /* floor: if negative, we must round up */
1315 roundUp128(&alo, &ahi);
1318 pack128toDecimal(pA, alo, ahi, 0, sign);
1321 void mono_decimalRound(/*[In, Out]*/decimal_repr* pA, gint32 decimals)
1326 MONO_ARCH_SAVE_REGS;
1328 DECTO128(pA, alo, ahi);
1329 scale = pA->signscale.scale;
1330 sign = pA->signscale.sign;
1331 if (scale > decimals) {
1332 div128DecadeFactor(&alo, &ahi, scale - decimals);
1336 pack128toDecimal(pA, alo, ahi, scale, sign);
1339 gint32 mono_decimalMult(/*[In, Out]*/decimal_repr* pA, /*[In]*/decimal_repr* pB)
1341 guint64 low, mid, high;
1343 int scale, sign, rc;
1345 MONO_ARCH_SAVE_REGS;
1347 mult96by96to192(pA->lo32, pA->mid32, pA->hi32, pB->lo32, pB->mid32, pB->hi32,
1350 /* adjust scale and sign */
1351 scale = (int)pA->signscale.scale + (int)pB->signscale.scale;
1352 sign = pA->signscale.sign ^ pB->signscale.sign;
1354 /* first scaling step */
1355 factor = constantsDecadeInt32Factors[DECIMAL_MAX_INTFACTORS];
1356 while (high != 0 || (mid>>32) >= factor) {
1358 factor /= 1000; /* we need some digits for final rounding */
1359 scale -= DECIMAL_MAX_INTFACTORS - 3;
1361 scale -= DECIMAL_MAX_INTFACTORS;
1364 div192by32(&low, &mid, &high, factor);
1367 /* second and final scaling */
1368 rc = rescale128(&low, &mid, &scale, 0, 0, DECIMAL_MAX_SCALE, 1);
1369 if (rc != DECIMAL_SUCCESS) return rc;
1371 return pack128toDecimal(pA, low, mid, scale, sign);
1374 static DECINLINE int decimalDivSub(/*[In]*/decimal_repr* pA, /*[In]*/decimal_repr* pB,
1375 guint64* pclo, guint64* pchi, int* pExp)
1377 guint64 alo, ami, ahi;
1378 guint64 tlo, tmi, thi;
1379 guint32 blo, bmi, bhi;
1380 int ashift, bshift, extraBit, texp;
1382 ahi = (((guint64)(pA->hi32)) << 32) | pA->mid32;
1383 ami = ((guint64)(pA->lo32)) << 32;
1389 if (blo == 0 && bmi == 0 && bhi == 0) {
1390 return DECIMAL_DIVIDE_BY_ZERO;
1393 if (ami == 0 && ahi == 0) {
1395 return DECIMAL_FINISHED;
1398 /* enlarge dividend to get maximal precision */
1402 for (ashift = 64; (ahi & LIT_GUINT64_HIGHBIT) == 0; ++ashift) {
1406 for (ashift = 0; (ahi & LIT_GUINT64_HIGHBIT) == 0; ++ashift) {
1407 lshift128(&ami, &ahi);
1411 /* ensure that divisor is at least 2^95 */
1420 //g_assert (g_bit_nth_msf (bhi, 32) == my_g_bit_nth_msf (bhi));
1422 hi_shift = 31 - my_g_bit_nth_msf (bhi);
1424 bshift = 64 + hi_shift;
1430 for (bshift = 32; (bhi & LIT_GUINT32_HIGHBIT) == 0; ++bshift) {
1432 bhi |= (bmi & LIT_GUINT32_HIGHBIT) >> 31;
1437 for (bshift = 0; (bhi & LIT_GUINT32_HIGHBIT) == 0; ++bshift) {
1439 bhi |= (bmi & LIT_GUINT32_HIGHBIT) >> 31;
1441 bmi |= (blo & LIT_GUINT32_HIGHBIT) >> 31;
1446 thi = ((guint64)bhi)<<32 | bmi;
1447 tmi = ((guint64)blo)<<32;
1449 if (ahi > thi || (ahi == thi && ami >= tmi)) {
1450 sub192(alo, ami, ahi, tlo, tmi, thi, &alo, &ami, &ahi);
1456 div192by96to128(alo, ami, ahi, blo, bmi, bhi, pclo, pchi);
1457 texp = 128 + ashift - bshift;
1460 rshift128(pclo, pchi);
1461 *pchi += LIT_GUINT64_HIGHBIT;
1465 /* try loss free right shift */
1466 while (texp > 0 && (*pclo & 1) == 0) {
1468 rshift128(pclo, pchi);
1474 return DECIMAL_SUCCESS;
1477 gint32 mono_decimalDiv(/*[Out]*/decimal_repr* pC, /*[In]*/decimal_repr* pA, /*[In]*/decimal_repr* pB)
1479 guint64 clo, chi; /* result */
1480 int scale, texp, rc;
1482 MONO_ARCH_SAVE_REGS;
1484 /* Check for common cases */
1485 if (mono_decimalCompare (pA, pB) == 0)
1487 return pack128toDecimal (pC, 1, 0, 0, 0);
1488 pA->signscale.sign = pA->signscale.sign ? 0 : 1;
1489 if (mono_decimalCompare (pA, pB) == 0)
1491 return pack128toDecimal (pC, 1, 0, 0, 1);
1492 pA->signscale.sign = pA->signscale.sign ? 0 : 1;
1494 rc = decimalDivSub(pA, pB, &clo, &chi, &texp);
1495 if (rc != DECIMAL_SUCCESS) {
1496 if (rc == DECIMAL_FINISHED) rc = DECIMAL_SUCCESS;
1500 /* adjust scale and sign */
1501 scale = (int)pA->signscale.scale - (int)pB->signscale.scale;
1503 /*test: printf("0: %.17e\n", (((double)chi) * pow(2,64) + clo) * pow(10, -scale) * pow(2, -exp));*/
1504 rc = rescale128(&clo, &chi, &scale, texp, 0, DECIMAL_MAX_SCALE, 1);
1505 if (rc != DECIMAL_SUCCESS) return rc;
1507 return pack128toDecimal(pC, clo, chi, scale, pA->signscale.sign ^ pB->signscale.sign);
1510 gint32 mono_decimalIntDiv(/*[Out]*/decimal_repr* pC, /*[In]*/decimal_repr* pA, /*[In]*/decimal_repr* pB)
1512 guint64 clo, chi; /* result */
1513 int scale, texp, rc;
1515 MONO_ARCH_SAVE_REGS;
1517 rc = decimalDivSub(pA, pB, &clo, &chi, &texp);
1518 if (rc != DECIMAL_SUCCESS) {
1519 if (rc == DECIMAL_FINISHED) rc = DECIMAL_SUCCESS;
1524 scale = (int)pA->signscale.scale - (int)pB->signscale.scale;
1526 /* truncate result to integer */
1527 rc = rescale128(&clo, &chi, &scale, texp, 0, 0, 0);
1528 if (rc != DECIMAL_SUCCESS) return rc;
1530 return pack128toDecimal(pC, clo, chi, scale, pA->signscale.sign);
1533 /* approximation for log2 of a
1534 If q is the exact value for log2(a), then q <= decimalLog2(a) <= q+1 */
1535 DECINLINE static int decimalLog2(/*[In]*/decimal_repr* pA)
1538 int scale = pA->signscale.scale;
1540 if (pA->hi32 != 0) tlog2 = 64 + log2_32(pA->hi32);
1541 else if (pA->mid32 != 0) tlog2 = 32 + log2_32(pA->mid32);
1542 else tlog2 = log2_32(pA->lo32);
1544 if (tlog2 != DECIMAL_LOG_NEGINF) {
1545 tlog2 -= (scale * 33219) / 10000;
1551 DECINLINE static int decimalIsZero(/*[In]*/decimal_repr* pA)
1553 return (pA->lo32 == 0 && pA->mid32 == 0 && pA->hi32 == 0);
1556 gint32 mono_decimalCompare(/*[In]*/decimal_repr* pA, /*[In]*/decimal_repr* pB)
1558 int log2a, log2b, delta, sign;
1561 MONO_ARCH_SAVE_REGS;
1563 sign = (pA->signscale.sign) ? -1 : 1;
1565 if (pA->signscale.sign ^ pB->signscale.sign) {
1566 return (decimalIsZero(pA) && decimalIsZero(pB)) ? 0 : sign;
1569 /* try fast comparison via log2 */
1570 log2a = decimalLog2(pA);
1571 log2b = decimalLog2(pB);
1572 delta = log2a - log2b;
1573 /* decimalLog2 is not exact, so we can say nothing
1574 if abs(delta) <= 1 */
1575 if (delta < -1) return -sign;
1576 if (delta > 1) return sign;
1580 mono_decimalIncr(&aa, pB);
1582 if (decimalIsZero(&aa)) return 0;
1584 return (aa.signscale.sign) ? 1 : -1;
1587 /* d=(-1)^sign * n * 2^(k-52) with sign (1bit), k(11bit), n-2^52(52bit) */
1588 DECINLINE static void buildIEEE754Double(double* pd, int sign, int texp, guint64 mantisse)
1590 guint64* p = (guint64*) pd;
1592 PRECONDITION(sign == 0 || sign == 1);
1593 *p = (((guint64)sign) << 63) | (((guint64)((1023+texp)&0x7ff)) << 52) | mantisse;
1595 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1598 guint32 *t = (guint32*)p;
1607 double mono_decimal2double(/*[In]*/decimal_repr* pA)
1610 guint64 alo, ahi, mantisse;
1611 guint32 overhang, factor, roundBits;
1612 int scale, texp, log5, i;
1614 MONO_ARCH_SAVE_REGS;
1616 ahi = (((guint64)(pA->hi32)) << 32) | pA->mid32;
1617 alo = ((guint64)(pA->lo32)) << 32;
1619 /* special case zero */
1620 if (ahi == 0 && alo == 0) return 0.0;
1623 scale = pA->signscale.scale;
1625 /* transform n * 10^-scale and exp = 0 => m * 2^-exp and scale = 0 */
1627 while ((ahi & LIT_GUINT64_HIGHBIT) == 0) {
1628 lshift128(&alo, &ahi);
1632 overhang = (guint32) (ahi >> 32);
1633 if (overhang >= 5) {
1635 log5 = (log2_32(overhang) * 1000) / 2322; /* ln(5)/ln(2) = 2.3219... */
1636 if (log5 < DECIMAL_MAX_INTFACTORS) {
1637 /* get maximal factor=5^i, so that overhang / factor >= 1 */
1638 factor = constantsDecadeInt32Factors[log5] >> log5; /* 5^n = 10^n/2^n */
1639 i = log5 + overhang / factor;
1641 i = DECIMAL_MAX_INTFACTORS; /* we have only constants up to 10^DECIMAL_MAX_INTFACTORS */
1643 if (i > scale) i = scale;
1644 factor = constantsDecadeInt32Factors[i] >> i; /* 5^n = 10^n/2^n */
1645 /* n * 10^-scale * 2^-exp => m * 10^-(scale-i) * 2^-(exp+i) with m = n * 5^-i */
1646 div128by32(&alo, &ahi, factor, 0);
1652 /* normalize significand (highest bit should be 1) */
1653 while ((ahi & LIT_GUINT64_HIGHBIT) == 0) {
1654 lshift128(&alo, &ahi);
1658 /* round to nearest even */
1659 roundBits = (guint32)ahi & 0x7ff;
1661 if ((ahi & LIT_GUINT64_HIGHBIT) == 0) { /* overflow ? */
1664 } else if ((roundBits & 0x400) == 0) ahi &= ~1;
1666 /* 96 bit => 1 implizit bit and 52 explicit bits */
1667 mantisse = (ahi & ~LIT_GUINT64_HIGHBIT) >> 11;
1669 buildIEEE754Double(&d, pA->signscale.sign, -texp+95, mantisse);
1675 gint32 mono_decimalSetExponent(/*[In, Out]*/decimal_repr* pA, gint32 texp)
1679 int scale = pA->signscale.scale;
1681 MONO_ARCH_SAVE_REGS;
1685 if (scale < 0 || scale > DECIMAL_MAX_SCALE) {
1686 DECTO128(pA, alo, ahi);
1687 rc = rescale128(&alo, &ahi, &scale, 0, 0, DECIMAL_MAX_SCALE, 1);
1688 if (rc != DECIMAL_SUCCESS) return rc;
1689 return pack128toDecimal(pA, alo, ahi, scale, pA->signscale.sign);
1691 pA->signscale.scale = scale;
1692 return DECIMAL_SUCCESS;
1696 #endif /* DISABLE_DECIMAL */