Pristine Ack-5.5
[Ack-5.5.git] / util / int / do_conv.c
1 /*
2  * Sources of the "CONVERT" group instructions
3  */
4
5 /* $Id: do_conv.c,v 2.5 1994/06/24 10:46:17 ceriel Exp $ */
6
7 #include        <em_abs.h>
8 #include        "nofloat.h"
9 #include        "global.h"
10 #include        "log.h"
11 #include        "mem.h"
12 #include        "trap.h"
13 #include        "text.h"
14 #include        "fra.h"
15 #include        "warn.h"
16
17 #ifndef NOFLOAT
18 extern double fpop();
19 #endif  /* NOFLOAT */
20
21 DoCII()
22 {
23         /* CII -: Convert integer to integer (*) */
24         register int newsize = swpop();
25         register long s;
26
27         LOG(("@C6 DoCII()"));
28         spoilFRA();
29         switch ((int)(10 * swpop() + newsize)) {
30         case 12:
31                 if (wsize == 4) {
32                         wtrap(WILLCONV, EILLINS);
33                 }
34                 npush(spop(1L), 2L);
35                 return;
36         case 14:
37                 npush(spop(1L), 4L);
38                 return;
39         case 22:
40                 if (wsize == 4) {
41                         wtrap(WILLCONV, EILLINS);
42                 }
43                 return;
44         case 24:
45                 npush(spop(2L), 4L);
46                 return;
47         case 42:
48                 if (wsize == 4) {
49                         wtrap(WILLCONV, EILLINS);
50                 }
51                 s = spop(4L);
52                 if (must_test && !(IgnMask&BIT(ECONV))) {
53                         if (s < I_MINS2 || s > I_MAXS2)
54                                 trap(ECONV);
55                 }
56                 npush(s, 2L);
57                 return;
58         case 44:
59                 return;
60         default:
61                 wtrap(WILLCONV, EILLINS);
62         }
63 }
64
65 DoCUI()
66 {
67         /* CUI -: Convert unsigned to integer (*) */
68         register int newsize = swpop();
69         register unsigned long u;
70
71         LOG(("@C6 DoCUI()"));
72         spoilFRA();
73         switch ((int)(10 * swpop() + newsize)) {
74         case 22:
75                 if (wsize == 4) {
76                         wtrap(WILLCONV, EILLINS);
77                 }
78                 u = upop(2L);
79                 if (must_test && !(IgnMask&BIT(ECONV))) {
80                         if (u > I_MAXS2)
81                                 trap(ECONV);
82                 }
83                 npush((long) u, 2L);
84                 return;
85         case 24:
86                 if (wsize == 4) {
87                         wtrap(WILLCONV, EILLINS);
88                 }
89                 npush((long) upop(2L), 4L);
90                 return;
91         case 42:
92                 if (wsize == 4) {
93                         wtrap(WILLCONV, EILLINS);
94                 }
95                 u = upop(4L);
96                 if (must_test && !(IgnMask&BIT(ECONV))) {
97                         if (u > I_MAXS2)
98                                 trap(ECONV);
99                 }
100                 npush((long) u, 2L);
101                 return;
102         case 44:
103                 u = upop(4L);
104                 if (must_test && !(IgnMask&BIT(ECONV))) {
105                         if (u > I_MAXS4)
106                                 trap(ECONV);
107                 }
108                 npush((long) u, 4L);
109                 return;
110         default:
111                 wtrap(WILLCONV, EILLINS);
112         }
113 }
114
115 DoCFI()
116 {
117         /* CFI -: Convert floating to integer (*) */
118 #ifndef NOFLOAT
119         register int newsize = swpop();
120         double f;
121
122         LOG(("@C6 DoCFI()"));
123         spoilFRA();
124         switch ((int)(10 * swpop() + newsize)) {
125         case 42:
126                 if (wsize == 4) {
127                         wtrap(WILLCONV, EILLINS);
128                 }
129                 f = fpop(4L);
130                 if (must_test && !(IgnMask&BIT(ECONV))) {
131                         if (f <= (FL_MINS2 - 1.0) || f > FL_MAXS2)
132                                 trap(ECONV);
133                 }
134                 npush((long) f, 2L);
135                 return;
136         case 44:
137                 f = fpop(4L);
138                 if (must_test && !(IgnMask&BIT(ECONV))) {
139                         if (f <= (FL_MINS4 - 1.0) || f > FL_MAXS4)
140                                 trap(ECONV);
141                 }
142                 npush((long) f, 4L);
143                 return;
144         case 82:
145                 if (wsize == 4) {
146                         wtrap(WILLCONV, EILLINS);
147                 }
148                 f = fpop(8L);
149                 if (must_test && !(IgnMask&BIT(ECONV))) {
150                         if (f <= (FL_MINS2 - 1.0) || f > FL_MAXS2)
151                                 trap(ECONV);
152                 }
153                 npush((long) f, 2L);
154                 return;
155         case 84:
156                 f = fpop(8L);
157                 if (must_test && !(IgnMask&BIT(ECONV))) {
158                         if (f <= (FL_MINS4 - 1.0) || f > FL_MAXS4)
159                                 trap(ECONV);
160                 }
161                 npush((long) f, 4L);
162                 return;
163         default:
164                 wtrap(WILLCONV, EILLINS);
165         }
166 #else   /* NOFLOAT */
167         nofloat();
168 #endif  /* NOFLOAT */
169 }
170
171 DoCIF()
172 {
173         /* CIF -: Convert integer to floating (*) */
174 #ifndef NOFLOAT
175         register int newsize = swpop();
176
177         LOG(("@C6 DoCIF()"));
178         spoilFRA();
179         switch ((int)(10 * swpop() + newsize)) {
180         case 24:
181                 if (wsize == 4) {
182                         wtrap(WILLCONV, EILLINS);
183                 }
184                 fpush((double) spop(2L), 4L);
185                 return;
186         case 28:
187                 if (wsize == 4) {
188                         wtrap(WILLCONV, EILLINS);
189                 }
190                 fpush((double) spop(2L), 8L);
191                 return;
192         case 44:
193                 fpush((double) spop(4L), 4L);
194                 return;
195         case 48:
196                 fpush((double) spop(4L), 8L);
197                 return;
198         default:
199                 wtrap(WILLCONV, EILLINS);
200         }
201 #else   /* NOFLOAT */
202         nofloat();
203 #endif  /* NOFLOAT */
204 }
205
206 DoCUF()
207 {
208         /* CUF -: Convert unsigned to floating (*) */
209 #ifndef NOFLOAT
210         register int newsize = swpop();
211         register unsigned long u;
212
213         LOG(("@C6 DoCUF()"));
214         spoilFRA();
215         switch ((int)(10 * swpop() + newsize)) {
216         case 24:
217                 if (wsize == 4) {
218                         wtrap(WILLCONV, EILLINS);
219                 }
220                 fpush((double) upop(2L), 4L);
221                 return;
222         case 28:
223                 if (wsize == 4) {
224                         wtrap(WILLCONV, EILLINS);
225                 }
226                 fpush((double) upop(2L), 8L);
227                 return;
228         case 44:
229                 if ((u = upop(4L)) > I_MAXS4) {
230                         u -= I_MAXS4;
231                         u -= 1;
232                         fpush(((double) u) - (double)(-I_MAXS4-1), 4L);
233                 }
234                 else fpush((double) u, 4L);
235                 return;
236         case 48:
237                 if ((u = upop(4L)) > I_MAXS4) {
238                         u -= I_MAXS4;
239                         u -= 1;
240                         fpush(((double) u) - (double)(-I_MAXS4-1), 8L);
241                 }
242                 else fpush((double) u, 8L);
243                 return;
244         default:
245                 wtrap(WILLCONV, EILLINS);
246         }
247 #else   /* NOFLOAT */
248         nofloat();
249 #endif  /* NOFLOAT */
250 }
251
252 DoCFF()
253 {
254         /* CFF -: Convert floating to floating (*) */
255 #ifndef NOFLOAT
256         register int newsize = swpop();
257
258         LOG(("@C6 DoCFF()"));
259         spoilFRA();
260         switch ((int)(10 * swpop() + newsize)) {
261         case 44:
262                 return;
263         case 48:
264                 fpush(fpop(4L), 8L);
265                 return;
266         case 88:
267                 return;
268         case 84:
269                 fpush(fpop(8L), 4L);
270                 return;
271         default:
272                 wtrap(WILLCONV, EILLINS);
273         }
274 #else   /* NOFLOAT */
275         nofloat();
276 #endif  /* NOFLOAT */
277 }
278
279 DoCIU()
280 {
281         /* CIU -: Convert integer to unsigned */
282         register int newsize = swpop();
283         register long u;
284
285         LOG(("@C6 DoCIU()"));
286         spoilFRA();
287         switch ((int)(10 * swpop() + newsize)) {
288         case 22:
289                 if (wsize == 4) {
290                         wtrap(WILLCONV, EILLINS);
291                 }
292                 return;
293         case 24:
294                 if (wsize == 4) {
295                         wtrap(WILLCONV, EILLINS);
296                 }
297                 npush((long) upop(2L), 4L);
298                 return;
299         case 42:
300                 if (wsize == 4) {
301                         wtrap(WILLCONV, EILLINS);
302                 }
303                 u = upop(4L);
304                 npush(u, 2L);
305                 return;
306         case 44:
307                 return;
308         default:
309                 wtrap(WILLCONV, EILLINS);
310         }
311 }
312
313 DoCUU()
314 {
315         /* CUU -: Convert unsigned to unsigned */
316         register int newsize = swpop();
317
318         LOG(("@C6 DoCUU()"));
319         spoilFRA();
320         switch ((int)(10 * swpop() + newsize)) {
321         case 22:
322                 if (wsize == 4) {
323                         wtrap(WILLCONV, EILLINS);
324                 }
325                 return;
326         case 24:
327                 if (wsize == 4) {
328                         wtrap(WILLCONV, EILLINS);
329                 }
330                 npush((long) upop(2L), 4L);
331                 return;
332         case 42:
333                 if (wsize == 4) {
334                         wtrap(WILLCONV, EILLINS);
335                 }
336                 npush((long) upop(4L), 2L);
337                 return;
338         case 44:
339                 return;
340         default:
341                 wtrap(WILLCONV, EILLINS);
342         }
343 }
344
345 DoCFU()
346 {
347         /* CFU -: Convert floating to unsigned */
348 #ifndef NOFLOAT
349         register int newsize = swpop();
350         double f;
351
352         LOG(("@C6 DoCFU()"));
353         spoilFRA();
354         switch ((int)(10 * swpop() + newsize)) {
355         case 42:
356                 if (wsize == 4) {
357                         wtrap(WILLCONV, EILLINS);
358                 }
359                 f = fpop(4L);
360                 npush((long) f, 2L);
361                 return;
362         case 44:
363                 f = fpop(4L);
364                 npush((long) f, 4L);
365                 return;
366         case 82:
367                 if (wsize == 4) {
368                         wtrap(WILLCONV, EILLINS);
369                 }
370                 f = fpop(8L);
371                 npush((long) f, 2L);
372                 return;
373         case 84:
374                 f = fpop(8L);
375                 npush((long) f, 4L);
376                 return;
377         default:
378                 wtrap(WILLCONV, EILLINS);
379         }
380 #else   /* NOFLOAT */
381         nofloat();
382 #endif  /* NOFLOAT */
383 }