[Libclc-dev] [PATCH 1/2] math: Add asin implementation

Jan Vesely jan.vesely at rutgers.edu
Mon Sep 8 14:35:48 PDT 2014


On Mon, 2014-09-08 at 10:09 -0500, Aaron Watry wrote:
> On Fri, Sep 5, 2014 at 12:54 PM, Jan Vesely <jan.vesely at rutgers.edu> wrote:
> > On Thu, 2014-09-04 at 12:35 -0500, Aaron Watry wrote:
> >> asin(x) = PI/2 - acos(x)
> >
> > LGTM.
> >
> > just out of curiosity.
> > How does the precision compare to just using
> > atan2(x, ( sqrt(1-x^2) ) )
> > from 5) of your acos patch?
> >
> > I assume (PI/2 -) does not shift the balance.
> >
> 
> The precision of both implementations looks ok.  The existing piglit
> tests pass when tightened down to a tolerance of 1 ULP and fail at 0
> ULP. Given that the spec gives us 4 ULP as allowed variance, it seems
> like we're good.
> 
> I did the following, alternate implementations and did a quick check
> on bitcode length and number of instructions on evergreen.  It seems
> like the second variation gives us sufficient precision and fewer
> hardware instructions for at least the tested architecture (CEDAR on
> latest svn llvm/clang).
> 
> If you prefer, I can commit the second implementation instead.

I'm ok with both, I'll leave the decision to you.

jan

> 
> --Aaron
> 
> 
> diff --git a/generic/lib/math/asin.inc b/generic/lib/math/asin.inc
> index f1a65b3..661663a 100644
> --- a/generic/lib/math/asin.inc
> +++ b/generic/lib/math/asin.inc
> @@ -5,7 +5,15 @@
>  #endif
> 
>  _CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE asin(__CLC_GENTYPE x) {
> +#if 0
> +  //Passes with 1ulp on evergreen, fails at 0
> +  //(float16): 1786 DW on CEDAR, 22 GPRs, %694 is highest numbered
> bitcode instr
>    return ( (__CLC_GENTYPE)PI2 - acos(x));
> +#else
> +  //Passes with 1ulp on evergreen, fails at 0
> +  //(float16): 1622 DW on CEDAR, 22 GPRs, %691 is highest numbered
> bitcode instr
> +  return atan2(x, sqrt((__CLC_GENTYPE)1.0 -(x*x) ) );
> +#endif
>  }
> 
>  #undef PI2
> 
> 
> 
> 
> > jan
> >
> >
> >>
> >> We already have an implementation of acos(x), so just use that.
> >>
> >> Signed-off-by: Aaron Watry <awatry at gmail.com>
> >> ---
> >>  generic/include/clc/clc.h         |  1 +
> >>  generic/include/clc/math/asin.h   |  2 ++
> >>  generic/include/clc/math/asin.inc |  1 +
> >>  generic/lib/SOURCES               |  1 +
> >>  generic/lib/math/asin.cl          |  8 ++++++++
> >>  generic/lib/math/asin.inc         | 11 +++++++++++
> >>  6 files changed, 24 insertions(+)
> >>  create mode 100644 generic/include/clc/math/asin.h
> >>  create mode 100644 generic/include/clc/math/asin.inc
> >>  create mode 100644 generic/lib/math/asin.cl
> >>  create mode 100644 generic/lib/math/asin.inc
> >>
> >> diff --git a/generic/include/clc/clc.h b/generic/include/clc/clc.h
> >> index 490893b..079c674 100644
> >> --- a/generic/include/clc/clc.h
> >> +++ b/generic/include/clc/clc.h
> >> @@ -33,6 +33,7 @@
> >>
> >>  /* 6.11.2 Math Functions */
> >>  #include <clc/math/acos.h>
> >> +#include <clc/math/asin.h>
> >>  #include <clc/math/atan.h>
> >>  #include <clc/math/atan2.h>
> >>  #include <clc/math/copysign.h>
> >> diff --git a/generic/include/clc/math/asin.h b/generic/include/clc/math/asin.h
> >> new file mode 100644
> >> index 0000000..2a85872
> >> --- /dev/null
> >> +++ b/generic/include/clc/math/asin.h
> >> @@ -0,0 +1,2 @@
> >> +#define __CLC_BODY <clc/math/asin.inc>
> >> +#include <clc/math/gentype.inc>
> >> diff --git a/generic/include/clc/math/asin.inc b/generic/include/clc/math/asin.inc
> >> new file mode 100644
> >> index 0000000..b4ad8ff
> >> --- /dev/null
> >> +++ b/generic/include/clc/math/asin.inc
> >> @@ -0,0 +1 @@
> >> +_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE asin(__CLC_GENTYPE x);
> >> diff --git a/generic/lib/SOURCES b/generic/lib/SOURCES
> >> index 8eaaa61..30e182f 100644
> >> --- a/generic/lib/SOURCES
> >> +++ b/generic/lib/SOURCES
> >> @@ -30,6 +30,7 @@ integer/sub_sat_if.ll
> >>  integer/sub_sat_impl.ll
> >>  integer/upsample.cl
> >>  math/acos.cl
> >> +math/asin.cl
> >>  math/atan.cl
> >>  math/atan2.cl
> >>  math/copysign.cl
> >> diff --git a/generic/lib/math/asin.cl b/generic/lib/math/asin.cl
> >> new file mode 100644
> >> index 0000000..d56dbd7
> >> --- /dev/null
> >> +++ b/generic/lib/math/asin.cl
> >> @@ -0,0 +1,8 @@
> >> +#include <clc/clc.h>
> >> +
> >> +#ifdef cl_khr_fp64
> >> +#pragma OPENCL EXTENSION cl_khr_fp64 : enable
> >> +#endif
> >> +
> >> +#define __CLC_BODY <asin.inc>
> >> +#include <clc/math/gentype.inc>
> >> diff --git a/generic/lib/math/asin.inc b/generic/lib/math/asin.inc
> >> new file mode 100644
> >> index 0000000..f1a65b3
> >> --- /dev/null
> >> +++ b/generic/lib/math/asin.inc
> >> @@ -0,0 +1,11 @@
> >> +#if __CLC_SCALAR_GENTYPE == double
> >> +#define PI2 M_PI_2
> >> +#else
> >> +#define PI2 M_PI_2_F
> >> +#endif
> >> +
> >> +_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE asin(__CLC_GENTYPE x) {
> >> +  return ( (__CLC_GENTYPE)PI2 - acos(x));
> >> +}
> >> +
> >> +#undef PI2
> >
> > --
> > Jan Vesely <jan.vesely at rutgers.edu>

-- 
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/20140908/aace7f1a/attachment.sig>


More information about the Libclc-dev mailing list