About 31, 32, 63 or 64 bits: I guess that you want to avoid integer overflow.
Intel has an "overflow" flag, changed for all instructions. For other CPUs,
you can use emulate this flag. Example with the type int that I used in my
GMP patch:
Add:
int a, b, sum;
sum = a + b;
exact = ((a < 0) ^ (b < 0)) || ((a < 0) == (sum < 0));
Substract:
int a, b, diff;
diff = a + b;
exact = ((a < 0) == (b < 0)) || ((a < 0) == (diff < 0));
Multiply:
int a, b, product;
if (a == 0 || b == 0) {
product = 0; /* exact */
} else if (a != INT_MIN || (b == 1)) {
product = a * b;
exact = (product / b) == a);
} else {
/* INT_MIN * -1 = -INT_MIN: overflow */
}
Divide:
int a, b, q;
if (a != INT_MIN || b != -1) {
q = a / b; /* exact */
} else {
/* INT_MIN / -1 = -INT_MIN: overflow */
}
Checking overflow may costs more than using a smaller base. Only a benchmark
can answer ;-)
--
Victor Stinner aka haypo
http://www.haypocalc.com/blog/
_______________________________________________
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe:
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com