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
About donations (wiki)
Bookstore
Computer Math and Games in Pascal
(preview)
Lazarus Handbook
Search
Advanced search
Recent
UK's Online Safety Act
by
MarkMLl
[
Today
at 12:29:22 pm]
Return nil/null of data t...
by
Warfley
[
Today
at 12:10:15 pm]
debugging code for te ACS...
by
dseligo
[
Today
at 12:08:18 pm]
Painting on a control - 3...
by
majolika
[
Today
at 10:56:33 am]
مشكلة في dbgrid
by
Gebo
[
Today
at 10:41:39 am]
مشكلة في التقارير
by
Gebo
[
Today
at 10:37:06 am]
TRichMemo: Popup menu not...
by
CM630
[
Today
at 09:57:53 am]
Getting format error load...
by
MISV
[
Today
at 08:42:52 am]
Generic methods inside a ...
by
Khrys
[
Today
at 07:13:28 am]
Compile on multiple distr...
by
dodgebros
[
Today
at 05:44:44 am]
Application.ProcessMessag...
by
440bx
[
Today
at 04:45:21 am]
SDL3 Headers for FPC
by
Guva
[
Today
at 04:05:01 am]
Does unit recompilation r...
by
Fred vS
[
Today
at 02:34:42 am]
Resolving procedure symbo...
by
TCH
[
Today
at 01:04:13 am]
Run ffmpeg via TProcess
by
TRon
[January 22, 2025, 10:52:55 pm]
Lazarus Bugfix Release 3....
by
Martin_fr
[January 22, 2025, 10:41:06 pm]
Lazarus Release Candidate...
by
zeljko
[January 22, 2025, 09:56:24 pm]
Minor Q: HowTo use 'Debug...
by
d7_2_laz
[January 22, 2025, 09:40:14 pm]
How C++ class multiple in...
by
Thaddy
[January 22, 2025, 07:55:26 pm]
Mormot2 and websocket cli...
by
BlueIcaro
[January 22, 2025, 07:25:35 pm]
crash db navigator with L...
by
calm_sea
[January 22, 2025, 06:04:33 pm]
Три вопроса новичка: OnPa...
by
majolika
[January 22, 2025, 05:30:20 pm]
Preparing FPC 3.2.4, poin...
by
BSaidus
[January 22, 2025, 04:19:20 pm]
Permuted index for RTL an...
by
Thaddy
[January 22, 2025, 03:52:14 pm]
My first "tool" written w...
by
TBMan
[January 22, 2025, 03:11:42 pm]
« previous
next »
Print
Pages: [
1
]
Author
Topic: x64 SwapEndian(<16bit>) via ROL is faster? (Read 1450 times)
AlexTP
Hero Member
Posts: 2520
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: 11
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