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!!!
`·.¸.·´

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

Hi Sarath,

It would have really helped if you had removed the commented out code and inlined the calls to your homemade helper functions before sending it…

You are doing this, in LLVM IR:

%0 = getelementptr %Value* %firstArg, i32 0 ; i8**

%1 = load i8** %0 ; i8*

%2 = bitcast i8* %1 to i64*

%3 = getelementptr %Value* %secondArg, i32 0 ; i8**

%4 = load i8** %3; i8*

%5 = bitcast i8* %4 to i64*

%6 = load i64* %2

%7 = load i64* %5

Then you call convertDoubleToInt64(). What does this actually do?

It’s in trouble by this point because it needs to do a fptosi on a double operand, but you’ve got an i64 operand and you can’t reinterpret-cast ints to floats in LLVM IR (AFAIK)

I can’t see what your code is doing afterwards as it uses calls to helper functions that you haven’t included.

The main point I’d raise is: if your value is stored as a double inside the Value structure, then why are you bitcasting to i64*? Why not bitcast to double* then load and perform a proper fptosi?

Cheers,

James

Yeah, that’s the fault…got the answer… Thanks James for the help… Struggling with this for so many days…

No problem.

Sometimes I find writing down the IR that I want generated first helps me track down obvious errors like this, then converting that later into C++ API calls.

Your mileage may vary!

James

You can bitcast ints to floats and vice-versa if they're the same size — for example, i32 to float and i64 to double.

John.

Good to know, thanks.

The docs are slightly hazy on that - they do mention type-to-type if same size, but the examples are only between pointers and integers, not floats. Would it be worthwhile my updating the documentation to explicitly state that or is it just me?

Cheers,

James

Hi James,

The docs are slightly hazy on that - they do mention type-to-type if same size, but the examples are only between pointers and integers, not floats.

you can't use bitcast to convert a pointer to an integer or vice-versa. You
must use ptrtoint/inttoptr.

Ciao, Duncan.

  Would it be worthwhile my updating the documentation to explicitly state that or is it just me?

Hi Duncan,

Sorry, I haven't had my early morning coffee yet. I meant ptr-to-ptr or ptr-to-vector/vice-versa.

James