[clang] [clang][Interp] Implement __builtin_clrsb (PR #72243)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 14 03:43:21 PST 2023


https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/72243

None

>From 0a5cc373736f6c9e29f4f8af438d37dd007f09e2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Tue, 14 Nov 2023 12:42:03 +0100
Subject: [PATCH] [clang][Interp] Implement __builtin_clrsb

---
 clang/lib/AST/Interp/InterpBuiltin.cpp      | 16 ++++++++++++++++
 clang/test/AST/Interp/builtin-functions.cpp | 17 ++++++++++++++++-
 2 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/clang/lib/AST/Interp/InterpBuiltin.cpp b/clang/lib/AST/Interp/InterpBuiltin.cpp
index f26d298f5b60045..69383d37440f704 100644
--- a/clang/lib/AST/Interp/InterpBuiltin.cpp
+++ b/clang/lib/AST/Interp/InterpBuiltin.cpp
@@ -439,6 +439,15 @@ static bool interp__builtin_popcount(InterpState &S, CodePtr OpPC,
   return true;
 }
 
+static bool interp__builtin_clrsb(InterpState &S, CodePtr OpPC,
+                                  const InterpFrame *Frame,
+                                  const Function *Func, const CallExpr *Call) {
+  PrimType ArgT = *S.getContext().classify(Call->getArg(0)->getType());
+  APSInt Val = peekToAPSInt(S.Stk, ArgT);
+  pushInt(S, Val.getBitWidth() - Val.getSignificantBits());
+  return true;
+}
+
 bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F,
                       const CallExpr *Call) {
   InterpFrame *Frame = S.Current;
@@ -576,6 +585,13 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F,
       return retInt(S, OpPC, Dummy);
     break;
 
+  case Builtin::BI__builtin_clrsb:
+  case Builtin::BI__builtin_clrsbl:
+  case Builtin::BI__builtin_clrsbll:
+    if (interp__builtin_clrsb(S, OpPC, Frame, F, Call))
+      return retInt(S, OpPC, Dummy);
+    break;
+
   default:
     return false;
   }
diff --git a/clang/test/AST/Interp/builtin-functions.cpp b/clang/test/AST/Interp/builtin-functions.cpp
index a78a0fbdf11b1d7..245d775bd366d53 100644
--- a/clang/test/AST/Interp/builtin-functions.cpp
+++ b/clang/test/AST/Interp/builtin-functions.cpp
@@ -274,6 +274,7 @@ namespace SourceLocation {
   }
 }
 
+#define BITSIZE(x) (sizeof(x) * 8)
 namespace popcount {
   static_assert(__builtin_popcount(~0u) == __CHAR_BIT__ * sizeof(unsigned int), "");
   static_assert(__builtin_popcount(0) == 0, "");
@@ -283,7 +284,6 @@ namespace popcount {
   static_assert(__builtin_popcountll(0) == 0, "");
 
   /// From test/Sema/constant-builtins-2.c
-#define BITSIZE(x) (sizeof(x) * 8)
   char popcount1[__builtin_popcount(0) == 0 ? 1 : -1];
   char popcount2[__builtin_popcount(0xF0F0) == 8 ? 1 : -1];
   char popcount3[__builtin_popcount(~0) == BITSIZE(int) ? 1 : -1];
@@ -295,3 +295,18 @@ namespace popcount {
   char popcount9[__builtin_popcountll(0xF0F0LL) == 8 ? 1 : -1];
   char popcount10[__builtin_popcountll(~0LL) == BITSIZE(long long) ? 1 : -1];
 }
+
+namespace clrsb {
+  char clrsb1[__builtin_clrsb(0) == BITSIZE(int) - 1 ? 1 : -1];
+  char clrsb2[__builtin_clrsbl(0L) == BITSIZE(long) - 1 ? 1 : -1];
+  char clrsb3[__builtin_clrsbll(0LL) == BITSIZE(long long) - 1 ? 1 : -1];
+  char clrsb4[__builtin_clrsb(~0) == BITSIZE(int) - 1 ? 1 : -1];
+  char clrsb5[__builtin_clrsbl(~0L) == BITSIZE(long) - 1 ? 1 : -1];
+  char clrsb6[__builtin_clrsbll(~0LL) == BITSIZE(long long) - 1 ? 1 : -1];
+  char clrsb7[__builtin_clrsb(1) == BITSIZE(int) - 2 ? 1 : -1];
+  char clrsb8[__builtin_clrsb(~1) == BITSIZE(int) - 2 ? 1 : -1];
+  char clrsb9[__builtin_clrsb(1 << (BITSIZE(int) - 1)) == 0 ? 1 : -1];
+  char clrsb10[__builtin_clrsb(~(1 << (BITSIZE(int) - 1))) == 0 ? 1 : -1];
+  char clrsb11[__builtin_clrsb(0xf) == BITSIZE(int) - 5 ? 1 : -1];
+  char clrsb12[__builtin_clrsb(~0x1f) == BITSIZE(int) - 6 ? 1 : -1];
+}



More information about the cfe-commits mailing list