LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

byte swap of an array

Hi,

I am trying to get byte swaped in each 32bit unsigned integer in memory of dynamically accocated array of 32bit integers. the size of the array is 576. What would be the best method to acive this?

 

this is what i came up with. but no luck with it.

unsigned long temp

void swapDword(unsigned long *pdword)
{

fot (int m=0;m<=576;m++)

{
   temp = pdword[m];
   for (signed char i=3; i<=0; i--)
   {
      *((char*)(pdword+m) + i) = temp;
      temp = temp >> 8;
   }

}
}

0 Kudos
Message 1 of 6
(4,010 Views)
void swapDword(unsigned long *pdword, int size)
{
  int m;
  unsigned long temp;

  for (m=0; m<size; m++)
  {
    temp = pdword[m];
    pdword[m] = ((temp&0xff)<<24) +
                ((temp&0xff00)<<8) +
                ((temp&0xff0000)>>8) +
                ((temp&0xff000000)>>24);
  }
}

 

S. Eren BALCI
IMESTEK
Message 2 of 6
(4,004 Views)

Does this get optimized to a bswap4 assembler operation ? Is the compiler/assembler intelligent enough to figure it out ?

0 Kudos
Message 3 of 6
(3,951 Views)

Hello,

 

You may have already considered this, but it sounds like the most efficient way of swapping bytes would be to call the assembly instruction within your C code in LabWindows/CVI. However, it doesn't look like that's possible:

 

http://force.natinst.com:8000/pls/nic3/niae_screenpop.main?p_incident_number=1922295

 

Thus, the next best thing might be to verify how the code posted by ebalci gets compiled into assembly. I'm currently looking into how to do this, as well as other options for optimizing this operation.

0 Kudos
Message 4 of 6
(3,926 Views)

I built a 32-bit static library in VC 2003 that uses the bswap instruction to swap an unsigned long (4 bytes). Include bswaplib.h in your source code and add bswaplib.lib to your project and call the bswap function as follows:

 

#include <ansi_c.h>

#include "bswaplib.h"

 

int main(int argc, char *argv[]) {

  unsigned long n;

 

  while (1)  {

    puts("Enter the number to swap (0 to exit) and press ENTER:");

    if (scanf("%x", &n) != 1 || n == 0)

      break;

    printf("Number: %X, Swapped: %X\n", n, bswap(n));

  }

 

  return 0

}

0 Kudos
Message 5 of 6
(3,909 Views)

Thanks, Mohan. That looks like a good solution. Hopefully it works out for you, Suni.

 

To answer the last question: "Does this get optimized to a bswap4 assembler operation ? Is the compiler/assembler intelligent enough to figure it out ?" I've confirmed with our R&D department that no, the CVI doesn't optimize to bswap. I was wondering if there was a way for CVI to output an assembly file so that you could see exact how the source is being compiled, but that functionality also isn't supported. There are, however, some third party "disassemblers" that can perform this operation:

 

http://stackoverflow.com/questions/273145/is-it-possible-to-decompile-a-windows-exe-or-at-least-view...

 

 

Finally, I also want to mention that CVI supports the use of third-party compilers. There may be another C compiler which allows you more control over low-level optimizations. You can configure this option by going to Options>>Build Options>>Build Process Options>>Active 32-bit Compiler.

 

0 Kudos
Message 6 of 6
(3,893 Views)