内联汇编的兼容性 - \ n \ t与;

时间:2015-07-08 09:18:34

标签: c compatibility inline-assembly

编写内联汇编的正确方法是什么(除了避免它)?最好使用;\n\t

(Microsoft编译器__asm除外)

1 个答案:

答案 0 :(得分:3)

多线装配与内联没有“正确”的方法。只要是最可读的东西。

例如:GMP对同一bswap函数使用不同的asm代码样式

/* bswap is available on i486 and up and is fast.  A combination rorw $8 /
   roll $16 / rorw $8 is used in glibc for plain i386 (and in the linux
   kernel with xchgb instead of rorw), but this is not done here, because
   i386 means generic x86 and mixing word and dword operations will cause
   partial register stalls on P6 chips.  */
#if defined (__GNUC__) && ! defined (NO_ASM)            \
  && HAVE_HOST_CPU_FAMILY_x86 && ! HAVE_HOST_CPU_i386   \
  && GMP_LIMB_BITS == 32
#define BSWAP_LIMB(dst, src)                        \
  do {                                  \
    __asm__ ("bswap %0" : "=r" (dst) : "0" (src));          \
  } while (0)
#endif

#if defined (__GNUC__) && ! defined (NO_ASM)            \
  && defined (__amd64__) && GMP_LIMB_BITS == 64
#define BSWAP_LIMB(dst, src)                        \
  do {                                  \
    __asm__ ("bswap %q0" : "=r" (dst) : "0" (src));         \
  } while (0)
#endif

#if defined (__GNUC__) && ! defined (__INTEL_COMPILER)          \
    && ! defined (NO_ASM) && defined (__ia64) && GMP_LIMB_BITS == 64
#define BSWAP_LIMB(dst, src)                        \
  do {                                  \
    __asm__ ("mux1 %0 = %1, @rev" : "=r" (dst) :  "r" (src));       \
  } while (0)
#endif

/* As per glibc. */
#if defined (__GNUC__) && ! defined (NO_ASM)                    \
  && HAVE_HOST_CPU_FAMILY_m68k && GMP_LIMB_BITS == 32
#define BSWAP_LIMB(dst, src)                        \
  do {                                  \
    mp_limb_t  __bswapl_src = (src);                    \
    __asm__ ("ror%.w %#8, %0\n\t"                   \
         "swap   %0\n\t"                        \
         "ror%.w %#8, %0"                       \
         : "=d" (dst)                       \
         : "0" (__bswapl_src));                 \
  } while (0)
#endif