Move numbers library from lib/strings to platform
PiperOrigin-RevId: 265196042
This commit is contained in:
parent
e8384fb1f7
commit
7bc83f2744
@ -30,7 +30,6 @@ tensorflow/core/lib/random/distribution_sampler.cc
|
||||
tensorflow/core/lib/random/random.cc
|
||||
tensorflow/core/lib/random/simple_philox.cc
|
||||
tensorflow/core/lib/random/weighted_picker.cc
|
||||
tensorflow/core/lib/strings/numbers.cc
|
||||
tensorflow/core/lib/strings/ordered_code.cc
|
||||
tensorflow/core/lib/strings/proto_text_util.cc
|
||||
tensorflow/core/lib/strings/strcat.cc
|
||||
@ -43,6 +42,7 @@ tensorflow/core/platform/denormal.cc
|
||||
tensorflow/core/platform/env.cc
|
||||
tensorflow/core/platform/file_system.cc
|
||||
tensorflow/core/platform/file_system_helper.cc
|
||||
tensorflow/core/platform/numbers.cc
|
||||
tensorflow/core/platform/posix/env.cc
|
||||
tensorflow/core/platform/posix/env_time.cc
|
||||
tensorflow/core/platform/posix/error.cc
|
||||
|
@ -2492,6 +2492,7 @@ cc_library(
|
||||
"//tensorflow/core/platform:abi",
|
||||
"//tensorflow/core/platform:annotation",
|
||||
"//tensorflow/core/platform:cpu_info",
|
||||
"//tensorflow/core/platform:numbers",
|
||||
"//tensorflow/core/platform:platform_strings",
|
||||
"//tensorflow/core/platform:scanner",
|
||||
"//tensorflow/core/platform:stringprintf",
|
||||
|
@ -30,7 +30,6 @@ cc_library(
|
||||
cc_library(
|
||||
name = "string_utils",
|
||||
srcs = [
|
||||
"numbers.cc",
|
||||
"strcat.cc",
|
||||
],
|
||||
hdrs = [
|
||||
@ -42,13 +41,13 @@ cc_library(
|
||||
"//tensorflow/core/lib/gtl:stl_util",
|
||||
"//tensorflow/core/platform:logging",
|
||||
"//tensorflow/core/platform:macros",
|
||||
"//tensorflow/core/platform:numbers",
|
||||
"//tensorflow/core/platform:str_util",
|
||||
"//tensorflow/core/platform:stringpiece",
|
||||
"//tensorflow/core/platform:stringprintf",
|
||||
"//tensorflow/core/platform:types",
|
||||
"@com_google_absl//absl/base:core_headers",
|
||||
"@com_google_absl//absl/strings",
|
||||
"@double_conversion//:double-conversion",
|
||||
],
|
||||
)
|
||||
|
||||
@ -78,7 +77,6 @@ filegroup(
|
||||
name = "legacy_lib_strings_all_srcs",
|
||||
srcs = [
|
||||
"base64.cc",
|
||||
"numbers.cc",
|
||||
"ordered_code.cc",
|
||||
"proto_serialization.cc",
|
||||
"proto_text_util.cc",
|
||||
@ -91,7 +89,6 @@ filegroup(
|
||||
name = "legacy_lib_strings_all_tests",
|
||||
srcs = [
|
||||
"base64_test.cc",
|
||||
"numbers_test.cc",
|
||||
"ordered_code_test.cc",
|
||||
"proto_serialization_test.cc",
|
||||
"strcat_test.cc",
|
||||
@ -145,7 +142,6 @@ filegroup(
|
||||
name = "legacy_low_level_library_tests",
|
||||
srcs = [
|
||||
"base64_test.cc",
|
||||
"numbers_test.cc",
|
||||
"strcat_test.cc",
|
||||
],
|
||||
visibility = ["//tensorflow/core:__pkg__"],
|
||||
|
@ -16,164 +16,6 @@ limitations under the License.
|
||||
#ifndef TENSORFLOW_CORE_LIB_STRINGS_NUMBERS_H_
|
||||
#define TENSORFLOW_CORE_LIB_STRINGS_NUMBERS_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "tensorflow/core/platform/stringpiece.h"
|
||||
#include "tensorflow/core/platform/types.h"
|
||||
|
||||
namespace tensorflow {
|
||||
namespace strings {
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// FastIntToBufferLeft()
|
||||
// These are intended for speed.
|
||||
//
|
||||
// All functions take the output buffer as an arg. FastInt() uses
|
||||
// at most 22 bytes, FastTime() uses exactly 30 bytes. They all
|
||||
// return a pointer to the beginning of the output, which is the same as
|
||||
// the beginning of the input buffer.
|
||||
//
|
||||
// NOTE: In 64-bit land, sizeof(time_t) is 8, so it is possible
|
||||
// to pass to FastTimeToBuffer() a time whose year cannot be
|
||||
// represented in 4 digits. In this case, the output buffer
|
||||
// will contain the string "Invalid:<value>"
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
// Previously documented minimums -- the buffers provided must be at least this
|
||||
// long, though these numbers are subject to change:
|
||||
// Int32, UInt32: 12 bytes
|
||||
// Int64, UInt64, Int, Uint: 22 bytes
|
||||
// Time: 30 bytes
|
||||
// Use kFastToBufferSize rather than hardcoding constants.
|
||||
static const int kFastToBufferSize = 32;
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// FastInt32ToBufferLeft()
|
||||
// FastUInt32ToBufferLeft()
|
||||
// FastInt64ToBufferLeft()
|
||||
// FastUInt64ToBufferLeft()
|
||||
//
|
||||
// These functions convert their numeric argument to an ASCII
|
||||
// representation of the numeric value in base 10, with the
|
||||
// representation being left-aligned in the buffer. The caller is
|
||||
// responsible for ensuring that the buffer has enough space to hold
|
||||
// the output. The buffer should typically be at least kFastToBufferSize
|
||||
// bytes.
|
||||
//
|
||||
// Returns the number of characters written.
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
size_t FastInt32ToBufferLeft(int32 i, char* buffer); // at least 12 bytes
|
||||
size_t FastUInt32ToBufferLeft(uint32 i, char* buffer); // at least 12 bytes
|
||||
size_t FastInt64ToBufferLeft(int64 i, char* buffer); // at least 22 bytes
|
||||
size_t FastUInt64ToBufferLeft(uint64 i, char* buffer); // at least 22 bytes
|
||||
|
||||
// Required buffer size for DoubleToBuffer is kFastToBufferSize.
|
||||
// Required buffer size for FloatToBuffer is kFastToBufferSize.
|
||||
size_t DoubleToBuffer(double value, char* buffer);
|
||||
size_t FloatToBuffer(float value, char* buffer);
|
||||
|
||||
// Convert a 64-bit fingerprint value to an ASCII representation.
|
||||
string FpToString(Fprint fp);
|
||||
|
||||
// Attempt to parse a fingerprint in the form encoded by FpToString. If
|
||||
// successful, stores the fingerprint in *fp and returns true. Otherwise,
|
||||
// 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.
|
||||
bool safe_strto32(StringPiece str, int32* value);
|
||||
|
||||
// Convert strings to unsigned 32bit integer values.
|
||||
// Leading and trailing spaces are allowed.
|
||||
// Return false with overflow or invalid input.
|
||||
bool safe_strtou32(StringPiece str, uint32* value);
|
||||
|
||||
// Convert strings to 64bit integer values.
|
||||
// Leading and trailing spaces are allowed.
|
||||
// Return false with overflow or invalid input.
|
||||
bool safe_strto64(StringPiece str, int64* value);
|
||||
|
||||
// Convert strings to unsigned 64bit integer values.
|
||||
// Leading and trailing spaces are allowed.
|
||||
// Return false with overflow or invalid input.
|
||||
bool safe_strtou64(StringPiece str, uint64* value);
|
||||
|
||||
// Convert strings to floating point values.
|
||||
// Leading and trailing spaces are allowed.
|
||||
// Values may be rounded on over- and underflow.
|
||||
// Returns false on invalid input or if `strlen(value) >= kFastToBufferSize`.
|
||||
bool safe_strtof(StringPiece str, float* value);
|
||||
|
||||
// Convert strings to double precision floating point values.
|
||||
// Leading and trailing spaces are allowed.
|
||||
// Values may be rounded on over- and underflow.
|
||||
// Returns false on invalid input or if `strlen(value) >= kFastToBufferSize`.
|
||||
bool safe_strtod(StringPiece str, double* value);
|
||||
|
||||
inline bool ProtoParseNumeric(StringPiece s, int32* value) {
|
||||
return safe_strto32(s, value);
|
||||
}
|
||||
|
||||
inline bool ProtoParseNumeric(StringPiece s, uint32* value) {
|
||||
return safe_strtou32(s, value);
|
||||
}
|
||||
|
||||
inline bool ProtoParseNumeric(StringPiece s, int64* value) {
|
||||
return safe_strto64(s, value);
|
||||
}
|
||||
|
||||
inline bool ProtoParseNumeric(StringPiece s, uint64* value) {
|
||||
return safe_strtou64(s, value);
|
||||
}
|
||||
|
||||
inline bool ProtoParseNumeric(StringPiece s, float* value) {
|
||||
return safe_strtof(s, value);
|
||||
}
|
||||
|
||||
inline bool ProtoParseNumeric(StringPiece s, double* value) {
|
||||
return safe_strtod(s, value);
|
||||
}
|
||||
|
||||
// Convert strings to number of type T.
|
||||
// Leading and trailing spaces are allowed.
|
||||
// Values may be rounded on over- and underflow.
|
||||
template <typename T>
|
||||
bool SafeStringToNumeric(StringPiece s, T* value) {
|
||||
return ProtoParseNumeric(s, value);
|
||||
}
|
||||
|
||||
// Converts from an int64 to a human readable string representing the
|
||||
// same number, using decimal powers. e.g. 1200000 -> "1.20M".
|
||||
string HumanReadableNum(int64 value);
|
||||
|
||||
// Converts from an int64 representing a number of bytes to a
|
||||
// human readable string representing the same number.
|
||||
// e.g. 12345678 -> "11.77MiB".
|
||||
string HumanReadableNumBytes(int64 num_bytes);
|
||||
|
||||
// Converts a time interval as double to a human readable
|
||||
// string. For example:
|
||||
// 0.001 -> "1 ms"
|
||||
// 10.0 -> "10 s"
|
||||
// 933120.0 -> "10.8 days"
|
||||
// 39420000.0 -> "1.25 years"
|
||||
// -10 -> "-10 s"
|
||||
string HumanReadableElapsedTime(double seconds);
|
||||
|
||||
} // namespace strings
|
||||
} // namespace tensorflow
|
||||
#include "tensorflow/core/platform/numbers.h"
|
||||
|
||||
#endif // TENSORFLOW_CORE_LIB_STRINGS_NUMBERS_H_
|
||||
|
@ -140,6 +140,21 @@ cc_library(
|
||||
hdrs = ["macros.h"],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "numbers",
|
||||
srcs = ["numbers.cc"],
|
||||
hdrs = ["numbers.h"],
|
||||
deps = [
|
||||
":logging",
|
||||
":macros",
|
||||
":str_util",
|
||||
":stringpiece",
|
||||
":stringprintf",
|
||||
":types",
|
||||
"@double_conversion//:double-conversion",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "rocm_rocdl_path",
|
||||
srcs = ["rocm_rocdl_path.cc"] + tf_additional_rocdl_srcs(),
|
||||
@ -317,6 +332,7 @@ filegroup(
|
||||
"**/rocm_rocdl_path.cc",
|
||||
"abi.cc",
|
||||
"cpu_info.cc",
|
||||
"numbers.cc",
|
||||
"platform_strings.cc",
|
||||
"protobuf.cc",
|
||||
"scanner.cc",
|
||||
@ -414,6 +430,7 @@ filegroup(
|
||||
"abi.cc",
|
||||
"annotation.cc",
|
||||
"cpu_info.cc",
|
||||
"numbers.cc",
|
||||
"platform_strings.cc",
|
||||
"protobuf.cc",
|
||||
"scanner.cc",
|
||||
|
@ -12,7 +12,7 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
==============================================================================*/
|
||||
|
||||
#include "tensorflow/core/lib/strings/numbers.h"
|
||||
#include "tensorflow/core/platform/numbers.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <float.h>
|
||||
@ -26,7 +26,7 @@ limitations under the License.
|
||||
#include <unordered_map>
|
||||
|
||||
#include "double-conversion/double-conversion.h"
|
||||
#include "tensorflow/core/lib/strings/str_util.h"
|
||||
#include "tensorflow/core/platform/str_util.h"
|
||||
#include "tensorflow/core/platform/logging.h"
|
||||
#include "tensorflow/core/platform/macros.h"
|
||||
#include "tensorflow/core/platform/stringprintf.h"
|
179
tensorflow/core/platform/numbers.h
Normal file
179
tensorflow/core/platform/numbers.h
Normal file
@ -0,0 +1,179 @@
|
||||
/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
==============================================================================*/
|
||||
|
||||
#ifndef TENSORFLOW_CORE_PLATFORM_NUMBERS_H_
|
||||
#define TENSORFLOW_CORE_PLATFORM_NUMBERS_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "tensorflow/core/platform/stringpiece.h"
|
||||
#include "tensorflow/core/platform/types.h"
|
||||
|
||||
namespace tensorflow {
|
||||
namespace strings {
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// FastIntToBufferLeft()
|
||||
// These are intended for speed.
|
||||
//
|
||||
// All functions take the output buffer as an arg. FastInt() uses
|
||||
// at most 22 bytes, FastTime() uses exactly 30 bytes. They all
|
||||
// return a pointer to the beginning of the output, which is the same as
|
||||
// the beginning of the input buffer.
|
||||
//
|
||||
// NOTE: In 64-bit land, sizeof(time_t) is 8, so it is possible
|
||||
// to pass to FastTimeToBuffer() a time whose year cannot be
|
||||
// represented in 4 digits. In this case, the output buffer
|
||||
// will contain the string "Invalid:<value>"
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
// Previously documented minimums -- the buffers provided must be at least this
|
||||
// long, though these numbers are subject to change:
|
||||
// Int32, UInt32: 12 bytes
|
||||
// Int64, UInt64, Int, Uint: 22 bytes
|
||||
// Time: 30 bytes
|
||||
// Use kFastToBufferSize rather than hardcoding constants.
|
||||
static const int kFastToBufferSize = 32;
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// FastInt32ToBufferLeft()
|
||||
// FastUInt32ToBufferLeft()
|
||||
// FastInt64ToBufferLeft()
|
||||
// FastUInt64ToBufferLeft()
|
||||
//
|
||||
// These functions convert their numeric argument to an ASCII
|
||||
// representation of the numeric value in base 10, with the
|
||||
// representation being left-aligned in the buffer. The caller is
|
||||
// responsible for ensuring that the buffer has enough space to hold
|
||||
// the output. The buffer should typically be at least kFastToBufferSize
|
||||
// bytes.
|
||||
//
|
||||
// Returns the number of characters written.
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
size_t FastInt32ToBufferLeft(int32 i, char* buffer); // at least 12 bytes
|
||||
size_t FastUInt32ToBufferLeft(uint32 i, char* buffer); // at least 12 bytes
|
||||
size_t FastInt64ToBufferLeft(int64 i, char* buffer); // at least 22 bytes
|
||||
size_t FastUInt64ToBufferLeft(uint64 i, char* buffer); // at least 22 bytes
|
||||
|
||||
// Required buffer size for DoubleToBuffer is kFastToBufferSize.
|
||||
// Required buffer size for FloatToBuffer is kFastToBufferSize.
|
||||
size_t DoubleToBuffer(double value, char* buffer);
|
||||
size_t FloatToBuffer(float value, char* buffer);
|
||||
|
||||
// Convert a 64-bit fingerprint value to an ASCII representation.
|
||||
string FpToString(Fprint fp);
|
||||
|
||||
// Attempt to parse a fingerprint in the form encoded by FpToString. If
|
||||
// successful, stores the fingerprint in *fp and returns true. Otherwise,
|
||||
// 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.
|
||||
bool safe_strto32(StringPiece str, int32* value);
|
||||
|
||||
// Convert strings to unsigned 32bit integer values.
|
||||
// Leading and trailing spaces are allowed.
|
||||
// Return false with overflow or invalid input.
|
||||
bool safe_strtou32(StringPiece str, uint32* value);
|
||||
|
||||
// Convert strings to 64bit integer values.
|
||||
// Leading and trailing spaces are allowed.
|
||||
// Return false with overflow or invalid input.
|
||||
bool safe_strto64(StringPiece str, int64* value);
|
||||
|
||||
// Convert strings to unsigned 64bit integer values.
|
||||
// Leading and trailing spaces are allowed.
|
||||
// Return false with overflow or invalid input.
|
||||
bool safe_strtou64(StringPiece str, uint64* value);
|
||||
|
||||
// Convert strings to floating point values.
|
||||
// Leading and trailing spaces are allowed.
|
||||
// Values may be rounded on over- and underflow.
|
||||
// Returns false on invalid input or if `strlen(value) >= kFastToBufferSize`.
|
||||
bool safe_strtof(StringPiece str, float* value);
|
||||
|
||||
// Convert strings to double precision floating point values.
|
||||
// Leading and trailing spaces are allowed.
|
||||
// Values may be rounded on over- and underflow.
|
||||
// Returns false on invalid input or if `strlen(value) >= kFastToBufferSize`.
|
||||
bool safe_strtod(StringPiece str, double* value);
|
||||
|
||||
inline bool ProtoParseNumeric(StringPiece s, int32* value) {
|
||||
return safe_strto32(s, value);
|
||||
}
|
||||
|
||||
inline bool ProtoParseNumeric(StringPiece s, uint32* value) {
|
||||
return safe_strtou32(s, value);
|
||||
}
|
||||
|
||||
inline bool ProtoParseNumeric(StringPiece s, int64* value) {
|
||||
return safe_strto64(s, value);
|
||||
}
|
||||
|
||||
inline bool ProtoParseNumeric(StringPiece s, uint64* value) {
|
||||
return safe_strtou64(s, value);
|
||||
}
|
||||
|
||||
inline bool ProtoParseNumeric(StringPiece s, float* value) {
|
||||
return safe_strtof(s, value);
|
||||
}
|
||||
|
||||
inline bool ProtoParseNumeric(StringPiece s, double* value) {
|
||||
return safe_strtod(s, value);
|
||||
}
|
||||
|
||||
// Convert strings to number of type T.
|
||||
// Leading and trailing spaces are allowed.
|
||||
// Values may be rounded on over- and underflow.
|
||||
template <typename T>
|
||||
bool SafeStringToNumeric(StringPiece s, T* value) {
|
||||
return ProtoParseNumeric(s, value);
|
||||
}
|
||||
|
||||
// Converts from an int64 to a human readable string representing the
|
||||
// same number, using decimal powers. e.g. 1200000 -> "1.20M".
|
||||
string HumanReadableNum(int64 value);
|
||||
|
||||
// Converts from an int64 representing a number of bytes to a
|
||||
// human readable string representing the same number.
|
||||
// e.g. 12345678 -> "11.77MiB".
|
||||
string HumanReadableNumBytes(int64 num_bytes);
|
||||
|
||||
// Converts a time interval as double to a human readable
|
||||
// string. For example:
|
||||
// 0.001 -> "1 ms"
|
||||
// 10.0 -> "10 s"
|
||||
// 933120.0 -> "10.8 days"
|
||||
// 39420000.0 -> "1.25 years"
|
||||
// -10 -> "-10 s"
|
||||
string HumanReadableElapsedTime(double seconds);
|
||||
|
||||
} // namespace strings
|
||||
} // namespace tensorflow
|
||||
|
||||
#endif // TENSORFLOW_CORE_PLATFORM_NUMBERS_H_
|
@ -13,7 +13,7 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
==============================================================================*/
|
||||
|
||||
#include "tensorflow/core/lib/strings/numbers.h"
|
||||
#include "tensorflow/core/platform/numbers.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <string>
|
Loading…
Reference in New Issue
Block a user