[LLVMdev] Math instructions

Morten Ofstad morten at hue.no
Thu Jan 6 08:30:54 PST 2005


Chris Lattner wrote:
> The way to deal with this is to add LLVM intrinsics, but only for 
> functions that set errno.  For example, you could add llvm.sqrt, which 
> is just undefined on a negative value other than -0.0.  For your uses, 
> you just emit llvm.sqrt, the C frontend will make use of it and wrap 
> errno handling around it as required.
> 
> For functions like sin/cos/etc, which do not set errno, you should be 
> able to recognize an external function with that name and generate code 
> for it.

Is this really safe? If you don't include math.h and don't link with the 
math library you're free to define sin/cos/etc any way you like, aren't you?

> Finally, for fabs, we should just be able to recognize the setcc/select 
> pair and generate that instruction.

the C math library has abs and absf, so it's easier to recognize the 
function calls. Of course the previous comment also applies to this...

- for now I've just implemented all of them as llvm.xxx intrinsics, but 
I'm a bit unsure how to lower these if the target does not support them 
and the program does not link with libmath... Maybe I worry too much ;)

m.




More information about the llvm-dev mailing list