<HTML>
<style> BODY { font-family:Arial, Helvetica, sans-serif;font-size:12px; }</style>Indeed, so if it can be done only once, that's a major
saving. Even if div and mod are separate assembler instructions on the
target platform, it's possible to make a saving if multiplication and
subtraction aren't too expensive, as then the modulus can be calculated
via r = n - qd.<br>
<div><br>
</div><div>The issue with DivMod is that the saving isn't that great
because of the penalty of calling a subroutine, which amounts to around
50 cycles on x86-64, about equal to DIV. This could be alleviated if
the function is able to be inlined or, when dealing with constants, can
be identified as a 'pure' function (I wrote a little request article
about this: <a href="http://wiki.freepascal.org/Feature_Ideas#.22pure.3B.22_function_directive" target="_blank">http://wiki.freepascal.org/Feature_Ideas#.22pure.3B.22_function_directive</a>
)<br>
<br>
Gareth aka. Kit</div> <br>
<br>
<span style="font-weight: bold;">On Sun 17/06/18 10:08 , "Christo Crause" christo.crause@gmail.com sent:<br>
</span><blockquote style="BORDER-LEFT: #F5F5F5 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT:0px; PADDING-LEFT: 5px; PADDING-RIGHT: 0px"><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="javascript:top.opencompose('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="javascript:top.opencompose('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>
</blockquote></HTML>