[Python-checkins] r50484 - sandbox/trunk/decimal-c/_decimal.c sandbox/trunk/decimal-c/decimal.h

mateusz.rukowicz python-checkins at python.org
Fri Jul 7 22:18:14 CEST 2006


Author: mateusz.rukowicz
Date: Fri Jul  7 22:18:13 2006
New Revision: 50484

Modified:
   sandbox/trunk/decimal-c/_decimal.c
   sandbox/trunk/decimal-c/decimal.h
Log:
Some minor speed ups with exp. Fixed compiling error when BIG_EXP not defined.


Modified: sandbox/trunk/decimal-c/_decimal.c
==============================================================================
--- sandbox/trunk/decimal-c/_decimal.c	(original)
+++ sandbox/trunk/decimal-c/_decimal.c	Fri Jul  7 22:18:13 2006
@@ -546,8 +546,8 @@
 static exp_t 
 exp_from_i(long a) {
     exp_t ret;
+    long i;
 
-    memset(ret.limbs, 0, sizeof(long) * EXP_LIMB_COUNT);
     ret.limbs[0] = a;
     ret.sign = 0;
 
@@ -556,7 +556,14 @@
         ret.limbs[0] *= -1;
     }
     
-    ret.size = _limb_normalize(ret.limbs, EXP_LIMB_COUNT);
+    for (i=0 ; i<EXP_LIMB_COUNT-1;i++) {
+        ret.limbs[i+1] = ret.limbs[i] / BASE;
+        ret.limbs[i] %= BASE;
+        if (!ret.limbs[i+1])
+            break;
+    }
+
+    ret.size = i+1;
 
     return ret;
 }
@@ -569,7 +576,6 @@
     long i;
     exp->sign = 0;
     exp->size = 1;
-    memset(exp->limbs, 0, sizeof(long) * EXP_LIMB_COUNT);
     
     if (buf[0] == '-') {
         exp->sign = 1;
@@ -598,7 +604,10 @@
         if (limb >= EXP_LIMB_COUNT)
             return 0;
         
-        exp->limbs[limb] += mul * (buf[i] - '0');
+        if (mul == 1)
+            exp->limbs[limb] = buf[i] - '0';
+        else
+            exp->limbs[limb] += mul * (buf[i] - '0');
 
         mul *= 10;
         if (mul == BASE) {
@@ -717,7 +726,7 @@
         cmp = _limb_compare(a->limbs, a->size, b.limbs, b.size);
 
         if (cmp == 0) {
-            memset(a->limbs, 0, sizeof(long) * a->size);
+            a->limbs[0] = 0;
             a->size = 1;
             a->sign = 0;
         }
@@ -728,7 +737,6 @@
 
         if (cmp == -1) {
             exp_t tmp;
-            memset(tmp.limbs, 0, sizeof(long) * EXP_LIMB_COUNT);
             tmp.size = _limb_sub(b.limbs, b.size * LOG, a->limbs, a->size * LOG, tmp.limbs);
             tmp.size = (tmp.size + LOG -1) / LOG;
             *a = tmp;
@@ -793,7 +801,7 @@
 static exp_t
 exp_mul(exp_t a, exp_t b) {
     exp_t ret;
-    memset(ret.limbs, 0, sizeof(long) *EXP_LIMB_COUNT);
+/*    memset(ret.limbs, 0, sizeof(long) *EXP_LIMB_COUNT); */
     ret.size = _limb_multiply_core(a.limbs, a.size, b.limbs, b.size, ret.limbs);
     ret.sign = a.sign ^ b.sign;
     if (ret.size == 1 && ret.limbs[0] == 0)
@@ -814,7 +822,7 @@
     for (i=0 ; i<LOG ;i++)
         mult *= 10;
     
-    memset(ret.limbs, 0, sizeof(long) * EXP_LIMB_COUNT);
+/*    memset(ret.limbs, 0, sizeof(long) * EXP_LIMB_COUNT); */
     *remainder = 0;
     
     for (i = a.size - 1; i>=0; i--) {
@@ -824,7 +832,7 @@
         *remainder %= b;    
     }
 
-    ret.size = EXP_LIMB_COUNT;
+    ret.size = a.size;
     while (!ret.limbs[ret.size-1] && ret.size > 1) ret.size --;
     ret.sign = a.sign ^ (b < 0);
 
@@ -1017,6 +1025,8 @@
 #define exp_max(a, b)       ((a) > (b) ? (a) : (b)) 
 #define exp_neg(a)          (-(a))          
 #define exp_mul_i(a, b)     ((a) * (b))     
+#define exp_to_pyobj(a)     (PyInt_FromLong(a))
+#define exp_from_pyobj(a)   (PyInt_AsLong(a))
 #endif
 
 
@@ -6980,8 +6990,13 @@
     char roundstr[20] = "ROUND_";
     char flags[250] = "["; /* 250 is enough for 12 error names of max. 17 chars */
     char traps[250] = "["; /* and commas inbetween. */
+#ifdef BIG_EXP
     char Emin[LOG * EXP_LIMB_COUNT + 1];
     char Emax[LOG * EXP_LIMB_COUNT + 1];
+#else
+    char Emin[20];
+    char Emax[20];
+#endif
     long flaglen = 1, traplen = 1;
 
     switch (self->rounding) {

Modified: sandbox/trunk/decimal-c/decimal.h
==============================================================================
--- sandbox/trunk/decimal-c/decimal.h	(original)
+++ sandbox/trunk/decimal-c/decimal.h	Fri Jul  7 22:18:13 2006
@@ -11,7 +11,9 @@
     
     
 #ifdef BIG_EXP
-#define EXP_LIMB_COUNT 10   /* maximal number of limbs per exp */
+/* At the moment this has significant speed effect, since
+ * there is many exp_t copied */
+#define EXP_LIMB_COUNT 10  /* maximal number of limbs per exp */
 typedef struct {
     long limbs[EXP_LIMB_COUNT];
     int sign;


More information about the Python-checkins mailing list