Wednesday, January 28, 2015

 

CVTPS2DQ using inline assembly

 //-------------------------------------------
//Use inline assembly to demonstrate the cvtps2dq
//instruction. The instruction converts single 
//precision floating point values to dword integers.
//The program will convert the four floats into integers
//and put the values in op_arr.
//--------------------------------------------
#include <stdio.h>  
 #include <stdlib.h>  
 int main() {  
  float f[] = {3.22779, 5.25, 6.8, 44.65};  
  int op_arr[] = {0,0,0,0};  
  int index=0;  
  asm  volatile (  
            "cvtps2dq %1, %%xmm0;\n"   
            "movups %%xmm0, %0;\n"  
          :"=m"(op_arr[0])  
            :"m"(f[0])  
            :  
        );   
 for (index=0; index < 4 ; index++) {  
   printf("float value %f: int value %d\n", f[index] , op_arr[index]);  
 }  
  return 0;  
 }   
 
 To build: 
 $ gcc -g -Fstabs cvtps2dq.c
 
 $ ./a.out 
float value 3.227790: int value 3
float value 5.250000: int value 5
float value 6.800000: int value 7
float value 44.650002: int value 45

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]