cx86-64

What's the instructions for type casting in Intel 64 ISA


What's the instructions for type casting in Intel 64 ISA?

Such as cast long int to double?

I do some test like this:

$ 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

The part for main is:

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
}

It uses cvtsi2sdq -0x8(%rbp),%xmm0 and movsd %xmm0,-0x10(%rbp) to cast from long int to double.

I am wondering what's the other methods usually used in Intel 64 ISA.


Solution

  • You might not find much else as SIMD has become quite well-implemented. Naturally, you will have the packed variant (cvtpi2pd) for multiple simultaneous conversions but this is likely not what you are asking.

    Your only real alternative would be the fildl/fstpl pair which will load your long int in the x87 floating point stack and then read it back out onto your stack.