gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 07:07:36 26 4
gpt4 key购买 nike

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

(微软编译器 __asm 除外)

最佳答案

多行汇编与内联没有“正确”的方法。只要是最易读的。

示例: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

关于内联汇编的兼容性 -\n\t 与 ;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31288398/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com