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