镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Unnecessary MSVC code #4

Description

@lemire

The current code uses special casing for MSVC, but this code is likely unneeded. It seems to seek to avoid memcpy calls. But Microsoft's compiler is smart.

Let us consider this example:

#include <cstdint>
#include <cstring>


#define BSWAP_4(x)                                                       \
  (((x) & 0xFF) << 24) | (((x) & 0xFF00) << 8) | (((x) >> 8) & 0xFF00) | \
      (((x) >> 24) & 0xFF)

bool SwapBytes32(char *data, size_t nbytes) {
   if (nbytes % sizeof(uint32_t) != 0) return false;
   uint32_t temp;
   for (size_t i = 0; i < nbytes; i += sizeof(temp)) {
     memcpy(&temp, &data[i], sizeof(temp));
     temp = BSWAP_4(temp);
     memcpy(&data[i], &temp, sizeof(temp));
   }

   return true;
 }

With /O2, MSVC compiles it down to

bool SwapBytes32(char *,unsigned __int64) PROC               ; SwapBytes32, COMDAT
        test    dl, 3
        je      SHORT $LN5@SwapBytes3
        xor     al, al
        ret     0
$LN5@SwapBytes3:
        test    rdx, rdx
        je      SHORT $LN3@SwapBytes3
        dec     rdx
        shr     rdx, 2
        inc     rdx
        npad    9
$LL4@SwapBytes3:
        mov     eax, DWORD PTR [rcx]
        lea     rcx, QWORD PTR [rcx+4]
        bswap   eax
        mov     DWORD PTR [rcx-4], eax
        sub     rdx, 1
        jne     SHORT $LL4@SwapBytes3
$LN3@SwapBytes3:
        mov     al, 1
        ret     0

I am sure one can do better, but it is just generally fine code.

Activity

  1. lemire commented on Aug 8, 2024

    @lemire
    MemberAuthor
  2. lemire commented on Aug 8, 2024

    @lemire
    MemberAuthor

    (@anonrig not having special case handing for MSVC is likely better long term.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions