[libc-commits] [PATCH] D136488: [libc.search] [PATCH 2/6] add ctz/clz for unsigned short

Schrodinger ZHU Yifan via Phabricator via libc-commits libc-commits at lists.llvm.org
Fri Oct 21 13:29:21 PDT 2022


SchrodingerZhu created this revision.
Herald added projects: libc-project, All.
Herald added a subscriber: libc-commits.
SchrodingerZhu requested review of this revision.

This patch adds support for calculating ctz/clz of unsigned short.
The idea is to use __builtin_clzs/__builtin_ctzs when available
or fallback to use unsigned int version with additional bit number
manipulations.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D136488

Files:
  libc/src/__support/builtin_wrappers.h


Index: libc/src/__support/builtin_wrappers.h
===================================================================
--- libc/src/__support/builtin_wrappers.h
+++ libc/src/__support/builtin_wrappers.h
@@ -26,6 +26,14 @@
 }
 
 template <typename T> static inline int clz(T val);
+template <> inline int clz<unsigned short>(unsigned short val) {
+#if __has_builtin(__builtin_clzs)
+  return __builtin_clzs(val);
+#else
+  return __builtin_clz(static_cast<unsigned int>(val)) -
+         8 * (sizeof<unsigned int> - sizeof<unsigned short>);
+#endif
+}
 template <> inline int clz<unsigned int>(unsigned int val) {
   return __builtin_clz(val);
 }
@@ -37,6 +45,13 @@
 }
 
 template <typename T> static inline int ctz(T val);
+template <> inline int ctz<unsigned short>(unsigned short val) {
+#if __has_builtin(__builtin_ctzs)
+  return __builtin_ctzs(val);
+#else
+  return __builtin_ctzs(static_cast<unsigned int>(val));
+#endif
+}
 template <> inline int ctz<unsigned int>(unsigned int val) {
   return __builtin_ctz(val);
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D136488.469746.patch
Type: text/x-patch
Size: 1020 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20221021/0f41cf9a/attachment.bin>


More information about the libc-commits mailing list