[sdiy] Approximating sine with plain integer math

John Ames commodorejohn at gmail.com
Thu Apr 7 22:54:42 CEST 2016


On 4/7/16, ASSI <Stromeko at nexgo.de> wrote:
> You don't want to divide on any microcontroller.
True enough, and what I should've said was "divide by (+max + 1),"
since that can be optimized down to a shift.

> This is wrong on many levels.  You clearly need a 32bit intermediate result
> for the multiplication, so sizeof(int)>=4. Instead of the division by
> SHORT_MAX (which is the constant you actually want to use) you could use a
> right shift by 15 (the resulting range is a tiny bit smaller).
You're quite right - that was the original intent, and I somewhat
botched the C version in that regard.

> You really
> don't want to rely on
>
> sign(saw) = (saw/abs(saw))
>
> especially since there's a singularity when saw is zero.  But you don't need
> that anyway since
>
> saw * abs(saw)
>
> already has the correct sign and will be optimized quite easily.
That's actually a really good point. Thanks for suggesting it!



More information about the Synth-diy mailing list