[Libclc-dev] [PATCH 2/2] Implement modf builtin

Pavel Ondračka via Libclc-dev libclc-dev at lists.llvm.org
Mon Jan 18 01:36:26 PST 2016


This is a port from the AMD builtin library.
---
 generic/include/clc/clc.h         |  1 +
 generic/include/clc/math/modf.h   | 24 ++++++++++
 generic/include/clc/math/modf.inc | 25 +++++++++++
 generic/lib/SOURCES               |  1 +
 generic/lib/math/modf.cl          | 92 +++++++++++++++++++++++++++++++++++++++
 5 files changed, 143 insertions(+)
 create mode 100644 generic/include/clc/math/modf.h
 create mode 100644 generic/include/clc/math/modf.inc
 create mode 100644 generic/lib/math/modf.cl

diff --git a/generic/include/clc/clc.h b/generic/include/clc/clc.h
index fea578a..bd2f67f 100644
--- a/generic/include/clc/clc.h
+++ b/generic/include/clc/clc.h
@@ -67,6 +67,7 @@
 #include <clc/math/log1p.h>
 #include <clc/math/log2.h>
 #include <clc/math/mad.h>
+#include <clc/math/modf.h>
 #include <clc/math/nextafter.h>
 #include <clc/math/pow.h>
 #include <clc/math/pown.h>
diff --git a/generic/include/clc/math/modf.h b/generic/include/clc/math/modf.h
new file mode 100644
index 0000000..f0fb6ca
--- /dev/null
+++ b/generic/include/clc/math/modf.h
@@ -0,0 +1,24 @@
+/*
+ * Copyright (c) 2014 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/math/modf.inc>
+#include <clc/math/gentype.inc>
diff --git a/generic/include/clc/math/modf.inc b/generic/include/clc/math/modf.inc
new file mode 100644
index 0000000..42bcf62
--- /dev/null
+++ b/generic/include/clc/math/modf.inc
@@ -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.
+ */
+
+_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE modf(__CLC_GENTYPE x, global __CLC_GENTYPE *iptr);
+_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE modf(__CLC_GENTYPE x, local __CLC_GENTYPE *iptr);
+_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE modf(__CLC_GENTYPE x, private __CLC_GENTYPE *iptr);
diff --git a/generic/lib/SOURCES b/generic/lib/SOURCES
index b186301..1daae8d 100644
--- a/generic/lib/SOURCES
+++ b/generic/lib/SOURCES
@@ -96,6 +96,7 @@ math/log10.cl
 math/log1p.cl
 math/log2.cl
 math/mad.cl
+math/modf.cl
 math/native_log.cl
 math/native_log2.cl
 math/tables.cl
diff --git a/generic/lib/math/modf.cl b/generic/lib/math/modf.cl
new file mode 100644
index 0000000..45a6995
--- /dev/null
+++ b/generic/lib/math/modf.cl
@@ -0,0 +1,92 @@
+/*
+ * Copyright (c) 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 "math.h"
+#include "../clcmacro.h"
+
+_CLC_OVERLOAD _CLC_DEF float modf(float x, float *iptr) {
+    int ux = as_int(x);
+    int e = ((ux >> 23) & 0xff) - 127;
+    int s = ux & 0x80000000;
+    int msk = 0xffffffff << (23 - e);
+    int i = msk & ux;
+    int r = as_uint(x - as_float(i)) | s;
+
+    r = e < 0 ? ux : r;
+    i = e < 0 ? s : i;
+
+    r = e >= 23 ? s : r;
+    i = e >= 23 ? ux : i;
+
+    r = (ux & 0x7fffffff) > 0x7f800000 ? ux : r;
+
+    *iptr = as_float(i);
+    return as_float(r);
+}
+_CLC_V_V_VP_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, modf, float, float);
+
+#define MODF_DEF(addrspace, type) \
+  _CLC_OVERLOAD _CLC_DEF type modf(type x, addrspace type *iptr) { \
+    type private_iptr; \
+    type ret = modf(x, &private_iptr); \
+    *iptr = private_iptr; \
+    return ret; \
+} \
+
+MODF_DEF(local, float);
+_CLC_V_V_VP_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, modf, float, local float);
+MODF_DEF(global, float);
+_CLC_V_V_VP_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, modf, float, global float);
+
+#ifdef cl_khr_fp64
+
+#pragma OPENCL EXTENSION cl_khr_fp64 : enable
+
+_CLC_OVERLOAD _CLC_DEF double modf(double x, double *iptr) {
+    long ux = as_long(x);
+    int e = ((int)(ux >> 52) & 0x7ff) - 1023;
+    long s = ux & 0x8000000000000000L;
+    long msk = 0xffffffffffffffffL << (52 - e);
+    long i = msk & ux;
+    long r = as_long(x - as_double(i));
+
+    r = e < 0 ? ux : r;
+    i = e < 0 ? s : i;
+
+    r = e >= 52 ? s : r;
+    i = e >= 52 ? ux : i;
+
+    r = (ux & 0x7fffffffffffffffL) > 0x7ff0000000000000L ? ux : r;
+
+    *iptr = as_double(i);
+    return as_double(r);
+}
+_CLC_V_V_VP_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, modf, double, double);
+
+MODF_DEF(local, double);
+_CLC_V_V_VP_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, modf, double, local double);
+MODF_DEF(global, double);
+_CLC_V_V_VP_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, modf, double, global double);
+
+#endif // cl_khr_fp64
-- 
2.5.0



More information about the Libclc-dev mailing list