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_CORE_PLATFORM_RAW_CODING_H_
17 #define TENSORFLOW_CORE_PLATFORM_RAW_CODING_H_
18
19 #include <string.h>
20 #include "tensorflow/core/platform/byte_order.h"
21 #include "tensorflow/core/platform/types.h"
22
23 namespace tensorflow {
24 namespace core {
25
26 // Lower-level versions of Get... that read directly from a character buffer
27 // without any bounds checking.
28
DecodeFixed16(const char * ptr)29 inline uint16 DecodeFixed16(const char* ptr) {
30 if (port::kLittleEndian) {
31 // Load the raw bytes
32 uint16 result;
33 memcpy(&result, ptr, sizeof(result)); // gcc optimizes this to a plain load
34 return result;
35 } else {
36 return ((static_cast<uint16>(static_cast<unsigned char>(ptr[0]))) |
37 (static_cast<uint16>(static_cast<unsigned char>(ptr[1])) << 8));
38 }
39 }
40
DecodeFixed32(const char * ptr)41 inline uint32 DecodeFixed32(const char* ptr) {
42 if (port::kLittleEndian) {
43 // Load the raw bytes
44 uint32 result;
45 memcpy(&result, ptr, sizeof(result)); // gcc optimizes this to a plain load
46 return result;
47 } else {
48 return ((static_cast<uint32>(static_cast<unsigned char>(ptr[0]))) |
49 (static_cast<uint32>(static_cast<unsigned char>(ptr[1])) << 8) |
50 (static_cast<uint32>(static_cast<unsigned char>(ptr[2])) << 16) |
51 (static_cast<uint32>(static_cast<unsigned char>(ptr[3])) << 24));
52 }
53 }
54
DecodeFixed64(const char * ptr)55 inline uint64 DecodeFixed64(const char* ptr) {
56 if (port::kLittleEndian) {
57 // Load the raw bytes
58 uint64 result;
59 memcpy(&result, ptr, sizeof(result)); // gcc optimizes this to a plain load
60 return result;
61 } else {
62 uint64 lo = DecodeFixed32(ptr);
63 uint64 hi = DecodeFixed32(ptr + 4);
64 return (hi << 32) | lo;
65 }
66 }
67
68 } // namespace core
69 } // namespace tensorflow
70
71 #endif // TENSORFLOW_CORE_PLATFORM_RAW_CODING_H_
72