Changeset 98
- Timestamp:
- 04/12/07 19:51:46 (1 year ago)
- Files:
-
- trunk/blade/Blade.d (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/blade/Blade.d
r97 r98 123 123 } 124 124 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 135 125 /** 136 126 * A proxy for a vector operation returning a vector type; conceptually, it … … 172 162 } 173 163 } 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) { 176 165 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); 181 167 } 182 168 } … … 192 178 static if (operations=="#a") { 193 179 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); 197 181 } 198 182 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); 202 184 } 203 185 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); 207 187 } 208 188 void opMulAssign(A)(A w) { // *= is not allowed to change the vector type. 209 189 static assert(is (typeof(BaseType*A) : BaseType), "Vector type mismatch in " ~ BaseType.stringof ~ "[] *= " ~ A.stringof); 210 190 performOperation!(void, "#a", "*=", knownlength, A, B[0])(w, values); 191 } 192 private: 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); 211 201 } 212 202 } … … 229 219 typeof(A.BaseType*B.BaseType) dot(A, B)(A a, B b) 230 220 { 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 }; 235 225 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); 236 226 } … … 276 266 277 267 // 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. 278 270 template singleType(A) 279 271 { … … 838 830 auto w = Vec([2.0+17.0i, 0+28.1i, 8.1+1i]); 839 831 840 w*=(35.0 + 2.1i);832 w*=(35.0 + 2.1i); 841 833 842 834 real d = dot(r, p+r+r);
