[Libclc-dev] [PATCH 1/2] Add hadd builtin

Aaron Watry awatry at gmail.com
Wed Aug 14 09:38:17 PDT 2013


(x + y) >> 1 gets changed to:
(x>>1) + (y>>1) + (x&y&1)

Saves us having to do any llvm assembly and overflow checking in the addition.
---
 generic/include/clc/clc.h            | 1 +
 generic/include/clc/integer/hadd.h   | 2 ++
 generic/include/clc/integer/hadd.inc | 1 +
 generic/lib/SOURCES                  | 1 +
 generic/lib/integer/hadd.cl          | 4 ++++
 generic/lib/integer/hadd.inc         | 6 ++++++
 6 files changed, 15 insertions(+)
 create mode 100644 generic/include/clc/integer/hadd.h
 create mode 100644 generic/include/clc/integer/hadd.inc
 create mode 100644 generic/lib/integer/hadd.cl
 create mode 100644 generic/lib/integer/hadd.inc

diff --git a/generic/include/clc/clc.h b/generic/include/clc/clc.h
index bc873c3..f9b2c38 100644
--- a/generic/include/clc/clc.h
+++ b/generic/include/clc/clc.h
@@ -65,6 +65,7 @@
 #include <clc/integer/abs_diff.h>
 #include <clc/integer/add_sat.h>
 #include <clc/integer/clz.h>
+#include <clc/integer/hadd.h>
 #include <clc/integer/mad24.h>
 #include <clc/integer/mul24.h>
 #include <clc/integer/mul_hi.h>
diff --git a/generic/include/clc/integer/hadd.h b/generic/include/clc/integer/hadd.h
new file mode 100644
index 0000000..37304e2
--- /dev/null
+++ b/generic/include/clc/integer/hadd.h
@@ -0,0 +1,2 @@
+#define __CLC_BODY <clc/integer/hadd.inc>
+#include <clc/integer/gentype.inc>
diff --git a/generic/include/clc/integer/hadd.inc b/generic/include/clc/integer/hadd.inc
new file mode 100644
index 0000000..f698989
--- /dev/null
+++ b/generic/include/clc/integer/hadd.inc
@@ -0,0 +1 @@
+_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE hadd(__CLC_GENTYPE x, __CLC_GENTYPE y);
diff --git a/generic/lib/SOURCES b/generic/lib/SOURCES
index b8322f2..0e66d71 100644
--- a/generic/lib/SOURCES
+++ b/generic/lib/SOURCES
@@ -11,6 +11,7 @@ integer/add_sat_impl.ll
 integer/clz.cl
 integer/clz_if.ll
 integer/clz_impl.ll
+integer/hadd.cl
 integer/mad24.cl
 integer/mul24.cl
 integer/mul_hi.cl
diff --git a/generic/lib/integer/hadd.cl b/generic/lib/integer/hadd.cl
new file mode 100644
index 0000000..749026e
--- /dev/null
+++ b/generic/lib/integer/hadd.cl
@@ -0,0 +1,4 @@
+#include <clc/clc.h>
+
+#define __CLC_BODY <hadd.inc>
+#include <clc/integer/gentype.inc>
diff --git a/generic/lib/integer/hadd.inc b/generic/lib/integer/hadd.inc
new file mode 100644
index 0000000..ea59d9b
--- /dev/null
+++ b/generic/lib/integer/hadd.inc
@@ -0,0 +1,6 @@
+//hadd = (x+y)>>1
+//This can be simplified to x>>1 + y>>1 + (1 if both x and y have the 1s bit set)
+//This saves us having to do any checks for overflow in the addition sum
+_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE hadd(__CLC_GENTYPE x, __CLC_GENTYPE y) {
+    return (x>>(__CLC_GENTYPE)1)+(y>>(__CLC_GENTYPE)1)+(x&y&(__CLC_GENTYPE)1);
+}
-- 
1.8.1.2





More information about the Libclc-dev mailing list