Aug. 27th, 2011

ewx: (geek)
#if __GNUC__ && __i386__
/* This is not entirely satisfactory: the xchgl would be unnecessary, if only
 * we had some way of communicating the detailed input and output assignments
 * of the registers to the compiler. */
#define BSWAP64(N)                                      \
({uint64_t __n = (N); __asm__("xchgl %%eax,%%edx\n"     \
                              "\tbswap %%eax\n"         \
                              "\tbswap %%edx"           \
                              : "+A"(__n));             \
  __n;})
#endif

The trouble is, the result ends up looking something like this:

        movl    0x04(%eax),%edx
        movl    (%eax),%eax
        xchgl   %edx,%eax
        bswap   %eax
        bswap   %edx
        movl    0x0c(%ebp),%esi
        movl    %eax,(%esi)
        movl    %edx,0x04(%esi)

…when obviously it would be better to:

        movl    0x04(%eax),%edx
        movl    (%eax),%eax
        bswap   %eax
        bswap   %edx
        movl    0x0c(%ebp),%esi
        movl    %edx,(%esi)
        movl    %eax,0x04(%esi)

However as far as I can see A is the only available constraint letter for 64-bit values on x86.

January 2026

S M T W T F S
    123
45678910
111213141516 17
18192021222324
25262728293031

Most Popular Tags

Expand Cut Tags

No cut tags