Hey Toni,<br><br>If this optimization is valid for any float, we should definitely do it, and this is a missed optimization. If it's not valid for all floats, I'm not sure how we should handle it, if at all.<div><br></div><div>Alex</div><br><div class="gmail_quote">On Wed Nov 05 2014 at 10:16:36 AM Toni Mattis <<a href="mailto:toni.mattis@student.hpi.uni-potsdam.de">toni.mattis@student.hpi.uni-potsdam.de</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello,<br>
<br>
I discovered that PyPy's JIT generates "DIVSD" instructions on xmm<br>
registers when dividing a float by a constant C. This consumes an order<br>
of magnitude more CPU cycles than the corresponding "MULSD" instruction<br>
with a precomputed 1/C.<br>
<br>
I know that only powers of two have an exact reciprocal floating point<br>
representation, but there might be a benefit in trading the least<br>
significant digit for a more significant speedup.<br>
<br>
So, is this a missed optimization (at least for reasonably accurate<br>
cases), a present or possibly future option (like -ffast-math in gcc) or<br>
are there more reasons against it?<br>
<br>
<br>
Thanks,<br>
<br>
Toni<br>
<br>
<br>
--- PS: Small Example ---<br>
<br>
This function takes on average 0.41 seconds to compute on an<br>
array.array('d') with 10**8 elements between 0 and 1:<br>
<br>
    def spikes_div(data, threshold=1.99):<br>
        count = 0<br>
        for i in data:<br>
            if i / 0.5 > threshold:<br>
                count += 1<br>
        return count<br>
<br>
Rewritten with a multiplication it takes about 0.29 seconds on average,<br>
speeding it up by factor 1.4:<br>
<br>
        ...<br>
            if i * 2.0 > threshold:<br>
        ...<br>
<br>
<br>
The traces contain the same instructions (except for the MULSD/DIVSD)<br>
and run the same number of times. I'm working with a fresh translation<br>
of the current PyPy default on Ubuntu 14.04 x64 with a 2nd generation<br>
Core i7 CPU.<br>
<br>
<br>
______________________________<u></u>_________________<br>
pypy-dev mailing list<br>
<a href="mailto:pypy-dev@python.org" target="_blank">pypy-dev@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/pypy-dev" target="_blank">https://mail.python.org/<u></u>mailman/listinfo/pypy-dev</a><br>
</blockquote></div>