Changeset 163
- Timestamp:
- 12/17/07 03:32:00 (10 months ago)
- Files:
-
- trunk/blade/Blade.d (modified) (1 diff)
- trunk/blade/BladeSimplify.d (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/blade/Blade.d
r162 r163 149 149 } 150 150 // TODO: disallow asm if any non-int/not FP types are used. 151 continue; // BUG: assume asm can always do scalars151 continue; 152 152 } 153 153 if (r>'1') return VecExpressionType.DExpression; // can only do scalars and vectors right now. trunk/blade/BladeSimplify.d
r162 r163 14 14 * be moved to every vector inside A. 15 15 * - Use associativity of *: A*(B*C[]) == (A*B)*C[] 16 * - Use * distributive law over + and -. (Not strictly correct).17 16 * (D) Expression standardisation 18 17 * - Move multiplies to left: Convert A[]*B into B*A[] (assumes * is commutative, … … 421 420 { 422 421 char [] expr; // vector or matrix expression; empty for a pure scalar expression 423 char [] multiplier; // scalar multiply of the entire expression 422 char [] multiplier; // scalar multiply of the entire expression. or "-" for unary minus 424 423 } 425 424 … … 438 437 ScalarFold left = doVisit(this_,args[0]); 439 438 ScalarFold right = doVisit(this_, args[1]); 440 char [] s = left.multiplier;441 if (s.length>0 && right.multiplier.length>0) s~= "*" ~ right.multiplier;442 if (s.length>0) s ~="*";443 439 return ScalarFold("", combineMul(combineMul(left.multiplier, right.multiplier), func ~ "(" ~ left.expr ~ "," ~ right.expr ~ ")")); 444 440 } else { … … 451 447 // Fold unary minus into a multiply, if possible. 452 448 ScalarFold left = doVisit(this_, expr); 453 if (left.multiplier!="") return ScalarFold(left.expr, "-" ~ wrapInParens(left.multiplier)); 454 else return ScalarFold("-" ~ wrapInParens(left.expr), left.multiplier); 449 if (left.multiplier=="-") return ScalarFold(left.expr, ""); 450 else if (left.multiplier!="") return ScalarFold(left.expr, "-" ~ wrapInParens(left.multiplier)); 451 else return ScalarFold(left.expr, "-"); 455 452 } else if (op=="+") { // just ignore unary plus 456 453 return doVisit(this_, expr); … … 485 482 if (op=="*=") { 486 483 assert(first.multiplier=="" && second.expr=="", "BLADE ICE"); 484 assert(second.multiplier!="-", "BLADE ICE"); // this would be a*=-b, where b is a vector 487 485 if (second.multiplier.length>1) return ScalarFold(wrapInParens(first.expr) ~ op ~ " {" ~ wrapInParens(second.multiplier) ~ "} ",""); 488 486 else return ScalarFold(wrapInParens(first.expr) ~ op ~ wrapInParens(second.multiplier),""); … … 492 490 } 493 491 if (first.expr=="" && second.expr=="") { // both are 100% scalars -- it remains a scalar. 492 // Note: neither can be a unary minus. 494 493 return ScalarFold("", 495 494 wrapInParens(combineMul(first.expr, first.multiplier)) ~ op ~ 496 495 wrapInParens(combineMul(second.expr, second.multiplier))); 497 496 } 497 // BUG: Should also do +=, -=. 498 if (op=="-" && second.multiplier=="-") { 499 if (first.multiplier=="-") { 500 // convert (-a)-(-b) into b-a 501 return ScalarFold(wrapInParens(second.expr) ~ "-" ~ 502 wrapInParens(first.expr), ""); 503 } 504 // convert a-(-b) into a+b 505 return ScalarFold(wrapInParens(combineMulWithCompound(first.expr, first.multiplier)) ~ "+" ~ 506 wrapInParens(second.expr), ""); 507 } 508 if (op=="+" && second.multiplier=="-") { 509 // convert a+(-b) into a-b 510 return ScalarFold(wrapInParens(combineMulWithCompound(first.expr, first.multiplier)) ~ "-" ~ 511 wrapInParens(second.expr), ""); 512 } 513 if (op=="+" && first.multiplier=="-") { 514 // convert (-a)+b into b-a 515 return ScalarFold(wrapInParens(combineMulWithCompound(second.expr, second.multiplier)) ~ "-" ~ 516 wrapInParens(first.expr), ""); 517 } 518 if (op=="-" && second.multiplier!="") { 519 // convert a-b into a+(-b) where possible. 520 return ScalarFold(wrapInParens(combineMulWithCompound(first.expr, first.multiplier)) ~ "+" ~ 521 wrapInParens(combineMulWithCompound(second.expr, "-" ~ wrapInParens(second.multiplier))), ""); 522 } 498 523 return ScalarFold(wrapInParens(combineMulWithCompound(first.expr, first.multiplier)) ~ op ~ 499 524 wrapInParens(combineMulWithCompound(second.expr, second.multiplier)), ""); … … 505 530 if (left.length==0) return right; 506 531 if (right.length==0) return left; 532 if (left=="-" && right=="-") return ""; 533 if (left=="-") return "-" ~ wrapInParens(right); 534 if (right=="-") return "-" ~ wrapInParens(left); 507 535 return wrapInParens(left) ~ "*" ~ wrapInParens(right); 508 536 } … … 513 541 assert(left.length>0); 514 542 if (right.length==0) return left; 543 if (right=="-") return "-" ~ wrapInParens(left); 515 544 if (right.length==1) return wrapInParens(left) ~ "*" ~ right; 516 545 return wrapInParens(left) ~ "* {" ~ wrapInParens(right) ~ "} "; … … 528 557 assert(foldScalars("A*=(B*C)", "100")== "A*= {(B*C)} "); 529 558 assert(foldScalars("d(A,(A*d(A,A)))", "1")==" {(d(A,A))*(d(A,A))} "); 530 } 559 assert(foldScalars("A-(B*C)", "101")=="A+(C* {(-B)} )"); 560 assert(foldScalars("A-(B*(-C))", "101")=="A+(C* {(-(-B))} )"); 561 assert(foldScalars("A+(-C)", "101")=="A-C"); 562 assert(foldScalars("A-(-C)", "101")=="A+C"); 563 assert(foldScalars("(-A)-(-C)", "101")=="C-A"); 564 assert(foldScalars("A*=-(-B)", "10")=="A*= {(-(-B))} "); 565 }
