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
WIKI Timeout issues
Please read here if you have trouble connecting to the wiki
Recent
Questions about GUID decl...
by
440bx
[
Today
at 11:44:59 pm]
LINUX: My program stops w...
by
n7800
[
Today
at 11:23:20 pm]
ChatGPT and ObjectPascal ...
by
PascalDragon
[
Today
at 11:22:21 pm]
controls.lpr(731,15) Erro...
by
Guser979
[
Today
at 11:22:13 pm]
[Solved] Reference to bsS...
by
Wilko500
[
Today
at 11:15:03 pm]
TBufDataset with Invalid ...
by
talisjonatas
[
Today
at 11:06:24 pm]
Synapse TCP/IP client and...
by
rca
[
Today
at 10:46:14 pm]
activex.pp definition pro...
by
gues1
[
Today
at 10:27:36 pm]
Hint windows misplaced an...
by
n7800
[
Today
at 09:59:53 pm]
Input Chinese using Micro...
by
n7800
[
Today
at 09:49:38 pm]
internal Compiler Error
by
marcov
[
Today
at 09:25:27 pm]
Zeromemory vs. Fillchar
by
TBMan
[
Today
at 09:22:44 pm]
Persistent MacOS Arm M ch...
by
Grahame Grieve
[
Today
at 07:56:35 pm]
Flow fields and BFS
by
TBMan
[
Today
at 06:58:22 pm]
Create a png image from a...
by
thijsvandien
[
Today
at 06:53:41 pm]
Platform eXtended Library...
by
pascalbythree
[
Today
at 05:10:57 pm]
Tangled Line Pattern Make...
by
Boleeman
[
Today
at 04:27:51 pm]
DBF edit & append problem
by
Petrus Vorster
[
Today
at 04:08:21 pm]
[Solved] Can't install mo...
by
lainz
[
Today
at 03:28:41 pm]
[Solved]Open Project fail...
by
cdbc
[
Today
at 03:06:30 pm]
Popup Form to close when ...
by
Thaddy
[
Today
at 02:10:55 pm]
Spikey Splat Creatures fr...
by
Boleeman
[
Today
at 01:19:35 pm]
Amigo programming languag...
by
paxscript
[
Today
at 12:20:33 pm]
Ballet of Lines and Dots ...
by
Boleeman
[
Today
at 09:19:59 am]
Using icons in own open s...
by
paule32
[
Today
at 08:14:05 am]
« previous
next »
Print
Pages: [
1
]
Author
Topic: x64 SwapEndian(<16bit>) via ROL is faster? (Read 1562 times)
AlexTP
Hero Member
Posts: 2577
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