Jajoo, Malhar via llvm-dev
2017-May-26 10:09 UTC
[llvm-dev] Printing out a 128 bit decimal
Hi, I was wondering how I can print out the result of some arithmetic on 128 bit integers. After having a look at the API , it seems it is possible to get the integer value from the "ConstantInt::getSextValue() " but the return value is only a int64_t. I wish to print it on the console output and printing as a string or decimal ( using C standard library printf ) My Question : How can I get the returned value of a 128 bit ( or more number of bits ) arithmetic ? Thanks, Malhar -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20170526/b7cf6855/attachment.html>
Tim Northover via llvm-dev
2017-May-26 14:01 UTC
[llvm-dev] Printing out a 128 bit decimal
On 26 May 2017 at 03:09, Jajoo, Malhar via llvm-dev <llvm-dev at lists.llvm.org> wrote:> "ConstantInt::getSextValue() " but the return value is only a int64_t.getValue returns an APInt (LLVM's arbitrary-precision int type), which has its own print method. So something like: Imm->getValue()->print(errs());> I wish to print it on the console output and printing as a string or decimal > ( using C standard library printf )I don't think printf supports 128-bit types. They're non-standard in C and C++ Cheers. Tim.
Tim Northover via llvm-dev
2017-May-26 14:57 UTC
[llvm-dev] Printing out a 128 bit decimal
Adding the list back, in case this ever crops up for someone else. On 26 May 2017 at 07:39, Jajoo, Malhar <malhar.jajoo14 at imperial.ac.uk> wrote:> On seei ng a "Value" , try and dynamic_cast it to a ConstantInt. > If it does cast , then get the APInt for this ConstantInt. > From the APInt get a std::string > Convert std::string to StringRef > Convert StringRef to Value* > Place this Value* in a vector<Value*> as argument to printfCall. > Use "%s" in format string of printf. > > My issue: step 5. How can I get that done ?Ah, so you actually want to print it at runtime rather than during compilation? You've got 2 options there. Your approach will work. You need to call IRBuilder::CreateGlobalStringPtr, which will insert a global variable into your Module which is initialized by your StringRef. Sort of like 'const char tmp[] = "whatever";' in C++. Then you can add the return result from that to a printf call. Alternatively you could write your own i128 printer, link against it and call that instead of printf from your code. Then the Value would just be the original ConstantInt you started with. This has the advantage that it'd work even with non-constant integers, and doesn't put a bunch of extra strings into your module. The disadvantage is the extra calls you'll have to make: printf("First part of line"); print128(value); printf("second half of line\n");. So the choice is yours. Tim.