Repository navigation
Conversation
|
In my experiments this helped with many use cases but regressed a few. I'll investigate this in more detail later this week hopefully. |
6afce5c to
bbb5f7a
Compare
|
@JukkaL I think I found it: with GCC, the old check I've pushed a change that tests the old limit first and the signed range only after that. It accepts the same operands, and with GCC the code for two non-negative operands is the same as on master again; negative operands take one more branch but are still multiplied inline. |
Fixes mypyc/mypyc#1254.
CPyTagged_IsMultiplyOverflow()compared the tagged operands with unsigned comparisons, so every negative short int looked too big.x * ywith a negative operand always boxed both operands and calledPyNumber_Multiply(), which is more than 10 times slower than the inline multiplication.Now each tagged operand has to fit in a signed integer of half the width of
CPyTagged(int32_ton 64-bit platforms,int16_ton 32-bit ones), checked with a round trip through that type. Non-negative operands have the same limit as before, and negative operands get the same magnitudes. For two such operands the tagged product2a * bhas|2ab| <= 2**(N-3), where N is the number of bits inCPyTagged, so it can't overflow and it's a valid short int.Negative operands never reached the inline multiplication before, so the new run test multiplies every combination of signs and magnitudes around the limits on 32-bit and 64-bit platforms (214 and 230 for the inline multiplication, 230 and 262 for short ints). It does that with two variable operands, and with constant operands, where the C compiler folds the check of the constant.
Performance
Compiled loops with 10**6 iterations:
total += i * k,k = -3total += i * -3total += i * k,k = 3total += i * 3