[Libclc-dev] [PATCH] Implement smoothstep builtin

Tom Stellard tom at stellard.net
Wed Feb 25 13:26:13 PST 2015


On Wed, Feb 25, 2015 at 08:00:28PM +0000, Tom Stellard wrote:
> ---
>  generic/include/clc/clc.h                 |  1 +
>  generic/include/clc/common/smoothstep.h   | 24 ++++++++++++++
>  generic/include/clc/common/smoothstep.inc | 28 ++++++++++++++++
>  generic/lib/SOURCES                       |  1 +
>  generic/lib/clcmacro.h                    | 23 +++++++++++++
>  generic/lib/common/smoothstep.cl          | 55 +++++++++++++++++++++++++++++++
>  6 files changed, 132 insertions(+)
>  create mode 100644 generic/include/clc/common/smoothstep.h
>  create mode 100644 generic/include/clc/common/smoothstep.inc
>  create mode 100644 generic/lib/common/smoothstep.cl
> 

I just discovered some bugs in my last 4 patches with respect to the directories the
files are in, so I will resend them all.

-Tom

> diff --git a/generic/include/clc/clc.h b/generic/include/clc/clc.h
> index 2ff3b15..1fdc148 100644
> --- a/generic/include/clc/clc.h
> +++ b/generic/include/clc/clc.h
> @@ -107,6 +107,7 @@
>  
>  /* 6.11.4 Common Functions */
>  #include <clc/common/sign.h>
> +#include <clc/common/smoothstep.h>
>  
>  /* 6.11.5 Geometric Functions */
>  #include <clc/geometric/cross.h>
> diff --git a/generic/include/clc/common/smoothstep.h b/generic/include/clc/common/smoothstep.h
> new file mode 100644
> index 0000000..4434fa6
> --- /dev/null
> +++ b/generic/include/clc/common/smoothstep.h
> @@ -0,0 +1,24 @@
> +/*
> + * Copyright (c) 2014,2015 Advanced Micro Devices, Inc.
> + *
> + * Permission is hereby granted, free of charge, to any person obtaining a copy
> + * of this software and associated documentation files (the "Software"), to deal
> + * in the Software without restriction, including without limitation the rights
> + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
> + * copies of the Software, and to permit persons to whom the Software is
> + * furnished to do so, subject to the following conditions:
> + *
> + * The above copyright notice and this permission notice shall be included in
> + * all copies or substantial portions of the Software.
> + *
> + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
> + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
> + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
> + * THE SOFTWARE.
> + */
> +
> +#define __CLC_BODY <clc/geometric/smoothstep.inc>
> +#include <clc/geometric/floatn.inc>
> diff --git a/generic/include/clc/common/smoothstep.inc b/generic/include/clc/common/smoothstep.inc
> new file mode 100644
> index 0000000..06519dd
> --- /dev/null
> +++ b/generic/include/clc/common/smoothstep.inc
> @@ -0,0 +1,28 @@
> +/*
> + * Copyright (c) 2014,2015 Advanced Micro Devices, Inc.
> + *
> + * Permission is hereby granted, free of charge, to any person obtaining a copy
> + * of this software and associated documentation files (the "Software"), to deal
> + * in the Software without restriction, including without limitation the rights
> + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
> + * copies of the Software, and to permit persons to whom the Software is
> + * furnished to do so, subject to the following conditions:
> + *
> + * The above copyright notice and this permission notice shall be included in
> + * all copies or substantial portions of the Software.
> + *
> + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
> + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
> + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
> + * THE SOFTWARE.
> + */
> +
> +_CLC_OVERLOAD _CLC_DECL __CLC_FLOATN smoothstep(__CLC_FLOATN edge0, __CLC_FLOATN edge1, __CLC_FLOATN x);
> +_CLC_OVERLOAD _CLC_DECL __CLC_FLOATN smoothstep(float edge0, float edge1, __CLC_FLOATN x);
> +
> +#ifdef cl_khr_fp64
> +_CLC_OVERLOAD _CLC_DECL __CLC_FLOATN smoothstep(double edge0, double edge1, __CLC_FLOATN x);
> +#endif
> diff --git a/generic/lib/SOURCES b/generic/lib/SOURCES
> index c48687d..7411f36 100644
> --- a/generic/lib/SOURCES
> +++ b/generic/lib/SOURCES
> @@ -28,6 +28,7 @@ cl_khr_local_int32_extended_atomics/atom_or.cl
>  cl_khr_local_int32_extended_atomics/atom_xor.cl
>  convert.cl
>  common/sign.cl
> +common/smoothstep.cl
>  geometric/cross.cl
>  geometric/degrees.cl
>  geometric/dot.cl
> diff --git a/generic/lib/clcmacro.h b/generic/lib/clcmacro.h
> index ef102ea..39636c9 100644
> --- a/generic/lib/clcmacro.h
> +++ b/generic/lib/clcmacro.h
> @@ -63,6 +63,29 @@
>      return (RET_TYPE##16)(FUNCTION(x.lo, y.lo, z.lo), FUNCTION(x.hi, y.hi, z.hi)); \
>    }
>  
> +#define _CLC_V_S_S_V_VECTORIZE(DECLSPEC, RET_TYPE, FUNCTION, ARG1_TYPE, ARG2_TYPE, ARG3_TYPE) \
> +  DECLSPEC RET_TYPE##2 FUNCTION(ARG1_TYPE x, ARG2_TYPE y, ARG3_TYPE##2 z) { \
> +    return (RET_TYPE##2)(FUNCTION(x, y, z.lo), FUNCTION(x, y, z.hi)); \
> +  } \
> +\
> +  DECLSPEC RET_TYPE##3 FUNCTION(ARG1_TYPE x, ARG2_TYPE y, ARG3_TYPE##3 z) { \
> +    return (RET_TYPE##3)(FUNCTION(x, y, z.x), FUNCTION(x, y, z.y), \
> +                         FUNCTION(x, y, z.z)); \
> +  } \
> +\
> +  DECLSPEC RET_TYPE##4 FUNCTION(ARG1_TYPE x, ARG2_TYPE y, ARG3_TYPE##4 z) { \
> +    return (RET_TYPE##4)(FUNCTION(x, y, z.lo), FUNCTION(x, y, z.hi)); \
> +  } \
> +\
> +  DECLSPEC RET_TYPE##8 FUNCTION(ARG1_TYPE x, ARG2_TYPE y, ARG3_TYPE##8 z) { \
> +    return (RET_TYPE##8)(FUNCTION(x, y, z.lo), FUNCTION(x, y, z.hi)); \
> +  } \
> +\
> +  DECLSPEC RET_TYPE##16 FUNCTION(ARG1_TYPE x, ARG2_TYPE y, ARG3_TYPE##16 z) { \
> +    return (RET_TYPE##16)(FUNCTION(x, y, z.lo), FUNCTION(x, y, z.hi)); \
> +  } \
> +\
> +
>  #define _CLC_DEFINE_BINARY_BUILTIN(RET_TYPE, FUNCTION, BUILTIN, ARG1_TYPE, ARG2_TYPE) \
>  _CLC_DEF _CLC_OVERLOAD RET_TYPE FUNCTION(ARG1_TYPE x, ARG2_TYPE y) { \
>    return BUILTIN(x, y); \
> diff --git a/generic/lib/common/smoothstep.cl b/generic/lib/common/smoothstep.cl
> new file mode 100644
> index 0000000..68d1a13
> --- /dev/null
> +++ b/generic/lib/common/smoothstep.cl
> @@ -0,0 +1,55 @@
> +/*
> + * Copyright (c) 2014,2015 Advanced Micro Devices, Inc.
> + *
> + * Permission is hereby granted, free of charge, to any person obtaining a copy
> + * of this software and associated documentation files (the "Software"), to deal
> + * in the Software without restriction, including without limitation the rights
> + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
> + * copies of the Software, and to permit persons to whom the Software is
> + * furnished to do so, subject to the following conditions:
> + *
> + * The above copyright notice and this permission notice shall be included in
> + * all copies or substantial portions of the Software.
> + *
> + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
> + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
> + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
> + * THE SOFTWARE.
> + */
> +
> +#include <clc/clc.h>
> +
> +#include "../clcmacro.h"
> +
> +_CLC_OVERLOAD _CLC_DEF float smoothstep(float edge0, float edge1, float x) {
> +  float t = clamp((x - edge0) / (edge1 - edge0), 0.0f, 1.0f);
> +  return t * t * (3.0f - 2.0f * t);
> +}
> +
> +_CLC_TERNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, smoothstep, float, float, float);
> +
> +_CLC_V_S_S_V_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, smoothstep, float, float, float);
> +
> +#ifdef cl_khr_fp64
> +#pragma OPENCL EXTENSION cl_khr_fp64 : enable
> +
> +#define SMOOTH_STEP_DEF(edge_type, x_type, impl) \
> +  _CLC_OVERLOAD _CLC_DEF x_type smoothstep(edge_type edge0, edge_type edge1, x_type x) { \
> +    double t = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0); \
> +    return t * t * (3.0 - 2.0 * t); \
> + }
> +
> +SMOOTH_STEP_DEF(double, double, SMOOTH_STEP_IMPL_D);
> +
> +_CLC_TERNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, smoothstep, double, double, double);
> +
> +SMOOTH_STEP_DEF(float, double, SMOOTH_STEP_IMPL_D);
> +SMOOTH_STEP_DEF(double, float, SMOOTH_STEP_IMPL_D);
> +
> +_CLC_V_S_S_V_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, smoothstep, float, float, double);
> +_CLC_V_S_S_V_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, smoothstep, double, double, float);
> +
> +#endif
> -- 
> 2.0.4
> 
> 
> _______________________________________________
> 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