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
FPC Unleashed (inline var...
by
Fibonacci
[
Today
at 07:15:58 pm]
fp-h2pas: New C header tr...
by
Gustavo 'Gus' Carreno
[
Today
at 06:32:22 pm]
Nezplug Library Player
by
Gigatron
[
Today
at 04:15:33 pm]
Eschecs (UCI chess GUI) 5...
by
Roland57
[
Today
at 04:10:12 pm]
[ANN] fpGUI Toolkit v2.0....
by
cdbc
[
Today
at 03:14:21 pm]
AI assisted translation o...
by
schuler
[
Today
at 02:25:26 pm]
I hope FreePascal can sup...
by
Zvoni
[
Today
at 01:58:08 pm]
Lazarus Bugfix Release 4....
by
dbannon
[
Today
at 01:52:18 pm]
Strange happenings with T...
by
jamie
[
Today
at 12:41:42 pm]
Little bit...
by
jamie
[
Today
at 12:17:43 pm]
Necromancer's Dos Navigat...
by
dbannon
[
Today
at 11:03:26 am]
Can't pass string to TEdi...
by
cdbc
[
Today
at 10:13:44 am]
How to merge multiple cla...
by
cdbc
[
Today
at 09:52:55 am]
Remote desktop software i...
by
Ericktux
[
Today
at 08:50:58 am]
Fixed Bzip2 unpacker from...
by
domasz
[
Today
at 08:09:54 am]
Ann: DeCoperators
by
Thaddy
[
Today
at 07:37:29 am]
Weird error
by
xiyi0616
[
Today
at 03:47:04 am]
Bad Sandwich
by
Guva
[April 12, 2026, 09:55:25 pm]
ZeosDB and sqlite3
by
dseligo
[April 12, 2026, 09:06:22 pm]
The ever re-appearing /= ...
by
Thaddy
[April 12, 2026, 08:07:09 pm]
Some Lazarus Graphics Rel...
by
Boleeman
[April 12, 2026, 03:45:30 pm]
Some Lazarus Utils N Stuf...
by
Boleeman
[April 12, 2026, 03:39:45 pm]
Ann: Deinline: a de-inlin...
by
Fred vS
[April 12, 2026, 03:17:40 pm]
BAScript - Simple scripti...
by
Ñuño_Martínez
[April 12, 2026, 01:00:21 pm]
NiceGrid component for La...
by
Alexandr R
[April 12, 2026, 11:02:54 am]
« previous
next »
Print
Pages: [
1
]
Author
Topic: x64 SwapEndian(<16bit>) via ROL is faster? (Read 1676 times)
AlexTP
Hero Member
Posts: 2696
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