<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<p>You may be right, but from observation, it crops up rather
frequently with the proposed "<span
class="merge-request-title-text js-onboarding-mr-item"><a
href="https://gitlab.com/freepascal.org/fpc/source/-/merge_requests/45">Fast
"x mod constant = 0" optimisation</a>" merge request and
occasionally with some divisions by constants. The argument
there though would be to simply encode the optimisation directly
into the code generation.<br>
</span></p>
<p><span class="merge-request-title-text js-onboarding-mr-item">Outside
of RISC processors like aarch64, it is generally an x86_64-only
thing though because a lot of instructions like ADD and CMP
don't support 64-bit immediates.</span></p>
<p><span class="merge-request-title-text js-onboarding-mr-item">Gareth
aka. Kit<br>
</span></p>
<div class="moz-cite-prefix">On 17/10/2021 14:52, Florian Klämpfl
via fpc-devel wrote:<br>
</div>
<blockquote type="cite"
cite="mid:E85F872B-A058-40F1-AFF4-07AA72226A82@freepascal.org">
<pre class="moz-quote-pre" wrap="">
</pre>
<blockquote type="cite">
<pre class="moz-quote-pre" wrap="">Am 17.10.2021 um 13:25 schrieb J. Gareth Moreton via fpc-devel <a class="moz-txt-link-rfc2396E" href="mailto:fpc-devel@lists.freepascal.org"><fpc-devel@lists.freepascal.org></a>:
Hi everyone,
While reading up on some algorithms, I came across a recommendation of using a shorter arithmetic function to change the value of a constant in a register rather than loading the new value directly. However, the algorithm assumes a RISC-like processor, so I'm not sure if it applies to an Intel x86-64 processor. Consider the following:
movq $0xaaaaaaaaaaaaaaab,%rax
imulq %rax,%rcx
movq $0x5555555555555555,%rax
cmpq %rax,%rcx
setle %al
This algorithm sets %al to 1 if %rcx is divisible by 3, and 0 if it's not, and was compiled from the following Pascal code (under -O3, but -O1 produces almost exactly the same):
function IsDivisible3(Numerator: QWord): Boolean;
begin
Result := (Numerator * $AAAAAAAAAAAAAAAB) <= $5555555555555555;
end;
(One of my merge requests produces this code from "Result := (x mod 3) = 0")
My question is this: can "movq $0x5555555555555555,%rax" be replaced with "shrq $0x1,%rax" without incurring an additional pipeline stall? The MOV instruction takes 10 bytes to store, while "SHR 1" takes only 3. Given that %rax is used beforehand and the CMP instruction has to wait until the IMUL instruction has finished executing, logic tells me that I can get away with it here, but I'm not sure if the metric to go by is the execution speed of IMUL (i.e. the IMUL instruction is the limiting factor before CMP can be executed), or the simple fact that the previous value of %rax was used and will be loaded with $AAAAAAAAAAAAAAAB by the time it comes to load it with a new value.
</pre>
</blockquote>
<pre class="moz-quote-pre" wrap="">
I’d expect that the shl is executed in parallel on with the imul on most modern out of order architectures. So no real issue. OTOH, this is a very rare case so it is questionable if it is useful to check for this situation.
_______________________________________________
fpc-devel maillist - <a class="moz-txt-link-abbreviated" href="mailto:fpc-devel@lists.freepascal.org">fpc-devel@lists.freepascal.org</a>
<a class="moz-txt-link-freetext" href="https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel">https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel</a>
</pre>
</blockquote>
<div id="DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2"><br />
<table style="border-top: 1px solid #D3D4DE;">
<tr>
<td style="width: 55px; padding-top: 13px;"><a href="https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=emailclient" target="_blank"><img src="https://ipmcdn.avast.com/images/icons/icon-envelope-tick-round-orange-animated-no-repeat-v1.gif" alt="" width="46" height="29" style="width: 46px; height: 29px;" /></a></td>
<td style="width: 470px; padding-top: 12px; color: #41424e; font-size: 13px; font-family: Arial, Helvetica, sans-serif; line-height: 18px;">Virus-free. <a href="https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=emailclient" target="_blank" style="color: #4453ea;">www.avast.com</a>
</td>
</tr>
</table><a href="#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2" width="1" height="1"> </a></div></body>
</html>