x64 SwapEndian(<16bit>) via ROL is faster? lazarus / 64-FPC development ---------------------------------------------------------------------- AlexTP · 2023-09-08 13:58 This page suggests to do SwapEndian via ROL https://stackoverflow.com/questions/3065335/how-to-convert-big-endian- numbers-to-native-numbers-delphi Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function SwapEndian16(Value: smallint): smallint; register;asm  rol   ax, 8end;  and we now have this in  rtl/x86_64/x86_64.inc Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i { SwapEndian(<16 Bit>) being inlined is faster than using assembler }function SwapEndian(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}  begin    { the extra Word type cast is necessary because the "AValue shr 8" }    { is turned into "longint(AValue) shr 8", so if AValue < 0 then    }    { the sign bits from the upper 16 bits are shifted in rather than  }    { zeroes.                                                        }    Result := SmallInt(((Word(AValue) shr 8) or (Word(AValue) shl 8)) and $ffff);  end;  function SwapEndian(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}  begin    Result := ((AValue shr 8) or (AValue shl 8)) and $ffff;  end;  Which is faster? ---------------------------------------------------------------------- birin · 2023-09-08 14:35 If SwapEndian16 are not inlined, then she needs CALL and RET. Instruction flow interruption is time consuming. I think the second version of SwapEndian doesn't need the and $ffff because all values are Word, and Word are unsigned.