vult-dsp / vult

Vult is a transcompiler well suited to write high-performance DSP code
https://vult-dsp.github.io/vult
Other
490 stars 25 forks source link

pow, atan function? #19

Closed cristiano-belloni closed 7 years ago

cristiano-belloni commented 7 years ago

Is there a reason why the generic exponentiation, like Math.pow in javscript, and arctangent, like Math.atan, are not supported? I guess they can be approximated (in the case of atan) or expressed in form of exp, but they're so useful. For example, there's a blog post where you use Mathematica to calculate the exponent of e to implement the midi to freq formula - wouldn't it be nice to just have exponents? :)

modlfo commented 7 years ago

I can add the functions. The only reason why I haven't add them is that I haven't require them for any audio processing :)

In the case of the midi to freq formula, I convert it to exponential because, as far as I remember, calculating the exponential is faster than the exponent (x^y) when the power is a real number. Most of the optimizations I make are because I try to make the code run as fast as possible in Arduino or in the Teensy using C/C++.

In the case of Js I haven't performed extensive profiling to try to to detect where the generated code can be improved.

cristiano-belloni commented 7 years ago

I agree that the goal should be c/c++ performance, but javascript is a powerful tool at prototyping (also, I see vult is exporting asm.js, so theoretically it should be possible to easy compile it into webassembly?). As for the functions, they'd be a cool addition, but only if you don't feel they'd drag down the performance or stability of the transpiler - I guess that a larger API surface increases potential bugs and it's harder to maintain. There should be a vult stdlib with optimised higher level functions - that's something I'd like to contribute (actually, I'd like to contribute to Vult too, but I don't speak Caml).

modlfo commented 7 years ago

I agree, the Js version has been very useful to prototype. That's why lately I have been focusing on developing more high level optimizations that are helpful for all target languages. For example the @[table()] attribute, which gives better performance in C/C++ and JavaScript.

Regarding the vult stdlib, one thing that I have close to that is this file https://github.com/modlfo/vult-examples/blob/master/src/util.vult in which I'm adding useful (and somehow optimized) functions for audio processing. For example, saturation (waveshaping), table-based implemetation of sin and a few conversion functions. These functions are not mathematically valid for all range of reals, but are good for audio DSP.

Feel free to contribute any function that you think can be useful. I will move the contents of that file into https://github.com/modlfo/vult/blob/master/examples/lib/util.vult

cristiano-belloni commented 7 years ago

I will, thanks!