[Python-Dev] Memory size overflows (original) (raw)
Tim Peters tim.one@comcast.net
Wed, 16 Oct 2002 12:31:44 -0400
- Previous message: [Python-Dev] Memory size overflows
- Next message: [Python-Dev] Memory size overflows
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
[Tim]
BTW, if we know we're not interested in anything other than 32-bit products,
double prod = (double)src1 * (double)src2; if (prod < 4294967296.0) /* 2**32 */ result = (sizet)prod; else overflow;
[Oren Tirosh]
s/prod;/src1 * src2;/
Re-calculating the product with integer multiplication is probably faster than converting a double to integer on any sane CPU.
This is so. Better, because it allows more instruction-level parallelism:
double dprod = (double)src1 * (double)src2;
size_t iprod = src1 * src2;
if (dprod < 4294967296.0) /* 2**32 */
result = iprod;
else
/* overflow */I wonder if the old SPARCs that don't have an integer multiply instruction actually have a floating point to integer conversion that is faster than integer mul.
I'll let someone with an old SPARC worry about that.
Wasting "expensive" multiplications this way still feels blasphemous :)
Well, we could simulate multiplication by hand, with a bit-at-a-time shift-and-add loop. This reduces the detect-mul-overflow case to the detect-add-overflow case. Bonus: on some odd boxes, it would be faster <wink/sigh>.
- Previous message: [Python-Dev] Memory size overflows
- Next message: [Python-Dev] Memory size overflows
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]