@@ -112,48 +112,45 @@ static bool __AlgebraicExpression_MulOverAdd(AlgebraicExpression **root) {
112112 else if ((_AlgebraicExpression_IsAdditionNode (l ) && !_AlgebraicExpression_IsAdditionNode (r )) ||
113113 (_AlgebraicExpression_IsAdditionNode (r ) && !_AlgebraicExpression_IsAdditionNode (l ))) {
114114
115- // Disconnect left and right children from root.
115+ // disconnect left and right children from root
116116 r = _AlgebraicExpression_OperationRemoveDest ((* root ));
117117 l = _AlgebraicExpression_OperationRemoveDest ((* root ));
118118 ASSERT (AlgebraicExpression_ChildCount (* root ) == 0 );
119119
120- AlgebraicExpression * add = AlgebraicExpression_NewOperation (AL_EXP_ADD );
121- AlgebraicExpression * lMul = AlgebraicExpression_NewOperation (AL_EXP_MUL );
122- AlgebraicExpression * rMul = AlgebraicExpression_NewOperation (AL_EXP_MUL );
123-
124- AlgebraicExpression_AddChild (add , lMul );
125- AlgebraicExpression_AddChild (add , rMul );
126-
127120 AlgebraicExpression * A ;
128121 AlgebraicExpression * B ;
129- AlgebraicExpression * C ;
122+ AlgebraicExpression * add = AlgebraicExpression_NewOperation ( AL_EXP_ADD ) ;
130123
131124 if (_AlgebraicExpression_IsAdditionNode (l )) {
132- // Lefthand side is addition.
133- // (A + B) * C = (A * C) + (B * C)
134-
135- A = _AlgebraicExpression_OperationRemoveSource (l );
136- B = _AlgebraicExpression_OperationRemoveDest (l );
137- C = r ;
138-
125+ // lefthand side is addition
126+ // (A + B + C) * D = (A * D) + (B * D) + (C * D)
127+ B = r ;
128+ uint child_count = AlgebraicExpression_ChildCount (l );
129+ for (uint i = 0 ; i < child_count ; i ++ ) {
130+ A = _AlgebraicExpression_OperationRemoveDest (l );
131+ AlgebraicExpression * mul = AlgebraicExpression_NewOperation (AL_EXP_MUL );
132+ AlgebraicExpression_AddChild (mul , A );
133+ if (i == 0 ) AlgebraicExpression_AddChild (mul , B );
134+ else AlgebraicExpression_AddChild (mul , AlgebraicExpression_Clone (B ));
135+ AlgebraicExpression_AddChild (add , mul );
136+ }
137+ ASSERT (AlgebraicExpression_ChildCount (l ) == 0 );
139138 AlgebraicExpression_Free (l );
140- AlgebraicExpression_AddChild (lMul , A );
141- AlgebraicExpression_AddChild (lMul , C );
142- AlgebraicExpression_AddChild (rMul , B );
143- AlgebraicExpression_AddChild (rMul , AlgebraicExpression_Clone (C ));
144139 } else {
145- // Righthand side is addition.
146- // C * (A + B) = (C * A) + (C * B)
147-
148- A = _AlgebraicExpression_OperationRemoveSource (r );
149- B = _AlgebraicExpression_OperationRemoveDest (r );
150- C = l ;
151-
140+ // righthand side is addition
141+ // D * (A + B + C) = (D * A) + (D * B) + (D * C)
142+ A = l ;
143+ uint child_count = AlgebraicExpression_ChildCount (r );
144+ for (uint i = 0 ; i < child_count ; i ++ ) {
145+ B = _AlgebraicExpression_OperationRemoveDest (r );
146+ AlgebraicExpression * mul = AlgebraicExpression_NewOperation (AL_EXP_MUL );
147+ if (i == 0 ) AlgebraicExpression_AddChild (mul , A );
148+ else AlgebraicExpression_AddChild (mul , AlgebraicExpression_Clone (A ));
149+ AlgebraicExpression_AddChild (mul , B );
150+ AlgebraicExpression_AddChild (add , mul );
151+ }
152+ ASSERT (AlgebraicExpression_ChildCount (r ) == 0 );
152153 AlgebraicExpression_Free (r );
153- AlgebraicExpression_AddChild (lMul , C );
154- AlgebraicExpression_AddChild (lMul , A );
155- AlgebraicExpression_AddChild (rMul , AlgebraicExpression_Clone (C ));
156- AlgebraicExpression_AddChild (rMul , B );
157154 }
158155 // Free original root and overwrite it with new addition root.
159156 AlgebraicExpression_Free (* root );
@@ -163,7 +160,7 @@ static bool __AlgebraicExpression_MulOverAdd(AlgebraicExpression **root) {
163160 }
164161 }
165162
166- // Recurse.
163+ // recurse
167164 uint child_count = AlgebraicExpression_ChildCount (* root );
168165 for (uint i = 0 ; i < child_count ; i ++ ) {
169166 if (__AlgebraicExpression_MulOverAdd ((* root )-> operation .children + i )) return true;
0 commit comments