[GiNaC-devel] Optimized C-style output
Stefan Weinzierl
stefanw at thep.physik.uni-mainz.de
Tue Mar 20 13:40:22 CET 2007
Hello,
recently I was generating C-style output from GiNaC expressions and ran
into the following problems:
- the compiled code was inefficient, as common subexpressions are
calculated over and over again,
- the compiler didn't really appreciate single lines with about one
million or more characters.
On the mailing list I found that the issue of long lines was already
discussed in July 2003, but it seems that this discussion did not lead to
additional functionality of GiNaC.
To overcome these problems, I wrote a routine which generates optimized
C code. This routine
- substitutes every subexpression, which occurs more than once with a
temporary variable.
- splits long expressions into several lines.
As this could be of use to other people as well, it could be included into
GiNaC. The source files are attached to this mail.
An example for this routine would be the following code fragment
symbol x("x"), y("y");
ex f = pow(sin(x+y),2)+sin(x+y)+log(sin(x+y));
optimized_C_code(std::cout, std::string("double"), std::string("tmp"), f);
which prints
double tmp1 = sin(y+x);
double tmp0 = tmp1+(tmp1*tmp1)+log(tmp1);
to standard output.
Best wishes,
Stefan
-------------- next part --------------
A non-text attachment was scrubbed...
Name: code_generation.h
Type: text/x-chdr
Size: 426 bytes
Desc: header file
URL: <http://www.ginac.de/pipermail/ginac-devel/attachments/20070320/86d52cb0/attachment.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: code_generation.cpp
Type: text/x-c++src
Size: 8215 bytes
Desc: cpp file
URL: <http://www.ginac.de/pipermail/ginac-devel/attachments/20070320/86d52cb0/attachment-0001.bin>
More information about the GiNaC-devel
mailing list