1 /* Copyright 2019 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_UTIL_TENSOR_BUNDLE_BYTE_SWAP_H_ 17 #define TENSORFLOW_CORE_UTIL_TENSOR_BUNDLE_BYTE_SWAP_H_ 18 19 #include "tensorflow/core/framework/tensor.h" 20 #include "tensorflow/core/lib/core/status.h" 21 #include "tensorflow/core/platform/byte_order.h" 22 #include "tensorflow/core/protobuf/meta_graph.pb.h" 23 24 // Define basic byte swapping operations. 25 // These operations must be macros to use compiler intrinsics. 26 // Note that the code here is written for portability, not speed. Byte swapping 27 // only happens when importing a checkpoint from one hardware architecture onto 28 // a different architecture. If these operations become part of a fast path, 29 // then the function ByteSwapArray() below should be rewritten to use 30 // architecture-appropriate SIMD instructions that swap multiple words at once. 31 32 #if defined(__linux__) 33 34 // Use the Gnu byte swap macros when available. See bswap(3) for more info. 35 #include <byteswap.h> 36 #define BYTE_SWAP_16(x) bswap_16(x) 37 #define BYTE_SWAP_32(x) bswap_32(x) 38 #define BYTE_SWAP_64(x) bswap_64(x) 39 40 #elif defined(PLATFORM_WINDOWS) 41 42 // On windows, byte-swapping is in winsock.h, and winsock2.h has a version of 43 // of htonl that can byte-swap 64-bit values. 44 #include <winsock2.h> 45 #define BYTE_SWAP_16(x) htons(x) 46 #define BYTE_SWAP_32(x) htonl(x) 47 // At the moment the 64-bit and 128-bit byte-swapping routines in Winsock2 are 48 // disabled in TensorFlow's standard Windows build environment, so we use 49 // htonl() instead of "#define BYTE_SWAP_64(x) htonll (x)". 50 #define BYTE_SWAP_64(x) \ 51 ((uint64_t(htonl((x)&0x00000000ffffffffUL)) << 32) | \ 52 (htonl(((x)&0xffffffff00000000UL) >> 32))) 53 54 #elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ 55 56 // On non-Linux, non-Windows, but little-endian, environments, use htonl/s, 57 // which byte-swap when the host byte order is little-endian. POSIX doesn't 58 // define a 64-bit version of these library functions, so we roll our own. 59 #include <arpa/inet.h> 60 #define BYTE_SWAP_16(x) htons(x) 61 #define BYTE_SWAP_32(x) htonl(x) 62 #define BYTE_SWAP_64(x) \ 63 ((uint64_t(htonl((x)&0x00000000ffffffffUL)) << 32) | \ 64 (htonl(((x)&0xffffffff00000000UL) >> 32))) 65 66 #else // not defined(__linux__) and not defined(PLATFORM_WINDOWS) 67 // and (__BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__) 68 69 // Fall back on a non-optimized implementation on other big-endian targets. 70 // This code swaps one byte at a time and is probably an order of magnitude 71 // slower. 72 73 #define BYTE_SWAP_16(x) ((((x)&0x00ff) << 8) | (((x)&0xff00) >> 8)) 74 75 #define BYTE_SWAP_32(x) \ 76 ((((x)&0x000000ffU) << 24) | (((x)&0x0000ff00U) << 8) | \ 77 (((x)&0x00ff0000U) >> 8) | (((x)&0xff000000U) >> 24)) 78 79 #define BYTE_SWAP_64(x) \ 80 ((((x)&0x00000000000000ffUL) << 56) | (((x)&0x000000000000ff00UL) << 40) | \ 81 (((x)&0x0000000000ff0000UL) << 24) | (((x)&0x00000000ff000000UL) << 8) | \ 82 (((x)&0x000000ff00000000UL) >> 8) | (((x)&0x0000ff0000000000UL) >> 24) | \ 83 (((x)&0x00ff000000000000UL) >> 40) | (((x)&0xff00000000000000UL) >> 56)) 84 85 #endif // defined(__linux__) 86 87 namespace tensorflow { 88 89 // Byte-swap an entire array of atomic C/C++ types in place. 90 // 91 // Note: When calling this function on arrays of std::complex<> types, 92 // multiply the number of elements by 2 and divide the bytes per element by 2. 93 // 94 // Args: 95 // array: Pointer to the beginning of the array 96 // bytes_per_elem: Number of bytes in each element of the array 97 // array_len: Number of elements in the array 98 // 99 // Returns: Status::OK() on success, -1 otherwise 100 // 101 Status ByteSwapArray(char *array, size_t bytes_per_elem, int array_len); 102 103 // Byte-swap a tensor's backing buffer in place. 104 // 105 // Args: 106 // t: Tensor to be modified IN PLACE. Any tensors that share a backing 107 // buffer with this one will also end up byte-swapped. 108 // Returns: Status::OK() on success, -1 otherwise 109 // TODO(frreiss): Should this be a member of the Tensor class? 110 Status ByteSwapTensor(Tensor *t); 111 112 // Swap tensor_content field of Const Op Tensors in the named functions 113 Status ByteSwapTensorContent(MetaGraphDef *meta_graph_def); 114 115 } // namespace tensorflow 116 117 #endif // TENSORFLOW_CORE_UTIL_TENSOR_BUNDLE_BYTE_SWAP_H_ 118