summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Support
diff options
context:
space:
mode:
authorOwen Anderson <resistor@mac.com>2012-08-13 23:32:49 +0000
committerOwen Anderson <resistor@mac.com>2012-08-13 23:32:49 +0000
commita40319b7f1640612dda14f1f4f46bed913299775 (patch)
treedd52d1730b23379f0d77311062c18c177db7b21e /llvm/lib/Support
parent88fc73b8f7ff8187110cab0f3175bdf7a3a6c41b (diff)
downloadbcm5719-llvm-a40319b7f1640612dda14f1f4f46bed913299775.tar.gz
bcm5719-llvm-a40319b7f1640612dda14f1f4f46bed913299775.zip
Add a roundToIntegral method to APFloat, which can be parameterized over various rounding modes. Use this to implement SelectionDAG constant folding of FFLOOR, FCEIL, and FTRUNC.
llvm-svn: 161807
Diffstat (limited to 'llvm/lib/Support')
-rw-r--r--llvm/lib/Support/APFloat.cpp26
1 files changed, 26 insertions, 0 deletions
diff --git a/llvm/lib/Support/APFloat.cpp b/llvm/lib/Support/APFloat.cpp
index 9f5ca3e3d52..2139df56205 100644
--- a/llvm/lib/Support/APFloat.cpp
+++ b/llvm/lib/Support/APFloat.cpp
@@ -1765,6 +1765,32 @@ APFloat::fusedMultiplyAdd(const APFloat &multiplicand,
return fs;
}
+/* Rounding-mode corrrect round to integral value. */
+APFloat::opStatus APFloat::roundToIntegral(roundingMode rounding_mode) {
+ opStatus fs;
+ assertArithmeticOK(*semantics);
+
+ // The algorithm here is quite simple: we add 2^(p-1), where p is the
+ // precision of our format, and then subtract it back off again. The choice
+ // of rounding modes for the addition/subtraction determines the rounding mode
+ // for our integral rounding as well.
+ APInt IntegerConstant(NextPowerOf2(semanticsPrecision(*semantics)),
+ 1 << (semanticsPrecision(*semantics)-1));
+ APFloat MagicConstant(*semantics);
+ fs = MagicConstant.convertFromAPInt(IntegerConstant, false,
+ rmNearestTiesToEven);
+ if (fs != opOK)
+ return fs;
+
+ fs = add(MagicConstant, rounding_mode);
+ if (fs != opOK && fs != opInexact)
+ return fs;
+
+ fs = subtract(MagicConstant, rounding_mode);
+ return fs;
+}
+
+
/* Comparison requires normalized numbers. */
APFloat::cmpResult
APFloat::compare(const APFloat &rhs) const
OpenPOWER on IntegriCloud