1 #include <torch/csrc/byte_order.h>     9 static inline void swapBytes16(
void *ptr)
    12   memcpy(&output, ptr, 
sizeof(uint16_t));
    13 #if defined(_MSC_VER) && !defined(_DEBUG)    14   output = _byteswap_ushort(output);
    15 #elif defined(__llvm__) || defined(__GNUC__) && !defined(__ICC)    16   output = __builtin_bswap16(output);
    18   uint16_t Hi = output >> 8;
    19   uint16_t Lo = output << 8;
    22   memcpy(ptr, &output, 
sizeof(uint16_t));
    25 static inline void swapBytes32(
void *ptr)
    28   memcpy(&output, ptr, 
sizeof(uint32_t));
    29 #if defined(_MSC_VER) && !defined(_DEBUG)    30   output = _byteswap_ulong(output);
    31 #elif defined(__llvm__) || defined(__GNUC__) && !defined(__ICC)    32   output = __builtin_bswap32(output);
    34    uint32_t Byte0 = output & 0x000000FF;
    35    uint32_t Byte1 = output & 0x0000FF00;
    36    uint32_t Byte2 = output & 0x00FF0000;
    37    uint32_t Byte3 = output & 0xFF000000;
    38    output = (Byte0 << 24) | (Byte1 << 8) | (Byte2 >> 8) | (Byte3 >> 24);
    40   memcpy(ptr, &output, 
sizeof(uint32_t));
    43 static inline void swapBytes64(
void *ptr)
    46   memcpy(&output, ptr, 
sizeof(uint64_t));
    48   output = _byteswap_uint64(output);
    49 #elif defined(__llvm__) || defined(__GNUC__) && !defined(__ICC)    50   output = __builtin_bswap64(output);
    52   uint64_t Byte0 = output & 0x00000000000000FF;
    53   uint64_t Byte1 = output & 0x000000000000FF00;
    54   uint64_t Byte2 = output & 0x0000000000FF0000;
    55   uint64_t Byte3 = output & 0x00000000FF000000;
    56   uint64_t Byte4 = output & 0x000000FF00000000;
    57   uint64_t Byte5 = output & 0x0000FF0000000000;
    58   uint64_t Byte6 = output & 0x00FF000000000000;
    59   uint64_t Byte7 = output & 0xFF00000000000000;
    60   output = (Byte0 << (7*8)) | (Byte1 << (5*8)) | (Byte2 << (3*8)) | (Byte3 << (1*8)) |
    61            (Byte7 >> (7*8)) | (Byte6 >> (5*8)) | (Byte5 >> (3*8)) | (Byte4 >> (1*8));
    63   memcpy(ptr, &output, 
sizeof(uint64_t));
    66 static inline uint16_t decodeUInt16LE(
const uint8_t *data) {
    68   memcpy(&output, data, 
sizeof(uint16_t));
    72 static inline uint16_t decodeUInt16BE(
const uint8_t *data) {
    73   uint16_t output = decodeUInt16LE(data);
    78 static inline uint32_t decodeUInt32LE(
const uint8_t *data) {
    80   memcpy(&output, data, 
sizeof(uint32_t));
    84 static inline uint32_t decodeUInt32BE(
const uint8_t *data) {
    85   uint32_t output = decodeUInt32LE(data);
    90 static inline uint64_t decodeUInt64LE(
const uint8_t *data) {
    92   memcpy(&output, data, 
sizeof(uint64_t));
    96 static inline uint64_t decodeUInt64BE(
const uint8_t *data) {
    97   uint64_t output = decodeUInt64LE(data);
   102 THPByteOrder THP_nativeByteOrder()
   105   return *(uint8_t*)&x ? THP_LITTLE_ENDIAN : THP_BIG_ENDIAN;
   108 void THP_decodeInt16Buffer(int16_t* dst, 
const uint8_t* src, THPByteOrder order, 
size_t len)
   110   for (
size_t i = 0; i < len; i++) {
   111     dst[i] = (int16_t) (order == THP_BIG_ENDIAN ? decodeUInt16BE(src) : decodeUInt16LE(src));
   112     src += 
sizeof(int16_t);
   116 void THP_decodeInt32Buffer(int32_t* dst, 
const uint8_t* src, THPByteOrder order, 
size_t len)
   118   for (
size_t i = 0; i < len; i++) {
   119     dst[i] = (int32_t) (order == THP_BIG_ENDIAN ? decodeUInt32BE(src) : decodeUInt32LE(src));
   120     src += 
sizeof(int32_t);
   124 void THP_decodeInt64Buffer(int64_t* dst, 
const uint8_t* src, THPByteOrder order, 
size_t len)
   126   for (
size_t i = 0; i < len; i++) {
   127     dst[i] = (int64_t) (order == THP_BIG_ENDIAN ? decodeUInt64BE(src) : decodeUInt64LE(src));
   128     src += 
sizeof(int64_t);
   132 void THP_decodeHalfBuffer(THHalf* dst, 
const uint8_t* src, THPByteOrder order, 
size_t len)
   134   for (
size_t i = 0; i < len; i++) {
   136     union { uint16_t x; THHalf f; };
   137     x = (order == THP_BIG_ENDIAN ? decodeUInt16BE(src) : decodeUInt16LE(src));
   139     src += 
sizeof(uint16_t);
   143 void THP_decodeBoolBuffer(
bool* dst, 
const uint8_t* src, THPByteOrder order, 
size_t len)
   145   for (
size_t i = 0; i < len; i++) {
   146     dst[i] = (int)src[i] != 0 ? 
true : 
false;
   150 void THP_decodeFloatBuffer(
float* dst, 
const uint8_t* src, THPByteOrder order, 
size_t len)
   152   for (
size_t i = 0; i < len; i++) {
   154     union { uint32_t x; 
float f; };
   155     x = (order == THP_BIG_ENDIAN ? decodeUInt32BE(src) : decodeUInt32LE(src));
   157     src += 
sizeof(float);
   161 void THP_decodeDoubleBuffer(
double* dst, 
const uint8_t* src, THPByteOrder order, 
size_t len)
   163   for (
size_t i = 0; i < len; i++) {
   165     union { uint64_t x; 
double d; };
   166     x = (order == THP_BIG_ENDIAN ? decodeUInt64BE(src) : decodeUInt64LE(src));
   168     src += 
sizeof(double);
   172 void THP_encodeInt16Buffer(uint8_t* dst, 
const int16_t* src, THPByteOrder order, 
size_t len)
   174   memcpy(dst, src, 
sizeof(int16_t) * len);
   175   if (order != THP_nativeByteOrder()) {
   176     for (
size_t i = 0; i < len; i++) {
   178       dst += 
sizeof(int16_t);
   183 void THP_encodeInt32Buffer(uint8_t* dst, 
const int32_t* src, THPByteOrder order, 
size_t len)
   185   memcpy(dst, src, 
sizeof(int32_t) * len);
   186   if (order != THP_nativeByteOrder()) {
   187     for (
size_t i = 0; i < len; i++) {
   189       dst += 
sizeof(int32_t);
   194 void THP_encodeInt64Buffer(uint8_t* dst, 
const int64_t* src, THPByteOrder order, 
size_t len)
   196   memcpy(dst, src, 
sizeof(int64_t) * len);
   197   if (order != THP_nativeByteOrder()) {
   198     for (
size_t i = 0; i < len; i++) {
   200       dst += 
sizeof(int64_t);
   205 void THP_encodeFloatBuffer(uint8_t* dst, 
const float* src, THPByteOrder order, 
size_t len)
   207   memcpy(dst, src, 
sizeof(
float) * len);
   208   if (order != THP_nativeByteOrder()) {
   209     for (
size_t i = 0; i < len; i++) {
   211       dst += 
sizeof(float);
   216 void THP_encodeDoubleBuffer(uint8_t* dst, 
const double* src, THPByteOrder order, 
size_t len)
   218   memcpy(dst, src, 
sizeof(
double) * len);
   219   if (order != THP_nativeByteOrder()) {
   220     for (
size_t i = 0; i < len; i++) {
   222       dst += 
sizeof(double);