[sdiy] OT - Linear interpolation in C
Tom Wiltshire
tom at electricdruid.net
Sun Jan 30 17:43:57 CET 2011
Hi Eric,
On 30 Jan 2011, at 15:28, Eric Brombaugh wrote:
> Tom,
>
> You've run into one of the main problems with using C on DSPs - there is no data type to explicitly represent fixed point arithmetic. Although you didn't say how you did it, I imagine you've found the best answer in the first section of you code by using shifts and logical operations to do bit selection.
Yes, that's exactly what I did.
I did it with bitshifts and & masking and such like, but I like Olivier's trick of setting up a struct with the different parts available individually. I'll have to have a play with that and try it out. I agree with him that his code looks more readable.
> When it comes time to do the multiplication things start to get nasty - you can break it down into simpler operations, but then you lose efficiency because while the CPU has fantastic math capabilities, the compiler doesn't know about them and won't use them.
And using a uP with DSP features without being able to use the DSP features seems *pritty stoopid*!
<snip>
> My approach so far has been to bundle the DSP code that I need into small assembly subroutines that are in separate source files and link those in. If you study the way the MCHP DSP library is implemented you can see a number of good examples of this. If you keep the granularity at a coarse enough level you won't lose much to the overhead of function calls and register saves.
>
> Short answer: You can't do it in C on the dsPIC, so keep to assembly for this stuff.
Olivier and others have suggested doing the same thing - putting some lower level assembly routines in to deal with stuff like this. I'll have a read-through of the MCHP DSP library. I have looked at their FIR filter stuff at some point, but I wasn't doing C back then, so I was mostly interested in the assembly parts not the C parts.
I guess I just write my "CalculateLFO()" function in assembly and paste in the code I've already got! I haven't done any inline assembly in C yet, so that's something new to try.
Thanks everyone. It's very useful to know that this is a problem that other people have struggled with, and not just me being thick and not seeing an obvious solution. It helps me focus my efforts on learning stuff that I need to know (like how to inline important bits of assembly) rather than wasting time trying to get the C compiler to write DSP code, which it doesn't seem to be inclined to do.
Tom
More information about the Synth-diy
mailing list