[Python-checkins] r75614 - sandbox/trunk/decimal/decimal_in_c/deccoeff.c
mark.dickinson
python-checkins at python.org
Thu Oct 22 18:03:51 CEST 2009
Author: mark.dickinson
Date: Thu Oct 22 18:03:51 2009
New Revision: 75614
Log:
Remove support for alternative limb representations
Modified:
sandbox/trunk/decimal/decimal_in_c/deccoeff.c
Modified: sandbox/trunk/decimal/decimal_in_c/deccoeff.c
==============================================================================
--- sandbox/trunk/decimal/decimal_in_c/deccoeff.c (original)
+++ sandbox/trunk/decimal/decimal_in_c/deccoeff.c Thu Oct 22 18:03:51 2009
@@ -75,22 +75,9 @@
* Primitive operations on limbs *
*********************************/
-/* The idea behind this section is that it should be easy to change the
- underlying representation of a limb without greatly affecting the rest of
- the code. For example, one might want to use alternative encodings like
- binary-coded decimal or densely packed decimal. In that case, only this
- section should have to be changed, provided that all following code uses
- only these primitive operations to operate on limbs.
-
- Ideally, later code should not assume that limbs can be operated on using
- the usual arithmetic operators, or that they are comparable with < or ==
- (some encodings may not be monotonic, or may have redundant encodings of
- the same integer, or may not even be encoded as a C integer type).
-*/
-
#define LIMB_ZERO ((limb_t)0)
#define LIMB_ONE ((limb_t)1)
-#define LIMB_BASE (LIMB_MAX+LIMB_ONE)
+#define LIMB_BASE ((limb_t)(LIMB_MAX+LIMB_ONE))
/* limb_error is called for internal errors */
@@ -173,14 +160,6 @@
return (limb_t)(hilo/c);
}
-/* return true if the limb is nonzero, else false */
-
-static bool
-limb_bool(limb_t a)
-{
- return a != 0;
-}
-
/* The following two functions are the primitive operations needed for base
conversion. Any value n in the range [0, LIMB_BASE * PyLong_BASE) can be
written either in the form
@@ -242,52 +221,6 @@
are provided for convenience. There is no need to change these if/when the
representation of a limb_t changes. */
-/* comparison: -1 if a < b, 0 if a == b, 1 if a > b */
-
-static int
-limb_cmp(limb_t a, limb_t b)
-{
- bool carry;
- limb_t diff;
- carry = limb_sbb(&diff, a, b, false);
- if (limb_bool(diff))
- return carry ? -1 : 1;
- else
- return 0;
-}
-
-/* true if a == b, else false */
-
-static bool
-limb_eq(limb_t a, limb_t b)
-{
- /* a == b iff a - b is zero */
- limb_t diff;
- limb_sbb(&diff, a, b, false);
- return !limb_bool(diff);
-}
-
-/* true if a < b, else false */
-
-static bool
-limb_lt(limb_t a, limb_t b)
-{
- /* a < b iff a - b overflows */
- limb_t dummy;
- return limb_sbb(&dummy, a, b, false);
-}
-
-/* true if a <= b, else false */
-/* Not currently used anywhere */
-
-static bool
-limb_le(limb_t a, limb_t b)
-{
- /* a <= b iff a - b - 1 overflows */
- limb_t dummy;
- return limb_sbb(&dummy, a, b, true);
-}
-
/* extract bottom n digits of a limb */
static limb_t
@@ -296,7 +229,7 @@
if (!(0 <= n && n <= LIMB_DIGITS))
limb_error("limb_mask: invalid count");
if (n < LIMB_DIGITS)
- limb_div(&a, LIMB_ZERO, a, powers_of_ten[n]);
+ (void)limb_div(&a, LIMB_ZERO, a, powers_of_ten[n]);
return a;
}
@@ -598,7 +531,7 @@
bool carry, neg;
/* reduce to case where top limbs differ */
- while (n > 0 && limb_eq(a[n-1], b[n-1])) {
+ while (n > 0 && a[n-1] == b[n-1]) {
res[n-1] = LIMB_ZERO;
n--;
}
@@ -606,7 +539,7 @@
return false;
/* now reduce to case a > b */
- neg = limb_lt(a[n-1], b[n-1]);
+ neg = a[n-1] < b[n-1];
if (neg) {
const limb_t *temp;
temp = a; a = b; b = temp;
@@ -688,7 +621,7 @@
/* top limb of b should be nonzero; a should contain at least as many
limbs as b */
- assert(a_size >= b_size && b_size > 0 && limb_bool(b[b_size-1]));
+ assert(a_size >= b_size && b_size > 0 && b[b_size-1] != LIMB_ZERO);
/* compute scale factor for normalization: floor(LIMB_BASE /
(b_top+1)) */
@@ -701,13 +634,13 @@
/* scale a and b */
top = limbs_mul1(w, b, b_size, scale);
bb = w;
- assert(!limb_bool(top));
+ assert(top == LIMB_ZERO);
top = limbs_mul1(w+b_size, a, a_size, scale);
aa = w+b_size;
/* catch most cases where quotient only needs a_size-b_size limbs */
- if (!limb_bool(top) && limb_lt(aa[a_size-1], bb[b_size-1]))
+ if (top == LIMB_ZERO && aa[a_size-1] < bb[b_size-1])
quot[a_size-b_size] = LIMB_ZERO;
else {
aa[a_size] = top;
@@ -721,7 +654,7 @@
a_top = aa[b_size];
assert(limb_le(a_top, b_top));
/* quotient q = aa / bb; may be overestimate */
- if (limb_lt(a_top, b_top))
+ if (a_top < b_top)
q = limb_div(&dummy, a_top, aa[b_size-1], b_top);
else
q = LIMB_MAX;
@@ -732,7 +665,7 @@
assert(!carry);
assert(limb_le(a_top, top));
/* correct if necessary */
- while (limb_lt(a_top, top)) {
+ while (a_top < top) {
carry = limbs_add(aa, aa, bb, b_size);
carry = limb_adc(&a_top, a_top, LIMB_ZERO, carry);
assert(!carry);
@@ -742,7 +675,7 @@
quot[j] = q;
}
top = limbs_div1(rem, aa, b_size, LIMB_ZERO, scale);
- assert(!limb_bool(top));
+ assert(top == LIMB_ZERO);
}
/* shift a_size-limb number left n digits (shifting zeros in); i.e., multiply
@@ -759,7 +692,7 @@
for (i = 0; i < n_limbs; i++)
res[i] = LIMB_ZERO;
high = limbs_mul1(res+n_limbs, a, a_size, powers_of_ten[n_digits]);
- assert(limb_lt(high, powers_of_ten[n_digits]));
+ assert(high < powers_of_ten[n_digits]);
if (n_digits != 0)
res[n_limbs + a_size] = high;
}
@@ -919,7 +852,7 @@
high = a[i];
for (j = 0; j < b_size; j++)
b[j] = digit_limb_swap(&high, b[j], high);
- while (limb_bool(high))
+ while (high != LIMB_ZERO)
b[b_size++] = digit_limb_swap(&high, 0, high);
}
return b_size;
@@ -1235,7 +1168,7 @@
{
Py_ssize_t v_size;
v_size = Py_SIZE(v);
- while (v_size > 0 && !limb_bool(v->ob_limbs[v_size-1]))
+ while (v_size > 0 && v->ob_limbs[v_size-1] == LIMB_ZERO)
--v_size;
Py_SIZE(v) = v_size;
return v;
@@ -1751,10 +1684,10 @@
goto fail2;
while (true) {
/* invariant quantity: apow**b*acc == a**bb. */
- lowbit = limbs_div1(b_limbs, b_limbs, b_size, LIMB_ZERO, 2);
- if (!limb_bool(b_limbs[b_size-1]))
+ lowbit = limbs_div1(b_limbs, b_limbs, b_size, LIMB_ZERO, (limb_t)2);
+ if (b_limbs[b_size-1] == LIMB_ZERO)
b_size--;
- if (limb_bool(lowbit)) {
+ if (lowbit != LIMB_ZERO) {
/* acc *= apow */
if (c == NULL)
temp = _deccoeff_multiply(apow, acc);
@@ -2054,17 +1987,14 @@
static int
_deccoeff_compare(deccoeff *a, deccoeff *b)
{
- int c;
Py_ssize_t a_size, b_size, i;
a_size = Py_SIZE(a);
b_size = Py_SIZE(b);
if (a_size != b_size)
return a_size < b_size ? -1 : 1;
- for (i = a_size-1; i >= 0; i--) {
- c = limb_cmp(a->ob_limbs[i], b->ob_limbs[i]);
- if (c != 0)
- return c;
- }
+ for (i = a_size-1; i >= 0; i--)
+ if (a->ob_limbs[i] != b->ob_limbs[i])
+ return a->ob_limbs[i] < b->ob_limbs[i] ? -1 : 1;
return 0;
}
More information about the Python-checkins
mailing list