[clang] [clang] Implement __builtin_stdc_rotate_left, __builtin_stdc_rotate_right (PR #160259)

Erich Keane via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 2 07:21:01 PDT 2025


================
@@ -14166,7 +14160,48 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
         !EvaluateInteger(E->getArg(1), Amt, Info))
       return false;
 
-    return Success(Val.rotr(Amt.urem(Val.getBitWidth())), E);
+    // Normalize shift amount to [0, BitWidth) range to match runtime behavior
+    unsigned BitWidth = Val.getBitWidth();
+    unsigned AmtBitWidth = Amt.getBitWidth();
+    APSInt Divisor;
+    if (AmtBitWidth > BitWidth) {
+      Divisor = APSInt(llvm::APInt(AmtBitWidth, BitWidth), Amt.isUnsigned());
+    } else {
+      Divisor = APSInt(llvm::APInt(BitWidth, BitWidth), Amt.isUnsigned());
+      Amt = Amt.extend(BitWidth);
+    }
+
+    Amt = Amt % Divisor;
+    if (Amt.isNegative()) {
+      Amt += Divisor;
+    }
+
+    // Determine rotation direction
+    bool IsRotateRight;
+    switch (BuiltinOp) {
----------------
erichkeane wrote:

Instead of this rigamarole, just return out of the switch.

https://github.com/llvm/llvm-project/pull/160259


More information about the cfe-commits mailing list