Jump to content
  • Advertisement
Sign in to follow this  
fathom88

Question On Setting Variables

This topic is 4604 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a data storeage class which stores data in floats. However, I have a device which gives me data in terms of unsigned char's. I don't have control of this. I've been given a .h file and some basic functions from the hardware device firm. In order to set my data storeage class, I have to cast the data. CopyToDataStoreage(unsigned char *data, int size) { for(int ii = 0; ii < size; ii++) { mData[ii] = (float)data[ii]; } } Usually my data source is a float and I can do a simple copy. However, this vendor defines its data as unsigned char's. Is there a way to speed this up? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
No, there is no way to speed that up. You may find that it's pretty fast as it is, however, since such a loop has good locality of reference (cacheability) and there's plenty of hardware support in modern processors for floating-point and integer math.

You don't need the cast, however, since the conversion is automatic.

Share this post


Link to post
Share on other sites
There might be. You could something like this:


for(int ii = 0; ii < size; ii+=4) {
char c1 = (float)data[ii];
char c2 = (float)data[ii+1];
char c3 = (float)data[ii+2];
char c4 = (float)data[ii+3];

mData[ii] = c1;
mData[ii+1] = c2;
mData[ii+2] = c3;
mData[ii+3] = c4;
}




Two things here. The obvious one is the loop unrolling. It might help the cpu perform multiple casts in parallel.

Second, I separated the casts from the memory stores, again to improve parallelism.

Give it a shot. :)

Or maybe (can't remember if it's possible), you could use SSE to cast 4 values at a time to int's. From there, casting to char is a lot faster. But as I said, I can't rememember if you can even use SSE to cast between these... Just an idea. [wink]

Share this post


Link to post
Share on other sites
Quote:
Original post by Spoonbender
There might be.


Yeah, you *might* have a brain-dead compiler that can't do this itself... :s

Do you know this is a performance bottleneck?

Have you considered modifying your storage class instead, perhaps by storing the char values and just converting to float when you retrieve them?

Share this post


Link to post
Share on other sites
I'm not sure this is faster, but who knows... I sometimes use this for parsing arrays (I like pointers).

CopyToDataStoreage(unsigned char *data, int size)
{
unsigned char *End = data + size;
float *Dat = mData;

while (Cur < End)
{
*Dat = float(*data);

Cur += 1; data += 1;
}
}

End gets the end adress whilst Cur gets the start, it iterates whilst the memory location Cur points to is smaller as the location End contains. Of course, data must not be const and all that, or you can still use another variable to parse.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
Do you know this is a performance bottleneck?


Quoted for emphasis. I wouldn't do any sort of optimization until you know for sure (ie have profiler results) that this copy is slowing you down. In particular depending on the device (if it's a hard drive for example) reading and writing to the device will be orders of magnitude slower than what you are doing now.

Share this post


Link to post
Share on other sites
This is possibly faster:


CopyToDataStoreage(unsigned char *data, int size)
{
static float const floatTable[ 256 ] =
{
0.0f, 1.0f, 2.0f, ..., 253.0f, 254.0f, 255.0f
};

for(int ii = 0; ii < size; ii++)
{
mData[ii] = floatTable[ data[ii] ];
}
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!