[libclc] r217509 - math: Add acos implementation

Aaron Watry awatry at gmail.com
Wed Sep 10 10:35:45 PDT 2014


On Wed, Sep 10, 2014 at 11:09 AM, Yaron Keren <yaron.keren at gmail.com> wrote:
> Hi Aaron,
>
> Math functions are usually not implemented as simple math identities, due ot
> loss of accuracy and lower performance.
> For example, see acos(double)
>
>   http://www.netlib.org/fdlibm/e_acos.c
>
> openlibm ( https://github.com/JuliaLang/openlibm ) is a good reference for
> more math functions.
>
> I don't know if this approach is appropriate or applicable for libclc OpenCL
> implementation, though.
>

Hi Yaron,

I'll take a look at netlib, fdlibm and openlibm to see if I can find
something that works
on architectures that can support a mixture of float/double/both.  The
situation with R600 is
that there's currently no libm implementations of the required
built-in clang functions, and so
we either need to add another architecture to libm, or we need to
define the functions in libclc
which is what I'm attempting here.

For CL and libclc, the precision requirements are such that I believe
that the math identity
is sufficient for a first implementation (it at least meets the
precision required for the existing
unit tests).

I won't argue the performance implications, and I'm willing to revisit
these functions in light of
any license-compatible optimized implementations that I can find in a
generic C version which
can be easily ported to CLC.

--Aaron

> Yaron
>
>
> 2014-09-10 18:43 GMT+03:00 Aaron Watry <awatry at gmail.com>:
>>
>> Author: awatry
>> Date: Wed Sep 10 10:43:29 2014
>> New Revision: 217509
>>
>> URL: http://llvm.org/viewvc/llvm-project?rev=217509&view=rev
>> Log:
>> math: Add acos implementation
>>
>> Passes the tests that were submitted to the piglit list
>>
>> Tested on R600 (Pitcairn)
>>
>> Signed-off-by: Aaron Watry <awatry at gmail.com>
>> Reviewed-by: Jan Vesely <jan.vesely at rutgers.edu>
>>
>> Added:
>>     libclc/trunk/generic/include/clc/math/acos.h
>>     libclc/trunk/generic/include/clc/math/acos.inc
>>     libclc/trunk/generic/lib/math/acos.cl
>>     libclc/trunk/generic/lib/math/acos.inc
>> Modified:
>>     libclc/trunk/generic/include/clc/clc.h
>>     libclc/trunk/generic/lib/SOURCES
>>
>> Modified: libclc/trunk/generic/include/clc/clc.h
>> URL:
>> http://llvm.org/viewvc/llvm-project/libclc/trunk/generic/include/clc/clc.h?rev=217509&r1=217508&r2=217509&view=diff
>>
>> ==============================================================================
>> --- libclc/trunk/generic/include/clc/clc.h (original)
>> +++ libclc/trunk/generic/include/clc/clc.h Wed Sep 10 10:43:29 2014
>> @@ -32,6 +32,7 @@
>>  #include <clc/workitem/get_group_id.h>
>>
>>  /* 6.11.2 Math Functions */
>> +#include <clc/math/acos.h>
>>  #include <clc/math/atan.h>
>>  #include <clc/math/atan2.h>
>>  #include <clc/math/copysign.h>
>>
>> Added: libclc/trunk/generic/include/clc/math/acos.h
>> URL:
>> http://llvm.org/viewvc/llvm-project/libclc/trunk/generic/include/clc/math/acos.h?rev=217509&view=auto
>>
>> ==============================================================================
>> --- libclc/trunk/generic/include/clc/math/acos.h (added)
>> +++ libclc/trunk/generic/include/clc/math/acos.h Wed Sep 10 10:43:29 2014
>> @@ -0,0 +1,2 @@
>> +#define __CLC_BODY <clc/math/acos.inc>
>> +#include <clc/math/gentype.inc>
>>
>> Added: libclc/trunk/generic/include/clc/math/acos.inc
>> URL:
>> http://llvm.org/viewvc/llvm-project/libclc/trunk/generic/include/clc/math/acos.inc?rev=217509&view=auto
>>
>> ==============================================================================
>> --- libclc/trunk/generic/include/clc/math/acos.inc (added)
>> +++ libclc/trunk/generic/include/clc/math/acos.inc Wed Sep 10 10:43:29
>> 2014
>> @@ -0,0 +1 @@
>> +_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE acos(__CLC_GENTYPE x);
>>
>> Modified: libclc/trunk/generic/lib/SOURCES
>> URL:
>> http://llvm.org/viewvc/llvm-project/libclc/trunk/generic/lib/SOURCES?rev=217509&r1=217508&r2=217509&view=diff
>>
>> ==============================================================================
>> --- libclc/trunk/generic/lib/SOURCES (original)
>> +++ libclc/trunk/generic/lib/SOURCES Wed Sep 10 10:43:29 2014
>> @@ -29,6 +29,7 @@ integer/sub_sat.cl
>>  integer/sub_sat_if.ll
>>  integer/sub_sat_impl.ll
>>  integer/upsample.cl
>> +math/acos.cl
>>  math/atan.cl
>>  math/atan2.cl
>>  math/copysign.cl
>>
>> Added: libclc/trunk/generic/lib/math/acos.cl
>> URL:
>> http://llvm.org/viewvc/llvm-project/libclc/trunk/generic/lib/math/acos.cl?rev=217509&view=auto
>>
>> ==============================================================================
>> --- libclc/trunk/generic/lib/math/acos.cl (added)
>> +++ libclc/trunk/generic/lib/math/acos.cl Wed Sep 10 10:43:29 2014
>> @@ -0,0 +1,8 @@
>> +#include <clc/clc.h>
>> +
>> +#ifdef cl_khr_fp64
>> +#pragma OPENCL EXTENSION cl_khr_fp64 : enable
>> +#endif
>> +
>> +#define __CLC_BODY <acos.inc>
>> +#include <clc/math/gentype.inc>
>>
>> Added: libclc/trunk/generic/lib/math/acos.inc
>> URL:
>> http://llvm.org/viewvc/llvm-project/libclc/trunk/generic/lib/math/acos.inc?rev=217509&view=auto
>>
>> ==============================================================================
>> --- libclc/trunk/generic/lib/math/acos.inc (added)
>> +++ libclc/trunk/generic/lib/math/acos.inc Wed Sep 10 10:43:29 2014
>> @@ -0,0 +1,21 @@
>> +/*
>> + * There are multiple formulas for calculating arccosine of x:
>> + * 1) acos(x) = (1/2*pi) + i * ln(i*x + sqrt(1-x^2)) (notice the 'i'...)
>> + * 2) acos(x) = pi/2 + asin(-x) (asin isn't implemented yet)
>> + * 3) acos(x) = pi/2 - asin(x) (ditto)
>> + * 4) acos(x) = 2*atan2(sqrt(1-x), sqrt(1+x))
>> + * 5) acos(x) = pi/2 - atan2(x, ( sqrt(1-x^2) ) )
>> + *
>> + * Options 1-3 are not currently usable, #5 generates more concise
>> radeonsi
>> + * bitcode and assembly than #4 (134 vs 132 instructions on radeonsi),
>> but
>> + * precision of #4 may be better.
>> + */
>> +
>> +_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE acos(__CLC_GENTYPE x) {
>> +  return (
>> +    (__CLC_GENTYPE) 2.0 * atan2(
>> +      sqrt((__CLC_GENTYPE) 1.0 - x),
>> +      sqrt((__CLC_GENTYPE) 1.0 + x)
>> +    )
>> +  );
>> +}
>>
>>
>> _______________________________________________
>> cfe-commits mailing list
>> cfe-commits at cs.uiuc.edu
>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
>
>



More information about the cfe-commits mailing list