Skip to content

Commit 6684a3a

Browse files
committed
Add SwapBytesInt32, rename SwapInt64 to SwapBytesInt64 + tests
1 parent f39b3e6 commit 6684a3a

2 files changed

Lines changed: 42 additions & 5 deletions

File tree

Source/dwsXPlatform.pas

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,8 @@ function DirectSetMXCSR(newValue : Word) : Word; register;
409409

410410
function SwapBytes(v : Cardinal) : Cardinal;
411411
procedure SwapBytesBlock(src, dest : PByte; nb : Integer);
412-
procedure SwapInt64(src, dest : PInt64);
412+
procedure SwapBytesInt32(src, dest : PUInt32);
413+
procedure SwapBytesInt64(src, dest : PInt64);
413414

414415
function RDTSC : UInt64;
415416

@@ -2552,6 +2553,12 @@ function SwapBytes(v : Cardinal) : Cardinal;
25522553
{$ifdef WIN32_ASM}
25532554
asm
25542555
bswap eax
2556+
end;
2557+
{$else}{$ifdef WIN64_ASM}
2558+
asm
2559+
bswap ecx
2560+
mov eax, ecx
2561+
end;
25552562
{$else}
25562563
type
25572564
TCardinalBytes = array [0..3] of Byte;
@@ -2560,8 +2567,8 @@ function SwapBytes(v : Cardinal) : Cardinal;
25602567
TCardinalBytes(Result)[1] := TCardinalBytes(v)[2];
25612568
TCardinalBytes(Result)[2] := TCardinalBytes(v)[1];
25622569
TCardinalBytes(Result)[3] := TCardinalBytes(v)[0];
2563-
{$endif}
25642570
end;
2571+
{$endif}{$endif}
25652572

25662573
// SwapBytesBlock
25672574
//
@@ -2576,9 +2583,33 @@ procedure SwapBytesBlock(src, dest : PByte; nb : Integer);
25762583
end;
25772584
end;
25782585

2579-
// SwapInt64
2586+
// SwapBytesInt32
2587+
//
2588+
procedure SwapBytesInt32(src, dest : PUInt32);
2589+
{$ifdef WIN64_ASM}
2590+
asm
2591+
mov eax, [rcx]
2592+
bswap eax
2593+
mov [rdx], eax
2594+
end;
2595+
{$else}{$ifdef WIN32_ASM}
2596+
asm
2597+
mov ecx, [eax]
2598+
bswap ecx
2599+
mov [edx], ecx
2600+
end;
2601+
{$else}
2602+
begin
2603+
PByteArray(dest)[0] := PByteArray(src)[3];
2604+
PByteArray(dest)[1] := PByteArray(src)[2];
2605+
PByteArray(dest)[2] := PByteArray(src)[1];
2606+
PByteArray(dest)[3] := PByteArray(src)[0];
2607+
end;
2608+
{$endif}{$endif}
2609+
2610+
// SwapBytesInt64
25802611
//
2581-
procedure SwapInt64(src, dest : PInt64);
2612+
procedure SwapBytesInt64(src, dest : PInt64);
25822613
{$ifdef WIN64_ASM}
25832614
asm
25842615
mov rax, [rcx]

Test/UdwsUtilsTests.pas

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1863,10 +1863,16 @@ procedure TdwsUtilsTests.WordsToBytesTest;
18631863
//
18641864
procedure TdwsUtilsTests.SwapPrimitives;
18651865
begin
1866+
var a32, b32 : UInt32;
1867+
a32 := $01020304;
1868+
b32 := 0;
1869+
SwapBytesInt32(@a32, @b32);
1870+
CheckEquals($04030201, b32, 'SwapInt32');
1871+
18661872
var a64, b64 : Int64;
18671873
a64 := $0102030405060708;
18681874
b64 := a64 shl 1;
1869-
SwapInt64(@a64, @b64);
1875+
SwapBytesInt64(@a64, @b64);
18701876
CheckEquals($0807060504030201, b64, 'SwapInt64');
18711877
end;
18721878

0 commit comments

Comments
 (0)