From 9f14ed1c60a1f6e8ab434a85d2ba9d82bc125cf4 Mon Sep 17 00:00:00 2001 From: David Blaikie Date: Wed, 25 Jul 2012 18:04:24 +0000 Subject: Don't add null characters to the end of the APFloat string buffer. Report/patch inspiration by Olaf Krzikalla. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@160744 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Support/APFloat.cpp | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) (limited to 'lib/Support/APFloat.cpp') diff --git a/lib/Support/APFloat.cpp b/lib/Support/APFloat.cpp index 409d4fb..9f5ca3e 100644 --- a/lib/Support/APFloat.cpp +++ b/lib/Support/APFloat.cpp @@ -3278,16 +3278,8 @@ APFloat::APFloat(double d) : exponent2(0), sign2(0) { } namespace { - static void append(SmallVectorImpl &Buffer, - unsigned N, const char *Str) { - unsigned Start = Buffer.size(); - Buffer.set_size(Start + N); - memcpy(&Buffer[Start], Str, N); - } - - template - void append(SmallVectorImpl &Buffer, const char (&Str)[N]) { - append(Buffer, N, Str); + void append(SmallVectorImpl &Buffer, StringRef Str) { + Buffer.append(Str.begin(), Str.end()); } /// Removes data from the given significand until it is no more -- cgit v1.1 From 7c626d30974c632ab500171ff185a24bcf2603bf Mon Sep 17 00:00:00 2001 From: Owen Anderson Date: Mon, 13 Aug 2012 23:32:49 +0000 Subject: 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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@161807 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Support/APFloat.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'lib/Support/APFloat.cpp') diff --git a/lib/Support/APFloat.cpp b/lib/Support/APFloat.cpp index 9f5ca3e..2139df5 100644 --- a/lib/Support/APFloat.cpp +++ b/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 -- cgit v1.1 From d7a85b17bdbb4cb3c3551e533d7b01984ad28a2f Mon Sep 17 00:00:00 2001 From: Owen Anderson Date: Tue, 14 Aug 2012 18:51:15 +0000 Subject: Fix the construction of the magic constant for roundToIntegral to be 64-bit safe. Fixes c-torture/execute/990826-0.c git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@161885 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Support/APFloat.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/Support/APFloat.cpp') diff --git a/lib/Support/APFloat.cpp b/lib/Support/APFloat.cpp index 2139df5..5ea75a6 100644 --- a/lib/Support/APFloat.cpp +++ b/lib/Support/APFloat.cpp @@ -1774,8 +1774,8 @@ APFloat::opStatus APFloat::roundToIntegral(roundingMode rounding_mode) { // 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)); + APInt IntegerConstant(NextPowerOf2(semanticsPrecision(*semantics)), 1); + IntegerConstant <<= semanticsPrecision(*semantics)-1; APFloat MagicConstant(*semantics); fs = MagicConstant.convertFromAPInt(IntegerConstant, false, rmNearestTiesToEven); -- cgit v1.1 From f7a5dfcb3b68b782543d25ba3151893111ff008f Mon Sep 17 00:00:00 2001 From: Owen Anderson Date: Wed, 15 Aug 2012 05:39:46 +0000 Subject: Fix a problem with APFloat::roundToIntegral where it would return incorrect results for negative inputs to trunc. Add unit tests to verify this behavior. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@161929 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Support/APFloat.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'lib/Support/APFloat.cpp') diff --git a/lib/Support/APFloat.cpp b/lib/Support/APFloat.cpp index 5ea75a6..84021de 100644 --- a/lib/Support/APFloat.cpp +++ b/lib/Support/APFloat.cpp @@ -1774,19 +1774,31 @@ APFloat::opStatus APFloat::roundToIntegral(roundingMode rounding_mode) { // 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. + // NOTE: When the input value is negative, we do subtractation followed by + // addition instead. APInt IntegerConstant(NextPowerOf2(semanticsPrecision(*semantics)), 1); IntegerConstant <<= semanticsPrecision(*semantics)-1; APFloat MagicConstant(*semantics); fs = MagicConstant.convertFromAPInt(IntegerConstant, false, rmNearestTiesToEven); + MagicConstant.copySign(*this); + if (fs != opOK) return fs; + // Preserve the input sign so that we can handle 0.0/-0.0 cases correctly. + bool inputSign = isNegative(); + fs = add(MagicConstant, rounding_mode); if (fs != opOK && fs != opInexact) return fs; fs = subtract(MagicConstant, rounding_mode); + + // Restore the input sign. + if (inputSign != isNegative()) + changeSign(); + return fs; } -- cgit v1.1 From 7c28978618cbbe472fe4415e56350a3461390658 Mon Sep 17 00:00:00 2001 From: Owen Anderson Date: Wed, 15 Aug 2012 16:42:53 +0000 Subject: Fix typo in comment. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@161956 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Support/APFloat.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/Support/APFloat.cpp') diff --git a/lib/Support/APFloat.cpp b/lib/Support/APFloat.cpp index 84021de..b42a168 100644 --- a/lib/Support/APFloat.cpp +++ b/lib/Support/APFloat.cpp @@ -1774,7 +1774,7 @@ APFloat::opStatus APFloat::roundToIntegral(roundingMode rounding_mode) { // 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. - // NOTE: When the input value is negative, we do subtractation followed by + // NOTE: When the input value is negative, we do subtraction followed by // addition instead. APInt IntegerConstant(NextPowerOf2(semanticsPrecision(*semantics)), 1); IntegerConstant <<= semanticsPrecision(*semantics)-1; -- cgit v1.1 From c82cc587a468d44e5cfabfa8f8a639667127845d Mon Sep 17 00:00:00 2001 From: Owen Anderson Date: Wed, 15 Aug 2012 18:28:45 +0000 Subject: Fix another roundToIntegral bug where very large values could become infinity. Problem and solution identified by Steve Canon. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@161969 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Support/APFloat.cpp | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'lib/Support/APFloat.cpp') diff --git a/lib/Support/APFloat.cpp b/lib/Support/APFloat.cpp index b42a168..ed261a4 100644 --- a/lib/Support/APFloat.cpp +++ b/lib/Support/APFloat.cpp @@ -1770,6 +1770,12 @@ APFloat::opStatus APFloat::roundToIntegral(roundingMode rounding_mode) { opStatus fs; assertArithmeticOK(*semantics); + // If the exponent is large enough, we know that this value is already + // integral, and the arithmetic below would potentially cause it to saturate + // to +/-Inf. Bail out early instead. + if (exponent+1 >= (int)semanticsPrecision(*semantics)) + return opOK; + // 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 -- cgit v1.1