[LLVMdev] llvm.sqrt intrinsic undefined behaviour

Nicholas Chapman admin at indigorenderer.com
Thu Oct 31 06:16:20 PDT 2013


Hi all,
I'm working on a language in which I would like all operations to be 
well defined. (and efficient)
I want to define a sqrt function in my language, that will return NaN 
for arguments < 0, and NaN for a NaN argument.
As far as I know, these semantics map nicely to the SQRTPS SSE 
instruction, which seems to return NaN on arguments < 0.
However, the LLVM lang ref states "llvm.sqrt has undefined behavior for 
negative numbers other than -0.0".

This means that, to avoid undefined behaviour, in general I will have to 
add a runtime branch to avoid passing values less than zero to llvm.sqrt().
This is unfortunate since I would like to avoid inefficient, unneeded 
branching.

I propose changing the llvm.sqrt() LLVM instrinsic to be well defined on 
all inputs, and be defined to return NaN on negative inputs.

Btw, I don't particularly care about errno or related, as my language is 
not C.  I realise there is some kind of issue here to do with code 
reordering and errno, but It would be a pity if these problems slowed 
down sqrt code emission for all LLVM users.

What do people think?

Thanks,
     Nick




More information about the llvm-dev mailing list