Is there any free library to let a user easily build a C mathematical expression, which can be used like any other function? I mean c expression/function which could be as quick as ‘inline’ mathematical expression and could be used many times in program.
I think I can be done in C somehow, but does anybody can tell if it could be real if it have to be a CUDA dveice function?
Is there any free library to let a user easily build a C mathematical
Share
There are a few options. I assume you want something that the user can “call” several times, like this:
One option is to implement this as a recursive structure holding function pointers and constants. Something like:
Then
make_funcwould parse the above string into something like:If you can understand that – the
enum type_itemin thestruct funcis used to point to the next node in the tree (or rather, the first element of that node, which is theenum), and theenumis what our code uses to find out what the item type is. Then, when we use thecall(void *, ...)function, it counts how many variables there are – this is how many extra arguments thecallfunction should have been passed – then replaces the variables with the values we’ve called it with, then does the calculations.The other option (which will probably be considerably faster and easier to extend) is to use something like libjit to do most of that work for you. I’ve never used it, but a JIT compiler gives you some basic building blocks (like add, multiply, etc. “instructions”) that you can string together as you need them, and it compiles them down to actual assembly code (so no going through a constructed syntax tree calling function pointers like we had to before) so that when you call it it’s as fast and dynamic as possible.
I don’t know libjit’s API, but it looks easily capable of doing what you seem to need. The
make_funcandfree_funccould all be pretty much the same as they are above (you might have to alter your calls tocall_func) and would basically construct, use, and destroy a JIT object based on how it parses the user’s string. The same as above, really, but you wouldn’t need to define the syntax tree, data types, etc. yourself.Hope that is somewhat helpful.