grammar updates
[mono.git] / mono / metadata / decimal.c
index e9e17cbcf5afb558c4cc9eb87d878fd50fe1053b..ca8c3fa783d85888add06ab41a2e8d456523b749 100644 (file)
 #include <math.h>
 
 /* needed for building microsoft dll */
+#ifdef __GNUC__
 #define DECINLINE __inline
+#else
+#define DECINLINE
+#endif
 
 #define LIT_GUINT32(x) x
 #define LIT_GUINT64(x) x##L
@@ -90,7 +94,7 @@
 
 #define DECIMAL_LOG_NEGINF -1000
 
-static guint32 constantsDecadeInt32Factors[DECIMAL_MAX_INTFACTORS+1] = {
+static const guint32 constantsDecadeInt32Factors[DECIMAL_MAX_INTFACTORS+1] = {
     LIT_GUINT32(1), LIT_GUINT32(10), LIT_GUINT32(100), LIT_GUINT32(1000), 
     LIT_GUINT32(10000), LIT_GUINT32(100000), LIT_GUINT32(1000000), 
     LIT_GUINT32(10000000), LIT_GUINT32(100000000), LIT_GUINT32(1000000000)
@@ -101,7 +105,7 @@ typedef struct {
     guint64 hi;
 } dec128_repr;
 
-static dec128_repr dec128decadeFactors[DECIMAL_MAX_SCALE+1] = {
+static const dec128_repr dec128decadeFactors[DECIMAL_MAX_SCALE+1] = {
     LIT_DEC128( 0, 0, 1u), /* == 1 */
     LIT_DEC128( 0, 0, 10u), /* == 10 */
     LIT_DEC128( 0, 0, 100u), /* == 100 */
@@ -345,7 +349,7 @@ DECINLINE static int div128by32(guint64* plo, guint64* phi, guint32 factor, guin
     if (pRest) *pRest = (guint32) a;
 
     a <<= 1;
-    return (a > factor || (a == factor && (c & 1) == 1)) ? 1 : 0;
+    return (a >= factor || (a == factor && (c & 1) == 1)) ? 1 : 0;
 }
 
 /* division: x(192bit) /= factor(32bit) 
@@ -737,12 +741,12 @@ DECINLINE static int rescale128(guint64* pclo, guint64* pchi, int* pScale, int t
 /* performs a += b */
 gint32 mono_decimalIncr(/*[In, Out]*/decimal_repr* pA, /*[In]*/decimal_repr* pB)
 {
-    MONO_ARCH_SAVE_REGS;
-
     guint64 alo, ahi, blo, bhi;
     int log2A, log2B, log2Result, log10Result, rc;
     int subFlag, sign, scaleA, scaleB;
 
+    MONO_ARCH_SAVE_REGS;
+
     DECTO128(pA, alo, ahi);
     DECTO128(pB, blo, bhi);
 
@@ -1051,8 +1055,6 @@ gint32 mono_string2decimal(/*[Out]*/decimal_repr* pA, MonoString* str, gint32 de
 gint32 mono_decimal2string(/*[In]*/decimal_repr* pA, gint32 digits, gint32 decimals,
                                    MonoArray* pArray, gint32 bufSize, gint32* pDecPos, gint32* pSign)
 {
-    MONO_ARCH_SAVE_REGS;
-
     guint16 tmp[41];
     guint16 *buf = (guint16*) mono_array_addr(pArray, guint16, 0);
     guint16 *q, *p = tmp;
@@ -1062,6 +1064,8 @@ gint32 mono_decimal2string(/*[In]*/decimal_repr* pA, gint32 digits, gint32 decim
     gint32 sigDigits, d;
     int i, scale, len;
 
+    MONO_ARCH_SAVE_REGS;
+
     scale = pA->signscale.scale;
     DECTO128(pA, alo, ahi);
     sigDigits = calcDigits(alo, ahi); /* significant digits */
@@ -1147,11 +1151,11 @@ gint32 mono_decimal2string(/*[In]*/decimal_repr* pA, gint32 digits, gint32 decim
  */
 gint32 mono_decimal2UInt64(/*[In]*/decimal_repr* pA, guint64* pResult)
 {
-    MONO_ARCH_SAVE_REGS;
-
     guint64 alo, ahi;
     int scale;
 
+    MONO_ARCH_SAVE_REGS;
+
     DECTO128(pA, alo, ahi);
     scale = pA->signscale.scale;
     if (scale > 0) {
@@ -1170,11 +1174,11 @@ gint32 mono_decimal2UInt64(/*[In]*/decimal_repr* pA, guint64* pResult)
  */
 gint32 mono_decimal2Int64(/*[In]*/decimal_repr* pA, gint64* pResult)
 {
-    MONO_ARCH_SAVE_REGS;
-
     guint64 alo, ahi;
     int sign, scale;
 
+    MONO_ARCH_SAVE_REGS;
+
     DECTO128(pA, alo, ahi);
     scale = pA->signscale.scale;
     if (scale > 0) {
@@ -1197,13 +1201,13 @@ gint32 mono_decimal2Int64(/*[In]*/decimal_repr* pA, gint64* pResult)
 
 void mono_decimalFloorAndTrunc(/*[In, Out]*/decimal_repr* pA, gint32 floorFlag)
 {
-    MONO_ARCH_SAVE_REGS;
-
     guint64 alo, ahi;
     guint32 factor, rest;
     int scale, sign, idx;
     int hasRest = 0;
 
+    MONO_ARCH_SAVE_REGS;
+
     scale = pA->signscale.scale;
     if (scale == 0) return; /* nothing to do */
 
@@ -1227,11 +1231,11 @@ void mono_decimalFloorAndTrunc(/*[In, Out]*/decimal_repr* pA, gint32 floorFlag)
 
 void mono_decimalRound(/*[In, Out]*/decimal_repr* pA, gint32 decimals)
 {
-    MONO_ARCH_SAVE_REGS;
-
     guint64 alo, ahi;
     int scale, sign;
 
+    MONO_ARCH_SAVE_REGS;
+
     DECTO128(pA, alo, ahi);
     scale = pA->signscale.scale;
     sign = pA->signscale.sign;
@@ -1245,12 +1249,12 @@ void mono_decimalRound(/*[In, Out]*/decimal_repr* pA, gint32 decimals)
 
 gint32 mono_decimalMult(/*[In, Out]*/decimal_repr* pA, /*[In]*/decimal_repr* pB)
 {
-    MONO_ARCH_SAVE_REGS;
-
     guint64 low, mid, high;
     guint32 factor;
     int scale, sign, rc;
 
+    MONO_ARCH_SAVE_REGS;
+
     mult96by96to192(pA->lo32, pA->mid32, pA->hi32, pB->lo32, pB->mid32, pB->hi32,
         &low, &mid, &high);
 
@@ -1345,11 +1349,11 @@ static int decimalDivSub(/*[In]*/decimal_repr* pA, /*[In]*/decimal_repr* pB,
 
 gint32 mono_decimalDiv(/*[Out]*/decimal_repr* pC, /*[In]*/decimal_repr* pA, /*[In]*/decimal_repr* pB)
 {
-    MONO_ARCH_SAVE_REGS;
-
     guint64 clo, chi; /* result */
     int scale, texp, rc;
 
+    MONO_ARCH_SAVE_REGS;
+
     rc = decimalDivSub(pA, pB, &clo, &chi, &texp);
     if (rc != DECIMAL_SUCCESS) {
         if (rc == DECIMAL_FINISHED) rc = DECIMAL_SUCCESS;
@@ -1368,11 +1372,11 @@ gint32 mono_decimalDiv(/*[Out]*/decimal_repr* pC, /*[In]*/decimal_repr* pA, /*[I
 
 gint32 mono_decimalIntDiv(/*[Out]*/decimal_repr* pC, /*[In]*/decimal_repr* pA, /*[In]*/decimal_repr* pB)
 {
-    MONO_ARCH_SAVE_REGS;
-
     guint64 clo, chi; /* result */
     int scale, texp, rc;
 
+    MONO_ARCH_SAVE_REGS;
+
     rc = decimalDivSub(pA, pB, &clo, &chi, &texp);
     if (rc != DECIMAL_SUCCESS) {
         if (rc == DECIMAL_FINISHED) rc = DECIMAL_SUCCESS;
@@ -1414,11 +1418,11 @@ DECINLINE static int decimalIsZero(/*[In]*/decimal_repr* pA)
 
 gint32 mono_decimalCompare(/*[In]*/decimal_repr* pA, /*[In]*/decimal_repr* pB)
 {
-    MONO_ARCH_SAVE_REGS;
-
     int log2a, log2b, delta, sign;
     decimal_repr aa;
 
+    MONO_ARCH_SAVE_REGS;
+
     sign = (pA->signscale.sign) ? -1 : 1;
 
     if (pA->signscale.sign ^ pB->signscale.sign) {
@@ -1454,13 +1458,13 @@ DECINLINE static void buildIEEE754Double(double* pd, int sign, int texp, guint64
 
 double mono_decimal2double(/*[In]*/decimal_repr* pA)
 {
-    MONO_ARCH_SAVE_REGS;
-
     double d;
     guint64 alo, ahi, mantisse;
     guint32 overhang, factor, roundBits;
     int scale, texp, log5, i;
 
+    MONO_ARCH_SAVE_REGS;
+
     ahi = (((guint64)(pA->hi32)) << 32) | pA->mid32;
     alo = ((guint64)(pA->lo32)) << 32;
 
@@ -1508,7 +1512,7 @@ double mono_decimal2double(/*[In]*/decimal_repr* pA)
     ahi += 0x400;
     if ((ahi & LIT_GUINT64_HIGHBIT) == 0) { /* overflow ? */
         ahi >>= 1;
-        texp++;
+       texp--;
     } else if ((roundBits & 0x400) == 0) ahi &= ~1;
 
     /* 96 bit => 1 implizit bit and 52 explicit bits */
@@ -1522,12 +1526,12 @@ double mono_decimal2double(/*[In]*/decimal_repr* pA)
 /* a *= 10^exp */
 gint32 mono_decimalSetExponent(/*[In, Out]*/decimal_repr* pA, gint32 texp)
 {
-    MONO_ARCH_SAVE_REGS;
-
     guint64 alo, ahi;
     int rc;
     int scale = pA->signscale.scale;
 
+    MONO_ARCH_SAVE_REGS;
+
     scale -= texp;
 
     if (scale < 0 || scale > DECIMAL_MAX_SCALE) {