[clang] 1684406 - [clang][Interp] Implement __builtin_fabs()

Timm Bäder via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 1 07:32:47 PDT 2023


Author: Timm Bäder
Date: 2023-08-01T16:32:29+02:00
New Revision: 1684406e63b75d81707900c442a8c69c95640338

URL: https://github.com/llvm/llvm-project/commit/1684406e63b75d81707900c442a8c69c95640338
DIFF: https://github.com/llvm/llvm-project/commit/1684406e63b75d81707900c442a8c69c95640338.diff

LOG: [clang][Interp] Implement __builtin_fabs()

Differential Revision: https://reviews.llvm.org/D155400

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Interp/Floating.h b/clang/lib/AST/Interp/Floating.h
index 60e7ce2461aadc..b0fae4562b7830 100644
--- a/clang/lib/AST/Interp/Floating.h
+++ b/clang/lib/AST/Interp/Floating.h
@@ -119,6 +119,13 @@ class Floating final {
     return Status;
   }
 
+  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,

diff  --git a/clang/lib/AST/Interp/InterpBuiltin.cpp b/clang/lib/AST/Interp/InterpBuiltin.cpp
index 714fa9ee02ce13..0f269ae5a329df 100644
--- a/clang/lib/AST/Interp/InterpBuiltin.cpp
+++ b/clang/lib/AST/Interp/InterpBuiltin.cpp
@@ -266,6 +266,20 @@ static bool interp__builtin_fpclassify(InterpState &S, CodePtr OpPC,
   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;
@@ -359,6 +373,14 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F) {
       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;
   }

diff  --git a/clang/test/AST/Interp/builtin-functions.cpp b/clang/test/AST/Interp/builtin-functions.cpp
index e3fabecf8c9378..5b8b2dbf09ce5e 100644
--- a/clang/test/AST/Interp/builtin-functions.cpp
+++ b/clang/test/AST/Interp/builtin-functions.cpp
@@ -140,3 +140,7 @@ namespace fpclassify {
   char classify_neg_zero[__builtin_fpclassify(-1, -1, -1, -1, +1, -0.0)];
   char classify_subnorm [__builtin_fpclassify(-1, -1, -1, +1, -1, 1.0e-38f)];
 }
+
+namespace fabs {
+  static_assert(__builtin_fabs(-14.0) == 14.0, "");
+}


        


More information about the cfe-commits mailing list