Changeset 155

Show
Ignore:
Timestamp:
12/05/07 03:02:24 (9 months ago)
Author:
Don Clugston
Message:

* Refactoring for strided vector length.
*Bugfix for the case of real compound expressions using x87.
* Checks for number of real scalars for x87.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/blade/Blade.d

    r154 r155  
    4949private import blade.BladeRank : isStrided; 
    5050private import blade.BladeSimplify : simplifySyntaxTree, RevisedExpression; 
    51 public import blade.CodegenX86 : generateCodeForAsmX87, MAX_X87_VECTORS, 
     51private import blade.CodegenX86 : generateCodeForAsmX87, MAX_X87_VECTORS, 
     52                                 MAX_87_REALSCALARSPLUSTEMPORARIES, 
    5253                                 generateCodeForSSE,  MAX_SSE_VECTORS; 
    5354 
     
    118119// SSE1 is possible only if all vectors are floats. 
    119120// X87 is possible for any mix of real, double, and float vectors. 
    120 // BUG: should also check if it will overflow the FPU stack 
    121  
     121// BUG: for X87, should also check number of temporaries (don't overflow the FPU stack 
    122122enum VecExpressionType { SSE1Expression, SSE2Expression, X87Expression, DExpression }; 
    123123 
     
    136136    int numvectors = 0; 
    137137    int numscalars = 0; 
     138    int numRealScalars = 0; // scalars other than float or double. 
    138139    for (int i=0; i<revised.mapping.length;++i) { 
    139140        char r = revised.rank[i]; 
     141        int x = revised.mapping[i]-'A'; 
    140142        if (r=='0') { 
    141143            ++numscalars; 
     144            if (x<tree.symbolTable.length) { 
     145                char [] t = tree.symbolTable[x].element; 
     146                if (t!="double" && t!="float" && t!="idouble" && t!="ifloat") { 
     147                    ++numRealScalars; 
     148                } 
     149            } 
    142150            // TODO: disallow asm if any non-int/not FP types are used. 
    143151            continue; // BUG: assume asm can always do scalars 
     
    145153        if (r>'1') return VecExpressionType.DExpression; // can only do scalars and vectors right now. 
    146154        // At this point, all compounds are an original symbol + indexing/slicing. 
    147         int x = revised.mapping[i]-'A'; 
    148155        int y = x; // for compounds, get the original type 
    149156        if (x>=tree.symbolTable.length) { 
     
    151158            // Check for a stride.. 
    152159            if (revised.compounds[x-tree.symbolTable.length][$-1]==']') {                
    153 //               assert(0,revised.compounds[x-tree.symbolTable.length]); 
    154160                strided |= isStrided(revised.compounds[x-tree.symbolTable.length]); 
    155161            } 
     
    171177    } 
    172178    if (strided) return VecExpressionType.DExpression; 
     179    if (numRealScalars > MAX_87_REALSCALARSPLUSTEMPORARIES) X87 = false; 
    173180    if (numvectors > MAX_X87_VECTORS) X87 = false; 
    174181    if (numvectors > MAX_SSE_VECTORS) { SSE1=false; SSE2=false; } 
     
    193200     
    194201    char [] vals; 
    195      
    196202    for (int i=0; i<revised.mapping.length;++i) { 
    197         vals ~= "," ~ getValueForSymbol(revised.mapping[i], tree, revised); 
    198203        char rnk = revised.rank[i]; 
     204        vals ~= ","; 
     205        if (rnk=='1') vals ~= "&"; 
     206        vals ~= getValueForSymbol(revised.mapping[i], tree, revised); 
    199207        int x = revised.mapping[i]-'A'; 
    200208        char [] t; 
     
    207215            // it's a compound 
    208216            if (rnk=='0') { 
    209                 t = "real"; // convert all compounds to real 
     217                t = "real"; // convert all compounds to real. 
     218                // TODO: if the number is exactly representable as a double 
     219                // or float, it could use less FPU stack space. 
    210220            } else { // for arrays, the type is the type of the original array 
    211                 t = tree.symbolTable[revised.compounds[x-tree.symbolTable.length][0]].element; 
     221                t = tree.symbolTable[revised.compounds[x-tree.symbolTable.length][0]-'A'].element; 
    212222            } 
    213223        } 
     
    225235            result ~= t ~ "*"; 
    226236            // for vectors, we only need the pointer, not the length 
    227             vals ~= ".ptr"; 
     237            vals ~= "[0]"; 
    228238        } 
    229239    } 
     
    339349} 
    340350 
     351bool hasDollar(char [] s) 
     352{ 
     353    foreach(c; s) { if (c=='$') return true; } 
     354    return false; 
     355} 
     356 
    341357char [] getDimensionLengthForSymbol(char c, AbstractSyntaxTree tree, RevisedExpression revised, int dimension) 
    342358{ 
     
    366382            int numbracks = 0; 
    367383            bool isSlice = false; 
    368             char [] newSlice; 
     384            char [] nextIndex; 
     385            char [] sliceTo; 
    369386     
    370387            for (int k = comp.length-1;k>=1; --k) {             
    371388                char d = comp[k]; 
    372389                if (d == ']') { ++numbracks; } 
    373                 if (d == '[') { --numbracks; }                
    374                 if (d==']' && numbracks == 1) { newSlice = ""; } 
    375                 else if (d=='.' && numbracks == 1) { isSlice = true;  
    376                     newSlice = "." ~ newSlice; 
    377                 } 
    378                 else if (d=='[' && numbracks==0) { 
     390                if (d == '[') { --numbracks; } 
     391                 
     392                if (d == ']' && numbracks == 1) { nextIndex = ""; } 
     393                else if (numbracks == 1 && comp[k-1..k+1]=="..") { 
     394                    isSlice = true; 
     395                    sliceTo = nextIndex; 
     396                    nextIndex = "";                      
     397                    --k; 
     398                } else if ((d == '[' && numbracks==0) || (d==',' && numbracks==1)) { 
    379399                    if (isSlice && numSlicesRemaining>0) { 
    380                         v = "[" ~ newSlice 
     400                        if (numSlicesRemaining==1) { 
     401                            if (!hasDollar(nextIndex) && !hasDollar(sliceTo)) { 
     402                                return "(" ~ sliceTo ~ "-" ~ nextIndex ~ ")"; 
     403                            } 
     404                        } 
     405                        v = "[" ~ nextIndex ~ ".." ~ sliceTo 
    381406                         ~ "].length"; 
    382407                        --numSlicesRemaining; 
    383408                    } else { 
    384                         v = "[" ~ newSlice ~ "]" ~ v; 
     409                        if (isSlice) 
     410                            v = "[" ~ nextIndex ~ ".." ~ sliceTo ~ "]" ~ v; 
     411                        else v = "[" ~ nextIndex ~ "]" ~ v; 
    385412                    } 
    386                     newSlice = ""; 
    387                     isSlice = false; 
    388                 } else if (d==',' && numbracks==1) { 
    389                     if (isSlice && numSlicesRemaining>0) {                         
    390                         v = "[" ~ newSlice 
    391                           ~ "].length"; 
    392                         --numSlicesRemaining; 
    393                     } else { 
    394                         v = "[" ~ newSlice ~ "]" ~ v; 
    395                     } 
    396                     newSlice = ""; 
     413                    nextIndex = ""; 
    397414                    isSlice = false; 
    398415                } else { 
    399                     if (d>='A' && d<='Z') newSlice = tree.symbolTable[d-'A'].value ~ newSlice
    400                     else newSlice = d ~newSlice
     416                    if (d>='A' && d<='Z') nextIndex = tree.symbolTable[d-'A'].value ~ nextIndex
     417                    else nextIndex = d ~ nextIndex
    401418                } 
    402419            } 
     
    482499 
    483500// Convert the compound expression str back into its values. 
    484 char [] getValueForCompoundExpression(char [] str, Symbol[] table) 
     501char [] reconstituteCompoundExpression(char [] str, Symbol[] table) 
    485502{ 
    486503    char [] v = ""; 
  • trunk/blade/BladeDemo.d

    r154 r155  
    1515// cdouble[] always remains aligned, even when sliced. 
    1616 
    17  
    18     
     17   
    1918void main() 
    2019{    
     
    3332    double [4][] another = [[33.1, 4543, 43, 878.7], [5.14, 455, 554, 2.43]]; 
    3433    real k=3.4; 
    35  
     34    
    3635    mixin(vectorize(` a += (d[2..$-1]*2.01*a[2]-another[][1])["abc".length-3..$]`)); 
    37 /+     
     36     
    3837    mixin(vectorize(" a-= 2.01*(        3.04+k)*r")); 
    3938    mixin(vectorize("q+= q*2.01")); 
     
    4342    mixin(vectorize("a+=6*another[1]")); 
    4443    mixin(vectorize("a+=6*another[1][]")); 
    45     +/ 
    46     mixin(vectorize("another[0..$,1]+=6*a[0..$-2]")); 
     44     
     45    mixin(vectorize("another[0..$,1]+=6*a[0..2]")); 
     46    mixin(vectorize("r-=another[0]")); 
    4747   
    4848    // Parses OK, but I don't think I'll support this. 
    4949//    mixin(vectorize("a+=6*another[1,[1,$]]")); 
    5050 
    51     //mixin(vectorize("dot(q,q*dot(q,q))")); // should simplify to: dot(q.q) * dot(q,q) 
     51 
     52// Parses, and rank checks OK. Doesn't simplify yet, no codegen. 
     53//    mixin(vectorize("dot(q,q*dot(q,q))")); // BUG: should simplify to: dot(q.q) * dot(q,q) 
    5254 
    5355    writefln("a=", a); 
  • trunk/blade/BladeRank.d

    r153 r155  
    221221                    right = end; 
    222222                } 
    223                                  
    224223                if (z==right.length-1) return totrank; 
    225224                right = right[z+2..$]; 
  • trunk/blade/BladeSimplify.d

    r153 r155  
    226226} 
    227227 
    228 // Allows [a,[b,c],d] syntax for indices, where [b,c] is a range. 
     228// Allows [a,[b,c],d..e,f] syntax for indices, where [b,c] is a range. 
    229229int indexRank(char [] s) 
    230230{ 
     
    320320    assert(exprSimplify("A=(((B*E)+(C*E))*D)", "11100","","")=="(A=(( {(D*E)} *B)+( {(D*E)} *C)))"); 
    321321    assert(exprSimplify("A=(D*((B*E)+(C*E)))", "11100","","")=="(A=(( {(D*E)} *B)+( {(D*E)} *C)))"); 
     322//    assert(exprSimplify("d(A,A*d(A,A))", "1", "","") == "d(A,A)*d(A,A)"); 
    322323 
    323324    RevisedExpression e = simplifyVectorExpression("A+=(((D[B])*C)[B])", "2004"); 
  • trunk/blade/CodegenX86.d

    r145 r155  
    266266const int MAX_X87_VECTORS = vectorRegister.length; 
    267267const int MAX_SSE_VECTORS = vectorRegister.length; 
     268// Maximum number of real scalars allowable in an expression ( 
     269// (max # temporaries + max # real scalars) must be <=8, otherwise FPU stack 
     270// will overflow). 
     271const int MAX_87_REALSCALARSPLUSTEMPORARIES = 8;  
    268272 
    269273private: