Changeset 131
- Timestamp:
- 11/10/07 01:33:33 (10 months ago)
- Files:
-
- trunk/blade/Blade.d (modified) (3 diffs)
- trunk/blade/BladeRank.d (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/blade/Blade.d
r129 r131 108 108 if (r==0) { 109 109 ++numscalars; 110 continue; // assume we can always do scalars110 continue; // BUG: assume we can always do scalars 111 111 } 112 112 if (startsWith(t, "double[")) { … … 275 275 } 276 276 277 /+ 277 278 278 // Simplify a vector expression 279 279 // - Use slicing distributive law: A[B..C] for expressions A,B,C … … 285 285 // Convert -A*B into +(-A)*B whenever possible. 286 286 // Combine all scalars into a single scalar. 287 void simplify(inout AbstractSyntaxTree tree)288 {289 // Part 1: simplifications which only change the expression string.290 for (int i=0; i<tree.expression.length; ++i) {291 int x = i+exprLength(expression[i..$]);292 if (x==tree.expression.length) continue;293 if (tree.expression[x+1]=='[') { // potentially a slice294 int y = x + 1 + exprLength(tree.expression[x+1..$-1])295 if (y+2<tree.expression.length && tree.expression[y..y+1]=="..") {296 // it's a slice297 }298 299 }300 }301 // Part 2: simplifications which change the types302 }303 +/304 287 305 288 // Categorise the expression, and dispatch to the appropriate code generator. 306 289 char [] makeVectorCode(AbstractSyntaxTree tree) 307 290 { 291 /* 292 int [] ranks=[]; 293 for (int i=0; i<tree.symbolTable.length; ++i) {ranks~=tree.symbolTable[i].rank; } 294 RevisedExpression e = simplifyVectorExpression(tree.expression, ranks); 295 */ 308 296 VecExpressionType exprType = categorizeExpression(tree); 309 297 if (exprType == VecExpressionType.SSE2Expression || exprType == VecExpressionType.SSE1Expression) { trunk/blade/BladeRank.d
r130 r131 36 36 37 37 unittest { 38 static assert( elementTupleToString!(real [], double) == ["real", "double"]);39 static assert( elementTupleToString!() == cast(char[][])([]));38 static assert( elementTupleToString!(real [], double) == ["real", "double"]); 39 static assert( elementTupleToString!() == cast(char[][])([])); 40 40 } 41 41 … … 58 58 unittest { 59 59 class Test { ulong opIndex(int k){ return 0; } } 60 double padding_for_test;60 double padding_for_test; 61 61 62 62 assert(TupleRank!(int[][], float, double[], double[45])==[2,0,1,1]); … … 110 110 } 111 111 112 113 114 /// Return the length of a sub-expression 112 /** Return the length of a sub-expression 113 * The sub-expression must be 114 * - a single character (eg "X"), OR 115 * - an expression in parenthesis. 116 */ 115 117 int exprLength(char [] s) 116 118 { 117 if ((s[0]>='A' && s[0]<='Z') || s[0]=='$') 118 return 0;119 i nt i = 0;120 if (s[0]>='a' && s[0]<='z')i=1; // next char is a parenthesis - so the code121 // below works122 119 if ((s[0]>='A' && s[0]<='Z') || s[0]=='$') return 0; 120 int i = 0; 121 if (s[0]>='a' && s[0]<='z'){ // function call 122 i=1; // next char is a parenthesis - so the code 123 // below works 124 } 123 125 int numParens = 0; 124 126 for (; i<s.length; ++i) { … … 279 281 280 282 unittest { 281 assert(hasScalarMultiply("(A*B)+(B*C)",[1,0,1]));282 assert(!hasScalarMultiply("(A*B)-(C*C)",[1,0,1]));283 assert(!hasScalarMultiply("A+(B*C)",[1,0,1]));284 assert(hasScalarMultiply("(A/B)-((A*B)+(C*B))",[1,0,1]));285 assert(!hasScalarMultiply("A[B]",[2,0]));286 assert(!hasScalarMultiply("(C[B])[B..A]",[0,0,2]) );283 assert(hasScalarMultiply("(A*B)+(B*C)",[1,0,1])); 284 assert(!hasScalarMultiply("(A*B)-(C*C)",[1,0,1])); 285 assert(!hasScalarMultiply("A+(B*C)",[1,0,1])); 286 assert(hasScalarMultiply("(A/B)-((A*B)+(C*B))",[1,0,1])); 287 assert(!hasScalarMultiply("A[B]",[2,0])); 288 assert(!hasScalarMultiply("(C[B])[B..A]",[0,0,2]) ); 287 289 } 288 290 … … 382 384 if (op=="[") { 383 385 // accumulate indexing and slicing operations 384 return subexprSimplify(left, rank, mulexpr, "[" ~ expr~ "]" ~ indexexpr);386 return subexprSimplify(left, rank, mulexpr, "[" ~ right ~ "]" ~ indexexpr); 385 387 } 386 388 int rrank = subexprRank(right, rank); … … 425 427 char [][] compounds; // the compound variables 426 428 int [] rank; // rank of all of the compound variables 427 } 429 char [] used; // which of the original variables are used. 430 } 431 432 int indexRank(char [] s) 433 { 434 int r=0; 435 int numbrack=0; 436 for(int i=1; i<s.length; ++i) { 437 if (s[i]==']') numbrack--; 438 if (s[i]=='[') { 439 if (numbrack==0) ++r; 440 numbrack++; 441 } 442 if (numbrack==0 && s[i]=='.' && s[i-1]=='.') { 443 // if it's a slice, it does not increase the rank 444 r--; 445 } 446 } 447 return r; 448 } 449 428 450 429 451 RevisedExpression simplifyVectorExpression(char [] expr, int [] rank) … … 433 455 char [][] comp; 434 456 char [] used=""; 457 for (int i=0; i<rank.length; ++i) used~="-"; 435 458 int [] r; 436 459 char next = cast(char)('A' + rank.length); … … 441 464 for (k=i+1; s[k]!=' '; ++k) {} 442 465 comp ~= s[i+2..k-1]; 443 // Can't just use exprRank, because the [] aren't wrapped by (). 444 if (s[k-2]==']') { 445 // it's a vector/matrix of some kind, with indexes. 446 r~=100; // BUG - not correct 466 if (s[k-2]==']') { 467 // it's a vector/matrix of some kind, with rank reduced 468 // by indices. Can't just use exprRank, because the [] 469 // aren't wrapped by (). 470 r ~= rank[s[i+2]-'A'] - indexRank(s[i+2..k-1]); 447 471 } else { 448 472 // it's a scalar expression. Note that it could involve … … 453 477 ++next; 454 478 i = k; 455 } else e ~= s[i]; 456 } 457 return RevisedExpression(e, comp, r); 458 } 459 460 unittest { 461 assert(exprSimplify("A+=(((D[E])*B)[E])", [1,0,3,3,0,0],"","")=="(A+=(B* {D[D[E]][((D[E])*B)[E]]} ))"); 462 assert(exprSimplify("A+=(B*((C[B])[B..E]))", [1,0,3,3,0,0],"","")=="(A+=(B* {C[C[B]][(C[B])[B..E]]} ))"); 463 assert(exprSimplify("A*=(B*C)", [1,0,0],"","")== "(A*= {(B*C)} )"); 479 } else { 480 e ~= s[i]; 481 if (s[i]>='A' && s[i]<='Z') used[s[i]-'A']=s[i]; 482 } 483 } 484 return RevisedExpression(e, comp, r, used); 485 } 486 487 unittest { 488 assert(exprSimplify("A+=(((D[E])*B)[E])", [1,0,3,3,0,0],"","")=="(A+=(B* {D[E][E]} ))"); 489 assert(exprSimplify("A+=(B*((C[B])[B..E]))", [1,0,3,3,0,0],"","")=="(A+=(B* {C[B][B..E]} ))"); 490 assert(exprSimplify("A*=(B*C)", [1,0,0],"","")== "(A*= {(B*C)} )"); 464 491 465 RevisedExpression e = simplifyVectorExpression("A+=(((D[ E])*B)[E])", [1,0,3,3,0, 0]);466 assert(e.expr == "A+=( B*G)");467 468 // char [] q = e.expr ~\n ~ e.compounds[0] ~\n; 469 // assert(0, q); 470 } 492 RevisedExpression e = simplifyVectorExpression("A+=(((D[B])*C)[B])", [2,0,0,4]); 493 assert(e.expr == "A+=(C*E)"); 494 assert(e.rank[0]==2); 495 assert(e.compounds[0]=="D[B][B]"); 496 assert(e.used=="A-C-"); 497 }
