<div dir="auto">The penalty for separate div & mod operations for your scenario is severe on targets that require software implementation (e.g. 8 bit AVR). There is a DivMod implementation in unit math, would be useful if this is available in system.<br><br><div class="gmail_quote" dir="auto"><div dir="ltr">On Sun, 17 Jun 2018, 00:22 J. Gareth Moreton, <<a href="mailto:gareth@moreton-family.com">gareth@moreton-family.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div><br></div><div>There are also situations in optimization where one might detect a repeat calculation that the programmer cannot eliminate themselves, but which the best optimization is to store the result and re-use it later... the most obvious situation where this arises is with div and mod with the same numerator and denominator. Currently, the compiler doesn't know any better and has to calculate the division twice, a relatively expensive operation, even though DIV returns both the quotient and the remainder in RAX and RDX respectively. I believe storing the mod result in a virtual register for later use will be far easier to manage when the registers have not yet been allocated, especially if it's determined that a new register has to be preserved in the function prologue, or there are no free registers at all and it has to be put on the stack, or if it's at all possible to use RDX itself as that temporary storage (the most ideal outcome in both speed and size), something that would be near impossible if RDX has been allocated for something in between.</div><div><br>
</div><div>Gareth aka. Kit<br>
</div> _______________________________________________<br>
fpc-devel maillist - <a href="mailto:fpc-devel@lists.freepascal.org" target="_blank" rel="noreferrer">fpc-devel@lists.freepascal.org</a><br>
<a href="http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel" rel="noreferrer noreferrer" target="_blank">http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel</a><br>
</blockquote></div></div>