[LLVMdev] Math instructions
Chris Lattner
sabre at nondot.org
Thu Jan 6 09:00:19 PST 2005
On Thu, 6 Jan 2005, Morten Ofstad wrote:
> 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?
No, not for C programs at least. In C, all standard C and math library
function names are reserved.
>> 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...
The problem there is that these functions are inline functions that expand
to the select/setcc pair. For this testcase:
#include <math.h>
double test1(double X) { return fabs(X); }
float test2(float X) { return fabsf(X); }
llvm-gcc compiles it to:
double %test1(double %X) {
%comp = setge double %X, 0.000000e+00
%abs.pos = sub double 0.000000e+00, %X
%abs.1 = select bool %comp, double %X, double %abs.pos
ret double %abs.1
}
float %test2(float %X) {
%comp = setge float %X, 0.000000e+00
%abs.pos = sub float 0.000000e+00, %X
%abs.1 = select bool %comp, float %X, float %abs.pos
ret float %abs.1
}
... so i won't help the C front-end at least. If you want to add support
for fabs and fabsf as calls, I can implement the pattern matching for the
forms above someday.
> - 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 ;)
Yeah, I would worry about this one too much for now.
-Chris
--
http://nondot.org/sabre/
http://llvm.cs.uiuc.edu/
More information about the llvm-dev
mailing list