简体   繁体   English

动态编译算术运算序列

[英]Compile sequence of arithmetic operations on the fly

I wrote a program in C to generate fractals using a formula, but it goes very slow. 我在C中编写了一个程序来使用公式生成分形,但它变得很慢。 Basically you pass the rendering function a bunch of unsigned char numbers that indicate functions to push values on to a stack or pop them off and perform and arithmetic operation and push the result back on the stack (kind of like reverse polish notation). 基本上你传递渲染函数一堆无符号的字符数字,它们指示将值推送到堆栈或将它们弹出并执行算术运算并将结果推回到堆栈上的函数(类似反向抛光表示法)。 The problem is, the program is reading these functions numbers, going through an if...else if...else if...else if...elseif... to find the right operation to perform, and pushing and popping a bunch of values for every iteration in every pixel. 问题是,程序正在读取这些函数数字,通过if ... else if ... else if ... else if ... elseif ...找到正确的操作执行,推送和弹出每个像素中每次迭代的一堆值。 Normally there would just be a formula (like the Mandelbrot) hard coded into the rendering function, but this program is a DLL I'm writing to be an all purpose fractal renderer. 通常会有一个公式(比如Mandelbrot)硬编码到渲染函数中,但是这个程序是一个DLL,我写的是一个通用的分形渲染器。 Is there any way I could write a little mini compiler that reads the formula before rendering begins and compiles a function on the fly which could then be efficiently re-used by the rendering routine? 有没有什么方法可以编写一个小的编译器,在渲染开始之前读取公式并动态编译一个函数然后可以被渲染例程有效地重用? After all, the whole point of Von Neumann architecture after all is that a computer can modify its own code. 毕竟,Von Neumann架构的重点毕竟是计算机可以修改自己的代码。

Thanks in advance! 提前致谢!

If using C++ is an option, I would suggest you try using LLVM - use their IRBuilder to convert the function into LLVM IR, run optimization passes, then use the LLVM JIT to compile the function at runtime 如果使用C ++是一个选项,我建议你尝试使用LLVM - 使用他们的IRBuilder将函数转换为LLVM IR,运行优化传递,然后使用LLVM JIT在运行时编译函数

http://llvm.org/docs/tutorial/LangImpl4.html http://llvm.org/docs/tutorial/LangImpl4.html

There is no notion of self-modifying code in C. Any attempt to pass control to a data block that is in the "data" memory, say, by casting a data pointer to a function pointer, is undefined behavior. 在C中没有自修改代码的概念。任何将控制传递给“数据”存储器中的数据块的尝试,例如通过将数据指针转换为函数指针,都是未定义的行为。 This model is there to let you program in C for non vov-neuman computers. 这个模型可以让你用C语言编写非vov-neuman计算机。

This said, you can probably optimize your RPN code to run much faster by replacing sequences of if-then-else statements with function pointers or a switch statement. 这就是说,你可以通过用函数指针或switch语句替换if-then-else语句的序列来优化你的RPN代码以更快地运行。 You could also program the target function into a dynamic library, read it at runtime, and use it in rendering. 您还可以将目标函数编程到动态库中,在运行时读取它,并在渲染中使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM