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 | // Endian-neutral encoding: |
17 | // * Fixed-length numbers are encoded with least-significant byte first |
18 | // * In addition we support variable length "varint" encoding |
19 | // * Strings are encoded prefixed by their length in varint format |
20 | |
21 | #ifndef TENSORFLOW_TSL_PLATFORM_CODING_H_ |
22 | #define TENSORFLOW_TSL_PLATFORM_CODING_H_ |
23 | |
24 | #include "tensorflow/tsl/platform/stringpiece.h" |
25 | #include "tensorflow/tsl/platform/types.h" |
26 | |
27 | namespace tsl { |
28 | namespace core { |
29 | |
30 | // Maximum number of bytes occupied by a varint32. |
31 | static const int kMaxVarint32Bytes = 5; |
32 | |
33 | // Maximum number of bytes occupied by a varint64. |
34 | static const int kMaxVarint64Bytes = 10; |
35 | |
36 | // Lower-level versions of Put... that write directly into a character buffer |
37 | // REQUIRES: dst has enough space for the value being written |
38 | extern void EncodeFixed16(char* dst, uint16 value); |
39 | extern void EncodeFixed32(char* dst, uint32 value); |
40 | extern void EncodeFixed64(char* dst, uint64 value); |
41 | extern void PutFixed16(string* dst, uint16 value); |
42 | extern void PutFixed32(string* dst, uint32 value); |
43 | extern void PutFixed64(string* dst, uint64 value); |
44 | |
45 | extern void PutVarint32(string* dst, uint32 value); |
46 | extern void PutVarint64(string* dst, uint64 value); |
47 | |
48 | extern void PutVarint32(tstring* dst, uint32 value); |
49 | extern void PutVarint64(tstring* dst, uint64 value); |
50 | |
51 | extern bool GetVarint32(StringPiece* input, uint32* value); |
52 | extern bool GetVarint64(StringPiece* input, uint64* value); |
53 | |
54 | extern const char* GetVarint32Ptr(const char* p, const char* limit, uint32* v); |
55 | extern const char* GetVarint64Ptr(const char* p, const char* limit, uint64* v); |
56 | |
57 | // Internal routine for use by fallback path of GetVarint32Ptr |
58 | extern const char* GetVarint32PtrFallback(const char* p, const char* limit, |
59 | uint32* value); |
60 | extern const char* GetVarint32Ptr(const char* p, const char* limit, |
61 | uint32* value); |
62 | extern char* EncodeVarint32(char* dst, uint32 v); |
63 | extern char* EncodeVarint64(char* dst, uint64 v); |
64 | |
65 | // Returns the length of the varint32 or varint64 encoding of "v" |
66 | extern int VarintLength(uint64_t v); |
67 | |
68 | } // namespace core |
69 | } // namespace tsl |
70 | |
71 | #endif // TENSORFLOW_TSL_PLATFORM_CODING_H_ |
72 | |