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