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
IRC channel
GIT
Mailing List
Other languages
Foundation
Website
Useful Wiki Links
Project Roadmap
Getting the Source
Screenshots
How to use the forum
About donations (wiki)
Bookstore
Computer Math and Games in Pascal
(preview)
Lazarus Handbook
Search
Advanced search
Recent
Fpcupdeluxe
by
Gustavo 'Gus' Carreno
[
Today
at 05:58:51 pm]
Lazarus 3.9.9 Variable Wa...
by
msintle
[
Today
at 05:20:23 pm]
[SOLVED] SVG thumbnails
by
Pe3s
[
Today
at 05:20:16 pm]
TWSButton descendance
by
zeljko
[
Today
at 05:18:24 pm]
Advance Record operators ...
by
ad1mt
[
Today
at 05:04:43 pm]
Autentication Oracle [wi...
by
CapitaoVirgulinoFerreira
[
Today
at 04:47:08 pm]
regex/and
by
BubikolRamios
[
Today
at 04:36:52 pm]
Unique Instance Terminate...
by
d7_2_laz
[
Today
at 04:26:01 pm]
[SOLVED] TLabeledEdit.Edi...
by
jipété
[
Today
at 02:08:02 pm]
CORDIC algorithms
by
BlueIcaro
[
Today
at 01:22:51 pm]
[SOLVED] Lazarus png file
by
circular
[
Today
at 12:53:57 pm]
Using tabs instead of spa...
by
Martin_fr
[
Today
at 12:45:40 pm]
Lazarus Release Candidate...
by
dbannon
[
Today
at 12:04:23 pm]
Automatize build system
by
Чебурашка
[
Today
at 12:00:41 pm]
Small Status Bar Demo
by
Handoko
[
Today
at 11:48:02 am]
Anyone interested in test...
by
TRon
[
Today
at 11:43:16 am]
How to "create" a TFont a...
by
Hartmut
[
Today
at 11:40:53 am]
BGRABitmap - showcase -
by
circular
[
Today
at 11:35:16 am]
lazarus controlling compi...
by
Thaddy
[
Today
at 08:21:16 am]
Chinese input in edit and...
by
myisjwj
[
Today
at 05:06:00 am]
Export Stringgrid to an x...
by
majid.ebru
[
Today
at 04:02:17 am]
[SOLVED] How to stay in t...
by
egsuh
[
Today
at 04:00:01 am]
Stimulus Control: a teach...
by
cpicanco
[
Today
at 02:07:11 am]
Conscious Artificial Inte...
by
cpicanco
[
Today
at 01:18:43 am]
How to convert Real to Bi...
by
ad1mt
[December 06, 2023, 11:24:07 pm]
« previous
next »
Print
Pages: [
1
]
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 »
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: 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.
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