diff options
author | Zachary Turner <zturner@google.com> | 2017-02-14 19:06:37 +0000 |
---|---|---|
committer | Zachary Turner <zturner@google.com> | 2017-02-14 19:06:37 +0000 |
commit | 8bd42a1a98377b717bfa3fae6efde4f72c18fea4 (patch) | |
tree | caf7175f59d9a144d2ac2423560a31086b1dcc84 /llvm/lib/Support/StringRef.cpp | |
parent | 01c3243fc1a20b5ddcb464a3183e5fd904d695a4 (diff) | |
download | bcm5719-llvm-8bd42a1a98377b717bfa3fae6efde4f72c18fea4.tar.gz bcm5719-llvm-8bd42a1a98377b717bfa3fae6efde4f72c18fea4.zip |
[Support] Add StringRef::getAsDouble.
Differential Revision: https://reviews.llvm.org/D29918
llvm-svn: 295089
Diffstat (limited to 'llvm/lib/Support/StringRef.cpp')
-rw-r--r-- | llvm/lib/Support/StringRef.cpp | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/llvm/lib/Support/StringRef.cpp b/llvm/lib/Support/StringRef.cpp index d81250e48dd..9b7cc1c1d18 100644 --- a/llvm/lib/Support/StringRef.cpp +++ b/llvm/lib/Support/StringRef.cpp @@ -8,6 +8,7 @@ //===----------------------------------------------------------------------===// #include "llvm/ADT/StringRef.h" +#include "llvm/ADT/APFloat.h" #include "llvm/ADT/APInt.h" #include "llvm/ADT/Hashing.h" #include "llvm/ADT/edit_distance.h" @@ -595,6 +596,18 @@ bool StringRef::getAsInteger(unsigned Radix, APInt &Result) const { return false; } +bool StringRef::getAsDouble(double &Result, bool AllowInexact) const { + APFloat F(0.0); + APFloat::opStatus Status = + F.convertFromString(*this, APFloat::rmNearestTiesToEven); + if (Status != APFloat::opOK) { + if (!AllowInexact || Status != APFloat::opInexact) + return true; + } + + Result = F.convertToDouble(); + return false; +} // Implementation of StringRef hashing. hash_code llvm::hash_value(StringRef S) { |