aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/core/lib
diff options
context:
space:
mode:
authorGravatar A. Unique TensorFlower <nobody@tensorflow.org>2016-02-29 08:17:58 -0800
committerGravatar TensorFlower Gardener <gardener@tensorflow.org>2016-02-29 10:48:41 -0800
commit977ea14361f7bceeeda2046f3073ea888806be02 (patch)
treeaadc117fa50c0e0df9b1bf4c1ac7cf4a7066b03d /tensorflow/core/lib
parentdb828ab20399697bb97c218ca6c435ee59b2a029 (diff)
Added more streamlined interfaces for converting rendezvous ids
to/from strings and used these in the rendezvous code. Improves performance for ptb_word_lm slightly (saves several allocations and an sscanf per CPU <-> GPU transfer). Change: 115852277
Diffstat (limited to 'tensorflow/core/lib')
-rw-r--r--tensorflow/core/lib/strings/numbers.cc33
-rw-r--r--tensorflow/core/lib/strings/numbers.h11
-rw-r--r--tensorflow/core/lib/strings/numbers_test.cc17
3 files changed, 61 insertions, 0 deletions
diff --git a/tensorflow/core/lib/strings/numbers.cc b/tensorflow/core/lib/strings/numbers.cc
index 778545b44b..a60de543cc 100644
--- a/tensorflow/core/lib/strings/numbers.cc
+++ b/tensorflow/core/lib/strings/numbers.cc
@@ -15,6 +15,7 @@ limitations under the License.
#include "tensorflow/core/lib/strings/numbers.h"
+#include <ctype.h>
#include <float.h>
#include <stdio.h>
#include <stdlib.h>
@@ -237,6 +238,38 @@ bool StringToFp(const string& s, Fprint* fp) {
}
}
+StringPiece Uint64ToHexString(uint64 v, char* buf) {
+ static const char* hexdigits = "0123456789abcdef";
+ const int num_byte = 16;
+ buf[num_byte] = '\0';
+ for (int i = num_byte - 1; i >= 0; i--) {
+ buf[i] = hexdigits[v & 0xf];
+ v >>= 4;
+ }
+ return StringPiece(buf, num_byte);
+}
+
+bool HexStringToUint64(const StringPiece& s, uint64* result) {
+ uint64 v = 0;
+ if (s.empty()) {
+ return false;
+ }
+ for (int i = 0; i < s.size(); i++) {
+ char c = s[i];
+ if (c >= '0' && c <= '9') {
+ v = (v << 4) + (c - '0');
+ } else if (c >= 'a' && c <= 'f') {
+ v = (v << 4) + 10 + (c - 'a');
+ } else if (c >= 'A' && c <= 'F') {
+ v = (v << 4) + 10 + (c - 'A');
+ } else {
+ return false;
+ }
+ }
+ *result = v;
+ return true;
+}
+
string HumanReadableNumBytes(int64 num_bytes) {
if (num_bytes == kint64min) {
// Special case for number with not representable negation.
diff --git a/tensorflow/core/lib/strings/numbers.h b/tensorflow/core/lib/strings/numbers.h
index 4dd0bcdec4..02903547a7 100644
--- a/tensorflow/core/lib/strings/numbers.h
+++ b/tensorflow/core/lib/strings/numbers.h
@@ -18,6 +18,7 @@ limitations under the License.
#include <string>
+#include "tensorflow/core/lib/core/stringpiece.h"
#include "tensorflow/core/platform/types.h"
namespace tensorflow {
@@ -81,6 +82,16 @@ string FpToString(Fprint fp);
// returns false.
bool StringToFp(const string& s, Fprint* fp);
+// Convert a 64-bit fingerprint value to an ASCII representation that
+// is terminated by a '\0'.
+// Buf must point to an array of at least kFastToBufferSize characters
+StringPiece Uint64ToHexString(uint64 v, char* buf);
+
+// Attempt to parse a uint64 in the form encoded by FastUint64ToHexString. If
+// successful, stores the value in *v and returns true. Otherwise,
+// returns false.
+bool HexStringToUint64(const StringPiece& s, uint64* v);
+
// Convert strings to 32bit integer values.
// Leading and trailing spaces are allowed.
// Return false with overflow or invalid input.
diff --git a/tensorflow/core/lib/strings/numbers_test.cc b/tensorflow/core/lib/strings/numbers_test.cc
index e583ffedb2..67fec856a1 100644
--- a/tensorflow/core/lib/strings/numbers_test.cc
+++ b/tensorflow/core/lib/strings/numbers_test.cc
@@ -41,6 +41,23 @@ TEST(FpToString, Ints) {
EXPECT_FALSE(StringToFp("0000000000000000xyz", &dummy));
}
+TEST(Uint64ToHexString, Ints) {
+ for (int s = 0; s < 64; s++) {
+ for (int delta = -1; delta <= 1; delta++) {
+ uint64 fp = (1ull << s) + delta;
+ char buf[kFastToBufferSize];
+ StringPiece s = Uint64ToHexString(fp, buf);
+ uint64 fp2;
+ EXPECT_TRUE(HexStringToUint64(s, &fp2));
+ EXPECT_EQ(fp, fp2) << s;
+ }
+ }
+ uint64 dummy;
+ EXPECT_FALSE(HexStringToUint64("", &dummy));
+ EXPECT_FALSE(HexStringToUint64("xyz", &dummy));
+ EXPECT_FALSE(HexStringToUint64("0000000000000000xyz", &dummy));
+}
+
TEST(HumanReadableNumBytes, Bytes) {
EXPECT_EQ("0B", HumanReadableNumBytes(0));
EXPECT_EQ("4B", HumanReadableNumBytes(4));