[PATCH] D155400: [clang][Interp] Implement __builtin_fabs()

Timm Bäder via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Jul 16 04:43:52 PDT 2023


tbaeder created this revision.
tbaeder added reviewers: aaron.ballman, erichkeane, shafik, cor3ntin.
Herald added a project: All.
tbaeder requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D155400

Files:
  clang/lib/AST/Interp/Floating.h
  clang/lib/AST/Interp/InterpBuiltin.cpp
  clang/test/AST/Interp/builtin-functions.cpp


Index: clang/test/AST/Interp/builtin-functions.cpp
===================================================================
--- clang/test/AST/Interp/builtin-functions.cpp
+++ clang/test/AST/Interp/builtin-functions.cpp
@@ -129,3 +129,7 @@
   char classify_zero    [__builtin_fpclassify(-1, -1, -1, -1, +1, 0.0)];
   char classify_neg_zero[__builtin_fpclassify(-1, -1, -1, -1, +1, -0.0)];
 }
+
+namespace fabs {
+  static_assert(__builtin_fabs(-14.0) == 14.0, "");
+}
Index: clang/lib/AST/Interp/InterpBuiltin.cpp
===================================================================
--- clang/lib/AST/Interp/InterpBuiltin.cpp
+++ clang/lib/AST/Interp/InterpBuiltin.cpp
@@ -265,6 +265,20 @@
   return true;
 }
 
+// The C standard says "fabs raises no floating-point exceptions,
+// even if x is a signaling NaN. The returned value is independent of
+// the current rounding direction mode."  Therefore constant folding can
+// proceed without regard to the floating point settings.
+// Reference, WG14 N2478 F.10.4.3
+static bool interp__builtin_fabs(InterpState &S, CodePtr OpPC,
+                                 const InterpFrame *Frame,
+                                 const Function *Func) {
+  const Floating &Val = getParam<Floating>(Frame, 0);
+
+  S.Stk.push<Floating>(Floating::abs(Val));
+  return true;
+}
+
 bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F) {
   InterpFrame *Frame = S.Current;
   APValue Dummy;
@@ -358,6 +372,14 @@
       return Ret<PT_Sint32>(S, OpPC, Dummy);
     break;
 
+  case Builtin::BI__builtin_fabs:
+  case Builtin::BI__builtin_fabsf:
+  case Builtin::BI__builtin_fabsl:
+  case Builtin::BI__builtin_fabsf128:
+    if (interp__builtin_fabs(S, OpPC, Frame, F))
+      return Ret<PT_Float>(S, OpPC, Dummy);
+    break;
+
   default:
     return false;
   }
Index: clang/lib/AST/Interp/Floating.h
===================================================================
--- clang/lib/AST/Interp/Floating.h
+++ clang/lib/AST/Interp/Floating.h
@@ -149,6 +149,13 @@
     return bitcastFromMemory(Buff + sizeof(void *), *Sem);
   }
 
+  static Floating abs(const Floating &F) {
+    APFloat V = F.F;
+    if (V.isNegative())
+      V.changeSign();
+    return Floating(V);
+  }
+
   // -------
 
   static APFloat::opStatus add(const Floating &A, const Floating &B,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D155400.540785.patch
Type: text/x-patch
Size: 2314 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230716/8c06d057/attachment.bin>


More information about the cfe-commits mailing list