Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/MIPS/LoongArch64/LoongArch64CompALU.cpp
3188 views
1
// Copyright (c) 2023- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#include "Common/CPUDetect.h"
19
#include "Core/MemMap.h"
20
#include "Core/MIPS/LoongArch64/LoongArch64Jit.h"
21
#include "Core/MIPS/LoongArch64/LoongArch64RegCache.h"
22
23
// This file contains compilation for integer / arithmetic / logic related instructions.
24
//
25
// All functions should have CONDITIONAL_DISABLE, so we can narrow things down to a file quickly.
26
// Currently known non working ones should have DISABLE. No flags because that's in IR already.
27
28
// #define CONDITIONAL_DISABLE { CompIR_Generic(inst); return; }
29
#define CONDITIONAL_DISABLE {}
30
#define DISABLE { CompIR_Generic(inst); return; }
31
#define INVALIDOP { _assert_msg_(false, "Invalid IR inst %d", (int)inst.op); CompIR_Generic(inst); return; }
32
33
namespace MIPSComp {
34
35
using namespace LoongArch64Gen;
36
using namespace LoongArch64JitConstants;
37
38
void LoongArch64JitBackend::CompIR_Arith(IRInst inst) {
39
CONDITIONAL_DISABLE;
40
41
bool allowPtrMath = true;
42
#ifdef MASKED_PSP_MEMORY
43
// Since we modify it, we can't safely.
44
allowPtrMath = false;
45
#endif
46
47
// LoongArch64 only adds signed immediates, so rewrite a small enough subtract to an add.
48
// We use -2047 and 2048 here because the range swaps.
49
if (inst.op == IROp::SubConst && (int32_t)inst.constant >= -2047 && (int32_t)inst.constant <= 2048) {
50
inst.op = IROp::AddConst;
51
inst.constant = (uint32_t)-(int32_t)inst.constant;
52
}
53
54
switch (inst.op) {
55
case IROp::Add:
56
regs_.Map(inst);
57
ADD_W(regs_.R(inst.dest), regs_.R(inst.src1), regs_.R(inst.src2));
58
regs_.MarkGPRDirty(inst.dest, true);
59
break;
60
61
case IROp::Sub:
62
regs_.Map(inst);
63
SUB_W(regs_.R(inst.dest), regs_.R(inst.src1), regs_.R(inst.src2));
64
regs_.MarkGPRDirty(inst.dest, true);
65
break;
66
67
case IROp::AddConst:
68
if ((int32_t)inst.constant >= -2048 && (int32_t)inst.constant <= 2047) {
69
// Typical of stack pointer updates.
70
if (regs_.IsGPRMappedAsPointer(inst.dest) && inst.dest == inst.src1 && allowPtrMath) {
71
regs_.MarkGPRAsPointerDirty(inst.dest);
72
ADDI_D(regs_.RPtr(inst.dest), regs_.RPtr(inst.dest), inst.constant);
73
} else {
74
regs_.Map(inst);
75
ADDI_W(regs_.R(inst.dest), regs_.R(inst.src1), inst.constant);
76
regs_.MarkGPRDirty(inst.dest, true);
77
}
78
} else {
79
regs_.Map(inst);
80
LI(SCRATCH1, (int32_t)inst.constant);
81
ADD_W(regs_.R(inst.dest), regs_.R(inst.src1), SCRATCH1);
82
regs_.MarkGPRDirty(inst.dest, true);
83
}
84
break;
85
86
case IROp::SubConst:
87
regs_.Map(inst);
88
LI(SCRATCH1, (int32_t)inst.constant);
89
SUB_W(regs_.R(inst.dest), regs_.R(inst.src1), SCRATCH1);
90
regs_.MarkGPRDirty(inst.dest, true);
91
break;
92
93
case IROp::Neg:
94
regs_.Map(inst);
95
SUB_W(regs_.R(inst.dest), R_ZERO, regs_.R(inst.src1));
96
regs_.MarkGPRDirty(inst.dest, true);
97
break;
98
99
default:
100
INVALIDOP;
101
break;
102
}
103
}
104
105
void LoongArch64JitBackend::CompIR_Logic(IRInst inst) {
106
CONDITIONAL_DISABLE;
107
108
bool resultNormalized = false;
109
switch (inst.op) {
110
case IROp::And:
111
if (inst.src1 != inst.src2) {
112
regs_.Map(inst);
113
AND(regs_.R(inst.dest), regs_.R(inst.src1), regs_.R(inst.src2));
114
} else if (inst.src1 != inst.dest) {
115
regs_.Map(inst);
116
MOVE(regs_.R(inst.dest), regs_.R(inst.src1));
117
regs_.MarkGPRDirty(inst.dest, regs_.IsNormalized32(inst.src1));
118
}
119
break;
120
121
case IROp::Or:
122
if (inst.src1 != inst.src2) {
123
// If both were normalized before, the result is normalized.
124
resultNormalized = regs_.IsNormalized32(inst.src1) && regs_.IsNormalized32(inst.src2);
125
regs_.Map(inst);
126
OR(regs_.R(inst.dest), regs_.R(inst.src1), regs_.R(inst.src2));
127
regs_.MarkGPRDirty(inst.dest, resultNormalized);
128
} else if (inst.src1 != inst.dest) {
129
regs_.Map(inst);
130
MOVE(regs_.R(inst.dest), regs_.R(inst.src1));
131
regs_.MarkGPRDirty(inst.dest, regs_.IsNormalized32(inst.src1));
132
}
133
break;
134
135
case IROp::Xor:
136
if (inst.src1 == inst.src2) {
137
regs_.SetGPRImm(inst.dest, 0);
138
} else {
139
regs_.Map(inst);
140
XOR(regs_.R(inst.dest), regs_.R(inst.src1), regs_.R(inst.src2));
141
}
142
break;
143
144
case IROp::AndConst:
145
resultNormalized = regs_.IsNormalized32(inst.src1);
146
regs_.Map(inst);
147
// LoongArch64's ANDI use unsigned 12-bit immediate
148
if ((int32_t)inst.constant >= 0 && (int32_t)inst.constant < 4096) {
149
ANDI(regs_.R(inst.dest), regs_.R(inst.src1), inst.constant);
150
} else {
151
LI(SCRATCH1, (int32_t)inst.constant);
152
AND(regs_.R(inst.dest), regs_.R(inst.src1), SCRATCH1);
153
}
154
// If the sign bits aren't cleared, and it was normalized before - it still is.
155
if ((inst.constant & 0x80000000) != 0 && resultNormalized)
156
regs_.MarkGPRDirty(inst.dest, true);
157
// Otherwise, if we cleared the sign bits, it's naturally normalized.
158
else if ((inst.constant & 0x80000000) == 0)
159
regs_.MarkGPRDirty(inst.dest, true);
160
break;
161
162
case IROp::OrConst:
163
resultNormalized = regs_.IsNormalized32(inst.src1);
164
regs_.Map(inst);
165
if ((int32_t)inst.constant >= 0 && (int32_t)inst.constant < 4096) {
166
ORI(regs_.R(inst.dest), regs_.R(inst.src1), inst.constant);
167
} else {
168
LI(SCRATCH1, (int32_t)inst.constant);
169
OR(regs_.R(inst.dest), regs_.R(inst.src1), SCRATCH1);
170
}
171
// Since our constant is normalized, oring its bits in won't hurt normalization.
172
regs_.MarkGPRDirty(inst.dest, resultNormalized);
173
break;
174
175
case IROp::XorConst:
176
regs_.Map(inst);
177
if ((int32_t)inst.constant >= 0 && (int32_t)inst.constant < 4096) {
178
XORI(regs_.R(inst.dest), regs_.R(inst.src1), inst.constant);
179
} else {
180
LI(SCRATCH1, (int32_t)inst.constant);
181
XOR(regs_.R(inst.dest), regs_.R(inst.src1), SCRATCH1);
182
}
183
break;
184
185
case IROp::Not:
186
regs_.Map(inst);
187
ORN(regs_.R(inst.dest), R_ZERO, regs_.R(inst.src1));
188
break;
189
190
default:
191
INVALIDOP;
192
break;
193
}
194
}
195
196
void LoongArch64JitBackend::CompIR_Assign(IRInst inst) {
197
CONDITIONAL_DISABLE;
198
199
switch (inst.op) {
200
case IROp::Mov:
201
if (inst.dest != inst.src1) {
202
regs_.Map(inst);
203
MOVE(regs_.R(inst.dest), regs_.R(inst.src1));
204
regs_.MarkGPRDirty(inst.dest, regs_.IsNormalized32(inst.src1));
205
}
206
break;
207
208
case IROp::Ext8to32:
209
regs_.Map(inst);
210
EXT_W_B(regs_.R(inst.dest), regs_.R(inst.src1));
211
regs_.MarkGPRDirty(inst.dest, true);
212
break;
213
214
case IROp::Ext16to32:
215
regs_.Map(inst);
216
EXT_W_H(regs_.R(inst.dest), regs_.R(inst.src1));
217
regs_.MarkGPRDirty(inst.dest, true);
218
break;
219
220
default:
221
INVALIDOP;
222
break;
223
}
224
}
225
226
void LoongArch64JitBackend::CompIR_Bits(IRInst inst) {
227
CONDITIONAL_DISABLE;
228
229
switch (inst.op) {
230
case IROp::ReverseBits:
231
regs_.Map(inst);
232
BITREV_W(regs_.R(inst.dest), regs_.R(inst.src1));
233
regs_.MarkGPRDirty(inst.dest, true);
234
break;
235
236
case IROp::BSwap16:
237
regs_.Map(inst);
238
REVB_2H(regs_.R(inst.dest), regs_.R(inst.src1));
239
regs_.MarkGPRDirty(inst.dest, true);
240
break;
241
242
case IROp::BSwap32:
243
regs_.Map(inst);
244
REVB_2W(regs_.R(inst.dest), regs_.R(inst.src1));
245
regs_.MarkGPRDirty(inst.dest, true);
246
break;
247
248
case IROp::Clz:
249
regs_.Map(inst);
250
CLZ_W(regs_.R(inst.dest), regs_.R(inst.src1));
251
regs_.MarkGPRDirty(inst.dest, true);
252
break;
253
254
default:
255
INVALIDOP;
256
break;
257
}
258
}
259
260
void LoongArch64JitBackend::CompIR_Shift(IRInst inst) {
261
CONDITIONAL_DISABLE;
262
263
switch (inst.op) {
264
case IROp::Shl:
265
regs_.Map(inst);
266
SLL_W(regs_.R(inst.dest), regs_.R(inst.src1), regs_.R(inst.src2));
267
regs_.MarkGPRDirty(inst.dest, true);
268
break;
269
270
case IROp::Shr:
271
regs_.Map(inst);
272
SRL_W(regs_.R(inst.dest), regs_.R(inst.src1), regs_.R(inst.src2));
273
regs_.MarkGPRDirty(inst.dest, true);
274
break;
275
276
case IROp::Sar:
277
regs_.Map(inst);
278
SRA_W(regs_.R(inst.dest), regs_.R(inst.src1), regs_.R(inst.src2));
279
regs_.MarkGPRDirty(inst.dest, true);
280
break;
281
282
case IROp::Ror:
283
regs_.Map(inst);
284
ROTR_W(regs_.R(inst.dest), regs_.R(inst.src1), regs_.R(inst.src2));
285
regs_.MarkGPRDirty(inst.dest, true);
286
break;
287
288
case IROp::ShlImm:
289
// Shouldn't happen, but let's be safe of any passes that modify the ops.
290
if (inst.src2 >= 32) {
291
regs_.SetGPRImm(inst.dest, 0);
292
} else if (inst.src2 == 0) {
293
if (inst.dest != inst.src1) {
294
regs_.Map(inst);
295
MOVE(regs_.R(inst.dest), regs_.R(inst.src1));
296
regs_.MarkGPRDirty(inst.dest, regs_.IsNormalized32(inst.src1));
297
}
298
} else {
299
regs_.Map(inst);
300
SLLI_W(regs_.R(inst.dest), regs_.R(inst.src1), inst.src2);
301
regs_.MarkGPRDirty(inst.dest, true);
302
}
303
break;
304
305
case IROp::ShrImm:
306
// Shouldn't happen, but let's be safe of any passes that modify the ops.
307
if (inst.src2 >= 32) {
308
regs_.SetGPRImm(inst.dest, 0);
309
} else if (inst.src2 == 0) {
310
if (inst.dest != inst.src1) {
311
regs_.Map(inst);
312
MOVE(regs_.R(inst.dest), regs_.R(inst.src1));
313
regs_.MarkGPRDirty(inst.dest, regs_.IsNormalized32(inst.src1));
314
}
315
} else {
316
regs_.Map(inst);
317
SRLI_W(regs_.R(inst.dest), regs_.R(inst.src1), inst.src2);
318
regs_.MarkGPRDirty(inst.dest, true);
319
}
320
break;
321
322
case IROp::SarImm:
323
// Shouldn't happen, but let's be safe of any passes that modify the ops.
324
if (inst.src2 >= 32) {
325
regs_.Map(inst);
326
SRAI_W(regs_.R(inst.dest), regs_.R(inst.src1), 31);
327
regs_.MarkGPRDirty(inst.dest, true);
328
} else if (inst.src2 == 0) {
329
if (inst.dest != inst.src1) {
330
regs_.Map(inst);
331
MOVE(regs_.R(inst.dest), regs_.R(inst.src1));
332
regs_.MarkGPRDirty(inst.dest, regs_.IsNormalized32(inst.src1));
333
}
334
} else {
335
regs_.Map(inst);
336
SRAI_W(regs_.R(inst.dest), regs_.R(inst.src1), inst.src2);
337
regs_.MarkGPRDirty(inst.dest, true);
338
}
339
break;
340
341
case IROp::RorImm:
342
if (inst.src2 == 0) {
343
if (inst.dest != inst.src1) {
344
regs_.Map(inst);
345
MOVE(regs_.R(inst.dest), regs_.R(inst.src1));
346
regs_.MarkGPRDirty(inst.dest, regs_.IsNormalized32(inst.src1));
347
}
348
} else {
349
regs_.Map(inst);
350
ROTRI_W(regs_.R(inst.dest), regs_.R(inst.src1), inst.src2 & 31);
351
regs_.MarkGPRDirty(inst.dest, true);
352
}
353
break;
354
355
default:
356
INVALIDOP;
357
break;
358
}
359
}
360
361
void LoongArch64JitBackend::CompIR_Compare(IRInst inst) {
362
CONDITIONAL_DISABLE;
363
364
LoongArch64Reg lhs = INVALID_REG;
365
LoongArch64Reg rhs = INVALID_REG;
366
switch (inst.op) {
367
case IROp::Slt:
368
regs_.Map(inst);
369
NormalizeSrc12(inst, &lhs, &rhs, SCRATCH1, SCRATCH2, true);
370
371
SLT(regs_.R(inst.dest), lhs, rhs);
372
regs_.MarkGPRDirty(inst.dest, true);
373
break;
374
375
case IROp::SltConst:
376
if (inst.constant == 0) {
377
// Basically, getting the sign bit. Let's shift instead.
378
regs_.Map(inst);
379
SRLI_W(regs_.R(inst.dest), regs_.R(inst.src1), 31);
380
regs_.MarkGPRDirty(inst.dest, true);
381
} else {
382
regs_.Map(inst);
383
NormalizeSrc1(inst, &lhs, SCRATCH1, false);
384
385
if ((int32_t)inst.constant >= -2048 && (int32_t)inst.constant <= 2047) {
386
SLTI(regs_.R(inst.dest), lhs, (int32_t)inst.constant);
387
} else {
388
LI(SCRATCH2, (int32_t)inst.constant);
389
SLT(regs_.R(inst.dest), lhs, SCRATCH2);
390
}
391
regs_.MarkGPRDirty(inst.dest, true);
392
}
393
break;
394
395
case IROp::SltU:
396
regs_.Map(inst);
397
// It's still fine to sign extend, the biggest just get even bigger.
398
NormalizeSrc12(inst, &lhs, &rhs, SCRATCH1, SCRATCH2, true);
399
400
SLTU(regs_.R(inst.dest), lhs, rhs);
401
regs_.MarkGPRDirty(inst.dest, true);
402
break;
403
404
case IROp::SltUConst:
405
if (inst.constant == 0) {
406
regs_.SetGPRImm(inst.dest, 0);
407
} else {
408
regs_.Map(inst);
409
NormalizeSrc1(inst, &lhs, SCRATCH1, false);
410
411
// We sign extend because we're comparing against something normalized.
412
// It's also the most efficient to set.
413
if ((int32_t)inst.constant >= -2048 && (int32_t)inst.constant <= 2047) {
414
SLTUI(regs_.R(inst.dest), lhs, (int32_t)inst.constant);
415
} else {
416
LI(SCRATCH2, (int32_t)inst.constant);
417
SLTU(regs_.R(inst.dest), lhs, SCRATCH2);
418
}
419
regs_.MarkGPRDirty(inst.dest, true);
420
}
421
break;
422
423
default:
424
INVALIDOP;
425
break;
426
}
427
}
428
429
void LoongArch64JitBackend::CompIR_CondAssign(IRInst inst) {
430
CONDITIONAL_DISABLE;
431
432
LoongArch64Reg lhs = INVALID_REG;
433
LoongArch64Reg rhs = INVALID_REG;
434
FixupBranch fixup;
435
switch (inst.op) {
436
case IROp::MovZ:
437
case IROp::MovNZ:
438
if (inst.dest == inst.src2)
439
return;
440
441
// We could have a "zero" with wrong upper due to XOR, so we have to normalize.
442
regs_.Map(inst);
443
NormalizeSrc1(inst, &lhs, SCRATCH1, true);
444
445
switch (inst.op) {
446
case IROp::MovZ:
447
fixup = BNEZ(lhs);
448
break;
449
case IROp::MovNZ:
450
fixup = BEQZ(lhs);
451
break;
452
default:
453
INVALIDOP;
454
break;
455
}
456
457
MOVE(regs_.R(inst.dest), regs_.R(inst.src2));
458
SetJumpTarget(fixup);
459
break;
460
461
case IROp::Max:
462
if (inst.src1 != inst.src2) {
463
CompIR_Generic(inst);
464
} else if (inst.dest != inst.src1) {
465
regs_.Map(inst);
466
MOVE(regs_.R(inst.dest), regs_.R(inst.src1));
467
regs_.MarkGPRDirty(inst.dest, regs_.IsNormalized32(inst.src1));
468
}
469
break;
470
471
case IROp::Min:
472
if (inst.src1 != inst.src2) {
473
CompIR_Generic(inst);
474
} else if (inst.dest != inst.src1) {
475
regs_.Map(inst);
476
MOVE(regs_.R(inst.dest), regs_.R(inst.src1));
477
regs_.MarkGPRDirty(inst.dest, regs_.IsNormalized32(inst.src1));
478
}
479
break;
480
481
default:
482
INVALIDOP;
483
break;
484
}
485
}
486
487
void LoongArch64JitBackend::CompIR_HiLo(IRInst inst) {
488
CONDITIONAL_DISABLE;
489
490
switch (inst.op) {
491
case IROp::MtLo:
492
regs_.MapWithExtra(inst, { { 'G', IRREG_LO, 2, MIPSMap::DIRTY } });
493
// 32-63 bits of IRREG_LO + 0-31 bits of inst.src1
494
BSTRINS_D(regs_.R(IRREG_LO), regs_.R(inst.src1), 31, 0);
495
break;
496
497
case IROp::MtHi:
498
regs_.MapWithExtra(inst, { { 'G', IRREG_LO, 2, MIPSMap::DIRTY } });
499
BSTRINS_D(regs_.R(IRREG_LO), regs_.R(inst.src1), 63, 32);
500
break;
501
502
case IROp::MfLo:
503
regs_.MapWithExtra(inst, { { 'G', IRREG_LO, 2, MIPSMap::INIT } });
504
// It won't be normalized, but that's fine...
505
MOVE(regs_.R(inst.dest), regs_.R(IRREG_LO));
506
break;
507
508
case IROp::MfHi:
509
regs_.MapWithExtra(inst, { { 'G', IRREG_LO, 2, MIPSMap::INIT } });
510
SRAI_D(regs_.R(inst.dest), regs_.R(IRREG_LO), 32);
511
regs_.MarkGPRDirty(inst.dest, true);
512
break;
513
514
default:
515
INVALIDOP;
516
break;
517
}
518
}
519
520
void LoongArch64JitBackend::CompIR_Mult(IRInst inst) {
521
CONDITIONAL_DISABLE;
522
523
auto putArgsIntoScratches = [&](LoongArch64Reg *lhs, LoongArch64Reg *rhs) {
524
MOVE(SCRATCH1, regs_.R(inst.src1));
525
MOVE(SCRATCH2, regs_.R(inst.src2));
526
*lhs = SCRATCH1;
527
*rhs = SCRATCH2;
528
};
529
530
LoongArch64Reg lhs = INVALID_REG;
531
LoongArch64Reg rhs = INVALID_REG;
532
switch (inst.op) {
533
case IROp::Mult:
534
// TODO: Maybe IR could simplify when HI is not needed or clobbered?
535
regs_.MapWithExtra(inst, { { 'G', IRREG_LO, 2, MIPSMap::NOINIT } });
536
NormalizeSrc12(inst, &lhs, &rhs, SCRATCH1, SCRATCH2, true);
537
MUL_D(regs_.R(IRREG_LO), lhs, rhs);
538
break;
539
540
case IROp::MultU:
541
// This is an "anti-norm32" case. Let's just zero always.
542
// TODO: If we could know that LO was only needed, we could use MULW.
543
regs_.MapWithExtra(inst, { { 'G', IRREG_LO, 2, MIPSMap::NOINIT } });
544
putArgsIntoScratches(&lhs, &rhs);
545
MULW_D_WU(regs_.R(IRREG_LO), lhs, rhs);
546
break;
547
548
case IROp::Madd:
549
regs_.MapWithExtra(inst, { { 'G', IRREG_LO, 2, MIPSMap::DIRTY } });
550
NormalizeSrc12(inst, &lhs, &rhs, SCRATCH1, SCRATCH2, true);
551
MUL_D(SCRATCH1, lhs, rhs);
552
ADD_D(regs_.R(IRREG_LO), regs_.R(IRREG_LO), SCRATCH1);
553
break;
554
555
case IROp::MaddU:
556
regs_.MapWithExtra(inst, { { 'G', IRREG_LO, 2, MIPSMap::DIRTY } });
557
putArgsIntoScratches(&lhs, &rhs);
558
MULW_D_WU(SCRATCH1, lhs, rhs);
559
ADD_D(regs_.R(IRREG_LO), regs_.R(IRREG_LO), SCRATCH1);
560
break;
561
562
case IROp::Msub:
563
regs_.MapWithExtra(inst, { { 'G', IRREG_LO, 2, MIPSMap::DIRTY } });
564
NormalizeSrc12(inst, &lhs, &rhs, SCRATCH1, SCRATCH2, true);
565
MUL_D(SCRATCH1, lhs, rhs);
566
SUB_D(regs_.R(IRREG_LO), regs_.R(IRREG_LO), SCRATCH1);
567
break;
568
569
case IROp::MsubU:
570
regs_.MapWithExtra(inst, { { 'G', IRREG_LO, 2, MIPSMap::DIRTY } });
571
putArgsIntoScratches(&lhs, &rhs);
572
MULW_D_WU(SCRATCH1, lhs, rhs);
573
SUB_D(regs_.R(IRREG_LO), regs_.R(IRREG_LO), SCRATCH1);
574
break;
575
576
default:
577
INVALIDOP;
578
break;
579
}
580
}
581
582
void LoongArch64JitBackend::CompIR_Div(IRInst inst) {
583
CONDITIONAL_DISABLE;
584
585
LoongArch64Reg numReg, denomReg;
586
switch (inst.op) {
587
case IROp::Div:
588
regs_.MapWithExtra(inst, { { 'G', IRREG_LO, 2, MIPSMap::NOINIT } });
589
// We have to do this because of the divide by zero and overflow checks below.
590
NormalizeSrc12(inst, &numReg, &denomReg, SCRATCH1, SCRATCH2, true);
591
DIV_W(regs_.R(IRREG_LO), numReg, denomReg);
592
MOD_W(R_RA, numReg, denomReg);
593
// Now to combine them. We'll do more with them below...
594
BSTRINS_D(regs_.R(IRREG_LO), R_RA, 63, 32);
595
596
// Now some tweaks for divide by zero and overflow.
597
{
598
// Start with divide by zero, the quotient and remainder are arbitrary numbers.
599
FixupBranch skipNonZero = BNEZ(denomReg);
600
// Clear the arbitrary number
601
XOR(regs_.R(IRREG_LO), regs_.R(IRREG_LO), regs_.R(IRREG_LO));
602
// Replace remainder to numReg
603
BSTRINS_D(regs_.R(IRREG_LO), numReg, 63, 32);
604
FixupBranch keepNegOne = BGE(numReg, R_ZERO);
605
// Replace quotient with 1.
606
ADDI_D(regs_.R(IRREG_LO), regs_.R(IRREG_LO), 1);
607
SetJumpTarget(keepNegOne);
608
// Replace quotient with -1.
609
ADDI_D(regs_.R(IRREG_LO), regs_.R(IRREG_LO), -1);
610
SetJumpTarget(skipNonZero);
611
612
// For overflow, LoongArch sets LO right, but remainder to zero.
613
// Cheating a bit by using R_RA as a temp...
614
LI(R_RA, (int32_t)0x80000000);
615
FixupBranch notMostNegative = BNE(numReg, R_RA);
616
LI(R_RA, -1);
617
FixupBranch notNegativeOne = BNE(denomReg, R_RA);
618
// Take our R_RA and put it in the high bits.
619
SLLI_D(R_RA, R_RA, 32);
620
OR(regs_.R(IRREG_LO), regs_.R(IRREG_LO), R_RA);
621
SetJumpTarget(notNegativeOne);
622
SetJumpTarget(notMostNegative);
623
}
624
break;
625
626
case IROp::DivU:
627
regs_.MapWithExtra(inst, { { 'G', IRREG_LO, 2, MIPSMap::NOINIT } });
628
// We have to do this because of the divide by zero check below.
629
NormalizeSrc12(inst, &numReg, &denomReg, SCRATCH1, SCRATCH2, true);
630
DIV_WU(regs_.R(IRREG_LO), numReg, denomReg);
631
MOD_WU(R_RA, numReg, denomReg);
632
633
// On divide by zero, special dealing with the 0xFFFF case.
634
{
635
FixupBranch skipNonZero = BNEZ(denomReg);
636
// Move -1 to quotient.
637
ADDI_D(regs_.R(IRREG_LO), R_ZERO, -1);
638
// Move numReg to remainder (stores in RA currently).
639
MOVE(R_RA, numReg);
640
// Luckily, we don't need SCRATCH2/denomReg anymore.
641
LI(SCRATCH2, 0xFFFF);
642
FixupBranch keepNegOne = BLTU(SCRATCH2, numReg);
643
MOVE(regs_.R(IRREG_LO), SCRATCH2);
644
SetJumpTarget(keepNegOne);
645
SetJumpTarget(skipNonZero);
646
}
647
648
// Now combine the remainder in.
649
BSTRINS_D(regs_.R(IRREG_LO), R_RA, 63, 32);
650
break;
651
652
default:
653
INVALIDOP;
654
break;
655
}
656
}
657
658
} // namespace MIPSComp
659
660