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