Elucidate a snippet of index.c?

Aug 11, 2008 at 4:06pm

Elucidate a snippet of index.c?

Hi all-

I’m staring at the perform routine of the MSP example file index.c,
specifically these lines:

temp = *++in;
f = temp + 0.5;
index = f;
if( index < 0 )

that’s a quote from this larger snippet:

double temp;
double f;
long index, chan, frames, nc;

[...]

while( –n ) {
temp = *++in;
f = temp + 0.5;
index = f;
if( index < 0 )
index = 0;
else if( index >= frames )
index = frames – 1;
if( nc > 1 )
index = index * nc + chan;
*++out = tab[index];
}

So is this just a fast way to round the floating point value to an
integer value?

That is, add 0.5 and truncate through the implicit conversion to a
double, then an implicit conversion to a long?

Or am I missing something?

Thanks for the help!

Charles

#39229
Aug 11, 2008 at 4:26pm

On 11 Aug 2008, at 18:06, Charles Turner wrote:
>
> temp = *++in;
> f = temp + 0.5;
> index = f;
> if( index < 0 )

> So is this just a fast way to round the floating point value to an
> integer value?

yes, i would say that’s what it is.
index~ doesn’t do interpolation.
vb

#137813
Aug 11, 2008 at 9:52pm

I would say it’s a simple way rather than the fastest.

The redundant ‘temp’ variable is probably optimised out by the compiler but there are faster algorithms than a standard cast to round a double to an int.

http://www.jjj.de/fxt/fxtbook.pdf

See the section on “Double precision float to signed integer conversion”. A similar algo is used in Juce although you won’t get access to it via the Max 5 SDK as I understand!

#137814

You must be logged in to reply to this topic.