about summary refs log tree commit diff stats
path: root/src/emu/x64runf20f.c
blob: 1225e07d65a20e9ecccff13575a614f1d7c882f9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#define _GNU_SOURCE
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>

#include "debug.h"
#include "box64stack.h"
#include "x64emu.h"
#include "x64run.h"
#include "x64emu_private.h"
#include "x64run_private.h"
#include "x64primop.h"
#include "x64trace.h"
#include "x87emu_private.h"
#include "box64context.h"
#include "bridge.h"

#include "modrm.h"

int RunF20F(x64emu_t *emu, rex_t rex)
{
    uint8_t opcode;
    uint8_t nextop;
    int8_t tmp8s;
    uint8_t tmp8u;
    int32_t tmp32s;
    reg64_t *oped, *opgd;
    sse_regs_t *opex, *opgx, eax1;
    mmx87_regs_t *opgm;
    #ifndef NOALIGN
    int is_nan;
    #endif

    opcode = F8;

    switch(opcode) {

    case 0x10:  /* MOVSD Gx, Ex */
        nextop = F8;
        GETEX(0);
        GETGX;
        GX->q[0] = EX->q[0];
        if((nextop&0xC0)!=0xC0) {
            // EX is not a register
            GX->q[1] = 0;
        }
        break;
    case 0x11:  /* MOVSD Ex, Gx */
        nextop = F8;
        GETEX(0);
        GETGX;
        EX->q[0] = GX->q[0];
        break;
    case 0x12:  /* MOVDDUP Gx, Ex */
        nextop = F8;
        GETEX(0);
        GETGX;
        GX->q[1] = GX->q[0] = EX->q[0];
        break;

    case 0x1E:  /* NOP */
        nextop = F8;
        GETED(0);
        break;

    case 0x2A:  /* CVTSI2SD Gx, Ed */
        nextop = F8;
        GETED(0);
        GETGX;
        if(rex.w) {
            GX->d[0] = ED->sq[0];
        } else {
            GX->d[0] = ED->sdword[0];
        }
        break;

    case 0x2C:  /* CVTTSD2SI Gd, Ex */
        nextop = F8;
        GETEX(0);
        GETGD;
        if(rex.w)
            GD->sq[0] = EX->d[0];
        else {
            GD->sdword[0] = EX->d[0];
            GD->dword[1] = 0;
        }
        break;
    case 0x2D:  /* CVTSD2SI Gd, Ex */
        nextop = F8;
        GETEX(0);
        GETGD;
        if(rex.w) {
            switch((emu->mxcsr>>13)&3) {
                case ROUND_Nearest:
                    GD->q[0] = floor(EX->d[0]+0.5);
                    break;
                case ROUND_Down:
                    GD->q[0] = floor(EX->d[0]);
                    break;
                case ROUND_Up:
                    GD->q[0] = ceil(EX->d[0]);
                    break;
                case ROUND_Chop:
                    GD->q[0] = EX->d[0];
                    break;
            }
        } else {
            switch((emu->mxcsr>>13)&3) {
                case ROUND_Nearest:
                    GD->sdword[0] = floor(EX->d[0]+0.5);
                    break;
                case ROUND_Down:
                    GD->sdword[0] = floor(EX->d[0]);
                    break;
                case ROUND_Up:
                    GD->sdword[0] = ceil(EX->d[0]);
                    break;
                case ROUND_Chop:
                    GD->sdword[0] = EX->d[0];
                    break;
            }
            GD->dword[1] = 0;
        }
        break;
        
    case 0x51:  /* SQRTSD Gx, Ex */
        nextop = F8;
        GETEX(0);
        GETGX;
        if(EX->d[0]<0.0 )
            GX->d[0] = -NAN;
        else
            GX->d[0] = sqrt(EX->d[0]);
        break;

    case 0x58:  /* ADDSD Gx, Ex */
        nextop = F8;
        GETEX(0);
        GETGX;
        #ifndef NOALIGN
        // add generate a -NAN only if doing inf + -inf
        if((isinf(GX->d[0]) && isinf(EX->d[0]) && (EX->q[0]&0x8000000000000000LL)!=(GX->q[0]&0x8000000000000000LL)))
            GX->d[0] = -NAN;
        else
        #endif
        GX->d[0] += EX->d[0];
        break;
    case 0x59:  /* MULSD Gx, Ex */
        nextop = F8;
        GETEX(0);
        GETGX;
        #ifndef NOALIGN
            // mul generate a -NAN only if doing (+/-)inf * (+/-)0
            if((isinf(GX->d[0]) && EX->d[0]==0.0) || (isinf(EX->d[0]) && GX->d[0]==0.0))
                GX->d[0] = -NAN;
            else
        #endif
        GX->d[0] *= EX->d[0];
        break;
    case 0x5A:  /* CVTSD2SS Gx, Ex */
        nextop = F8;
        GETEX(0);
        GETGX;
        GX->f[0] = EX->d[0];
        break;

    case 0x5C:  /* SUBSD Gx, Ex */
        nextop = F8;
        GETEX(0);
        GETGX;
        #ifndef NOALIGN
            // sub generate a -NAN only if doing inf - inf
            if((isinf(GX->d[0]) && isinf(EX->d[0]) && (EX->q[0]&0x8000000000000000LL)==(GX->q[0]&0x8000000000000000LL)))
                GX->d[0] = -NAN;
            else
        #endif
        GX->d[0] -= EX->d[0];
        break;
    case 0x5D:  /* MINSD Gx, Ex */
        nextop = F8;
        GETEX(0);
        GETGX;
        if (isnan(GX->d[0]) || isnan(EX->d[0]) || isless(EX->d[0], GX->d[0]))
            GX->d[0] = EX->d[0];
        break;
    case 0x5E:  /* DIVSD Gx, Ex */
        nextop = F8;
        GETEX(0);
        GETGX;
        #ifndef NOALIGN
        is_nan = isnan(GX->d[0]) || isnan(EX->d[0]);
        #endif
        GX->d[0] /= EX->d[0];
        #ifndef NOALIGN
        if(!is_nan && isnan(GX->d[0]))
            GX->d[0] = -NAN;
        #endif
        break;
    case 0x5F:  /* MAXSD Gx, Ex */
        nextop = F8;
        GETEX(0);
        GETGX;
        if (isnan(GX->d[0]) || isnan(EX->d[0]) || isgreater(EX->d[0], GX->d[0]))
            GX->d[0] = EX->d[0];
        break;

    case 0x70:  /* PSHUFLW Gx, Ex, Ib */
        nextop = F8;
        GETEX(1);
        GETGX;
        tmp8u = F8;
        if(GX==EX) {
            for (int i=0; i<4; ++i)
                eax1.uw[i] = EX->uw[(tmp8u>>(i*2))&3];
            GX->q[0] = eax1.q[0];
        } else {
            for (int i=0; i<4; ++i)
                GX->uw[i] = EX->uw[(tmp8u>>(i*2))&3];
            GX->q[1] = EX->q[1];
        }
        break;

    case 0x7C:  /* HADDPS Gx, Ex */
        nextop = F8;
        GETEX(0);
        GETGX;
        GX->f[0] += GX->f[1];
        GX->f[1] = GX->f[2] + GX->f[3];
        if(EX==GX) {
            GX->f[2] = GX->f[0];
            GX->f[3] = GX->f[1];
        } else {
            GX->f[2] = EX->f[0] + EX->f[1];
            GX->f[3] = EX->f[2] + EX->f[3];
        }
        break;

    GOCOND(0x80
        , tmp32s = F32S; CHECK_FLAGS(emu);
        , R_RIP += tmp32s;
        ,
    )                               /* 0x80 -> 0x8F Jxx */
        
    case 0xC2:  /* CMPSD Gx, Ex, Ib */
        nextop = F8;
        GETEX(1);
        GETGX;
        tmp8u = F8;
        tmp8s = 0;
        switch(tmp8u&7) {
            case 0: tmp8s=(GX->d[0] == EX->d[0]); break;
            case 1: tmp8s=isless(GX->d[0], EX->d[0]) && !(isnan(GX->d[0]) || isnan(EX->d[0])); break;
            case 2: tmp8s=islessequal(GX->d[0], EX->d[0]) && !(isnan(GX->d[0]) || isnan(EX->d[0])); break;
            case 3: tmp8s=isnan(GX->d[0]) || isnan(EX->d[0]); break;
            case 4: tmp8s=isnan(GX->d[0]) || isnan(EX->d[0]) || (GX->d[0] != EX->d[0]); break;
            case 5: tmp8s=isnan(GX->d[0]) || isnan(EX->d[0]) || isgreaterequal(GX->d[0], EX->d[0]); break;
            case 6: tmp8s=isnan(GX->d[0]) || isnan(EX->d[0]) || isgreater(GX->d[0], EX->d[0]); break;
            case 7: tmp8s=!isnan(GX->d[0]) && !isnan(EX->d[0]); break;
        }
        GX->q[0]=(tmp8s)?0xffffffffffffffffLL:0LL;
        break;

    case 0xD0:  /* ADDSUBPS Gx, Ex */
        nextop = F8;
        GETEX(0);
        GETGX;
        GX->f[0] -= EX->f[0];
        GX->f[1] += EX->f[1];
        GX->f[2] -= EX->f[2];
        GX->f[3] += EX->f[3];
        break;

    case 0xD6:  /* MOVDQ2Q Gm, Ex */
        nextop = F8;
        GETEX(0);
        GETGM;
        GM->q = EX->q[0];
        break;

    case 0xE6:  /* CVTPD2DQ Gx, Ex */
        nextop = F8;
        GETEX(0);
        GETGX;
        switch((emu->mxcsr>>13)&3) {
            case ROUND_Nearest:
                GX->sd[0] = floor(EX->d[0]+0.5);
                GX->sd[1] = floor(EX->d[1]+0.5);
                break;
            case ROUND_Down:
                GX->sd[0] = floor(EX->d[0]);
                GX->sd[1] = floor(EX->d[1]);
                break;
            case ROUND_Up:
                GX->sd[0] = ceil(EX->d[0]);
                GX->sd[1] = ceil(EX->d[1]);
                break;
            case ROUND_Chop:
                GX->sd[0] = EX->d[0];
                GX->sd[1] = EX->d[1];
                break;
        }
        GX->q[1] = 0;
        break;

    case 0xF0:  /* LDDQU Gx, Ex */
        nextop = F8;
        GETEX(0);
        GETGX;
        memcpy(GX, EX, 16);
        break;

   default:
        return 1;
    }
    return 0;
}