[LLVMdev] FW: Need help in converting int to double

James Molloy James.Molloy at arm.com
Thu Sep 22 03:11:47 PDT 2011


Re-cc'ing list. Please direct all responses to the mailing list, so others can see it!

Cheers,

James

-----Original Message-----
From: James Molloy
Sent: 22 September 2011 11:11
To: 'sarath chandra'
Subject: RE: [LLVMdev] Need help in converting int to double

Hi Sarath,

Your example will break.

If the values are held internally as doubles casted to void* (which incidentally will only work on 64-bit systems), just casting void* -> int* will not get you a valid integer value. It will get you the double's internal representation as an integer. For example (actual values are made up):

    double a = 42.0;
    void *v = (void*)&a;
    int b = *(int*)v;

"b" will not contain 42. It will contain what looks like random garbage (although it is the IEE754 floating point representation of "42.0").

You need to perform an actual cast:

    double a  = 42.0;
    void *v = (void*)&a;
    double b = *(double*)v;
    int c = (int)b;

"c" will contain 42.

In LLVM speak, you need to LOAD as a double*, perform a fptosi to create an int64, perform the shift, then sitofp and store:

%1 = load bitcast i8* %arg0 to double*  ; Cast the void* to double*, then load it.
%2 = load bitcast i8* %arg1 to double*

%3 = fptosi double %1 to i64 ; Perform explicit double -> int conversion.
%4 = fptosi double %2 to i64

%5 = shr i64 %3, %4 ; Perform your operation.

%6 = sitofp i64 %5 to double ; Convert explicitly back to integer representation.

%7 = store bitcast i8* %dest to double*, double %6 ; Store back, reinterpreting your void* (which is i8* in LLVM) as a double*.


Hopefully this makes sense, especially if you know how you would have to do it in C.

Cheers,

James

From: sarath chandra [mailto:sarathcse19 at gmail.com]
Sent: 22 September 2011 10:49
To: James Molloy
Subject: Re: [LLVMdev] Need help in converting int to double

Hi James,

    CreateShl() accepts only integers or vectors as arguments. At the starting my arguments , let us suppose LHS,RHS, are double values. To pass them as arguments to CreateShl(), i used Pointer Casting to convert the Void* to Int*. Now the problem is after getting the result is, the result variable which captures the output is Int*.. i wanted to make it Double*..(so that i can accomadate it in value structure)
On Thu, Sep 22, 2011 at 3:12 PM, James Molloy <James.Molloy at arm.com> wrote:
Hi Sarath,

If you can only hold doubles (not integers), and you originally converted the doubles to integers to do an integer shift, why can you not just convert the result back to a double using CreateFPToSI ?

CreateFPToSI(CreateShr(CreateSIToFP(arg0), CreateSIToFP(arg1)))

Cheers,

James

From: llvmdev-bounces at cs.uiuc.edu [mailto:llvmdev-bounces at cs.uiuc.edu] On Behalf Of sarath chandra
Sent: 22 September 2011 10:37
To: llvmdev at cs.uiuc.edu
Subject: [LLVMdev] Need help in converting int to double

Hi,

     I'm pursuing M.Tech course. As a part of the project work i'm using LLVM as back-end. My project area is "Enhancing the performance of V8 javascript engine using LLVM as a back-end".

    Now i'm writing code for shift left(SHL) operator. I had my own Value Structure .. it's like this

Struct Value
{
void *val  ;
char type;
}

  The "char type" holds DoubleType,DoubleConst,StringType,StringConst...

  when i'm executing the IrBuilder.CreateShl(LHS,RHS) instruction it is returning an integer value as output.. i'm unable to store the value in my structure....(because my structure can hold Doubles,Strings).

  Is there any way to store the integer output in my structure( i used CreateSIToFP() to change int to double)........

Thanks in advance

Regards,

  (¨`·.·´¨)
   `·.¸(¨`·.·´¨)
  (¨`·.·´¨)¸.·´ Sarath!!!
    `·.¸.·´

-- IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.



--
Sairam,

  (¨`·.·´¨)
   `·.¸(¨`·.·´¨)
  (¨`·.·´¨)¸.·´ Sarath!!!
    `·.¸.·´

-- IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium.  Thank you.





More information about the llvm-dev mailing list