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