Generating Floating point constants

Hi,

We need to generate "Floating point constants" in our code. In http://llvm.org/docs/LangRef.html it is explained that FP has to follow special encoding rules to be handled by LLVM later one (hexadecimal coding...)

Is there any code available in LLVM to handle this kind of "standard float to LLVM float" conversion?

Thanks.

Stéphane Letz

If you're writing C++ code, just stick your float into an APFloat and
doesn't worry about the hexadecimal coding. If you're generating
textual IR, and you don't care about the precise hexadecimal
representation, AFAIK just printing a decimal float works. If you
really want a hexadecimal encoding, just reinterpret the
floating-point number as an integer and print in hexadecimal; an "LLVM
float" is just an IEEE float printed in hexadecimal.

-Eli

Hi,

We need to generate "Floating point constants" in our code. In http://llvm.org/docs/LangRef.html it is explained that FP has to follow special encoding rules to be handled by LLVM later one (hexadecimal coding...)

Is there any code available in LLVM to handle this kind of "standard float to LLVM float" conversion?

If you're writing C++ code, just stick your float into an APFloat and
doesn't worry about the hexadecimal coding. If you're generating
textual IR,

Yes we are generating textual IR,

and you don't care about the precise hexadecimal
representation, AFAIK just printing a decimal float works.

Seems like some values cannot be assembled later on (for instance with "llc")

If you
really want a hexadecimal encoding, just reinterpret the
floating-point number as an integer and print in hexadecimal; an "LLVM
float" is just an IEEE float printed in hexadecimal.

I will try.

Thanks

Stéphane Letz

Yes, there are floating point values (such as NaNs) that cannot be expressed in decimal, and others that would require a prohibitively large number of digits. You don't want to do that.

The code that writes out APFloats is in WriteConstantInt [sic] in VMCore/AsmWriter.cpp. It's not set up to do conversions separately from writing out, but you should be able to figure it out.

More fully, if you have a host floating point constant, you want to build an APFloat from it (there's a constructor that takes a float), then use the APFloat routines to manipulate it. If your source is in string form use convertFromString. Host floating point arithmetic is not portable; even float and double are not because of the x87 roundoff problem.

Thanks... but this is linked to the LLVM code base right? Is there any self-contained code that can be used to handle floats when writing textual LLVM IR?

Thanks

Stéphane Letz

and you don't care about the precise hexadecimal
representation, AFAIK just printing a decimal float works.

Seems like some values cannot be assembled later on (for instance with "llc")

Yes, there are floating point values (such as NaNs) that cannot be expressed in decimal, and others that would require a prohibitively large number of digits. You don't want to do that.

The code that writes out APFloats is in WriteConstantInt [sic] in VMCore/AsmWriter.cpp. It's not set up to do conversions separately from writing out, but you should be able to figure it out.

Thanks... but this is linked to the LLVM code base right?

It's part of LLVM, yes.

Is there any self-contained code that can be used to handle floats when writing textual LLVM IR?

Not that I know of.