c++ - Quickest way to change endianness -
What is the fastest way to reverse the ends of a 16-bit and 32-bit integer I usually do something like this Coding was done in Visual Studio in C ++):
  union bytes 4 {__int32 value; Four f [4]; }; Union bytes 2 {__int16 value; Four f [2]; }; __int16Chimpindianness16 (__Int 16Wal) {bytes 2 temporary; Temp.value = vals; Char x = temp.ch [0]; Temp.ch [0] = temp.ch [1]; Temp.ch [1] = x; Return temp.value; } __int32 changeEndianness32 (__ int32 val) {byte 4 temporary; Temp.value = vals; Char x; X = temp.ch [0]; Temp.ch [0] = temp.ch [1]; Temp.ch [1] = x; X = temp.ch [2]; Temp.ch [2] = temp.ch [3]; Temp.ch [3] = x; Return temp.value; }    Is there any way to do this  fast , in which I do not have to do so many calculations?    
  Why are not you using the built in  swab  function, which could possibly : Is your code better optimized?   In addition, should start with normal bit-shift operations, and are used so widely that they can be recognized by the optimizer and can also change the code better.  
  Because there are serious bugs in the other answers, I will post a better implementation:  
  int16_t changeEndianness16 (int16_t val) {return (val & lt; & lt ; 8). // Left shift always fills with zero ((Val> & 8; & amp; 0x00ff); // Right-shift sign-extend, therefore forced to zero}    I did not create any code tested by  rolw  for this code I think that a bit more sequence (in the case of instruction number) is really fast Benchmark will be interesting. For 32-bit, there are some possible commands for operations:    // version 1 int32_t changeEndianness32 (int32_t val) {return (tmp  24). ((TMP & lt; & lt; 8) & amp; 0x00ff0000) | ((TMP> 8) & amp; 0x0000ff00). ((TMP> 24) & amp; 0x000000ff); } // version 2, a lesser or, but data dependencies, int32_t, changes the Indianness 32 (int32_t val) {int32_t tmp = (val & lt; & lt; 16). ((Val> & gt; 16) & amp; 0x00ffff); Return ((TMP> 8) & amp; 0x00ff00ff). ((TMP and 0x00ff00ff) & lt; <8); }    
 
   
Comments
Post a Comment