I am doing audio processing using float. The result should be converted back to PCM samples, and I noticed that casting from float to int is surprisingly expensive. Also, the disappointment is that I need to copy the result into a short range (from -32768 to 32767). Although I usually assumed that this could be guaranteed by simply resetting the float to a short one, this will not abate in Java, since at the bytecode level it leads to F2I, followed by I2S. Therefore, instead of simple:
int sample = (short) flotVal;
I needed to resort to this ugly sequence:
int sample = (int) floatVal;
if (sample > 32767) {
sample = 32767;
} else if (sample < -32768) {
sample = -32768;
}
Is there a faster way to do this?
(about 6% of the total execution time seems to be spent on casting, and 6% seems to be not so much at first glance, it is amazing when I consider that the processing part includes a good piece of matrix multiplications and IDCT)
The EDITED throw / clipping code above (not surprisingly) in the body of the loop, which reads the float values from float [] and puts them in byte []. I have a test suite that measures total execution time in several test cases (processing about 200 MB of source audio data). 6% were inferred from the difference in runtime when the translation destination "int sample = (int) floatVal" was replaced by the destination of the loop index for the sample.
EDIT @leopoldkot: Java, ( - F2I, I2S). , , Java - F2S, , , ( 68K, "fmove.w FP0, D0" , ).