Performance: float to int cast and clippling result to range
Posted
by durandai
on Stack Overflow
See other posts from Stack Overflow
or by durandai
Published on 2010-05-27T16:05:26Z
Indexed on
2010/05/27
16:11 UTC
Read the original article
Hit count: 205
I'm doing some audio processing with float. The result needs to be converted back to PCM samples, and I noticed that the cast from float to int is surprisingly expensive. Whats furthermore frustrating that I need to clip the result to the range of a short (-32768 to 32767). While I would normally instictively assume that this could be assured by simply casting float to short, this fails miserably in Java, since on the bytecode level it results in F2I followed by I2S. So instead of a 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 runtime seems to be spent on casting, while 6% seem to be not that much at first glance, its astounding when I consider that the processing part involves a good chunk of matrix multiplications and IDCT)
© Stack Overflow or respective owner