comparison dmd/expression/Div.d @ 0:10317f0c89a5

Initial commit
author korDen
date Sat, 24 Oct 2009 08:42:06 +0400
parents
children e28b18c23469
comparison
equal deleted inserted replaced
-1:000000000000 0:10317f0c89a5
1 module dmd.expression.Div;
2
3 import dmd.Type;
4 import dmd.Expression;
5 import dmd.Loc;
6 import dmd.RealExp;
7 import dmd.ComplexExp;
8 import dmd.IntegerExp;
9 import dmd.Complex;
10
11 Expression Div(Type type, Expression e1, Expression e2)
12 {
13 Expression e;
14 Loc loc = e1.loc;
15
16 if (type.isfloating())
17 {
18 Complex!(real) c;
19 real r;
20
21 //e1.type.print();
22 //e2.type.print();
23 if (e2.type.isreal())
24 {
25 if (e1.type.isreal())
26 {
27 e = new RealExp(loc, e1.toReal() / e2.toReal(), type);
28 return e;
29 }
30
31 //r = e2.toReal();
32 //c = e1.toComplex();
33 //printf("(%Lg + %Lgi) / %Lg\n", creall(c), cimagl(c), r);
34 r = e2.toReal();
35 c = e1.toComplex();
36 c = Complex!(real)(c.re / r, c.im / r);
37 }
38 else if (e2.type.isimaginary())
39 {
40 //r = e2.toImaginary();
41 //c = e1.toComplex();
42 //printf("(%Lg + %Lgi) / %Lgi\n", creall(c), cimagl(c), r);
43 r = e2.toImaginary();
44 c = e1.toComplex();
45 c = Complex!(real)(c.im / r, -c.re / r);
46 }
47 else
48 {
49 Complex!(real) c1 = e1.toComplex();
50 Complex!(real) c2 = e2.toComplex();
51
52 real denumerator = c2.re*c2.re + c2.im*c2.im;
53 real numerator_re = c1.re*c2.re + c1.im*c2.im;
54 real numerator_im = c1.im*c2.re - c1.re*c2.im;
55
56 c = Complex!(real)(numerator_re / denumerator, numerator_im / denumerator);
57 }
58
59 if (type.isreal())
60 e = new RealExp(loc, c.re, type);
61 else if (type.isimaginary())
62 e = new RealExp(loc, c.im, type);
63 else if (type.iscomplex())
64 e = new ComplexExp(loc, c, type);
65 else
66 assert(0);
67 }
68 else
69 {
70 long n1;
71 long n2;
72 long n;
73
74 n1 = e1.toInteger();
75 n2 = e2.toInteger();
76
77 if (n2 == 0)
78 {
79 e2.error("divide by 0");
80 e2 = new IntegerExp(loc, 1, e2.type);
81 n2 = 1;
82 }
83
84 if (e1.type.isunsigned() || e2.type.isunsigned())
85 n = (cast(ulong) n1) / (cast(ulong) n2);
86 else
87 n = n1 / n2;
88
89 e = new IntegerExp(loc, n, type);
90 }
91
92 return e;
93 }