# C++ code generation: any way of deflating large expressions?

hugo_lec at club-internet.fr hugo_lec at club-internet.fr
Thu Jul 10 20:06:13 CEST 2003

```Hello,

I also worked on such a program : I have to convert huge
expressions in
optimized C++ code (to automate design of finite element code). I
tried to
make something which does not calculate two times the same
thing. There's an
algorithm to use the less temporary variables as possible (in order
to help
the compiler to optimize). It also looks for factorizations with * and
+
(A=x*y; B=sin(x*y*z) -> B=sin(A*z)... ) and for the cost of
operations
(A=x/y; B=z/y; gives double tmp0=1/y; A=x*tmp0; B=z*tmp0)... It can
generate
code for huge expressions in a few seconds.

It is a bit crappy because it does not use the internal
representation given
by GiNaC. But if someone wants to clean it... For now it is usable
enough for
me.

A simple example :

Optimized_ex_output mm("float");
symbol x ("x"), y ("y"), z ("z"), u("u"), v("v");
mm.write(std::cout,2); // 2 spaces before each line
mm.display_tree(); // graph output

produces :

float reg0 = v;
float reg1 = z;
float reg2 = u;
float reg3 = x;
float reg4 = y;
float reg5 = reg3;
reg3 += reg4;
reg2 = 1 / reg2;
float reg6 = reg2;
reg2 *= reg3;
reg2 *= reg0;
reg0 *= reg6;
reg3 += reg1;
reg4 *= reg3;
reg0 *= reg5;
A{1] += reg0;
reg4 =  cos( reg4 );
reg4 += reg2;
float B = reg4;

(and a nice dot graph)

In the near future, I've planed to allow generation of vectorial code
to
produce 4x the same calculi on different data. I've also planed to
generate
code for GPUs but not for the near future.

Hugo LECLERC.

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: optimized_ex_output.tbz2
Url: http://www.cebix.net/pipermail/ginac-list/attachments/20030710/70cb0abd/optimized_ex_output.ksh
```