1 | /* Copyright 2015 The TensorFlow Authors. All Rights Reserved. |
2 | |
3 | Licensed under the Apache License, Version 2.0 (the "License"); |
4 | you may not use this file except in compliance with the License. |
5 | You may obtain a copy of the License at |
6 | |
7 | http://www.apache.org/licenses/LICENSE-2.0 |
8 | |
9 | Unless required by applicable law or agreed to in writing, software |
10 | distributed under the License is distributed on an "AS IS" BASIS, |
11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | See the License for the specific language governing permissions and |
13 | limitations under the License. |
14 | ==============================================================================*/ |
15 | |
16 | #ifndef TENSORFLOW_TSL_PLATFORM_NUMBERS_H_ |
17 | #define TENSORFLOW_TSL_PLATFORM_NUMBERS_H_ |
18 | |
19 | #include <cstdint> |
20 | #include <string> |
21 | |
22 | #include "tensorflow/tsl/platform/stringpiece.h" |
23 | #include "tensorflow/tsl/platform/types.h" |
24 | |
25 | namespace tsl { |
26 | namespace strings { |
27 | |
28 | // ---------------------------------------------------------------------- |
29 | // FastIntToBufferLeft() |
30 | // These are intended for speed. |
31 | // |
32 | // All functions take the output buffer as an arg. FastInt() uses |
33 | // at most 22 bytes, FastTime() uses exactly 30 bytes. They all |
34 | // return a pointer to the beginning of the output, which is the same as |
35 | // the beginning of the input buffer. |
36 | // |
37 | // NOTE: In 64-bit land, sizeof(time_t) is 8, so it is possible |
38 | // to pass to FastTimeToBuffer() a time whose year cannot be |
39 | // represented in 4 digits. In this case, the output buffer |
40 | // will contain the string "Invalid:<value>" |
41 | // ---------------------------------------------------------------------- |
42 | |
43 | // Previously documented minimums -- the buffers provided must be at least this |
44 | // long, though these numbers are subject to change: |
45 | // Int32, UInt32: 12 bytes |
46 | // Int64, UInt64, Int, Uint: 22 bytes |
47 | // Time: 30 bytes |
48 | // Use kFastToBufferSize rather than hardcoding constants. |
49 | static const int kFastToBufferSize = 32; |
50 | |
51 | // ---------------------------------------------------------------------- |
52 | // FastInt32ToBufferLeft() |
53 | // FastUInt32ToBufferLeft() |
54 | // FastInt64ToBufferLeft() |
55 | // FastUInt64ToBufferLeft() |
56 | // |
57 | // These functions convert their numeric argument to an ASCII |
58 | // representation of the numeric value in base 10, with the |
59 | // representation being left-aligned in the buffer. The caller is |
60 | // responsible for ensuring that the buffer has enough space to hold |
61 | // the output. The buffer should typically be at least kFastToBufferSize |
62 | // bytes. |
63 | // |
64 | // Returns the number of characters written. |
65 | // ---------------------------------------------------------------------- |
66 | |
67 | size_t FastInt32ToBufferLeft(int32_t i, char* buffer); // at least 12 bytes |
68 | size_t FastUInt32ToBufferLeft(uint32_t i, char* buffer); // at least 12 bytes |
69 | size_t FastInt64ToBufferLeft(int64_t i, char* buffer); // at least 22 bytes |
70 | size_t FastUInt64ToBufferLeft(uint64_t i, char* buffer); // at least 22 bytes |
71 | |
72 | // Required buffer size for DoubleToBuffer is kFastToBufferSize. |
73 | // Required buffer size for FloatToBuffer is kFastToBufferSize. |
74 | size_t DoubleToBuffer(double value, char* buffer); |
75 | size_t FloatToBuffer(float value, char* buffer); |
76 | |
77 | // Convert a 64-bit fingerprint value to an ASCII representation. |
78 | std::string FpToString(Fprint fp); |
79 | |
80 | // Attempt to parse a fingerprint in the form encoded by FpToString. If |
81 | // successful, stores the fingerprint in *fp and returns true. Otherwise, |
82 | // returns false. |
83 | bool StringToFp(const std::string& s, Fprint* fp); |
84 | |
85 | // Convert a 64-bit fingerprint value to an ASCII representation that |
86 | // is terminated by a '\0'. |
87 | // Buf must point to an array of at least kFastToBufferSize characters |
88 | StringPiece Uint64ToHexString(uint64_t v, char* buf); |
89 | |
90 | // Attempt to parse a uint64 in the form encoded by FastUint64ToHexString. If |
91 | // successful, stores the value in *v and returns true. Otherwise, |
92 | // returns false. |
93 | bool HexStringToUint64(const StringPiece& s, uint64_t* result); |
94 | |
95 | // Convert strings to 32bit integer values. |
96 | // Leading and trailing spaces are allowed. |
97 | // Return false with overflow or invalid input. |
98 | bool safe_strto32(StringPiece str, int32_t* value); |
99 | |
100 | // Convert strings to unsigned 32bit integer values. |
101 | // Leading and trailing spaces are allowed. |
102 | // Return false with overflow or invalid input. |
103 | bool safe_strtou32(StringPiece str, uint32_t* value); |
104 | |
105 | // Convert strings to 64bit integer values. |
106 | // Leading and trailing spaces are allowed. |
107 | // Return false with overflow or invalid input. |
108 | bool safe_strto64(StringPiece str, int64_t* value); |
109 | |
110 | // Convert strings to unsigned 64bit integer values. |
111 | // Leading and trailing spaces are allowed. |
112 | // Return false with overflow or invalid input. |
113 | bool safe_strtou64(StringPiece str, uint64_t* value); |
114 | |
115 | // Convert strings to floating point values. |
116 | // Leading and trailing spaces are allowed. |
117 | // Values may be rounded on over- and underflow. |
118 | // Returns false on invalid input or if `strlen(value) >= kFastToBufferSize`. |
119 | bool safe_strtof(StringPiece str, float* value); |
120 | |
121 | // Convert strings to double precision floating point values. |
122 | // Leading and trailing spaces are allowed. |
123 | // Values may be rounded on over- and underflow. |
124 | // Returns false on invalid input or if `strlen(value) >= kFastToBufferSize`. |
125 | bool safe_strtod(StringPiece str, double* value); |
126 | |
127 | inline bool ProtoParseNumeric(StringPiece s, int32_t* value) { |
128 | return safe_strto32(s, value); |
129 | } |
130 | |
131 | inline bool ProtoParseNumeric(StringPiece s, uint32_t* value) { |
132 | return safe_strtou32(s, value); |
133 | } |
134 | |
135 | inline bool ProtoParseNumeric(StringPiece s, int64_t* value) { |
136 | return safe_strto64(s, value); |
137 | } |
138 | |
139 | inline bool ProtoParseNumeric(StringPiece s, uint64_t* value) { |
140 | return safe_strtou64(s, value); |
141 | } |
142 | |
143 | inline bool ProtoParseNumeric(StringPiece s, float* value) { |
144 | return safe_strtof(s, value); |
145 | } |
146 | |
147 | inline bool ProtoParseNumeric(StringPiece s, double* value) { |
148 | return safe_strtod(s, value); |
149 | } |
150 | |
151 | // Convert strings to number of type T. |
152 | // Leading and trailing spaces are allowed. |
153 | // Values may be rounded on over- and underflow. |
154 | template <typename T> |
155 | bool SafeStringToNumeric(StringPiece s, T* value) { |
156 | return ProtoParseNumeric(s, value); |
157 | } |
158 | |
159 | // Converts from an int64 to a human readable string representing the |
160 | // same number, using decimal powers. e.g. 1200000 -> "1.20M". |
161 | std::string HumanReadableNum(int64_t value); |
162 | |
163 | // Converts from an int64 representing a number of bytes to a |
164 | // human readable string representing the same number. |
165 | // e.g. 12345678 -> "11.77MiB". |
166 | std::string HumanReadableNumBytes(int64_t num_bytes); |
167 | |
168 | // Converts a time interval as double to a human readable |
169 | // string. For example: |
170 | // 0.001 -> "1 ms" |
171 | // 10.0 -> "10 s" |
172 | // 933120.0 -> "10.8 days" |
173 | // 39420000.0 -> "1.25 years" |
174 | // -10 -> "-10 s" |
175 | std::string HumanReadableElapsedTime(double seconds); |
176 | |
177 | } // namespace strings |
178 | } // namespace tsl |
179 | |
180 | #endif // TENSORFLOW_TSL_PLATFORM_NUMBERS_H_ |
181 | |