3 ms·
Except for the cache hit.
by kstenerud 10y ago
Except for the cache hit.
- Animats 10y agoIf you're interpolating and only need 16 bits of precision, you need only a small table. 32 or 64 entries should be enough for sine and cosine.
- raphlinus 10y agoAn example of the linear-interpolated lookup table approach using fixed-point math is the sine generation[0] in my music synthesizer. It generates a table of 1024 entries, then lerps between those. This gives you precision beyond the least perceptible audio error, and is pretty fast. When I first wrote that code, I was targeting 32 bit ARM, with possibly very weak floating point units. These days, on the types of chips you'd see in phones (or computers in the $9-$40 range like rpi 3), the floating point calculation is _so_ fast (especially with simd) that the cost of the memory lookup starts dominating. So, the NEON implementation computes 4 sin values in a gulp, using a floating point polynomial approximation about the same precision as above[1]. [0] https://github.com/google/music-synthesizer-for-android/blob/master/app/src/main/jni/sin.cc https://github.com/google/music-synthesizer-for-android/blob... [1] https://github.com/google/music-synthesizer-for-android/blob/master/app/src/main/jni/fm_op_kernel.cc#L36 https://github.com/google/music-synthesizer-for-android/blob...
- jacobolus 10y agoIt’s probably a generally better idea to use a polynomial approximation if you have a floating point unit; a degree 8 polynomial for sin(x) on the range [0, π/2] gets you to just about the limits of single precision floating point. If you only need about 4 digits of precision, you can use a degree 5 polynomial.
- adrianratnapala 10y agoCouldn't you do better by only using the range [0, π/4] and noting that sin(x) = cos(x - π/2)?
- jacobolus 10y agoSure, then you can get away with an even smaller degree polynomial.
- sehugg 10y agoFWIW, Applesoft for the 6502 uses a 5th order poly.