### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: x64 SwapEndian(<16bit>) via ROL is faster?  (Read 763 times)

#### AlexTP

• Hero Member
• Posts: 2294
##### x64 SwapEndian(<16bit>) via ROL is faster?
« on: September 08, 2023, 03:58:21 pm »
https://stackoverflow.com/questions/3065335/how-to-convert-big-endian-numbers-to-native-numbers-delphi
Code: Pascal  [Select][+][-]
1. function SwapEndian16(Value: smallint): smallint; register;
2. asm
3.   rol   ax, 8
4. end;
5.

and we now have this in  rtl/x86_64/x86_64.inc
Code: Pascal  [Select][+][-]
1. { SwapEndian(<16 Bit>) being inlined is faster than using assembler }
2. function SwapEndian(const AValue: SmallInt): SmallInt;{\$ifdef SYSTEMINLINE}inline;{\$endif}
3.   begin
4.     { the extra Word type cast is necessary because the "AValue shr 8" }
5.     { is turned into "longint(AValue) shr 8", so if AValue < 0 then    }
6.     { the sign bits from the upper 16 bits are shifted in rather than  }
7.     { zeroes.                                                          }
8.     Result := SmallInt(((Word(AValue) shr 8) or (Word(AValue) shl 8)) and \$ffff);
9.   end;
10.
11.
12. function SwapEndian(const AValue: Word): Word;{\$ifdef SYSTEMINLINE}inline;{\$endif}
13.   begin
14.     Result := ((AValue shr 8) or (AValue shl 8)) and \$ffff;
15.   end;
16.

Which is faster?
« Last Edit: September 08, 2023, 04:05:48 pm by AlexTP »

#### birin

• New member
• Posts: 8
##### Re: x64 SwapEndian(<16bit>) via ROL is faster?
« Reply #1 on: September 08, 2023, 04:35:23 pm »
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.