Generating Floating point constants

------------------------------

Message: 4
Date: Wed, 2 Jun 2010 11:07:39 -0700
From: Dale Johannesen <dalej@apple.com>
Subject: Re: [LLVMdev] Generating Floating point constants
To: St?phane Letz <letz@free.fr>
Cc: llvmdev@cs.uiuc.edu
Message-ID: <AEC895CC-E887-4329-8743-FA606BD401F6@apple.com>
Content-Type: text/plain; charset=iso-8859-1

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.

I'm becoming crazy with this stuff, for instance

0.8f get converted in 0x3FE99999A0000000 by LLVM (looking at textual generated IR) but the following tool:

http://babbage.cs.qc.edu/IEEE-754/Decimal.html gives:

0x3FE999999999999A instead and this value cannot be read back by "llc"...

Am I the only one to need to generate constants float in LLVL IR ?

Stéphane Letz

Well

For float 0.8 :

http://babbage.cs.qc.edu/IEEE-754/Decimal.html

3F4CCCCD for Single precision (32 bits):

3FE999999999999A for Double precision (64 bits):

Still different compared to LLVM...

Any idea?

Stéphane

0x3FE999999999999A is the correct value; note that "(double)0.8f" and
"0.8" are not the same value in C.

-Eli

Martin is right. Floats have only 36 significant bits (sign + 11 exponent + 24 mantissa) and are stored that way. (The exponent is in double format for some reason I've forgotten, probably because it was easier somewhere.) Any resemblance to IEEE-754/Decimal is coincidental. And no, I don't know of anybody else having trouble with this. Why can't you connect to the AsmWriter in your code?

LLVM serializes and deserializes float constants in double precision. I don't know why.

0x3F4CCCCD = (sign=0, exp=01111110 (-1), significand=0x99999A)
0x3FE99999A0000000 = (sign = 0, exp=01111111110 (-1), significand=0x99999A0000000)

If you try to deserialize 0x3FE999999999999A as a float, it loses a lot of precision, which I presume is why llc complains.

John.

Because this seems like a very big dependency constraint to be able to do something as simple as using "floats" in a textual format. I would have been happy to be able to copy a 10 lines function in my own code to do that conversion.

Stéphane Letz