comparison dmd/ComplexExp.d @ 0:10317f0c89a5

Initial commit
author korDen
date Sat, 24 Oct 2009 08:42:06 +0400
parents
children ccbc1e0bb3f0
comparison
equal deleted inserted replaced
-1:000000000000 0:10317f0c89a5
1 module dmd.ComplexExp;
2
3 import dmd.Expression;
4 import dmd.InterState;
5 import dmd.Type;
6 import dmd.OutBuffer;
7 import dmd.Loc;
8 import dmd.Scope;
9 import dmd.IRState;
10 import dmd.HdrGenState;
11 import dmd.Type;
12 import dmd.TOK;
13 import dmd.TY;
14 import dmd.Port;
15 import dmd.Complex;
16
17 import dmd.backend.dt_t;
18 import dmd.backend.elem;
19 import dmd.backend.Util;
20 import dmd.backend.TYM;
21 import dmd.backend.mTY;
22
23 class ComplexExp : Expression
24 {
25 Complex!(real) value;
26
27 this(Loc loc, Complex!(real) value, Type type)
28 {
29 super(loc, TOK.TOKcomplex80, ComplexExp.sizeof);
30 this.value = value;
31 this.type = type;
32 //printf("ComplexExp.ComplexExp(%s)\n", toChars());
33 }
34
35 int equals(Object o)
36 {
37 assert(false);
38 }
39
40 Expression semantic(Scope sc)
41 {
42 if (!type)
43 type = Type.tcomplex80;
44 else
45 type = type.semantic(loc, sc);
46 return this;
47 }
48
49 Expression interpret(InterState* istate)
50 {
51 assert(false);
52 }
53
54 string toChars()
55 {
56 assert(false);
57 }
58
59 ulong toInteger()
60 {
61 return cast(ulong) toReal();
62 }
63
64 ulong toUInteger()
65 {
66 return cast(long) toReal();
67 }
68
69 real toReal()
70 {
71 return value.re;
72 }
73
74 real toImaginary()
75 {
76 return value.im;
77 }
78
79 Complex!(real) toComplex()
80 {
81 return value;
82 }
83
84 Expression castTo(Scope sc, Type t)
85 {
86 Expression e = this;
87 if (type != t)
88 {
89 if (type.iscomplex() && t.iscomplex())
90 {
91 e = copy();
92 e.type = t;
93 }
94 else
95 e = Expression.castTo(sc, t);
96 }
97 return e;
98 }
99
100 int isConst()
101 {
102 assert(false);
103 }
104
105 bool isBool(bool result)
106 {
107 assert(false);
108 }
109
110 void toCBuffer(OutBuffer buf, HdrGenState* hgs)
111 {
112 assert(false);
113 }
114
115 void toMangleBuffer(OutBuffer buf)
116 {
117 assert(false);
118 }
119 version (_DH) {
120
121 OutBuffer hexp;
122 }
123
124 elem* toElem(IRState* irs)
125 {
126 eve c;
127 tym_t ty;
128
129 //printf("ComplexExp.toElem(%p) %s\n", this, toChars());
130
131 ///memset(&c, 0, c.sizeof);
132
133 ty = type.totym();
134 switch (tybasic(ty))
135 {
136 case TYcfloat:
137 {
138 c.Vcfloat.re = cast(float) value.re;
139 if (Port.isSignallingNan(value.re)) {
140 (cast(uint*)&c.Vcfloat.re)[0] &= 0xFFBFFFFFL;
141 std.stdio.writeln("float.re is snan");
142 }
143 c.Vcfloat.im = cast(float) value.im;
144 if (Port.isSignallingNan(value.im)) {
145 (cast(uint*)&c.Vcfloat.im)[0] &= 0xFFBFFFFFL;
146 std.stdio.writeln("float.im is snan");
147 }
148 break;
149 }
150
151 case TYcdouble:
152 {
153 c.Vcdouble.re = cast(double) value.re;
154 if (Port.isSignallingNan(value.re)) {
155 std.stdio.writeln("double.re is snan");
156 (cast(uint*)&c.Vcdouble.re)[1] &= 0xFFF7FFFFL;
157 }
158 c.Vcdouble.im = cast(double) value.im;
159 if (Port.isSignallingNan(value.im)) {
160 (cast(uint*)&c.Vcdouble.im)[1] &= 0xFFF7FFFFL;
161 std.stdio.writeln("double.im is snan");
162 }
163 break;
164 }
165
166 case TYcldouble:
167 {
168 static if (true) {
169 c.Vcldouble = value;
170 } else {
171 {
172 ushort* p = cast(ushort*)&c.Vcldouble;
173 for (int i = 0; i < (LNGDBLSIZE*2)/2; i++) printf("%04x ", p[i]);
174 printf("\n");
175 }
176 c.Vcldouble.im = im;
177 {
178 ushort* p = cast(ushort*)&c.Vcldouble;
179 for (int i = 0; i < (LNGDBLSIZE*2)/2; i++) printf("%04x ", p[i]);
180 printf("\n");
181 }
182 c.Vcldouble.re = re;
183 {
184 ushort* p = cast(ushort*)&c.Vcldouble;
185 for (int i = 0; i < (LNGDBLSIZE*2)/2; i++) printf("%04x ", p[i]);
186 printf("\n");
187 }
188 }
189 break;
190 }
191
192 default:
193 assert(0);
194 }
195 return el_const(ty, &c);
196 }
197
198 static private char[6] zeropad;
199
200 dt_t** toDt(dt_t** pdt)
201 {
202 //printf("ComplexExp.toDt() '%s'\n", toChars());
203 float fvalue;
204 double dvalue;
205 real evalue;
206
207 switch (type.toBasetype().ty)
208 {
209 case Tcomplex32:
210 fvalue = value.re;
211 pdt = dtnbytes(pdt,4,cast(char*)&fvalue);
212 fvalue = value.im;
213 pdt = dtnbytes(pdt,4,cast(char*)&fvalue);
214 break;
215
216 case Tcomplex64:
217 dvalue = value.re;
218 pdt = dtnbytes(pdt,8,cast(char*)&dvalue);
219 dvalue = value.im;
220 pdt = dtnbytes(pdt,8,cast(char*)&dvalue);
221 break;
222
223 case Tcomplex80:
224 evalue = value.re;
225 pdt = dtnbytes(pdt,REALSIZE - REALPAD,cast(char*)&evalue);
226 pdt = dtnbytes(pdt,REALPAD,zeropad.ptr);
227 evalue = value.im;
228 pdt = dtnbytes(pdt,REALSIZE - REALPAD, cast(char*)&evalue);
229 pdt = dtnbytes(pdt,REALPAD,zeropad.ptr);
230 break;
231
232 default:
233 assert(0);
234 break;
235 }
236 return pdt;
237 }
238 }
239