[Python-Dev] Memory size overflows

Tim Peters tim.one@comcast.net
Wed, 16 Oct 2002 12:31:44 -0400


[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 = (size_t)_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>.