[Python-checkins] r51047 - sandbox/trunk/decimal-c/_decimal.c sandbox/trunk/decimal-c/decimal.h
mateusz.rukowicz
python-checkins at python.org
Wed Aug 2 19:08:49 CEST 2006
Author: mateusz.rukowicz
Date: Wed Aug 2 19:08:45 2006
New Revision: 51047
Modified:
sandbox/trunk/decimal-c/_decimal.c
sandbox/trunk/decimal-c/decimal.h
Log:
Get rid of 'digits' array. Some minor fixes.
Modified: sandbox/trunk/decimal-c/_decimal.c
==============================================================================
--- sandbox/trunk/decimal-c/_decimal.c (original)
+++ sandbox/trunk/decimal-c/_decimal.c Wed Aug 2 19:08:45 2006
@@ -1305,8 +1305,6 @@
res = _new_decimalobj(type, thing->ob_size, sign, exp_from_i(0));
if (!res) return NULL;
- for (i = 0; i < thing->ob_size; i++)
- res->digits[i] = thing->digits[i]; /* DELETE */
for (i = 0; i< res->limb_count;i++)
res->limbs[i] = thing->limbs[i];
@@ -1419,8 +1417,6 @@
res = _new_decimalobj(type, ctx->prec,
lsign, exp_sub_i(ctx->Emax, ctx->prec - 1));
if (res) {
- for (i = 0; i < ctx->prec; i++)
- res->digits[i] = 9;
_limb_fill(res->limbs, ctx->prec, 9);
return res;
}
@@ -1432,8 +1428,6 @@
res = _new_decimalobj(type, ctx->prec,
lsign, exp_sub_i(ctx->Emax, ctx->prec - 1));
if (res) {
- for (i = 0; i < ctx->prec; i++)
- res->digits[i] = 9;
_limb_fill(res->limbs, ctx->prec, 9);
return res;
}
@@ -1475,11 +1469,6 @@
return NULL;
}
- arr = PyObject_MALLOC(ndigits);
- if (!arr) {
- PyErr_NoMemory();
- goto err;
- }
arr2 = PyObject_MALLOC(limb_c * sizeof(long));
@@ -1493,7 +1482,6 @@
new->sign = sign;
new->exp = exp;
new->ob_size = ndigits;
- new->digits = arr;
new->limb_count = limb_c;
new->limbs = arr2;
return new;
@@ -1604,8 +1592,6 @@
long i;
decimalobject *new = _NEW_decimalobj(prec, self->sign, exp_add(self->exp, expdiff));
if (!new) return NULL;
- for (i = 0; i < prec; i++)
- new->digits[i] = self->digits[i];
_limb_first_n_digits(self->limbs, self->ob_size, 0, new->limbs, prec);
return new;
@@ -1619,8 +1605,6 @@
decimalobject *new = _NEW_decimalobj(prec, self->sign, exp_add(self->exp, expdiff));
decimalobject *new2 = NULL;
if (!new) return NULL;
- for (i = 0; i < prec; i++)
- new->digits[i] = self->digits[i];
_limb_first_n_digits(self->limbs, self->ob_size, 0, new->limbs, prec);
for (i = prec; i < self->ob_size; i++)
@@ -1676,8 +1660,6 @@
assert(exp_g_i(expdiff, 0));
tmp = _NEW_decimalobj(prec, self->sign, exp_add(self->exp, expdiff));
if (!tmp) return NULL;
- for (i = 0; i < prec; i++)
- tmp->digits[i] = self->digits[i];
last = _limb_first_n_digits(self->limbs, self->ob_size, 0, tmp->limbs, prec);
if (last == 5) {
@@ -1700,8 +1682,6 @@
assert(exp_g_i(expdiff, 0));
tmp = _NEW_decimalobj(prec, self->sign, exp_add(self->exp, expdiff));
if (!tmp) return NULL;
- for (i = 0; i < prec; i++)
- tmp->digits[i] = self->digits[i];
last = _limb_first_n_digits(self->limbs, self->ob_size, 0, tmp->limbs, prec);
if (last == 5) {
for (i = prec+1; i < self->ob_size; i++) {
@@ -1722,8 +1702,6 @@
long i;
tmp = _NEW_decimalobj(prec, self->sign, exp_add(self->exp, expdiff));
if (!tmp) return NULL;
- for (i = 0; i < prec; i++)
- tmp->digits[i] = self->digits[i];
_limb_first_n_digits(self->limbs, self->ob_size, 0, tmp->limbs, prec);
return _do_round_half_up(self, prec, expdiff, ctx, tmp);
}
@@ -1794,8 +1772,6 @@
new = _NEW_decimalobj(i, self->sign,
exp_add_i(self->exp, self->ob_size - prec));
if (!new) return NULL;
- while (i--)
- new->digits[i] = 0;
_limb_fill(new->limbs, new->ob_size,0);
if (handle_Rounded(ctx, NULL) != 0) {
@@ -1808,17 +1784,12 @@
if (prec == 0) {
new = _NEW_decimalobj(self->ob_size+1, self->sign, self->exp);
if (!new) return NULL;
- new->digits[0] = 0;
- for (i = 1; i < new->ob_size; i++)
- new->digits[i] = self->digits[i-1];
_limb_first_n_digits(self->limbs, self->ob_size, -1, new->limbs, new->ob_size);
prec = 1;
} else if (prec < 0) {
new = _NEW_decimalobj(2, self->sign,
exp_add_i(self->exp, self->ob_size - prec - 1));
if (!new) return NULL;
- new->digits[0] = 0;
- new->digits[1] = 1;
new->limbs[0] = 1;
prec = 1;
} else {
@@ -1838,12 +1809,6 @@
Py_DECREF(new);
return NULL;
}
- for (i = 0; i < new->ob_size; i++) {
- new2->digits[i] = new->digits[i];
- }
- for (i = new->ob_size; i < new2->ob_size; i++) {
- new2->digits[i] = 0;
- }
_limb_first_n_digits(new->limbs, new->ob_size, 0, new2->limbs, new2->ob_size);
Py_DECREF(new);
@@ -1935,7 +1900,6 @@
ans = _NEW_decimalobj(1, self->sign, exp);
if (!ans)
return NULL;
- ans->digits[0] = 0;
ans->limbs[0] = 0;
return ans;
}
@@ -1952,17 +1916,12 @@
ans = _NEW_decimalobj(2, self->sign, exp_sub_i(self->exp, digits));
if (!ans)
return NULL;
- ans->digits[0] = 0;
- ans->digits[1] = 1;
ans->limbs[0] = 1;
digits = 1;
} else { /* SLOW */
ans = _NEW_decimalobj(self->ob_size+1, self->sign, self->exp);
if (!ans)
return NULL;
- for (i = 0; i < self->ob_size; i++)
- ans->digits[i+1] = self->digits[i];
- ans->digits[0] = 0;
for(i=0;i<ans->limb_count;i++)
ans->limbs[i] = 0;
_limb_first_n_digits(self->limbs, self->ob_size, 0, ans->limbs, ans->ob_size-1);
@@ -1975,8 +1934,6 @@
if(_limb_get_digit(tmp -> limbs, tmp->ob_size, 0) == 0 && tmp->ob_size > 1){
/* We need one digit less, just clobber tmp. */
- for (i = 0; i < tmp->ob_size-1; i++)
- tmp->digits[i] = tmp->digits[i+1]; /* TODO make last digit 0 */
tmp->ob_size--;
tmp->limb_count = (tmp->ob_size + LOG -1)/LOG;
}
@@ -2055,6 +2012,7 @@
}
if (handle_Subnormal(ctx, NULL) != 0)
goto err;
+ /* TODO actually, it needs fixing */
if (_is_flag_set(ctx->flags, S_INEXACT)) {
if (handle_Underflow(ctx, NULL) != 0)
goto err;
@@ -3407,8 +3365,6 @@
new = _NEW_decimalobj(self->ob_size, self->sign, self->exp);
if (!new)
return NULL;
- for (i = 0; i < self->ob_size; i++)
- new->digits[i] = self->digits[i];
for (i = 0;i < new->limb_count;i++)
new->limbs[i] = self->limbs[i];
return new;
@@ -5010,7 +4966,6 @@
sign = other->sign;
res = _new_decimalobj(self->ob_type, 1, sign, exp);
if (!res) return NULL;
- res->digits[0] = 0;
res->limbs[0] = 0;
ret = _decimal_fix(res, ctx);
Py_DECREF(res);
@@ -5631,7 +5586,7 @@
/* we take context precision or size of self if greater
* and add some constant */
ctx2->prec = ctx->prec > self->ob_size ? ctx->prec : self->ob_size;
- ctx2->prec += 10;
+ ctx2->prec += 14;
ret = _do_decimal__ln(self, ctx2);
if (!ret) {
@@ -5737,7 +5692,7 @@
}
firstprec = ctx->prec;
- ctx->prec += 1;
+ ctx->prec += 2;
{
long t = n;
if(!t)
@@ -5749,9 +5704,6 @@
t/=10;
}
}
- /* when n < 0 we need to extend precision - then every tests pass =] */
- if (n<0)
- ctx->prec += 1;
/* TODO shouldn't it be Invalid_Context ? */
if (!mod && exp_l_i(PyDecimal_DefaultContext->Emax, ctx->prec)) {
@@ -6161,7 +6113,6 @@
static void
decimal_dealloc(decimalobject *d)
{
- PyObject_FREE(d->digits);
PyObject_FREE(d->limbs);
d->ob_type->tp_free(d);
}
@@ -6222,8 +6173,6 @@
new = _new_decimalobj(type, size, sign, exp_from_i(0));
if (!new)
return NULL;
- for (i = 0; i < size; i++)
- new->digits[i] = *(start+i) -48;
for(i=0;i<new->limb_count;i++)
new->limbs[i] = 0;
@@ -6272,7 +6221,6 @@
new = _new_decimalobj(type, 1, sign, exp_from_i(0));
if (!new)
return NULL;
- new->digits[0] = 0;
new->limbs[0] = 0;
return new;
}
@@ -6468,7 +6416,6 @@
if (value == 0) {
new = _new_decimalobj(type, 1, 0, exp_from_i(0));
if (!new) return NULL;
- new->digits[0] = 0;
new->limbs[0] = 0;
return (PyObject *)new;
} else if (value < 0) {
@@ -6486,11 +6433,6 @@
new = _new_decimalobj(type, ndigits, (neg ? SIGN_NEG : SIGN_POS), exp_from_i(0));
if (!new) return NULL;
- while (value) {
- new->digits[ndigits-i-1] = value % 10;
- value /= 10;
- i++;
- }
for(i=0; i < new->limb_count; i++)
{
@@ -6548,29 +6490,6 @@
goto err;
}
new = _new_decimalobj(type, PyTuple_GET_SIZE(digtup), sign, exp);
- for (i = 0; i < new->ob_size; i++) {
- item = PyTuple_GET_ITEM(digtup, i);
- long x;
- if (PyInt_Check(item)) {
- x = PyInt_AsLong(item);
- } else if (PyLong_Check(item)) {
- x = PyLong_AsLong(item);
- if (x == -1 && PyErr_Occurred())
- goto err;
- } else {
- PyErr_SetString(PyExc_ValueError, "The second value in the tuple "
- "must be composed of non negative integer elements.");
- goto err;
- }
-
- if(x < 0 || x > 9)
- {
- PyErr_Format(PyExc_ValueError, "Invalid digit: %ld", x);
- goto err;
- }
-
- new->digits[i] = x;
- } /* this loop will go out soon XXX */
for(i = 0;i < new->limb_count;i++)
new->limbs[i] = 0;
@@ -6674,7 +6593,6 @@
if (value == NULL) {
decimalobject *new = _new_decimalobj(type, 1, 0, exp_from_i(0));
if (!new) return NULL;
- new->digits[0] = 0;
new->limbs[0] = 0;
return (PyObject *)new;
}
@@ -6830,6 +6748,7 @@
static int
_decimal_set_int(decimalobject *self, PyObject *value)
{
+ /*
long i, size, val;
char *arr;
PyObject *item;
@@ -6862,7 +6781,7 @@
PyObject_FREE(self->digits);
self->ob_size = size;
self->digits = arr;
- return 0;
+ return 0; TODO */
}
static PyObject *
Modified: sandbox/trunk/decimal-c/decimal.h
==============================================================================
--- sandbox/trunk/decimal-c/decimal.h (original)
+++ sandbox/trunk/decimal-c/decimal.h Wed Aug 2 19:08:45 2006
@@ -13,7 +13,7 @@
#ifdef BIG_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 */
+#define EXP_LIMB_COUNT 5 /* maximal number of limbs per exp */
typedef struct {
long limbs[EXP_LIMB_COUNT];
int sign;
@@ -23,9 +23,9 @@
#define exp_t long
#endif
-#define BASE 1000 /* biggest value of limb power of 10 */
-#define LOG 3 /* number of digits per limb LOG = log10(BASE) */
-#define LOG_STR "3" /* any ideas how to avoid that? :P */
+#define BASE 10000 /* biggest value of limb power of 10 */
+#define LOG 4 /* number of digits per limb LOG = log10(BASE) */
+#define LOG_STR "4" /* any ideas how to avoid that? :P */
/* decimalobject struct ******************************************************/
typedef struct {
@@ -33,7 +33,7 @@
long ob_size; /* number of digits */
unsigned int sign; /* see sign contstants above */
exp_t exp;
- signed char *digits; /* digits are stored as the actual numbers 0-9 */
+/* signed char *digits;*/ /* digits are stored as the actual numbers 0-9 */
long limb_count; /* number of limbs */
long *limbs;
} PyDecimalObject;
More information about the Python-checkins
mailing list