【问题标题】:What's the instructions for type casting in Intel 64 ISA英特尔 64 ISA 中类型转换的说明是什么
【发布时间】:2011-01-18 05:33:29
【问题描述】:

在 Intel 64 ISA 中类型转换的说明是什么?

比如将long int转换成double?

我做了一些这样的测试:

$ cat type_cast.c 
#include <stdio.h>
#include <stdlib.h>

int main()
{
    long int a = 8l;
    double b;

    b = (double)a;

    printf("%f", b);

    return 0;
}

$ gcc -O0 -g -Wall  type_cast.c  -o type_cast
$ objdump -S type_cast

主要部分是:

int main()
{
  4004c4: 55                    push   %rbp
  4004c5: 48 89 e5              mov    %rsp,%rbp
  4004c8: 48 83 ec 10           sub    $0x10,%rsp
    long int a = 8l;
  4004cc: 48 c7 45 f8 08 00 00  movq   $0x8,-0x8(%rbp)
  4004d3: 00 
    double b;

    b = (double)a;
  4004d4: f2 48 0f 2a 45 f8     cvtsi2sdq -0x8(%rbp),%xmm0
  4004da: f2 0f 11 45 f0        movsd  %xmm0,-0x10(%rbp)

    printf("%f", b);
  4004df: b8 f8 05 40 00        mov    $0x4005f8,%eax
  4004e4: f2 0f 10 45 f0        movsd  -0x10(%rbp),%xmm0
  4004e9: 48 89 c7              mov    %rax,%rdi
  4004ec: b8 01 00 00 00        mov    $0x1,%eax
  4004f1: e8 c2 fe ff ff        callq  4003b8 <printf@plt>

    return 0;
  4004f6: b8 00 00 00 00        mov    $0x0,%eax
}

它使用 cvtsi2sdq -0x8(%rbp),%xmm0 和 movsd %xmm0,-0x10(%rbp) 从 long int 转换为 double。

我想知道 Intel 64 ISA 中通常使用的其他方法是什么。

【问题讨论】:

  • 您是否对生成的指令特别感兴趣?如果是这样,您将如何应用这些知识?
  • @wallyk 我对说明感兴趣。我只是从 C 中生成指令,看看哪些指令可用于类型转换。我想知道 Intel 64 ISA 中通用且高效的类型转换方法。

标签: c compiler-construction x86-64


【解决方案1】:

您可能找不到太多其他内容,因为 SIMD 已得到很好的实施。当然,您将拥有用于多个同时转换的打包变体 (cvtpi2pd),但这可能不是您所要求的。

您唯一真正的替代方案是 fildl/fstpl 对,它将把您的 long int 加载到 x87 浮点堆栈中,然后将其读回到您的堆栈中。

【讨论】:

    猜你喜欢
    • 2014-04-17
    • 2016-11-25
    • 1970-01-01
    • 2014-04-02
    • 1970-01-01
    • 2012-11-01
    • 2021-02-06
    相关资源
    最近更新 更多