[Libclc-dev] [PATCH] generic/math: Add acos implementation

Jan Vesely jan.vesely at rutgers.edu
Fri Sep 5 10:27:57 PDT 2014


On Sat, 2014-08-30 at 11:12 -0500, Aaron Watry wrote:
> Passes the tests that were submitted to the piglit list
> 
> Tested on R600 (Pitcairn)
> 
> Signed-off-by: Aaron Watry <awatry at gmail.com>
> ---
>  generic/include/clc/clc.h         |  1 +
>  generic/include/clc/math/acos.h   |  2 ++
>  generic/include/clc/math/acos.inc |  1 +
>  generic/lib/SOURCES               |  1 +
>  generic/lib/math/acos.cl          |  8 ++++++++
>  generic/lib/math/acos.inc         | 21 +++++++++++++++++++++
>  6 files changed, 34 insertions(+)
>  create mode 100644 generic/include/clc/math/acos.h
>  create mode 100644 generic/include/clc/math/acos.inc
>  create mode 100644 generic/lib/math/acos.cl
>  create mode 100644 generic/lib/math/acos.inc
> 
> diff --git a/generic/include/clc/clc.h b/generic/include/clc/clc.h
> index 84b25ac..534b1dd 100644
> --- a/generic/include/clc/clc.h
> +++ b/generic/include/clc/clc.h
> @@ -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>
> diff --git a/generic/include/clc/math/acos.h b/generic/include/clc/math/acos.h
> new file mode 100644
> index 0000000..e753dee
> --- /dev/null
> +++ b/generic/include/clc/math/acos.h
> @@ -0,0 +1,2 @@
> +#define __CLC_BODY <clc/math/acos.inc>
> +#include <clc/math/gentype.inc>
> diff --git a/generic/include/clc/math/acos.inc b/generic/include/clc/math/acos.inc
> new file mode 100644
> index 0000000..4ca8c75
> --- /dev/null
> +++ b/generic/include/clc/math/acos.inc
> @@ -0,0 +1 @@
> +_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE acos(__CLC_GENTYPE x);
> diff --git a/generic/lib/SOURCES b/generic/lib/SOURCES
> index 21fc7ca..bf02233 100644
> --- a/generic/lib/SOURCES
> +++ b/generic/lib/SOURCES
> @@ -28,6 +28,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/cos.cl
> diff --git a/generic/lib/math/acos.cl b/generic/lib/math/acos.cl
> new file mode 100644
> index 0000000..3ce9655
> --- /dev/null
> +++ b/generic/lib/math/acos.cl
> @@ -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>
> diff --git a/generic/lib/math/acos.inc b/generic/lib/math/acos.inc
> new file mode 100644
> index 0000000..8612415
> --- /dev/null
> +++ b/generic/lib/math/acos.inc
> @@ -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))

tested on my turks gpu, and it passes the piglit.

LGTM.

Although proving that
2*atan2(sqrt(1-x), sqrt(1+x)) == atan2(sqrt(1-x^2), x) took me while.

jan

> + * 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)
> +    )
> +  );
> +}

-- 
Jan Vesely <jan.vesely at rutgers.edu>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: This is a digitally signed message part
URL: <http://lists.llvm.org/pipermail/libclc-dev/attachments/20140905/5ea13deb/attachment.sig>


More information about the Libclc-dev mailing list