Home | History | Annotate | Download | only in core
      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_LIB_CORE_CODING_H_
     22 #define TENSORFLOW_LIB_CORE_CODING_H_
     23 
     24 #include "tensorflow/core/lib/core/raw_coding.h"
     25 #include "tensorflow/core/lib/core/stringpiece.h"
     26 #include "tensorflow/core/platform/types.h"
     27 
     28 namespace tensorflow {
     29 namespace core {
     30 
     31 // Maximum number of bytes occupied by a varint32.
     32 static const int kMaxVarint32Bytes = 5;
     33 
     34 // Maximum number of bytes occupied by a varint64.
     35 static const int kMaxVarint64Bytes = 10;
     36 
     37 // Lower-level versions of Put... that write directly into a character buffer
     38 // REQUIRES: dst has enough space for the value being written
     39 extern void EncodeFixed16(char* dst, uint16 value);
     40 extern void EncodeFixed32(char* dst, uint32 value);
     41 extern void EncodeFixed64(char* dst, uint64 value);
     42 extern void PutFixed16(string* dst, uint16 value);
     43 extern void PutFixed32(string* dst, uint32 value);
     44 extern void PutFixed64(string* dst, uint64 value);
     45 
     46 extern void PutVarint32(string* dst, uint32 value);
     47 extern void PutVarint64(string* dst, uint64 value);
     48 
     49 extern bool GetVarint32(StringPiece* input, uint32* value);
     50 extern bool GetVarint64(StringPiece* input, uint64* value);
     51 
     52 extern const char* GetVarint32Ptr(const char* p, const char* limit, uint32* v);
     53 extern const char* GetVarint64Ptr(const char* p, const char* limit, uint64* v);
     54 
     55 // Internal routine for use by fallback path of GetVarint32Ptr
     56 extern const char* GetVarint32PtrFallback(const char* p, const char* limit,
     57                                           uint32* value);
     58 inline const char* GetVarint32Ptr(const char* p, const char* limit,
     59                                   uint32* value) {
     60   if (p < limit) {
     61     uint32 result = *(reinterpret_cast<const unsigned char*>(p));
     62     if ((result & 128) == 0) {
     63       *value = result;
     64       return p + 1;
     65     }
     66   }
     67   return GetVarint32PtrFallback(p, limit, value);
     68 }
     69 
     70 extern char* EncodeVarint32(char* dst, uint32 v);
     71 extern char* EncodeVarint64(char* dst, uint64 v);
     72 
     73 // Returns the length of the varint32 or varint64 encoding of "v"
     74 extern int VarintLength(uint64_t v);
     75 
     76 }  // namespace core
     77 }  // namespace tensorflow
     78 
     79 #endif  // TENSORFLOW_LIB_CORE_CODING_H_
     80