[Libclc-dev] [PATCH] Implementations for exp(float) and exp(double) v2

Aaron Watry awatry at gmail.com
Tue Jun 3 09:44:09 PDT 2014


Assuming that the math is right, I don't see any inherent issues with
this method.  We'll have to extend it eventually to handle half
floats, but it's at least going to give us exactly the value of
M_LOG2E that we want.

An alternative that also works, but I haven't double checked the
precision for float inputs when double is enabled:
#ifdef cl_khr_fp64
  return exp2(val * (__CLC_GENTYPE)M_LOG2E);
#else
  return exp2(val * M_LOG2E_F);
#endif

Also, if we want to add half to this method, we just need to add
another cast of M_LOG2E_F in the else branch (if double is not
available but half is).  Once again, I haven't checked to make sure if
the cast values meet the precision requirements of the spec...

--Aaron


On Sun, May 11, 2014 at 4:36 AM, Jeroen Ketema <j.ketema at imperial.ac.uk> wrote:
> Use separate implementations instead of a macro
> to ensure the constant multiplied with is of
> higher precision.
>
> v2: Use the correct formula, spotted by Dan Liew
> ---
> generic/include/clc/math/exp.h       |  9 +++++++--
> generic/include/clc/math/gentype.inc |  4 ++++
> generic/lib/SOURCES                  |  1 +
> generic/lib/math/exp.cl              |  8 ++++++++
> generic/lib/math/exp.inc             | 10 ++++++++++
> 5 files changed, 30 insertions(+), 2 deletions(-)
> create mode 100644 generic/lib/math/exp.cl
> create mode 100644 generic/lib/math/exp.inc
>
> diff --git a/generic/include/clc/math/exp.h b/generic/include/clc/math/exp.h
> index dbc4b84..9866524 100644
> --- a/generic/include/clc/math/exp.h
> +++ b/generic/include/clc/math/exp.h
> @@ -1,4 +1,9 @@
> #undef exp
>
> -// exp(x) = exp2(x * log2(e)
> -#define exp(val) (__clc_exp2((val) * 1.44269504f))
> +#define __CLC_BODY <clc/math/unary_decl.inc>
> +#define __CLC_FUNCTION exp
> +
> +#include <clc/math/gentype.inc>
> +
> +#undef __CLC_BODY
> +#undef __CLC_FUNCTION
> diff --git a/generic/include/clc/math/gentype.inc b/generic/include/clc/math/gentype.inc
> index bff4f56..9f79f6e 100644
> --- a/generic/include/clc/math/gentype.inc
> +++ b/generic/include/clc/math/gentype.inc
> @@ -1,4 +1,5 @@
> #define __CLC_SCALAR_GENTYPE float
> +#define __CLC_FPSIZE 32
>
> #define __CLC_GENTYPE float
> #define __CLC_SCALAR
> @@ -26,10 +27,12 @@
> #include __CLC_BODY
> #undef __CLC_GENTYPE
>
> +#undef __CLC_FPSIZE
> #undef __CLC_SCALAR_GENTYPE
>
> #ifdef cl_khr_fp64
> #define __CLC_SCALAR_GENTYPE double
> +#define __CLC_FPSIZE 64
>
> #define __CLC_SCALAR
> #define __CLC_GENTYPE double
> @@ -57,6 +60,7 @@
> #include __CLC_BODY
> #undef __CLC_GENTYPE
>
> +#undef __CLC_FPSIZE
> #undef __CLC_SCALAR_GENTYPE
> #endif
>
> diff --git a/generic/lib/SOURCES b/generic/lib/SOURCES
> index 6ccdf48..a35542c 100644
> --- a/generic/lib/SOURCES
> +++ b/generic/lib/SOURCES
> @@ -27,6 +27,7 @@ integer/sub_sat.cl
> integer/sub_sat_if.ll
> integer/sub_sat_impl.ll
> integer/upsample.cl
> +math/exp.cl
> math/fmax.cl
> math/fmin.cl
> math/hypot.cl
> diff --git a/generic/lib/math/exp.cl b/generic/lib/math/exp.cl
> new file mode 100644
> index 0000000..dbf4a93
> --- /dev/null
> +++ b/generic/lib/math/exp.cl
> @@ -0,0 +1,8 @@
> +#include <clc/clc.h>
> +
> +#ifdef cl_khr_fp64
> +#pragma OPENCL EXTENSION cl_khr_fp64 : enable
> +#endif
> +
> +#define __CLC_BODY <exp.inc>
> +#include <clc/math/gentype.inc>
> diff --git a/generic/lib/math/exp.inc b/generic/lib/math/exp.inc
> new file mode 100644
> index 0000000..525fb59
> --- /dev/null
> +++ b/generic/lib/math/exp.inc
> @@ -0,0 +1,10 @@
> +_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE exp(__CLC_GENTYPE val) {
> +  // exp(x) = exp2(x * log2(e))
> +#if __CLC_FPSIZE == 32
> +  return exp2(val * M_LOG2E_F);
> +#elif __CLC_FPSIZE == 64
> +  return exp2(val * M_LOG2E);
> +#else
> +#error unknown _CLC_FPSIZE
> +#endif
> +}
> --
> 1.8.5.2 (Apple Git-48)
>
>
> _______________________________________________
> Libclc-dev mailing list
> Libclc-dev at pcc.me.uk
> http://www.pcc.me.uk/cgi-bin/mailman/listinfo/libclc-dev




More information about the Libclc-dev mailing list