<HTML>
<div><style> BODY { font-family:Arial, Helvetica, sans-serif;font-size:12px; }</style>Hence why I wish to delegate this to someone more senior to decide if this is a bug or a feature. Seems that logic has more than one path. Mathematically, yes, shl 8 and shr 8 should cancel out completely and be a null operation.</div><div><br>
</div><div>Gareth<br>
</div> <br>
<br>
<span style="font-weight: bold;">On Tue 12/06/18 12:42 , Giuliano Colla giuliano.colla@fastwebnet.it sent:<br>
</span><blockquote style="BORDER-LEFT: #F5F5F5 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px; PADDING-LEFT: 5px; PADDING-RIGHT: 0px">Il 12/06/2018 02:07, J. Gareth Moreton ha scritto:
<br>
<span style="color: rgb(102, 102, 102);">> Someone pointed out that if you perform "(x shl 8) shr 8", where x is
</span><br>
<span style="color: rgb(102, 102, 102);">> of type Word, the result is always x, even though logic dictates that
</span><br>
<span style="color: rgb(102, 102, 102);">> the upper 8 bits should be shifted out and hence the result actually
</span><br>
<span style="color: rgb(102, 102, 102);">> be equal to "x and $FF".
</span><br>
<br>
IMHO logic dictates that (x shl 8) shr 8 should give x, because the shr
<br>
cancels the effect of shl, and that a further compiler optimization
<br>
might simply suppress the code at all. Register size when performing a
<br>
calculation is not dictated by logic, but by physical constraints, which
<br>
have nothing to do with logic.
<br>
<br>
If the programmer wishes to mask out some bits he should explicitly use
<br>
an *and* instruction, making the code both more readable and more
<br>
portable. Generating a less efficient code, with and instructions added
<br>
with the sole purpose of supporting the improper usage of a shl
<br>
instruction wouldn't be very smart.
<br>
<br>
I'm in favour of a compiler which generates the most efficient code, and
<br>
leaves to the programmer the responsibility to mask unwanted bits.
<br>
<br>
Just my 2 cents.
<br>
<br>
Giuliano
<br>
<br>
_______________________________________________
<br>
fpc-devel maillist - <a href="mailto:fpc-devel@lists.freepascal.org">fpc-devel@lists.freepascal.org</a>
<br>
<a target="_blank" href="<a href="http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel">http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel</a>"><span style="color: red;">http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel</span></a>
<br>
<br>
<br>
</blockquote></HTML>