Author: mateusz.rukowicz Date: Sun Aug 20 02:50:34 2006 New Revision: 51414 Modified: sandbox/trunk/decimal-c/_decimal.c Log: Now decimal compiles under Visual Studio, and passes all tests under windows. Modified: sandbox/trunk/decimal-c/_decimal.c ============================================================================== --- sandbox/trunk/decimal-c/_decimal.c (original) +++ sandbox/trunk/decimal-c/_decimal.c Sun Aug 20 02:50:34 2006 @@ -104,8 +104,8 @@ static void _limb_move_left(long *self, long limbs, long count) { - assert(count >= 0); long i; + assert(count >= 0); for (i = limbs - 1; i - count >= 0; i--) self[i] = self[i-count]; @@ -124,6 +124,8 @@ long new_mult = 1; long last_digit = 0; /* digit from start_pos -1 */ long i; + long diff; + long actual_limb; for (i = 0; i < new_digits; i += LOG) new[i/LOG] = 0; @@ -147,7 +149,7 @@ pos ++; } - long diff = start_pos; + diff = start_pos; while (diff < 0) /* put 0s to new */ { new[new_limb] = 0; @@ -161,16 +163,17 @@ } /* now we will just copy from self to digit */ - long actual_limb = self[self_limb]; + actual_limb = self[self_limb]; actual_limb /= self_mult; /* while don't go out of self and new */ while (pos < ndigits && pos - start_pos < new_digits) { + long x; if (self_mult == 1) actual_limb = self[self_limb]; - long x = actual_limb % 10; + x = actual_limb % 10; new[new_limb] += x * new_mult; new_mult *= 10; if (new_mult == BASE) @@ -215,13 +218,14 @@ long full_limb = 0; long mult = 1; long i; + long limbs; while(mult != BASE) { full_limb += x * mult; mult *= 10; } - long limbs = ndigits / LOG; + limbs = ndigits / LOG; for(i = 0;i<limbs;i++) self[i] = full_limb; @@ -241,14 +245,15 @@ static long _limb_get_digit(long *self, long ndigits, long i) { - if(i >= ndigits || i < 0) - return 0; long pos = ndigits - i - 1; long limb = pos / LOG; + long tmp; /* = self[limb]; */ pos %= LOG; - long tmp = self[limb]; - while(pos) - { + if (i >= ndigits || i < 0) + return 0; + + tmp = self[limb]; + while (pos) { tmp/=10; pos --; } @@ -261,8 +266,9 @@ { long limbs = (size + LOG -1)/ LOG; long slimbs; + long size_at_most; slimbs = _limb_size(self, limbs); - long size_at_most = slimbs * LOG; + size_at_most = slimbs * LOG; while(_limb_get_digit(self, size_at_most, 0) == 0 && size_at_most > 1) size_at_most --; @@ -349,6 +355,8 @@ long blimbs = (bsize + LOG -1)/LOG; long slimbs = (ssize + LOG -1)/LOG; long i; + long left_limbs; + long left_digits; for(i=0;i<blimbs;i++) out[i] = big[i]; @@ -365,10 +373,10 @@ } } - long left_limbs = blimbs-1; + left_limbs = blimbs-1; while(!out[left_limbs]) left_limbs --; - long left_digits = (left_limbs+1) * LOG; + left_digits = (left_limbs+1) * LOG; while(_limb_get_digit(out, left_digits, 0) == 0) left_digits --; @@ -488,6 +496,11 @@ long out_pos = prec - 1; /* where we write result starts at prec - 1 stops at 0*/ long new_pos = flimbs - 2; /* where is new digit to add at the end of rest, if new_pos<0 add 0 */ + long *tmp = (long*)malloc(sizeof(long) * (slimbs+1)); + if (!tmp) { + PyErr_NoMemory(); + return 0; + } rest[0] = first[flimbs-1]; @@ -499,7 +512,8 @@ down = 0; while(1) { - long tmp[slimbs+1]; + +/* long tmp[slimbs+1]; */ long tmp_limbs; long diff; long mid; @@ -566,11 +580,13 @@ out[i] = out[i+last_written]; for (i = prec - last_written; i < prec ;i++) out[i] = 0; + free(tmp); return new_pos; } new_pos --; } + free(tmp); return new_pos; } /* @@ -4641,7 +4657,7 @@ return ans; } -DECIMAL_UNARY_FUNC(exponent); +DECIMAL_UNARY_FUNC(exponent) static PyObject * _do_decimal_exp(decimalobject *self, contextobject *ctx) { @@ -4655,7 +4671,7 @@ return (PyObject*)_do_decimal_exponent(self, ctx); } -DECIMAL_UNARY_FUNC(exp); +DECIMAL_UNARY_FUNC(exp) int ln_lookup[] = {9016, 8652, 8316, 8008, 7724, 7456, 7208, 6972, 6748, 6540, 6340, 6148, 5968, 5792, 5628, 5464, 5312, @@ -4887,7 +4903,7 @@ return NULL; } -DECIMAL_UNARY_FUNC(_ln); +DECIMAL_UNARY_FUNC(_ln) static PyObject * _do_decimal_ln(decimalobject *self, contextobject *ctx) { @@ -4896,7 +4912,7 @@ return (PyObject*)_do_decimal__ln(self, ctx); } -DECIMAL_UNARY_FUNC(ln); +DECIMAL_UNARY_FUNC(ln) static decimalobject * _do_decimal_log10(decimalobject *self, contextobject *ctx) { @@ -5022,7 +5038,7 @@ } } -DECIMAL_UNARY_FUNC(log10); +DECIMAL_UNARY_FUNC(log10) /* due to some nasty special cases, this code looks quite ugly */ @@ -5149,7 +5165,7 @@ return (decimalobject*)decimal_from_long(self->ob_type, res); } -DECIMAL_BINARY_FUNC(comparetotal); +DECIMAL_BINARY_FUNC(comparetotal) static PyMethodDef decimal_methods[] = { {"comparetotal", (PyCFunction)decimal_comparetotal, @@ -5301,12 +5317,14 @@ _do_decimal_add(decimalobject *self, decimalobject *other, contextobject *ctx) { - assert(PyDecimal_Check(other)); - assert(PyDecimal_Check(self)); int shouldround, sign, negativezero = 0; exp_t exp, oexp; long prec,cmp; decimalobject *res, *res2, *ret, *o1, *o2; + decimalobject *tmp, *oother; + long numdigits; + assert(PyDecimal_Check(other)); + assert(PyDecimal_Check(self)); prec = ctx->prec; if (ISSPECIAL(self) || ISSPECIAL(other)) { @@ -5376,19 +5394,15 @@ } } - decimalobject *tmp; /* we borrow refference */ - decimalobject *oother; - long numdigits = exp_to_i(exp_sub(self->exp, other->exp)); + numdigits = exp_to_i(exp_sub(self->exp, other->exp)); - if(numdigits < 0) - { + if (numdigits < 0) { numdigits *= -1; tmp = other; oother = self; } - else - { + else { tmp = self; oother = other; } @@ -5708,6 +5722,7 @@ contextobject *ctx) { decimalobject *ret; + PyObject *seq; if (ISSPECIAL(self) || ISSPECIAL(other)) { decimalobject *nan; if (_check_nans(self, other, ctx, &nan)) @@ -5717,7 +5732,7 @@ if (decimal_nonzero(self) && !decimal_nonzero(other)) { return handle_InvalidOperation(self->ob_type, ctx, "x % 0", NULL); } - PyObject *seq = _do_decimal__divide(self, other, 3, ctx); + seq = _do_decimal__divide(self, other, 3, ctx); if (!seq) return NULL; @@ -5795,12 +5810,13 @@ { contextobject *ctx2; decimalobject *one; + PyObject *flags; ctx2 = context_copy(ctx); if (!ctx2) return NULL; - PyObject *flags = context_ignore_all_flags(ctx2); + flags = context_ignore_all_flags(ctx2); if (!flags){ Py_DECREF(ctx2); return NULL; @@ -5942,10 +5958,10 @@ /* non-integer case */ /* we calculate it using exp(ln(self) * other) */ if (use_exp) { - if (!check_ctx(self, ctx)) - return handle_InvalidContext(self->ob_type, ctx, NULL); decimalobject *tmp; contextobject *ctx2; + if (!check_ctx(self, ctx)) + return handle_InvalidContext(self->ob_type, ctx, NULL); ctx2 = context_shallow_copy(ctx); if (!ctx2) return NULL; @@ -6497,6 +6513,8 @@ decimalobject *new; long size = 0, i; char *start = NULL, *p; + long mult; + long limb; if (len < 3) return NULL; @@ -6548,8 +6566,8 @@ for(i=0;i<new->limb_count;i++) new->limbs[i] = 0; - long mult = 1; - long limb = 0; + mult = 1; + limb = 0; for(i = size -1; i>=0; i--) { assert(limb < new->limb_count); @@ -6578,11 +6596,24 @@ _decimal_fromliteralinfinity(PyTypeObject *type, char *str) { decimalobject *new; + long i; char **p = infinities; char sign = 0; + char *str2; + + str2 = (char*)malloc(strlen(str) + 1); + if (!str2) { + PyErr_NoMemory(); + return NULL; + } + + for (i = 0 ; i < strlen(str) ; i++) { + str2[i] = tolower(str[i]); + } + str2[strlen(str)] = '\0'; do { - if (strcasecmp(str, *p) == 0) { - if (str[0] == '-') + if (strcmp(str2, *p) == 0) { + if (str2[0] == '-') sign = SIGN_NEGINF; else sign = SIGN_POSINF; @@ -6594,8 +6625,10 @@ if (!new) return NULL; new->limbs[0] = 0; + free(str2); return new; } + free(str2); return NULL; } @@ -6616,6 +6649,7 @@ int any_digit = 0; char *c; long i; + long mult, limb; decimalobject *new; if(!len) @@ -6727,8 +6761,8 @@ return new; } - long mult = 1; /* now we just read integer till '\0' or 'e'/'E', dont care about '.' */ - long limb = 0; + mult = 1; /* now we just read integer till '\0' or 'e'/'E', dont care about '.' */ + limb = 0; for(c = last_digit; c >= first_digit; c--) { @@ -6831,6 +6865,7 @@ PyObject *tup, *digits, *digtup = NULL, *item; int sign; long i; + long mult, limb; exp_t exp; PyObject *tmp_exp; @@ -6868,13 +6903,14 @@ for(i = 0;i < new->limb_count;i++) new->limbs[i] = 0; - long mult = 1; - long limb = 0; + mult = 1; + limb = 0; new->limbs[0] = 0; for(i = new->ob_size-1; i>=0; i--) /* limb[0] keeps least significant limb */ { - item = PyTuple_GET_ITEM(digtup, i); long x; + item = PyTuple_GET_ITEM(digtup, i); + if(PyInt_Check(item)) x = PyInt_AsLong(item); else if (PyLong_Check(item)) { @@ -7008,11 +7044,12 @@ /* try buffer interface (e.g. strings and unicode) */ if (PyObject_AsCharBuffer(value, (const char **)&buffer, &buf_len) == 0) { + PyObject *res; if (buf_len > LONG_MAX) { PyErr_NoMemory(); return NULL; } - PyObject *res = (PyObject *)decimal_from_string(type, buffer, buf_len, ctx); + res = (PyObject *)decimal_from_string(type, buffer, buf_len, ctx); return res; } @@ -7554,9 +7591,9 @@ CONTEXT_UNARY_FUNC(sqrt, sqrt) CONTEXT_UNARY_FUNC(to_eng_string, to_eng_string) CONTEXT_UNARY_FUNC(exp, exp) -CONTEXT_UNARY_FUNC(ln, ln); -CONTEXT_UNARY_FUNC(log10, log10); -CONTEXT_BINARY_FUNC(comparetotal, comparetotal); +CONTEXT_UNARY_FUNC(ln, ln) +CONTEXT_UNARY_FUNC(log10, log10) +CONTEXT_BINARY_FUNC(comparetotal, comparetotal) /* Unfortunately, the following methods are non-standard and can't @@ -7567,6 +7604,8 @@ { static char *kwlist[] = {"a", 0}; decimalobject *a; + decimalobject *tmp; + PyObject *ret; if(!PyArg_ParseTupleAndKeywords(args, kwds, "O:_apply", kwlist, &a)) return NULL; @@ -7577,11 +7616,11 @@ return NULL; } - decimalobject *tmp = _decimal_fix(a, self); + tmp = _decimal_fix(a, self); if(!tmp) return NULL; - PyObject *ret = decimal_str(tmp); + ret = decimal_str(tmp); Py_DECREF(tmp); return ret;
participants (1)
-
mateusz.rukowicz