bswap intrinsic

  • Thread starter Thread starter George M. Garner Jr.
  • Start date Start date
G

George M. Garner Jr.

Is there an intrinsic to wrap the bswap x86 instruction. I have written a
function to do this with inline assembler but I am wondering if an intrinsic
wouldn't be a better solution.

Regards,

George.
 
George said:
Is there an intrinsic to wrap the bswap x86 instruction. I have
written a function to do this with inline assembler but I am
wondering if an intrinsic wouldn't be a better solution.

it would be. AFIAK there is no such intrinsic.

-cd
 
_byteswap_ulong
_byteswap_uint64

Both in current Whidbey builds.

#include <intrin.h>

int main()

{

volatile unsigned long l = 1;

volatile unsigned long long ll = 1;

l = _byteswap_ulong(l);

ll = _byteswap_uint64(ll);

}

in a release build gives you

PUBLIC _main

; Function compile flags: /Ogtpy

; File d:\documents and settings\ronaldl\my documents\visual studio
2005\projects\byteswap\byteswap.cpp

; COMDAT _main

_TEXT SEGMENT

_l$ = -12 ; size = 4

_ll$ = -8 ; size = 8

_main PROC ; COMDAT

; 8 : {

sub esp, 12 ; 0000000cH

; 9 : volatile unsigned long l = 1;

mov eax, 1

mov DWORD PTR _l$[esp+12], eax

; 10 : volatile unsigned long long ll = 1;

mov DWORD PTR _ll$[esp+12], eax

mov DWORD PTR _ll$[esp+16], 0

; 11 :

; 12 : l = _byteswap_ulong(l);

mov eax, DWORD PTR _l$[esp+12]

bswap eax

mov DWORD PTR _l$[esp+12], eax

; 13 : ll = _byteswap_uint64(ll);

mov ecx, DWORD PTR _ll$[esp+12]

mov edx, DWORD PTR _ll$[esp+16]

bswap edx

bswap ecx

mov DWORD PTR _ll$[esp+12], edx

mov DWORD PTR _ll$[esp+16], ecx

; 14 : }

xor eax, eax

add esp, 12 ; 0000000cH

ret 0

_main ENDP

_TEXT ENDS

END

Or for X64 which has a quadword bswap instruction:

Function compile flags: /Ogtpy

; File d:\documents and settings\ronaldl\my documents\visual studio
2005\projects\byteswap\byteswap.cpp

; COMDAT main

_TEXT SEGMENT

l$ = 8

ll$ = 16

main PROC ; COMDAT

; 9 : volatile unsigned long l = 1;

mov DWORD PTR l$[rsp], 1

; 10 : volatile unsigned long long ll = 1;

mov QWORD PTR ll$[rsp], 1

; 11 :

; 12 : l = _byteswap_ulong(l);

mov eax, DWORD PTR l$[rsp]

bswap eax

mov DWORD PTR l$[rsp], eax

; 13 : ll = _byteswap_uint64(ll);

mov rax, QWORD PTR ll$[rsp]

bswap rax

mov QWORD PTR ll$[rsp], rax

; 14 : }

xor eax, eax

ret 0

main ENDP

_TEXT ENDS

END

Ronald Laeremans
Visual C++ team
 
Back
Top