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

Exponential/Power Functions #29

Closed Miserlou closed 1 year ago

Miserlou commented 4 years ago

Heyo! Loving Vult so far!

I'm looking at the language reference, and I don't see anyway to do power functions ( ex: 3**3 == 27 or pow(3,3)==27). There is an exp(), but it only takes one argument so I'm not sure what that's doing.

Can you either add this feature, or add the documentation for it if it already exists?

Thanks! Rich

Miserlou commented 4 years ago

If it helps nudge you, this function is used for peak/shelf filters..

Miserlou commented 4 years ago

(My interim solution so far has been to compute and cache it in C++, then pass that value into Vult, but it feels very inelegant.)

ritschwumm commented 4 years ago

it might be better to support a natural logarithm, as it is used in many DSP algorithms. then one can build on pow(a,x) == exp(x * ln(a))

modlfo commented 4 years ago

There's the function pow(a,b) It's undocumented. I added it not so long ago. Here's the part of the code that lists all the builtins.

https://github.com/modlfo/vult/blob/master/src/core/env.ml#L593

I try to avoid it because it's slow. You can cache it directly in Vult if you have one of the arguments fixed. For example:

fun pow1_4(x) @[table(size=127,min=0.0,max=10)] {
   return pow(x, 1.4);
}
modlfo commented 4 years ago

In general, you can add missing functions as follows:

external atan2(x:real, y:real) : real "atan2f";

That adds the C++ atan2f and can be used for code generation. The problem when using external function is that the code becomes less portable.