PDA

View Full Version : Abs function is special?



Atmapuri
05-12-2011, 01:02 AM
Hi!

I notice that Abs is not defined for float and other negative range types. Can somebody elaborate what is the reasoning behind this? There are many other math functions which use "if" or branching internally for sure.

Thanks!
Atmapuri

david.garcia
05-12-2011, 04:52 AM
I notice that abs() is not defined for float and other negative range types.

From section 6.11.3 of the CL 1.1. specification:


We use the generic type name gentype to indicate that the function can take char,
char{2|3|4|8|16}, uchar, uchar{2|3|4|8|16}, short,
short{2|3|4|8|16}, ushort, ushort{2|3|4|8|16}, int,
int{2|3|4|8|16}, uint, uint{2|3|4|8|16}, long, long{2|3|4|8|16}
ulong, or ulong{2|3|4|8|16} as the type for the arguments. We use the generic type
name ugentype to refer to unsigned versions of gentype. For example, if gentype is
char4, ugentype is uchar4.

The builtin function abs() is defined as:

ugentype abs (gentype x)

This means that, for example, the following code is valid:


signed int foo;
unsigned int bar;

bar = abs(foo);


As for floating point inputs, use the builtin fabs() instead (defined in section 6.11.2).