https://github.com/wenju-he created 
https://github.com/llvm/llvm-project/pull/135309

None

>From 31423376e35f34ca032fe3d11998537912ba2c63 Mon Sep 17 00:00:00 2001
From: Wenju He <wenju...@intel.com>
Date: Thu, 10 Apr 2025 20:10:41 -0700
Subject: [PATCH] [libclc] add ctz built-in implementation to clc and generic

---
 libclc/clc/include/clc/integer/clc_ctz.h  | 20 ++++++++++
 libclc/clc/lib/generic/SOURCES            |  1 +
 libclc/clc/lib/generic/integer/clc_ctz.cl | 48 +++++++++++++++++++++++
 libclc/generic/include/clc/clc.h          |  1 +
 libclc/generic/include/clc/integer/ctz.h  | 15 +++++++
 libclc/generic/lib/SOURCES                |  1 +
 libclc/generic/lib/integer/ctz.cl         | 15 +++++++
 7 files changed, 101 insertions(+)
 create mode 100644 libclc/clc/include/clc/integer/clc_ctz.h
 create mode 100644 libclc/clc/lib/generic/integer/clc_ctz.cl
 create mode 100644 libclc/generic/include/clc/integer/ctz.h
 create mode 100644 libclc/generic/lib/integer/ctz.cl

diff --git a/libclc/clc/include/clc/integer/clc_ctz.h 
b/libclc/clc/include/clc/integer/clc_ctz.h
new file mode 100644
index 0000000000000..1e6365100827b
--- /dev/null
+++ b/libclc/clc/include/clc/integer/clc_ctz.h
@@ -0,0 +1,20 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef __CLC_INTEGER_CLC_CTZ_H__
+#define __CLC_INTEGER_CLC_CTZ_H__
+
+#define __CLC_FUNCTION __clc_ctz
+#define __CLC_BODY <clc/shared/unary_decl.inc>
+
+#include <clc/integer/gentype.inc>
+
+#undef __CLC_BODY
+#undef __CLC_FUNCTION
+
+#endif // __CLC_INTEGER_CLC_CTZ_H__
diff --git a/libclc/clc/lib/generic/SOURCES b/libclc/clc/lib/generic/SOURCES
index 4503a20ad9848..1e73627c3a270 100644
--- a/libclc/clc/lib/generic/SOURCES
+++ b/libclc/clc/lib/generic/SOURCES
@@ -7,6 +7,7 @@ integer/clc_abs.cl
 integer/clc_abs_diff.cl
 integer/clc_add_sat.cl
 integer/clc_clz.cl
+integer/clc_ctz.cl
 integer/clc_hadd.cl
 integer/clc_mad24.cl
 integer/clc_mad_sat.cl
diff --git a/libclc/clc/lib/generic/integer/clc_ctz.cl 
b/libclc/clc/lib/generic/integer/clc_ctz.cl
new file mode 100644
index 0000000000000..50fda4a214b24
--- /dev/null
+++ b/libclc/clc/lib/generic/integer/clc_ctz.cl
@@ -0,0 +1,48 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include <clc/clcmacro.h>
+#include <clc/integer/clc_ctz.h>
+#include <clc/internal/clc.h>
+
+_CLC_OVERLOAD _CLC_DEF char __clc_ctz(char x) {
+  return __clc_ctz(__clc_as_uchar(x));
+}
+
+_CLC_OVERLOAD _CLC_DEF uchar __clc_ctz(uchar x) { return __builtin_ctzg(x, 8); 
}
+
+_CLC_OVERLOAD _CLC_DEF short __clc_ctz(short x) {
+  return __clc_ctz(__clc_as_ushort(x));
+}
+
+_CLC_OVERLOAD _CLC_DEF ushort __clc_ctz(ushort x) {
+  return __builtin_ctzg(x, 16);
+}
+
+_CLC_OVERLOAD _CLC_DEF int __clc_ctz(int x) {
+  return __clc_ctz(__clc_as_uint(x));
+}
+
+_CLC_OVERLOAD _CLC_DEF uint __clc_ctz(uint x) { return __builtin_ctzg(x, 32); }
+
+_CLC_OVERLOAD _CLC_DEF long __clc_ctz(long x) {
+  return __clc_ctz(__clc_as_ulong(x));
+}
+
+_CLC_OVERLOAD _CLC_DEF ulong __clc_ctz(ulong x) {
+  return __builtin_ctzg(x, 64);
+}
+
+_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, char, __clc_ctz, char)
+_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, uchar, __clc_ctz, uchar)
+_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, short, __clc_ctz, short)
+_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, ushort, __clc_ctz, ushort)
+_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, int, __clc_ctz, int)
+_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, uint, __clc_ctz, uint)
+_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, long, __clc_ctz, long)
+_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, ulong, __clc_ctz, ulong)
diff --git a/libclc/generic/include/clc/clc.h b/libclc/generic/include/clc/clc.h
index 51a4f3413e725..d950fa5b76cca 100644
--- a/libclc/generic/include/clc/clc.h
+++ b/libclc/generic/include/clc/clc.h
@@ -153,6 +153,7 @@
 #include <clc/integer/abs_diff.h>
 #include <clc/integer/add_sat.h>
 #include <clc/integer/clz.h>
+#include <clc/integer/ctz.h>
 #include <clc/integer/hadd.h>
 #include <clc/integer/mad24.h>
 #include <clc/integer/mad_hi.h>
diff --git a/libclc/generic/include/clc/integer/ctz.h 
b/libclc/generic/include/clc/integer/ctz.h
new file mode 100644
index 0000000000000..e8a91cd0ac1fc
--- /dev/null
+++ b/libclc/generic/include/clc/integer/ctz.h
@@ -0,0 +1,15 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#define __CLC_FUNCTION ctz
+#define __CLC_BODY <clc/shared/unary_decl.inc>
+
+#include <clc/integer/gentype.inc>
+
+#undef __CLC_BODY
+#undef __CLC_FUNCTION
diff --git a/libclc/generic/lib/SOURCES b/libclc/generic/lib/SOURCES
index 5f473ff8b9424..4726b38bdd541 100644
--- a/libclc/generic/lib/SOURCES
+++ b/libclc/generic/lib/SOURCES
@@ -66,6 +66,7 @@ integer/abs.cl
 integer/abs_diff.cl
 integer/add_sat.cl
 integer/clz.cl
+integer/ctz.cl
 integer/hadd.cl
 integer/mad24.cl
 integer/mad_hi.cl
diff --git a/libclc/generic/lib/integer/ctz.cl 
b/libclc/generic/lib/integer/ctz.cl
new file mode 100644
index 0000000000000..1bfb9877915ce
--- /dev/null
+++ b/libclc/generic/lib/integer/ctz.cl
@@ -0,0 +1,15 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include <clc/clc.h>
+#include <clc/integer/clc_ctz.h>
+
+#define FUNCTION ctz
+#define __CLC_BODY <clc/shared/unary_def.inc>
+
+#include <clc/integer/gentype.inc>

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to