Name: Anonymous 2007-12-06 20:03
Here is my situation.
I have an array of 16-bit signed integers that is updated every iteration of the while(1) loop in my code's main function. Obviously I am aiming to optimize the updating algorithms as much as possible. Now, my problem is this: Other optimizations I have made in other places in the code (involving lookup tables) expect the value of each element to be somewhere between -127 and 127. The new value of the array is dependent upon previous adjacent values in the array. Occasionally it is possible for the calculations to result in a value outside of the expected bounds. Once this happens, it is indeed possible for a systemic breakdown in the integrity of the data (insofar as more and more values go outside of the expected bounds).
I have been thinking about a quick way to clip the values to -127 or 127 if the values are outside of these bounds. Obviously I could do an "if(array[i] > 127) array[i] = 127;" but that isn't very efficient. I tried it bitwise doing the following:
array[i] = (snipped expression) & -127;
but that did not work either. My reasoning was that that particular value would eliminate any bits outside of the sign and the bits corresponding with 2^7 and above.
Can someone tell me what I am doing wrong, or suggest an efficient alternative?
I have an array of 16-bit signed integers that is updated every iteration of the while(1) loop in my code's main function. Obviously I am aiming to optimize the updating algorithms as much as possible. Now, my problem is this: Other optimizations I have made in other places in the code (involving lookup tables) expect the value of each element to be somewhere between -127 and 127. The new value of the array is dependent upon previous adjacent values in the array. Occasionally it is possible for the calculations to result in a value outside of the expected bounds. Once this happens, it is indeed possible for a systemic breakdown in the integrity of the data (insofar as more and more values go outside of the expected bounds).
I have been thinking about a quick way to clip the values to -127 or 127 if the values are outside of these bounds. Obviously I could do an "if(array[i] > 127) array[i] = 127;" but that isn't very efficient. I tried it bitwise doing the following:
array[i] = (snipped expression) & -127;
but that did not work either. My reasoning was that that particular value would eliminate any bits outside of the sign and the bits corresponding with 2^7 and above.
Can someone tell me what I am doing wrong, or suggest an efficient alternative?