Quickest way to change endianness

前端 未结 4 511
一整个雨季
一整个雨季 2021-02-06 17:33

What is the quickest way to reverse the endianness of a 16 bit and 32 bit integer. I usually do something like (this coding was done in Visual Studio in C++):

un         


        
4条回答
  •  無奈伤痛
    2021-02-06 18:22

    Who says it does too many calculations?

    out = changeEndianness16(in);
    
    gcc 4.6.0
    
    movzwl  -4(%rsp), %edx
    movl    %edx, %eax
    movsbl  %dh, %ecx
    movb    %cl, %al
    movb    %dl, %ah
    movw    %ax, -2(%rsp)
    
    clang++ 2.9
    
    movw    -2(%rsp), %ax
    rolw    $8, %ax
    movw    %ax, -4(%rsp)
    
    Intel C/C++ 11.1
    
    movzwl    4(%rsp), %ecx
    rolw      $8, %cx
    xorl      %eax, %eax
    movw      %cx, 6(%rsp)
    

    What does your compiler produce?

提交回复
热议问题