10-10-2012 10:03 PM
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;
}
}
}
10-11-2012 12:06 AM
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); } }
10-16-2012 04:23 AM
Does this get optimized to a bswap4 assembler operation ? Is the compiler/assembler intelligent enough to figure it out ?
10-17-2012 12:45 PM
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.
10-18-2012 09:46 AM - edited 10-18-2012 09:48 AM
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
}
10-18-2012 01:29 PM
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:
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.