[LLVMdev] fptoui vs fptosi

Tim Northover t.p.northover at gmail.com
Sat Aug 2 04:04:16 PDT 2014


Hi Andreas,

On 2 August 2014 10:19, Andreas Grünwald <andreasgruenwald at live.de> wrote:
> Wouldn't the nearest unsigned integer for a negative value be 0? Or is this
> undefined behavior?

Yep, undefined behaviour (mostly), the next sentence is: "If the value
cannot fit in ty2, the results are undefined.". And negative numbers
certainly can't fit into an unsigned value.

It's modelled after C's restrictions (naturally enough); the wording
there is slightly better: "When a finite value of real floating type
is converted to an integer type other than _Bool, the fractional part
is discarded (i.e., the value is truncated toward zero). If the value
of the integral part cannot be represented by the integer type, the
behavior is undefined.)".

Interestingly, this means that values in the range (-1,0) are required
to convert to 0.

> It would be helpful if someone could clarify for me, in which cases the two
> instructions produce different results.

I believe they will never produce different results on their common domain.

Cheers.

Tim.




More information about the llvm-dev mailing list