[Libclc-dev] [PATCH 3/4] Implement smoothstep builtin v2

Tom Stellard thomas.stellard at amd.com
Wed Feb 25 13:20:49 PST 2015


v2:
  - Fix typo in smoothstep.h
---
 generic/include/clc/clc.h                 |  1 +
 generic/include/clc/common/smoothstep.h   | 25 ++++++++++++++
 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, 133 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

diff --git a/generic/include/clc/clc.h b/generic/include/clc/clc.h
index 2353682..e1f888b 100644
--- a/generic/include/clc/clc.h
+++ b/generic/include/clc/clc.h
@@ -109,6 +109,7 @@
 #include <clc/common/degrees.h>
 #include <clc/common/radians.h>
 #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..7a270dc
--- /dev/null
+++ b/generic/include/clc/common/smoothstep.h
@@ -0,0 +1,25 @@
+/*
+ * 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/common/smoothstep.inc>
+#include <clc/math/gentype.inc>
+#undef __CLC_BODY
diff --git a/generic/include/clc/common/smoothstep.inc b/generic/include/clc/common/smoothstep.inc
new file mode 100644
index 0000000..286eeac
--- /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_GENTYPE smoothstep(__CLC_GENTYPE edge0, __CLC_GENTYPE edge1, __CLC_GENTYPE x);
+_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE smoothstep(float edge0, float edge1, __CLC_GENTYPE x);
+
+#ifdef cl_khr_fp64
+_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE smoothstep(double edge0, double edge1, __CLC_GENTYPE x);
+#endif
diff --git a/generic/lib/SOURCES b/generic/lib/SOURCES
index 7745d69..aaedada 100644
--- a/generic/lib/SOURCES
+++ b/generic/lib/SOURCES
@@ -30,6 +30,7 @@ convert.cl
 common/degrees.cl
 common/radians.cl
 common/sign.cl
+common/smoothstep.cl
 geometric/cross.cl
 geometric/dot.cl
 geometric/length.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





More information about the Libclc-dev mailing list