[LLVMdev] Related constant folding of floating point values

Renato Golin renato.golin at linaro.org
Wed Oct 9 07:47:14 PDT 2013


Hi Arsen,

On 9 October 2013 12:53, Arsen Hakobyan <artinetstudio at gmail.com> wrote:

>  if (((a - 8.1) >= FLT_EPSILON) || ((a - 8.1) <= -FLT_EPSILON)) {    //I am
> using FLT_EPSILON to check whether (a != 2.0).
>

This comment is wrong. You're trying to check if subtraction has any
residual value (module) due to floating point inaccuracies that are higher
than FLT_EPSILON.

with -O3 optimization level clang generates already incorrect LLVM IR:
> ; Function Attrs: nounwind uwtable
> define i32 @main() #0 {
> entry:
>   store i32 1, i32* @err, align 4, !tbaa !0
>   ret i32 0
> }
>

This is correct, since FLT_EPSILON is denormalized to 0.0, thus "(a - 8.1)
>= FLT_EPSILON" -> "0.0 >= 0.0", which is always true, and that branch will
always be taken, thus, err will always be equal 1.

If you change your float to double, you'll see that it no longer
denormalizes, and err=0;

cheers,
--renato
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20131009/de13bb91/attachment.html>


More information about the llvm-dev mailing list