Changeset 98

Show
Ignore:
Timestamp:
04/12/07 19:51:46 (1 year ago)
Author:
Don Clugston
Message:

Refactored the expression template assignment code -- major simplification.

Files:

Legend:

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

    r97 r98  
    123123} 
    124124 
    125 // Check for type mismatches when performing vector assignment. 
    126 template AssignableVectors(A, B) 
    127 { 
    128     static if(is (A:real) && is(B: real) 
    129            || is (A:ireal) && is(B: ireal) 
    130            || is (A:creal) && is(B: creal)) 
    131      const bool AssignableVectors=true; 
    132     else const bool AssignableVectors=false; 
    133 } 
    134  
    135125/** 
    136126 * A proxy for a vector operation returning a vector type; conceptually, it 
     
    172162        } 
    173163    } else { 
    174         // trick: typeof(C*C) converts imag to real, but leaves real & complex unchanged. 
    175         JoinResult!(typeof(BaseType*C), "*", "#a", typeof(C*C)) opMul(C)(C x) { 
     164        JoinResult!(typeof(BaseType*C), "*", "#a", C) opMul(C)(C x) { 
    176165            static assert(is(C: real) || is(C:ireal) || is(C:creal), "Can only multiply by scalars"); 
    177             static if (is(C: ireal)) { 
    178                 return JoinResult!(typeof(BaseType*C), "*", "#a", typeof(C*C))(values, x.im); 
    179             } else 
    180                 return JoinResult!(typeof(BaseType*C), "*", "#a", C)(values, x); 
     166            return JoinResult!(typeof(BaseType*C), "*", "#a", C)(values, x); 
    181167        } 
    182168    } 
     
    192178  static if (operations=="#a") { 
    193179    void opAssign(A)(A expr) { 
    194         static assert(AssignableVectors!(BaseType,A.BaseType), "Vector type mismatch in " ~ BaseType.stringof ~ "[] = " ~ A.BaseType.stringof ~ "[]"); 
    195         static assert(len==0 || expr.len == 0 || len == expr.len, "Vector lengths must match"); 
    196         performOperation!(void, expr.ops, "=", len==0? expr.len : len, expr.ValueTuple, B[0])(expr.values, values); 
     180        doAssign!("=", A)(expr); 
    197181    } 
    198182    void opAddAssign(A)(A expr) { 
    199         static assert(AssignableVectors!(BaseType,A.BaseType), "Vector type mismatch in " ~ BaseType.stringof ~ "[] += " ~ A.BaseType.stringof ~ "[]"); 
    200         static assert(len==0 || expr.len == 0 || len == expr.len, "Vector lengths must match"); 
    201         performOperation!(void, expr.ops, "+=", len==0? expr.len : len, expr.ValueTuple, B[0])(expr.values, values); 
     183        doAssign!("+=", A)(expr); 
    202184    } 
    203185    void opSubAssign(A)(A expr) { 
    204         static assert(AssignableVectors!(BaseType,A.BaseType), "Vector type mismatch in " ~ BaseType.stringof ~ "[] -= " ~ A.BaseType.stringof ~ "[]"); 
    205         static assert(len==0 || expr.len == 0 || len == expr.len, "Vector lengths must match"); 
    206         performOperation!(void, expr.ops, "-=", len==0? expr.len : len, expr.ValueTuple, B[0])(expr.values, values); 
     186        doAssign!("-=", A)(expr); 
    207187    } 
    208188    void opMulAssign(A)(A w) { // *= is not allowed to change the vector type. 
    209189        static assert(is (typeof(BaseType*A) : BaseType), "Vector type mismatch in " ~ BaseType.stringof ~ "[] *= " ~ A.stringof); 
    210190        performOperation!(void, "#a", "*=", knownlength, A, B[0])(w, values); 
     191    } 
     192private: 
     193    void doAssign(char [] assignOperation, A)(A expr) { 
     194        // Check for type mismatches and length mismatches when performing vector assignment. 
     195        static assert( is (BaseType:real) && is(A.BaseType: real) 
     196           || is (BaseType:ireal) && is(A.BaseType: ireal) 
     197           || is (BaseType:creal) && is(A.BaseType: creal), 
     198           "Vector type mismatch in " ~ BaseType.stringof ~ "[] " ~ assignOperation ~ A.BaseType.stringof ~ "[]"); 
     199        static assert(len==0 || expr.len == 0 || len == expr.len, "Vector length mismatch"); 
     200        performOperation!(void, expr.ops, assignOperation, len==0? expr.len : len, expr.ValueTuple, B[0])(expr.values, values); 
    211201    } 
    212202  } 
     
    229219typeof(A.BaseType*B.BaseType) dot(A, B)(A a, B b) 
    230220{ 
    231     static assert (a.len==0 || b.len==0 || a.len==b.len); 
    232     static if (is(A.BaseType: ireal) && is(B.BaseType:ireal)) 
    233         const MUL = -1
    234     else const MUL = 1
     221    static assert (a.len==0 || b.len==0 || a.len==b.len,  "Vector length mismatch"); 
     222    static if (is(A.BaseType: ireal) && is(B.BaseType:ireal)){ 
     223        enum { MUL = -1 }
     224    } else enum { MUL = 1 }
    235225    return MUL * performOperation!(typeof(A.BaseType*B.BaseType), joinExpressions(".", A.ops, B.ops), ".", a.len==0? b.len:a.len, A.ValueTuple, B.ValueTuple)(a.values, b.values); 
    236226} 
     
    276266 
    277267// Create a single-character string representing the type A. 
     268// Pure imaginary types are treated identically to pure reals, since they result in the 
     269// same asm code. 
    278270template singleType(A) 
    279271{ 
     
    838830    auto w = Vec([2.0+17.0i, 0+28.1i, 8.1+1i]); 
    839831 
    840         w*= (35.0 + 2.1i); 
     832    w*=(35.0 + 2.1i); 
    841833 
    842834    real d = dot(r, p+r+r);