[PATCH] Returns NaN for sqrt with negative fp argument

Tim Northover t.p.northover at gmail.com
Tue Jun 10 01:12:27 PDT 2014

Hi Jiangning,

> LLVM IR language spec says,
>     llvm.sqrt has undefined behavior for negative numbers other than -0.0
> (which allows for better optimization, because there is no need to worry
> about errno being set).
> But obviously right now LLVM is returning fp zero covering -0.0, right?

Yep. One valid choice for undefined behaviour is returning 0.0.

>> I don't find that compelling, either. Particularly when -ffast-math is
>> involved, we shouldn't be expected to replicate the precise points
>> where GCC decides to ignore NaNs and how it does so.
> I'm fine with this, but it's harmless to follow the same "undefined"
> behavior even with -ffast-math enabled, right?

Well, it conceals invalid assumptions in the IR, which seems like a
bad thing to me. In this case, @llvm.sqrt is undefined for negative
numbers. In my view, it's worse if it happens to be IEEE compliant if
it gets constant folded, but not otherwise.

> Or vice versa, why is fp zero a more reasonable implementation than NaN for
> this case?

I'd say the best implementation would be undef (possibly even
reporting a warning, though that's strictly QoI and probably better as
a sanitizer anyway). I'm not sure why 0.0 is there at the moment



More information about the llvm-commits mailing list