[PATCH] D75236: [APFloat] Overload unary operator-

Jay Foad via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 27 05:01:39 PST 2020


foad updated this revision to Diff 246916.
foad added a comment.

Test neg(A) as well as -A.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D75236/new/

https://reviews.llvm.org/D75236

Files:
  llvm/include/llvm/ADT/APFloat.h
  llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
  llvm/lib/IR/ConstantFold.cpp
  llvm/unittests/ADT/APFloatTest.cpp


Index: llvm/unittests/ADT/APFloatTest.cpp
===================================================================
--- llvm/unittests/ADT/APFloatTest.cpp
+++ llvm/unittests/ADT/APFloatTest.cpp
@@ -2990,6 +2990,17 @@
   EXPECT_TRUE(Inf.bitwiseIsEqual(neg(NegInf)));
   EXPECT_TRUE(NegQNaN.bitwiseIsEqual(neg(QNaN)));
   EXPECT_TRUE(QNaN.bitwiseIsEqual(neg(NegQNaN)));
+
+  EXPECT_TRUE(NegOne.bitwiseIsEqual(-One));
+  EXPECT_TRUE(One.bitwiseIsEqual(-NegOne));
+  EXPECT_TRUE(NegZero.bitwiseIsEqual(-Zero));
+  EXPECT_TRUE(Zero.bitwiseIsEqual(-NegZero));
+  EXPECT_TRUE(NegInf.bitwiseIsEqual(-Inf));
+  EXPECT_TRUE(Inf.bitwiseIsEqual(-NegInf));
+  EXPECT_TRUE(NegInf.bitwiseIsEqual(-Inf));
+  EXPECT_TRUE(Inf.bitwiseIsEqual(-NegInf));
+  EXPECT_TRUE(NegQNaN.bitwiseIsEqual(-QNaN));
+  EXPECT_TRUE(QNaN.bitwiseIsEqual(-NegQNaN));
 }
 
 TEST(APFloatTest, ilogb) {
Index: llvm/lib/IR/ConstantFold.cpp
===================================================================
--- llvm/lib/IR/ConstantFold.cpp
+++ llvm/lib/IR/ConstantFold.cpp
@@ -986,7 +986,7 @@
     default:
       break;
     case Instruction::FNeg:
-      return ConstantFP::get(C->getContext(), neg(CV));
+      return ConstantFP::get(C->getContext(), -CV);
     }
   } else if (VectorType *VTy = dyn_cast<VectorType>(C->getType())) {
     // Do not iterate on scalable vector. The number of elements is unknown at
Index: llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
===================================================================
--- llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
+++ llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
@@ -5579,7 +5579,7 @@
     // Don't invert constant FP values after legalization unless the target says
     // the negated constant is legal.
     if (isOperationLegal(ISD::ConstantFP, VT) ||
-        isFPImmLegal(neg(cast<ConstantFPSDNode>(Op)->getValueAPF()), VT,
+        isFPImmLegal(-cast<ConstantFPSDNode>(Op)->getValueAPF(), VT,
                      ForCodeSize))
       return NegatibleCost::Neutral;
     break;
@@ -5597,7 +5597,7 @@
       return NegatibleCost::Neutral;
     if (llvm::all_of(Op->op_values(), [&](SDValue N) {
           return N.isUndef() ||
-                 isFPImmLegal(neg(cast<ConstantFPSDNode>(N)->getValueAPF()), VT,
+                 isFPImmLegal(-cast<ConstantFPSDNode>(N)->getValueAPF(), VT,
                               ForCodeSize);
         }))
       return NegatibleCost::Neutral;
Index: llvm/include/llvm/ADT/APFloat.h
===================================================================
--- llvm/include/llvm/ADT/APFloat.h
+++ llvm/include/llvm/ADT/APFloat.h
@@ -1242,6 +1242,12 @@
   return X;
 }
 
+/// Returns the negated value of the argument.
+inline APFloat operator-(APFloat X) {
+  X.changeSign();
+  return X;
+}
+
 /// Implements IEEE minNum semantics. Returns the smaller of the 2 arguments if
 /// both are not NaN. If either argument is a NaN, returns the other argument.
 LLVM_READONLY


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D75236.246916.patch
Type: text/x-patch
Size: 2947 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200227/f53b05ae/attachment.bin>


More information about the llvm-commits mailing list