Lazarus
Home
Help
TinyPortal
Search
Login
Register
Lazarus
»
Forum
»
Free Pascal
»
FPC development
(Moderators:
FPK
,
Tomas Hajny
) »
x64 SwapEndian(<16bit>) via ROL is faster?
Free Pascal
Website
Downloads
Wiki
Documentation
Bugtracker
Mailing List
Lazarus
Website
Downloads (Laz+FPC)
Packages (OPM)
FAQ
Wiki
Documentation (RTL/FCL/LCL)
Bugtracker
CCR Bugs
GIT
Mailing List
Other languages
Foundation
Website
Useful Wiki Links
Project Roadmap
Getting the Source
Screenshots
How to use the forum
Forum Rules
About donations (wiki)
Bookstore
Computer Math and Games in Pascal
(preview)
Lazarus Handbook
Search
Advanced search
Recent
Synchronizing Lazarus pro...
by
Martin_fr
[
Today
at 12:26:09 pm]
Need help with project ma...
by
dseligo
[
Today
at 12:12:04 pm]
Debian removes FPC/Lazaru...
by
marcov
[
Today
at 11:58:36 am]
Qt6/Wayland clipboard: pa...
by
zeljko
[
Today
at 11:53:24 am]
88 year D. Knuth changes ...
by
Thaddy
[
Today
at 11:26:44 am]
Questions about TFuncSeri...
by
hedgehog
[
Today
at 09:50:46 am]
CADSys4 3D.
by
creaothceann
[
Today
at 09:34:10 am]
macOS 14+ Install Instruc...
by
CM630
[
Today
at 07:56:45 am]
class not found
by
cdbc
[
Today
at 07:40:10 am]
ShortStrings vs long stri...
by
Khrys
[
Today
at 07:39:21 am]
Any way to "embed" Window...
by
egsuh
[
Today
at 05:13:50 am]
Update a table with an Au...
by
CraigC
[March 10, 2026, 10:54:23 pm]
TSplitter color property ...
by
backprop
[March 10, 2026, 08:43:28 pm]
Z80 ZX Spectrum and Syste...
by
Cascade
[March 10, 2026, 08:22:14 pm]
could Ardour's YTK be use...
by
robert rozee
[March 10, 2026, 03:18:36 pm]
[revisited] triple click
by
Thaddy
[March 10, 2026, 01:08:40 pm]
Hello! Anything new?
by
JuhaManninen
[March 10, 2026, 11:22:16 am]
C/C++ code to Object Pasc...
by
domasz
[March 10, 2026, 10:44:20 am]
[Solved] Saving a workboo...
by
Kaljakoira
[March 10, 2026, 09:09:59 am]
Pipewire API
by
Thausand
[March 10, 2026, 07:38:06 am]
Good GPU accelerated math...
by
myisjwj
[March 10, 2026, 03:17:06 am]
REST Server/Client, how t...
by
valdir.marcos
[March 09, 2026, 10:18:36 pm]
TSpeedButton qt6 styleshe...
by
dsiders
[March 09, 2026, 09:40:21 pm]
Purpose of TProcessUTF8
by
Thaddy
[March 09, 2026, 09:08:03 pm]
How to remove consoleApp ...
by
Thaddy
[March 09, 2026, 08:51:40 pm]
« previous
next »
Print
Pages: [
1
]
Author
Topic: x64 SwapEndian(<16bit>) via ROL is faster? (Read 1648 times)
AlexTP
Hero Member
Posts: 2678
x64 SwapEndian(<16bit>) via ROL is faster?
«
on:
September 08, 2023, 03:58:21 pm »
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]
[+]
[-]
function
SwapEndian16
(
Value
:
smallint
)
:
smallint
;
register
;
asm
rol ax
,
8
end
;
and we now have this in rtl/x86_64/x86_64.inc
Code: Pascal
[Select]
[+]
[-]
{ 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?
«
Last Edit: September 08, 2023, 04:05:48 pm by AlexTP
»
Logged
CudaText editor
-
ATSynEdit
-
More from me
birin
New Member
Posts: 12
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.
Logged
Print
Pages: [
1
]
« previous
next »
Lazarus
»
Forum
»
Free Pascal
»
FPC development
(Moderators:
FPK
,
Tomas Hajny
) »
x64 SwapEndian(<16bit>) via ROL is faster?
TinyPortal
© 2005-2018