[LLVMdev] Need help in converting int to double

sarath chandra sarathcse19 at gmail.com
Thu Sep 22 03:35:06 PDT 2011


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

On Thu, Sep 22, 2011 at 4:02 PM, James Molloy <James.Molloy at arm.com> wrote:

>  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****
>
> ** **
>
> *From:* sarath chandra [mailto:sarathcse19 at gmail.com]
> *Sent:* 22 September 2011 11:18
> *To:* James Molloy; llvmdev at cs.uiuc.edu
>
> *Subject:* Re: [LLVMdev] Need help in converting int to double****
>
>  ** **
>
> ** **
>
> On Thu, Sep 22, 2011 at 3:46 PM, sarath chandra <sarathcse19 at gmail.com>
> wrote:****
>
> Hi James,
>
> First i converted the void * to int* and then did FPToSI...then did SHL...(
> because CreateShl only accepts integers... i pointer casted it to int64 type
> first)... Below is the code snippet....
>
>
> lhs = mBuilder.CreateStructGEP(firstArg, 0);
>     lhs = mBuilder.CreateLoad(lhs);
>     lhs = mBuilder.CreatePointerCast(lhs, PointerType::get(
>             mBuilder.getInt64Ty(), 0));
>     int typelhs = getValueType(lhs);
>     rhs = mBuilder.CreateStructGEP(secondArg, 0);
>     rhs = mBuilder.CreateLoad(rhs);
>     rhs = mBuilder.CreatePointerCast(rhs, PointerType::get(
>             mBuilder.getInt64Ty(), 0));
>
>     lhs = mBuilder.CreateLoad(lhs);
>     rhs = mBuilder.CreateLoad(rhs);
>
>     lhs = convertDoubleToInt64(lhs); // used FPToSI
>     rhs = convertDoubleToInt64(rhs);//    "         "
>
>     //lhs = mBuilder.CreateLoad(lhs);
>     //rhs = mBuilder.CreateLoad(rhs);
>
>     lhs = mBuilder.CreateShl(lhs, rhs);
>     //lhs = mBuilder.CreatePointerCast(lhs,
> PointerType::get(mBuilder.getDoubleTy(),0));
>     lhs = convertIntToDouble(lhs);
>     typelhs = getValueType(lhs);
>
>
>
>     llvm::Value* returnValue = mBuilder.CreateAlloca(PointerType::get(
>             mModule->getTypeByName("Value"), 0));
>
>     llvm::Value* valueStructSize = getValueStructSize();
>     llvm::Value* memory = insertCallToMalloc(valueStructSize);
>
>     memory = mBuilder.CreatePointerCast(memory, PointerType::get(
>             mModule->getTypeByName("Value"), 0));
>
>     mBuilder.CreateStore(memory, returnValue);
>
>     allocateAndAssignDoubleTypeVar(mBuilder.CreateLoad(returnValue), lhs);
>
>     //return the value
>     mBuilder.CreateRet(mBuilder.CreateLoad(returnValue));
>
>
>     /*
>      * create call to SHL function
>      */
>     mCurrentFunction = previousFunction;
>     mBuilder.SetInsertPoint(previousBlock);
>
>     return mBuilder.CreateCall2(shlFunction, aLHS, aRHS);****
>
>
>
> ****
>
> On Thu, Sep 22, 2011 at 3:40 PM, James Molloy <James.Molloy at arm.com>
> wrote:****
>
> 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.****
>
>
>
> ****
>
> --
> Sairam,
>
>   (¨`·.·´¨)
>    `·.¸(¨`·.·´¨)
>   (¨`·.·´¨)¸.·´ Sarath!!!
>     `·.¸.·´****
>
>
>
>
> --
> 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.
>



-- 
Sairam,

  (¨`·.·´¨)
   `·.¸(¨`·.·´¨)
  (¨`·.·´¨)¸.·´ Sarath!!!
    `·.¸.·´
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20110922/6edc8062/attachment.html>


More information about the llvm-dev mailing list