Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(3494)

Issue 4738043: [PATCH] Add an APFloat::convertToInt(APSInt) function (Closed)

Can't Edit
Can't Publish+Mail
Start Review
Created:
12 years, 9 months ago by Jeffrey Yasskin
Modified:
12 years, 9 months ago
Reviewers:
llvm-commits, clattner
Visibility:
Public.

Description

Add an APFloat::convertToInt(APSInt) function that automatically manages the memory for the result. Used in http://codereview.appspot.com/4728042.

Patch Set 1 #

Unified diffs Side-by-side diffs Delta from patch set Stats (+69 lines, -0 lines) Patch
M include/llvm/ADT/APFloat.h View 2 chunks +2 lines, -0 lines 0 comments Download
M lib/Support/APFloat.cpp View 2 chunks +18 lines, -0 lines 0 comments Download
M unittests/ADT/APFloatTest.cpp View 2 chunks +49 lines, -0 lines 0 comments Download

Messages

Total messages: 2
Jeffrey Yasskin
Initial diff at http://codereview.appspot.com/download/issue4738043_1.diff.
12 years, 9 months ago (2011-07-15 00:05:44 UTC) #1
clattner_apple.com
12 years, 9 months ago (2011-07-15 00:52:33 UTC) #2
On Jul 14, 2011, at 5:05 PM, jyasskin@gmail.com wrote:

> Reviewers: llvm-commits_cs.uiuc.edu,
> 
> Message:
> Initial diff at
> http://codereview.appspot.com/download/issue4738043_1.diff.
> 
> Description:
> Add an APFloat::convertToInt(APSInt) function that automatically manages
> the memory for the result.

Looks reasonable, only comment is that you don't need to mark bitWidth/status as
'const':

Please commit,

-Chris

> +APFloat::convertToInteger(APSInt &result,
> +                          roundingMode rounding_mode, bool *isExact) const
> +{
> +  const unsigned bitWidth = result.getBitWidth();
> +  SmallVector<uint64_t, 4> parts(result.getNumWords());
> +  const opStatus status = convertToInteger(
> +    parts.data(), bitWidth, result.isSigned(), rounding_mode, isExact);
> +  // Keeps the original signed-ness.
> +  result = APInt(bitWidth, parts.size(), parts.data());
> +  return status;
> +}
> +
>  /* Convert an unsigned integer SRC to a floating point number,
>     rounding according to ROUNDING_MODE.  The sign of the floating
>     point number is not modified.  */
> Index: unittests/ADT/APFloatTest.cpp
> diff --git a/unittests/ADT/APFloatTest.cpp b/unittests/ADT/APFloatTest.cpp
> index  
>
5f05b861693326e3fb3513d48423d033e919ff7e..08ac2a05254a54fcc1077104ae6645288426d20c
 
> 100644
> --- a/unittests/ADT/APFloatTest.cpp
> +++ b/unittests/ADT/APFloatTest.cpp
> @@ -12,6 +12,7 @@
>  #include "llvm/Support/raw_ostream.h"
>  #include "gtest/gtest.h"
>  #include "llvm/ADT/APFloat.h"
> +#include "llvm/ADT/APSInt.h"
>  #include "llvm/ADT/SmallString.h"
>  #include "llvm/ADT/SmallVector.h"
> 
> @@ -344,6 +345,54 @@ TEST(APFloatTest, toString) {
>    ASSERT_EQ("8.731834E+2", convertToString(873.1834, 0, 0));
>  }
> 
> +TEST(APFloatTest, toInteger) {
> +  bool isExact = false;
> +  APSInt result(5, /*isUnsigned=*/true);
> +
> +  EXPECT_EQ(APFloat::opOK,
> +            APFloat(APFloat::IEEEdouble, "10")
> +            .convertToInteger(result, APFloat::rmTowardZero, &isExact));
> +  EXPECT_TRUE(isExact);
> +  EXPECT_EQ(APSInt(APInt(5, 10), true), result);
> +
> +  EXPECT_EQ(APFloat::opInvalidOp,
> +            APFloat(APFloat::IEEEdouble, "-10")
> +            .convertToInteger(result, APFloat::rmTowardZero, &isExact));
> +  EXPECT_FALSE(isExact);
> +  EXPECT_EQ(APSInt::getMinValue(5, true), result);
> +
> +  EXPECT_EQ(APFloat::opInvalidOp,
> +            APFloat(APFloat::IEEEdouble, "32")
> +            .convertToInteger(result, APFloat::rmTowardZero, &isExact));
> +  EXPECT_FALSE(isExact);
> +  EXPECT_EQ(APSInt::getMaxValue(5, true), result);
> +
> +  EXPECT_EQ(APFloat::opInexact,
> +            APFloat(APFloat::IEEEdouble, "7.9")
> +            .convertToInteger(result, APFloat::rmTowardZero, &isExact));
> +  EXPECT_FALSE(isExact);
> +  EXPECT_EQ(APSInt(APInt(5, 7), true), result);
> +
> +  result.setIsUnsigned(false);
> +  EXPECT_EQ(APFloat::opOK,
> +            APFloat(APFloat::IEEEdouble, "-10")
> +            .convertToInteger(result, APFloat::rmTowardZero, &isExact));
> +  EXPECT_TRUE(isExact);
> +  EXPECT_EQ(APSInt(APInt(5, -10, true), false), result);
> +
> +  EXPECT_EQ(APFloat::opInvalidOp,
> +            APFloat(APFloat::IEEEdouble, "-17")
> +            .convertToInteger(result, APFloat::rmTowardZero, &isExact));
> +  EXPECT_FALSE(isExact);
> +  EXPECT_EQ(APSInt::getMinValue(5, false), result);
> +
> +  EXPECT_EQ(APFloat::opInvalidOp,
> +            APFloat(APFloat::IEEEdouble, "16")
> +            .convertToInteger(result, APFloat::rmTowardZero, &isExact));
> +  EXPECT_FALSE(isExact);
> +  EXPECT_EQ(APSInt::getMaxValue(5, false), result);
> +}
> +
>  static APInt nanbits(const fltSemantics &Sem,
>                       bool SNaN, bool Negative, uint64_t fill) {
>    APInt apfill(64, fill);
> 
> 
> _______________________________________________
> llvm-commits mailing list
> llvm-commits@cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits

Sign in to reply to this message.

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b